Results 1 to 4 of 4
- 11-13-2010, 06:05 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 1
- Rep Power
- 0
Java Generic Container Question - please help!!!
Hi all,
I am currently learning Java from a university course. I am stuck with one of my assignment. I am a database developer, object-oriented is not my thing. Please help out!
The given class is following:
abstract class Products {
protected float price;
// return the price of a particular product
abstract float price();
}
//------------------------------------------------------------
class ComputerPart extends Products {
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 Products {
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; }
}
The first requirement is:Design a generic container called GenericOrder that acts as a collection of an arbitrary number of objects in Products.java. 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.
Please help me with this in anyway you can think of, i am totally lost on this.
Thanks in advance
- 11-13-2010, 08:42 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
A generic container is a container that holds only one type of data (and it's extending classes, but that's nitpicking). For example, if you instantiate an ArrayList like this:
it can hold any data type. You might think this a good thing, as you can shoot anthing its way:Java Code:ArrayList list = new ArrayList();
but the problem comes when you want to retrieve an object from the list, since it can hold any type of data, it stores them as Objects, so when working with a non-generic list, you need to know what type of data your getting:Java Code:list.add(3); list.add('a'); list.add("abc"); list.add(new ArrayList());
To simplify this process, generics where designed:Java Code:Object o = list.get(2); //what type is this element?
This way, the arraylist hold only Integer objects, but more importantly, it returns Integer objects as well, taking the guesswork out of the get method:Java Code:ArrayList<Integer> list = new ArrayList<Integer>();
Look up the APIs for any list, set or map, and take a look at a tutorial or two on making a class use generics.Java Code:list.add(3); list.add(new Integer(5)); list.add("abc"); //throws an exception Integer a = list.get(0);Ever seen a dog chase its tail? Now that's an infinite loop.
- 11-13-2010, 10:51 AM #3
- 11-13-2010, 11:12 AM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Similar Threads
-
Generic Collections---Dysfuntional Java
By beezerbutt in forum New To JavaReplies: 6Last Post: 06-28-2009, 07:37 PM -
Applet as container .
By pawankumarom in forum Java AppletsReplies: 5Last Post: 04-23-2009, 06:17 AM -
[SOLVED] Coordinates container --> in parent container.
By Cleite in forum AWT / SwingReplies: 3Last Post: 04-21-2009, 11:01 PM -
[SOLVED] Java container class misunderstood
By AngrYkIdzrUlE in forum New To JavaReplies: 8Last Post: 04-06-2009, 07:57 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks