Results 1 to 8 of 8
  1. #1
    momo97 is offline Member
    Join Date
    Jul 2007
    Posts
    8
    Rep Power
    0

    Default Help with nonstatic classes

    Design a non static InventoryItem class that contains the following information...

    * an ID value for the inventory item
    * a brief description of the item
    * the unit cost.

    Write a brief main() method that will create two InventoryItem objects.

    This is the assignment and I have no idea what to do! >.<

  2. #2
    momo97 is offline Member
    Join Date
    Jul 2007
    Posts
    8
    Rep Power
    0

    Default

    Any saintly soul out there willing to help out a poor student in class? :-(

  3. #3
    shanePreater is offline Member
    Join Date
    Jul 2007
    Location
    England, Bath
    Posts
    47
    Rep Power
    0

    Default

    Surely you are not requesting someone to do your homework?

    You know the syntax for creating a class right?
    Java Code:
     <scope> [modifer[ modifier]] class <name> [extends <super class>] [implements <interface>[, <interface>]] {
    <body>
    }
    Where scope is one of default,private,protected or public
    Modifiers can be blank or static, final etc etc
    then you can extend a parent class (I.E. make your new class a more specific version of a given parent so Dog is to Animal etc).
    Then you can choose to implement some interfaces if you want.

    What type of member variables do you think you would need in the class?
    An ID which I would assume is some form of number?
    A description which would be a String of characters?
    A unit cost?

    Then you would simply need to create some methods in your class:
    Java Code:
    <scope> [modifier[ modifier]] <return value> <name>([args]) [throws <exception> [, <exception>]] {
    <body of method>
    }
    You would probably need to provide a way of setting each of your member variables, a way of getting each of the member variables and a static main method to generate the test instances.

    Hopefully this will get you going without actually doing it for you.

  4. #4
    momo97 is offline Member
    Join Date
    Jul 2007
    Posts
    8
    Rep Power
    0

    Default

    I came up with this much, but I dont know how to make it nonstatic


    /*
    * Title: RecordMaker2.java
    * Description: a very basic implementation of the classic list program.
    */
    public class RecordMaker2 {
    public static void main(String[] args) {
    String[] XList = new String[25];
    int HowMany;
    HowMany = Record(XList, 25);
    }
    //This method will allow the user to fill the list..
    public static int Record(String[] List, int Size)
    {
    int K= 0;
    String ID, Description, Price;
    String ID2, Description2, Price2;
    System.out.print("Enter ID Number of Product: ");
    ID = SimpleIO.inputString();
    System.out.print("Enter Product Description: ");
    Description = SimpleIO.inputString();
    System.out.print("Enter Price: ");
    Price = SimpleIO.inputString();
    System.out.print("Enter ID Number of Product: ");
    ID2 = SimpleIO.inputString();
    System.out.print("Enter Product Description: ");
    Description2 = SimpleIO.inputString();
    System.out.print("Enter Price: ");
    Price2 = SimpleIO.inputString();
    System.out.println("============================== ");
    System.out.println("ID: "+ID);
    System.out.println("Desciption: "+Description);
    System.out.println("Price: $"+Price);
    System.out.println("============================== ");
    System.out.println("ID: "+ID2);
    System.out.println("Desciption: "+Description2);
    System.out.println("Price: $"+Price2);
    return K; //Send back the number actually added..
    }
    }

  5. #5
    shanePreater is offline Member
    Join Date
    Jul 2007
    Location
    England, Bath
    Posts
    47
    Rep Power
    0

    Default

    OK not a bad first pass but you want to hold the details for each of the Items as its own class so you would then create a new Item class instance for each iteration then you can simply put the loading into a list and voila.

  6. #6
    momo97 is offline Member
    Join Date
    Jul 2007
    Posts
    8
    Rep Power
    0

    Default

    Huh? sorry, can you explain that please

  7. #7
    momo97 is offline Member
    Join Date
    Jul 2007
    Posts
    8
    Rep Power
    0

    Default

    I dont understand what you mean by hiding the details. DOes that have to do with making the program nonstatic?

  8. #8
    shanePreater is offline Member
    Join Date
    Jul 2007
    Location
    England, Bath
    Posts
    47
    Rep Power
    0

    Default

    Ok let me try and explain a bit more.
    Java is object orientated language so you should group all the details of a specific object in its own class.
    In your case you have an Item object which holds the ID, description and price.
    These member variables will probably be private as you don't want just anyone playing with your members now do you :D
    You will then need to provide a way to modify and obtain this values, in java the convention is that these methods are called getters and setters and take the form:
    Java Code:
     public <class> get<Capitalised name of variable>();
    and
    Java Code:
     public void set<Capitalised name of variable>(<class> <name>);
    So if we take the ID field (which we should really call identifier) in the Item class you would have the following 3 items:
    Java Code:
    private long identifier;
    public long getIdentifier() {
    return identifier;
    }
    
    public void setIdentifier(long identifier) {
    this.identifier = identifier;
    }
    The Item class is therefore the right object to store details of a particular item in.

    Once you have all the member variables, getters and setters you can then simpy create a new instance of the Item class in each loop of your code and then assign the parts to the item object:
    Java Code:
    Item item = new Item();
    item.setIdentifier(Long.parseLong(SimpleIO.inputString());
    //repeat for the other variables.
    ....
    Please let me know if you need some more help and I'll be happy to oblige.

Similar Threads

  1. Using a JAR from other classes
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-02-2008, 07:08 PM
  2. Objects and Classes
    By Aleve in forum New To Java
    Replies: 8
    Last Post: 12-31-2007, 08:05 AM
  3. Help for dynamic classes
    By Bender in forum Advanced Java
    Replies: 1
    Last Post: 12-19-2007, 08:46 PM
  4. When do we use inner classes?
    By cruxblack in forum New To Java
    Replies: 5
    Last Post: 08-10-2007, 05:00 PM
  5. EJB, classes Model
    By Felissa in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 07-06-2007, 03:17 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
  •