Results 1 to 15 of 15
  1. #1
    adriand is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Complete newb and struggling!

    I'm a complete newb to Java and programming in general. I'm taking online courses for Java programming right now and it's kicking my butt. For me, trying to learn this material without one on one instruction is difficult and the material that's provided just makes it more confusing at times.

    I could really use the help of some forum members to get me through this class.

    This is the assignment I am currently working on:

    • Choose a product that lends itself to an inventory (for example, products at your workplace, office supplies, music CDs, DVD movies, or software).

    • Create a class named after the product you have chosen that holds the item number, the name of the product, the number of units in stock, and the price of each unit. Make sure this class includes get and set methods for each of the attributes above.

    For Example:
    If your product is shoes, make your product class Shoe.java etc.

    • Create a second class named Inventory that creates a single instance of your class, and then uses that instance to display the product number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory (the number of units in stock multiplied by the price of each unit). Pay attention to the good programming practices in the text to ensure your source code is readable and well documented.


    The material we have talks about certain items but says nothing on how to integrate them together. Like I said I'm totally lost. If anyone could guide me in the right direction and help me understand Java better I would be extremely thankful!

    This is what I have so far:

    Java Code:
    public class Belts 
    {
        public static void main(String args[]);
        {
            String itemName = null;  //Item name
            float itemNumber = 0;  //Item part number
            float itemQuantity = 0;  //Item quantity
            float itemPrice;  //Item price
            float itemValue;  //Item value
        }        
                
    }
    And this:
    Java Code:
    public class Inventory
    {
        public static void main( String args[])
           {
                 Belts myBelts = new Belts();
         
                 System.out.println("\n\nItem Name: "+myBelts.getName()); // Display item name
                         System.out.println("Item Number: "+myBelts.getNumber()); // Display item part number
                         System.out.println("Quantity in Stock: "+myBelts.getQuantity()); // Display quantity in stock
                         System.out.println("Item Price:$ "+myBelts.getPrice()); // Display item price
                         System.out.println("Value of Inventory:$ "+myBelts.calcValue()); // Display total value of inventory for this item
         
                 
         System.out.println("\n\nEnter item name or enter the word stop to quit:");
                   myBelts.setItemName(input.next()); 
     
     
       } // end main method
         
           } 
        }
    Last edited by pbrockway2; 07-02-2012 at 06:07 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Complete newb and struggling!

    Hi adriand, welcome to the forum!

    When you post code, use the "code" tags. They are described in this faq and the general idea is to preserve the formtting. [code] goes at the start of the code and [/code] at the end.

    Work one step at a time (ie remove Inventory because you haven't got there yet). A few things occur to me about the Belts class:

    (1) The name is wrong. It should be Belt (singular). That's following the example given in the assignment (Shoe not Shoes) and also the general rule that classes name the sort of thing that they are modelling in code: in this case instances of the class will represent a single (type of) belt.

    (2) You weren't asked to create a main() method in the Belt class. Remove it.

    (3) The Belt class is supposed to hold "the item number, the name of the product, the number of units in stock, and the price of each unit." I'm guessing that "hold" in this case means that there are supposed to be instance variables for the item number, name etc. Instance variables, not local variables in a main() method. The assignment refers to them as "attributes". You might have to look up how these variables are declared. Think about the type of each variable: name is a string, quantity is probably an int because 1/2 a belt is a pretty useless thing to keep in stock. Where fractional amounts are used double is much more common than float. You should also decide on the access modifiers you are using for these variables: almost certainly your notes/textbook/whatever will recommend that they be private.

    (4) There is no "value" mentioned in the assignment with respect to a Belt instance. Price, but not value. Value does occur, but as part of the Inventory class which you haven't got to yet. Resist the impulse to think ahead.

    (5) You are asked to include "get and set methods". Do that. Again your notes etc are the place to apply to see how get and set methods are written to accompany attributes (instance variables).

    -----

    I realise there's a lot there. But I wanted to put things in a pretty strict order so you can work through them one at a time.

    Post if you get stuck, can't understand some point, or want to confirm that your Belt class does do what it is supposed to do.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Complete newb and struggling!

    (6) Forgot one of the *most* important things... Document! You were asked to document everything. So when you create an instance variable (attribute), document it by saying what aspect of a belt it is supposed to represent. When you write a setter or gette,r write an accompanying comment that describes (briefly but completely) what its behaviour is (ie what it returns or what it does to the belt).

  4. #4
    adriand is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Re: Complete newb and struggling!

    Here's my Belt class reworked:

    Java Code:
    package inventory;
    
    
    
    public class Belt 
    {    
        
            private String itemName; //Item name
            private int itemNumber;  //Item part number
            private int itemQuantity;  //Item quantity
            private int itemPrice;  //Item price
            
            public void setItemName(String name) //public method to set item name
            {
                itemName = name; // store the item name
            } // end method setItemName
            
            public String getItemName() //method to retrieve item name
            {
                return itemName;
            } // end method getItemName
            
            public void setItemNumber( int number) //public method to set item number
            {
                itemNumber = number; // store the item number
            } // end method setItemNumber 
            
            public int getItemNumber() //method to retrieve item number
            {
                return itemNumber;            
            } // end method getItemNumber
            
            public void setItemQuantity(int quantity) //public method to set item quantity
            {
                itemQuantity = quantity; // store the item quantity            
            } // end method setItemQuantity
            
            public int getItemQuantity() //method to retrieve item quantity
            {
                return itemQuantity;
            } // end method getItemQuantity
            
            public void setItemPrice(int price) //public method to set item price
            {
                itemPrice = price; // store the item price            
            }// end method setItemPrice
            
            public int getItemPrice() //method to retrieve item price
            {
               return itemPrice; 
            } // end method getItemPrice
            
            
            } // end class Belt

    What's next?!!!

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Complete newb and struggling!

    That seems to cover everything in the first part of the assignment. (I'm assuming it compiles OK or you would have said).

    About comments, I tend to prefer javadoc style comments. You may or may not have covered them (and I'm not suggesting you change anything) but they are common:

    Java Code:
    package inventory;
    
       /**
         * A class representing a type of belt held in stock.
         */
    public class Belt 
    { 
            /** The belt's name. */
        private String itemName; 
            /** The belt's part number. */
        private int itemNumber;
            /** The belt's quantity. */
        private int itemQuantity;
            /** The price of the belt. */ 
        private int itemPrice; 
    
            /** Sets this belt's name to a given string. */
        public void setItemName(String name) 
        {
            itemName = name; 
        }
    
        // etc
    The other thing about comments is that someone may well remark that there are too many and they are distracting. Possibly true, but (1) the assignment said they are to be there and (2) comments are easier to remove than to add. Removing unnecessary comments is just an exercise in using the delete key, adding them involves figuring out exactly what the code does. For a decent sized chunk of code, and six months after the fact these are not symmetrical problems.

    -----

    I suppose the next thing is the Inventory class. It will be pretty much as you had it before. Think about whether you want to change the Belt class to give it a calcValue() method: or do exactly what the assignment says (grab the quantity and the price and have the Inventory class do the calculation).

    With only a small amount of luck Inventory should compile OK and do *something* when run. Then it's just a matter of checking that the something is what you expect or intend.
    Last edited by pbrockway2; 07-03-2012 at 03:05 AM.

  6. #6
    adriand is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Re: Complete newb and struggling!

    Whats the proper way of creating a single instance as stated in the instructions?

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Complete newb and struggling!

    One way is exactly what you had before. In main()

    Java Code:
    Belt myBelt = new Belt();
    Was there any more to the assignment? I ask, because Inventory as described there seems a bit odd.

    In any case the code you had does create a (single) belt.

  8. #8
    adriand is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Re: Complete newb and struggling!

    When I run what I have I get this:

    run:
    java.lang.NoClassDefFoundError: inventory/Inventory
    Caused by: java.lang.ClassNotFoundException: inventory.Inventory
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 47)
    Could not find the main class: inventory.Inventory. Program will exit.
    Exception in thread "main" Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Complete newb and struggling!

    What is the code for the Inventory class?

    I'm off for a bit. (but someone is sure to jump in if there's an obvious problem). One thing to check is that Inventory is in the inventory package, and that its main() method is properly defined (the "public static void" magic has to be correct).

  10. #10
    adriand is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Re: Complete newb and struggling!

    Here is the inventory class:

    Java Code:
    public class Inventory
    {
        
        
        public static void main( String args[])
           {
                 Belt myBelt = new Belt();
         
                         System.out.println("Item Name: "+myBelt.getItemName()); // Display item name
                         System.out.println("Item Number: "+myBelt.getItemNumber()); // Display item part number
                         System.out.println("Quantity in Stock: "+myBelt.getItemQuantity()); // Display quantity in stock
                         System.out.println("Item Price:$ "+myBelt.getItemPrice()); // Display item price
                         System.out.println("Value of Inventory:$ "+myBelt.calcValue()); // Display total value of inventory for this item
         
                 
                         System.out.println("Enter item name or enter stop to quit the program:");
                            myBelt.setItemName(input.next()); 
     
     
       } 
         
           } // end main method

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Complete newb and struggling!

    That class doesn't appear to be in a package named inventory. Add a package statement at the top of the file as you do for the Belt class. (Both Belt.java and Inventory.java should be in a folder called inventory. Source files are kept in folders corresponding to their packages.)

    If you continue to get an error describe exactly the locations of the two .java files and the process you are using to compile and run.

  12. #12
    adriand is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Re: Complete newb and struggling!

    I forgot to include that another requirement was to:
    Important Note:
    We are preparing to create a GUI class, so the command line features will start to go away, starting with data input. When you create your instance of your product class, just give it some hard coded values. Do not prompt the user for data.

    So I made some corrections to the code to meet the requirements:

    Java Code:
    package inventory;
    
    
    public class Inventory
    {
           float itemValue;
           
        public static void main( String args[])
           {
                 //Belt myBelt = new Belt();
         
                         System.out.println("Item Name:Belt 1"); // Display item name
                         System.out.println("Item Number:123456"); // Display item part number
                         System.out.println("Quantity in Stock:50"); // Display quantity in stock
                         System.out.println("Item Price:$20"); // Display item price                                                                
                         System.out.println("Value of Inventory:$2000"); // Display total value of inventory for this item
                                              
                         
        } 
         
           } // end main method
    I'm sure it can be cleaned up a little but that's what I submitted so we'll see what the instructor says.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Complete newb and struggling!

    Please go through the Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    adriand is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Re: Complete newb and struggling!

    Sorry about that, I'll be sure to describe my problem more clearer in my title next time.

  15. #15
    adriand is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default Adding ArrayList to my inventory program

    I wanted to thank everyone for their help with my assignment. I still had some work to do to it but at least I had something to turn in.

    This is the feedback I got for the assignment I turned in:
    "Your Belt class is missing a constructor and a method to calculate its value.
    Also, you were supposed to create an instance of it in Inventory, and display
    the values."

    I believe I corrected that in the updated code below.

    Java Code:
    package inventory;
    
    
    public class Belt 
    {    
        
            private String itemName; //Item name
            private double itemNumber;  //Item part number
            private double itemQuantity;  //Item quantity
            private double itemPrice;  //Item price
            private double valueCalc;  //Calculate value of belt inventory
            
            public Belt(Double calc)  //Constructor initializing valueCalc
            {
                valueCalc = calc;  //initialize valueCalc
            }  //end constructor
            
            public void setValueCalc(Double calc)
            {
                valueCalc = calc;  //store the calculated value
            }//end method setValueCalc
            
            public double getValueCalc()  //method to retrieve the calculated value
            {
                return valueCalc;
            }//end method getValueCalc
                    
            public void setItemName(String name) //public method to set item name
            {
                itemName = name; // store the item name
            } // end method setItemName
            
            public String getItemName() //method to retrieve item name
            {
                return itemName;
            } // end method getItemName
            
            public void setItemNumber( double number) //public method to set item number
            {
                itemNumber = number; // store the item number
            } // end method setItemNumber 
            
            public double getItemNumber() //method to retrieve item number
            {
                return itemNumber;            
            } // end method getItemNumber
            
            public void setItemQuantity(double quantity) //public method to set item quantity
            {
                itemQuantity = quantity; // store the item quantity            
            } // end method setItemQuantity
            
            public double getItemQuantity() //method to retrieve item quantity
            {
                return itemQuantity;
            } // end method getItemQuantity
            
            public void setItemPrice(double price) //public method to set item price
            {
                itemPrice = price; // store the item price            
            }// end method setItemPrice
            
            public double getItemPrice() //method to retrieve item price
            {
               return itemPrice; 
            } // end method getItemPrice
            
            
            } // end class Belt
    Now I'm working on the 2nd part of the assignment which is:

    • Modify the Inventory Program so the application can handle multiple items. Use an ArrayList to store the items. DO NOT PROMPT USER FOR INPUT; initialize the array with hard coded values.
    • The output should display the information the first product in your ArrayList, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product.
    • Create a method to calculate the value of the entire inventory. Call this method and display the results. (Hint: Use a loop to make your calculation.)
    • Create another method to sort the array items by the name of the product.


    Here's what I have so far:
    Java Code:
    package inventory;
    
    import java.util.ArrayList;
    
    
    public class Inventory
    {
           float itemValue;
           
        public static void main( String args[])
           {
                 //Belt myBelt = new Belt();
         
                         System.out.println("Item Name:Belt 1"); // Display item name
                         System.out.println("Item Number:123456"); // Display item part number
                         System.out.println("Quantity in Stock:50"); // Display quantity in stock
                         System.out.println("Item Price:$20"); // Display item price                                                                
                         System.out.println("Value of Inventory:$1000"); // Display total value of inventory for this item
                                              
            ArrayList<String> belt = new ArrayList<String>();           
            
            // add items to the list
            belt.add(0,"Belt 1"); //insert Belt 1 at index 0
            belt.add("Belt 2");  
            belt.add("Belt 3");
            belt.add("Belt 4");
            belt.add("Belt 5");
        } 
         
           } // end main method
    Last edited by adriand; 07-08-2012 at 07:09 AM.

Similar Threads

  1. A newb question
    By JavaJunkie2341 in forum New To Java
    Replies: 2
    Last Post: 03-30-2012, 07:00 PM
  2. newb needs help
    By dumle29 in forum New To Java
    Replies: 3
    Last Post: 02-24-2011, 08:43 AM
  3. Newb here, need help with JSP.
    By grep in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-25-2010, 09:04 PM
  4. Hello, I'm a newb
    By smith427 in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 02:54 AM
  5. Newb Topics
    By dkizzy in forum New To Java
    Replies: 2
    Last Post: 09-17-2008, 07:01 AM

Posting Permissions

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