Results 1 to 4 of 4
  1. #1
    zhoujackji is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default 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

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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:
    Java Code:
    ArrayList list = new ArrayList();
    it can hold any data type. You might think this a good thing, as you can shoot anthing its way:
    Java Code:
    list.add(3);
    list.add('a');
    list.add("abc");
    list.add(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:
    Object o = list.get(2); //what type is this element?
    To simplify this process, generics where designed:
    Java Code:
    ArrayList<Integer> list = new ArrayList<Integer>();
    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:
    list.add(3);
    list.add(new Integer(5));
    list.add("abc"); //throws an exception
    
    Integer a = list.get(0);
    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.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,239
    Rep Power
    19

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Tsk tsk, and to think I wrote up all of that...
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Generic Collections---Dysfuntional Java
    By beezerbutt in forum New To Java
    Replies: 6
    Last Post: 06-28-2009, 07:37 PM
  2. Applet as container .
    By pawankumarom in forum Java Applets
    Replies: 5
    Last Post: 04-23-2009, 06:17 AM
  3. Replies: 3
    Last Post: 04-21-2009, 11:01 PM
  4. [SOLVED] Java container class misunderstood
    By AngrYkIdzrUlE in forum New To Java
    Replies: 8
    Last Post: 04-06-2009, 07:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •