Thread: Java Generics

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    17
    Rep Power
    0

    Java Generics


    Hello All,

    I have an assignment and I'm kind of stuck now. I really need help. I appreciate your time and effort. Blow are the details:

    1.Carefully study the class structure in Products.java.

    Code:
    abstract class Product {
        protected float price;
    
        // return the price of a particular product
        abstract float price();
    }
    
    //------------------------------------------------------------
    
    class ComputerPart extends Product {
        public ComputerPart(float p) {
    	price = p;
        }
    
        public float price() { return price; }
    }
    
    class Motherboard extends ComputerPart {
        protected String manufacturer;
        public Motherboard(String mfg, float p) {
    	super(p);
    	manufacturer = mfg;
        }
        public String getManufacturer() { return manufacturer; }
    }
    
    class RAM extends ComputerPart {
        protected int size;
        protected String manufacturer;
        public RAM(String mfg, int size, float p) {
    	super(p);
    	this.manufacturer = mfg;
    	this.size = size;
        }
        public String getManufacturer() { return manufacturer; }
    }
    
    class Drive extends ComputerPart {
        protected String type;
        protected int speed;
        public Drive(String type, int speed, float p) {
    	super(p);
    	this.type = type;
    	this.speed = speed;
        }
        public String getType() { return type; }
        public int getSpeed() { return speed; }
    }
    
    
    class Peripheral extends Product {
        public Peripheral(float p) {
    	price = p;
        }
        public float price() { return price; }
    }
    
    class Printer extends Peripheral {
        protected String model;
        public Printer(String model, float p) {
    	super(p);
    	this.model = model;
        }
        public String getModel() { return model; }
    }
    
    class Monitor extends Peripheral {
        protected String model;
        public Monitor(String model, float p) {
    	super(p);
    	this.model = model;
        }
        public String getModel() { return model; }
    }
    
    class Service extends Product {
        public Service(float p) {
    	price = p;
        }
        public float price() { return price; }
    }
    
    class AssemblyService extends Service {
        String provider;
        public AssemblyService(String pv, float p) {
    	super(p);
    	provider = pv;
        }
        public String getProvider() { return provider; }
    }
    
    class DeliveryService extends Service {
        String courier;
        public DeliveryService(String c, float p) {
    	super(p);
    	courier = c;
        }
        public String getCourier() { return courier; }
    }
    
    //-------------------------------------------------------
    class Cheese extends Product {
        public Cheese(float p) {
    	price = p;
        }
        public float price() { return price; }
    }
    
    class Cheddar extends Cheese {
        public Cheddar(float p) {
    	super(p);
        }
    }
    class Mozzarella extends Cheese {
        public Mozzarella(float p) {
    	super(p);
        }
    }
    
    class Fruit extends Product {
        public Fruit(float p) {
    	price = p;
        }
        public float price() { return price; }
    }
    class Apple extends Fruit {
        public Apple(float p) {
    	super(p);
        }
    }
    class Orange extends Fruit {
        public Orange(float p) {
    	super(p);
        }
    }

    2.Design a generic container called GenericOrder that acts as a collection of an arbitrary number of objects in Products.java.

    3.Design a mechanism that gives each instance of the container a unique identifier. Implement as many methods as necessary. You must use Java generics features.

    4.Design and implement a subclass of GenericOrder called ComputerOrder that takes an arbitrary number of different classes of ComputerPart objects, Peripheral objects, and Service objects. Implement as many methods as necessary.

    5.Design and implement a subclass of GenericOrder called PartyTrayOrder that takes an arbitrary number of different classes of Cheese objects, Fruit objects, and Service objects. Implement as many methods as necessary.

    6.Design and implement a class called OrderProcessor. You must implement at least the following methods:
    accept; // this method accepts a GenericOrder or any of its subclass objects and stores it in any internal collection of OrderProcessor.

    process; // this method sorts all accepted orders in the internal collection of GenericOrder into collections of ComputerPart, Peripheral, Cheese, Fruit, and Service. You must associate each object with the unique identifier. You may refer to the TwoTuple.java example in the text book.

    dispatchXXX; // this method simulates the dispatch of the sorted collections. For example, the method dispatchComputerParts() should produce this output:

    Motherboard name=Asus, price=$37.5, order number=123456

    Motherboard name=Asus, price=$37.5, order number=987654

    RAM name=Kingston, size=512, price=$25.0, order number=123456

    You may overload each of the above methods as you think necessary.

    **Here is what I have so far**
    Code:
    import java.util.*;
    public class GenericOrder<T> {
    	private List<T> orderedProducts;
    	private static long serial=0;
    	private static long orderNumber= serial++;
    	
    	public GenericOrder(){
    		orderedProducts= new ArrayList<T>();
    	}
    	//Unique identifier
    	public long getOrderNumber(){
    		return orderNumber;
    	}
    	
    	public void addItem(T newItem){
    		orderedProducts.add(newItem);
    		
    	}
    	
    	public List<T> getItems(){
    		return orderedProducts;
    	}
    	
    	public void setProducts(List<T> orderedProducts){
    		
    		this.orderedProducts= orderedProducts;
    	}
    	
    	public class ComputerOrder<T extends Product,Peripheral,Service>{
    	
    		
    	}
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Can you explain what problems you are having now? If there are error messages,
    copy the full text and paste it here.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    17
    Rep Power
    0
    I'd like to know how to proceed further. I'm stuck at the following question:

    4.Design and implement a subclass of GenericOrder called ComputerOrder that takes an arbitrary number of different classes of ComputerPart objects, Peripheral objects, and Service objects. Implement as many methods as necessary.

    My code:

    Code:
    public class ComputerOrder<T extends GenericOrder>{
    	
    	ComputerPart comPart= new ComputerPart(0);
    	Peripheral peripheralObject= new Peripheral(0);
    	Service serviceObject=new Service(0);
    	
    	}
    Is this all I have to do for this part?

IMN logo majestic logo threadwatch logo seochat tools logo