Results 1 to 17 of 17
  1. #1
    dscales is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default How to add methods from a class based on defined data in a main class

    So I am driving myself crazy with this assignment and I can't seem to figure out the code involved in adding the "warehouse stock" to the different segments.

    Here is the first base class with the methods I need to fill in (what i have so far) /** * Replace comments below with code. Replace this comment with your name an - Pastebin.com

    Here is the main class which contains all predefine data public class WarehouseDriver { public static void main(String[] args) { - Pastebin.com

    Final output should look like this: http://i.imgur.com/JPWwSB2.png?1

    Directions are : Include methods in the warehouse class to add stock to each item and methods to subtract stock from each item (six methods needed for add/subtract).

    I'm not looking for this to be solved for me, I am just looking for some help with how I can add the stock to the various warehouses

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: How to add methods from a class based on defined data in a main class

    Please post the code you have questions about here on the forum.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    dscales is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: How to add methods from a class based on defined data in a main class

    Ok sorry, I posted the code to links to make it easier but here goes :

    ere is the first base class with the methods I need to fill in:
    Java Code:
    	
    
        
        public class Warehouse {
            /*
             * Do not change the instance variables
             */
         
            private int radio;
            private int tv;
            private int computer;
         
            public Warehouse() {
                radio = 0;
                tv = 0;
                computer = 0;
            }
         
            public Warehouse(int t, int c) {
                radio = 0;
                tv = t;
                computer = c;
            }
         
            public Warehouse(int r, int t, int c) {
                radio = r;
                tv = t;
                computer = c;
            }
         
            public void addRadio(int amount) {
                /*
                 * add code for this method here
                 */
                }
               
            }
         
            public void removeRadio(int amount) {
                /*
                 * add code for this method here if amount is greater than radio display
                 * the message, Insufficient radio inventory
                 */
            }
         
            public void addTV(int amount) {
                /*
                 * add code for this method here
                 */
            }
         
            public void removeTV(int amount) {
                /*
                 * add code for this method here if amount is greater than tv display
                 * the message, Insufficient TV inventory
                 */
            }
         
            public void addComputer(int amount) {
                /*
                 * add code for this method here
                 */
            }
         
            public void removeComputer(int amount) {
                /*
                 * add code for this method here if amount is greater than computer
                 * display the message, Insufficient computer inventory
                 */
            }
         
            /*
             * toString() method is complete do not change code
             */
            public String toString() {
                return "  The number of radios is " + radio
                        + "\n  The number of TV's is " + tv
                        + "\n  The number of computers is " + computer + "\n";
         
            }
        }// end class
    Here is the main class which contains all predefine data:
    Java Code:
    	
    
        public class WarehouseDriver {
         
            public static void main(String[] args) {
                // 3 objects created
                Warehouse whOne = new Warehouse(212, 65, 75);
                Warehouse whTwo = new Warehouse(55, 170);
                Warehouse whThree = new Warehouse();
         
                System.out.println("Warehouse 1 contents:\n" + whOne);
                System.out.println("Warehouse 2 contents:\n" + whTwo);
                System.out.println("Warehouse 3 contents:\n" + whThree);
         
                whOne.addRadio(15);
                whOne.addTV(6);
                whOne.addComputer(30);
                whTwo.addRadio(40);
                whTwo.addComputer(17);
                whTwo.addRadio(23);
                whThree.addRadio(13);
                whThree.addTV(123);
                whThree.addComputer(49);
         
                System.out.println("Warehouse 1 contents:\n" + whOne);
                System.out.println("Warehouse 2 contents:\n" + whTwo);
                System.out.println("Warehouse 3 contents:\n" + whThree);
               
         
                whOne.removeRadio(55);
                whOne.removeTV(71);
                whOne.removeComputer(19);
                whTwo.removeRadio(12);
                whTwo.removeTV(94);
                whTwo.removeComputer(39);
                whThree.removeRadio(22);
                whThree.removeTV(7);
                whThree.removeComputer(52);
         
                System.out.println("Warehouse 1 contents:\n" + whOne);
                System.out.println("Warehouse 2 contents:\n" + whTwo);
                System.out.println("Warehouse 3 contents:\n" + whThree);
            }
        }
    output should read:
    How to add methods from a class based on defined data in a main class-output.jpg

  4. #4
    dscales is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: How to add methods from a class based on defined data in a main class

    I can't figure out how to incorporate and add the whOne, whTwo, whThree's into the methods...

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: How to add methods from a class based on defined data in a main class

    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    dscales is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: How to add methods from a class based on defined data in a main class

    all of the public void spots which are required to be filled, the problem is initializing the different values .... my if / else statements aren't working

    i have read those, thanks, but ill keep digging deeper ...

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: How to add methods from a class based on defined data in a main class

    Where is the code you are having problems with? What are the names of the methods? What class are they in?

    List the methods and add what problem you are having with each one.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    dscales is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: How to add methods from a class based on defined data in a main class

    For example ...

    Java Code:
    public void addRadio(int amount) {
            /*
             * add code for this method here
             */
            }
            
        }
    needs to receive an update in warehouse 1 to add
    Java Code:
    whOne.addRadio(15);
    to the radios in the warehouse for warehouse 1

    so i did the following , considering that warehouse 1 holds 212 radios to begin with

    amount = radio;
    if (radio >= 212)
    radio = 212 + 15;

    So that updates the amount of radios by 15 which gives us 227 in warehouse1..

    my trouble arrives when i try to set up the output for warehouse 2. given that i only have 1 "public void addRadio(int amount) { }" i dont know how to add the second warehouse data without interfering with the first one

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: How to add methods from a class based on defined data in a main class

    These statements define 3 Warehouse objects.
    Java Code:
            Warehouse whOne = new Warehouse(212, 65, 75);
            Warehouse whTwo = new Warehouse(55, 170);
            Warehouse whThree = new Warehouse();
    how to add the second warehouse data without interfering with the first one
    The contents of the three objects are NOT shared. Each has its own contents.
    Use the variable for the object whose contents you want to update when you call a method in the Warehouse class.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    dscales is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: How to add methods from a class based on defined data in a main class

    How would that look though?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: How to add methods from a class based on defined data in a main class

    Just like you have already coded. Just replace the variable name: whOne with one of the others:
    Java Code:
      whOne.addRadio(15);
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    dscales is offline Member
    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    Default Re: How to add methods from a class based on defined data in a main class

    Just declaring them like this gives me tons of error messages

    public void addRadio(int amount) {
    whOne.addRadio(15);
    whTwo.addRadio(40);
    }

    not sure what ur getting at ... sry

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: How to add methods from a class based on defined data in a main class

    Give an example of such an error that you get? Very likely the errors are caused by something else entirely.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    animit is offline Member
    Join Date
    Mar 2012
    Location
    Nepal
    Posts
    14
    Rep Power
    0

    Default Re: How to add methods from a class based on defined data in a main class

    Well first of all, you need to call the method toString() to print the output.

    so instead of doing this:
    Java Code:
    System.out.println("Warehouse 1 contents:\n" + whOne);
    do this:
    Java Code:
    System.out.println("Warehouse 1 contents:\n" + whOne.toString());
    And for updating your values of your variables, for eg radio, write the following code

    Java Code:
    public void addRadio(int amount)
    {
    this.radio+=amount;
    }
    you can do the same with tv and computer

  15. #15
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    936
    Rep Power
    2

    Default Re: How to add methods from a class based on defined data in a main class

    Quote Originally Posted by animit View Post
    Well first of all, you need to call the method toString() to print the output.
    so instead of doing this:
    Java Code:
    System.out.println("Warehouse 1 contents:\n" + whOne);
    do this:
    Java Code:
    System.out.println("Warehouse 1 contents:\n" + whOne.toString());
    The compiler will do that automatically for you.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  16. #16
    animit is offline Member
    Join Date
    Mar 2012
    Location
    Nepal
    Posts
    14
    Rep Power
    0

    Default Re: How to add methods from a class based on defined data in a main class

    Didn't know that. Learned new thing today. :D

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: How to add methods from a class based on defined data in a main class

    The WarehouseDriver class's main() method has several samples of code that calls methods in the Warehouse class.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Error: Could not find or load main class Test.class
    By java_aim in forum New To Java
    Replies: 11
    Last Post: 08-13-2013, 05:05 PM
  2. Replies: 7
    Last Post: 06-04-2013, 05:31 PM
  3. Replies: 1
    Last Post: 11-29-2012, 09:46 AM
  4. Running main method class from another main class
    By tlrocketman in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 08:30 AM
  5. Replies: 9
    Last Post: 09-20-2009, 10:14 AM

Tags for this Thread

Posting Permissions

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