Results 1 to 12 of 12
  1. #1
    luron31 is offline Member
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Default Hints on how to make a Java Class

    Hello everyone,
    i am doing a project and im really stuck on it...
    i dont want answers. (i have a very guilty conscience.) i just need some hints as to what im doing wrong or what i could do right...


    "A VendingMachine object represents a vending machine that sells products like candy or soda. It has a fixed number of slots, each of which can hold one kind of product for sale. For example, a machine might have 10 Snickers bars in slot 0 and 7 cans of Pepsi in slot 1, but it cannot have some Snickers and some Pepsi in the same slot. There is a maximum number of items that can fit into any one slot - for example, if the maximum was 10, no more Snickers could fit into slot 0. The same kind of product can be in more than one slot, however. The machine must keep track of the name, price, and current quantity of each product, as well as the amount of cash in the machine.

    Slots of the vending machine are be numbered starting at zero.

    Define appropriate fields, and complete the following methods of VendingMachine:

    • VendingMachine(int numslots, int maxperslot, double cash) (constructor): Takes the number of slots in the vending machine, the maximum number of items that can fit into any one slot, and the amount of cash in the machine initially. This is the constructor method.
    • void setProduct(int slot, String product, double price): Make the given slot hold items of the specified kind of product, sold for the given price. The initial quantity of the product in this slot should be zero. If the slot already held another kind of product, the old product should be removed from this slot.
    • void restockProduct(String product, int quantity): Add the given quantity of the specified product to the vending machine. Put as many of the items as possible into the first slot that has been designated to hold that particular kind of product (using setProduct()). If not all of the items will fit into the first slot, put as many of the rest as possible into the second slot that holds that kind of product, etc. For partial credit, your method should at least be able to find the first slot designated for the specified product and put all of the items there.
    • double getCashOnHand(): Return the amount of cash now in the vending machine (this amount should increase whenever an item is purchased).
    • int getQuantity(int slot): Return the number of items in the given slot.
    • int getQuantity(String product): Return the total number of items of the specified kind of product that are in the vending machine. Remember that this product may be in more than one slot. If the product is not in the vending machine at all, simply return zero.
    • boolean buyItem(int slot): Attempt to buy one item from the given slot. Return true if successful.
    "



    public class VendingMachine
    {
    //fields?
    public int numslots; //???
    public int maxperslot; //???
    public double cash;
    double amountcash;
    string quantity;
    double quant;

    //is this supposed to be a different word than below?

    public VendingMachine(int numslots, int maxperslot, double cash)
    {
    //the constructor
    //whats supposed to go in front of the equals sign?
    int numberOfSlots = numslots;
    int maxItemsInSlot = maxperslot;
    double totalCashInMachine = cash;
    // complete this method
    }

    public class setProduct(int slot, String product, double price)
    {
    //this.product= price;

    //int[] slot = {String product};
    //String[] product;
    //String[] product = {double price};
    }

    public class restockProduct(String product, int quantity)
    {
    if (quantity <= maxperslot)
    {System.out.println("This is the " + quantity);}

    }

    public double getCashOnHand()
    {
    return amountcash; // replace this line with your code
    }

    public int getQuantity(int slot)
    {
    return quant; // replace this line with your code
    }

    public int getQuantity(String product)
    {
    return quantity; // replace this line with your code
    }

    public boolean buyItem(int slot)
    {
    return false; // replace this line with your code
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Suggestion 1: Use your constructor parameters to set your class's fields. If the field's name is the same as the parameter's name, you MUST place a "this." before the field name in the constructor so that the compiler con distinguish between the two. If they are different, this isn't necessary.

    For e.g.,
    Java Code:
    public class Car {
      private String color;
      private int price;
      private String serialNumber;
      
      // constructor
      public Car(String color, int thePrice, String sn) {
        // if the parameter name is the same as a field, you 
        // MUST use this. before the field name to distinguish the two:
        this.color = color; // this.color is the field, color on the right is the parameter
        
        // if the parameter name is different from the field, you can place
        // this. before the field, 
        this.price = thePrice;
        
        // but you don't have to...
        serialNumber = sn;    
      }
    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Next, are you allowed to use more than one class here? It would be nice to be able to create a Product class as well as a Slot class the latter knowing which product it holds as well as how many of this Product it has.

    Regardless, you are going to have to have an array of slots, so you may wish to work on that.

    Good luck

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Next, are you allowed to use more than one class here? It would be nice to be able to create a Product class as well as a Slot class the latter knowing which product it holds as well as how many of this Product it has.

    Regardless, you are going to have to have an array of slots, so you may wish to work on that.

    Good luck

  5. #5
    luron31 is offline Member
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Next, are you allowed to use more than one class here? It would be nice to be able to create a Product class as well as a Slot class the latter knowing which product it holds as well as how many of this Product it has.

    Regardless, you are going to have to have an array of slots, so you may wish to work on that.

    Good luck
    I'm not too sure if I'm allowed to use more than one class. What do you think?:

    "For this assignment, you will complete one class in one separate file, which you will hand in (the .java file, not the .class file). You are not required to submit test cases, though of course you should construct a good set of test cases in order to test your own code.

    Since this is a class and not a complete program, it will not have a main method, and will not interact directly with a human user. Therefore, you should NOT use the IO module in any of the methods you write. Data needed by a method is already provided by its arguments, and results of a method should be returned as the method's value. If you wish to write your own driver program to test your class, you may use the IO module in the program.

    Do not use any classes or modules from outside java.lang (do not import any libraries). We want you to learn how to implement these operations on your own. You may use modules (such as Math) and classes (such as String) from the Java library that you have learned. "

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It states that "you will complete one class..." which sounds to me as if you are not to make a second Slot class, and this is a shame.

    This suggests that you may have to use "parallel arrays" to hold the information for the slots, meaning an array of String for the product names, an array of ints for the count, and array of (shudder!) double for the price.

    When you study OOPS more, you'll learn why parallel arrays are a bad idea and how to avoid them.

  7. #7
    luron31 is offline Member
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Default

    If I was able to use more than one class, just out of curiosity, how would I go about doing that exactly?

    I feel like I'm doing everything wrong.

  8. #8
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    I feel like I'm doing everything wrong.
    You made a few mistakes, but your learning. If you already knew how to do it right you wouldn't be taking the class.

    Was the code you posted part of the assignment or is that what you've come up with? (so we know what we can and can not do).

    I see a few syntax errors in it, which lead me to believe it is your work, though you may have just copied it down wrong.

    Instead of using parallel arrays, I would use a map of the name of the item and an array carrying the amount of items and the cost.

    But if that is the code your instructor provided then you probably have to use parallel arrays as fubarable stated.

    If I was able to use more than one class, just out of curiosity, how would I go about doing that exactly?
    You would create an Item class which could contain the price of the item.
    You would also create a Slot class which would contain arrays of Items.

    for example

    Java Code:
    double snickersPrice = 0.75;
    Slot slot1 = new Slot("Snickers");
    slot1.add(new Item(snickersPrice);

  9. #9
    luron31 is offline Member
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Mr.Beans View Post
    You made a few mistakes, but your learning. If you already knew how to do it right you wouldn't be taking the class.

    Was the code you posted part of the assignment or is that what you've come up with? (so we know what we can and can not do).

    I see a few syntax errors in it, which lead me to believe it is your work, though you may have just copied it down wrong.

    Instead of using parallel arrays, I would use a map of the name of the item and an array carrying the amount of items and the cost.

    But if that is the code your instructor provided then you probably have to use parallel arrays as fubarable stated.


    You would create an Item class which could contain the price of the item.
    You would also create a Slot class which would contain arrays of Items.

    for example

    Java Code:
    double snickersPrice = 0.75;
    Slot slot1 = new Slot("Snickers");
    slot1.add(new Item(snickersPrice);
    the code was from me and the assignment. My teacher gives us a little bit but we make up the methods...

    Thank you so much for your help by the way. I am so frustrated/upset that I have to teach this basically to myself and be expected to know all this.

  10. #10
    luron31 is offline Member
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Default

    let me rephrase that: I wish I could make what my teach is talking about I seem to understand it in class and then we get these projects and they are so hard for me.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Just like any new intellectual endeavor, the more effort exerted the more you practice the easier it will become.

  12. #12
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    This may be skipping around the rules, but what about inner classes? They are still technically part of the class...

    Interesting project... Maybe I'll try it and see if I can come up with snippets if you need them.

    You seem to be doing okay... at least you are trying. Unless you think inner classes are allowed, you will need parallel arrays or Maps of some sort. Parallel arrays are ugly, but they shouldn't be too bad in this case, as you know what the size will be already (numSlots)

    public class restockProduct(String product, int quantity)

    {

    if (quantity <= maxperslot)
    {System.out.println("This is the " + quantity);}

    }
    This one is breaking the rules... you are not allowed to use IO, and you can't return class... It doesn't exist, and returning Class would be pointless. You probably want the method to return a boolean indicating whether the restock was successful. The setProduct method has the same problem with it's return type.

    My final point (promise!): The constructor should set the values of the classes fields. I think the code is from your teacher, but it needs to be changed

    Good Luck,
    Singing Boyo
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. how do i make java external/internal?
    By zoon81 in forum New To Java
    Replies: 2
    Last Post: 06-29-2009, 09:59 AM
  2. Replies: 14
    Last Post: 05-28-2009, 05:44 AM
  3. Can anybody make this on java ???
    By eliCanzee in forum AWT / Swing
    Replies: 9
    Last Post: 04-30-2009, 01:14 AM
  4. Would this be easy to make in java?
    By tooner in forum Java Applets
    Replies: 3
    Last Post: 03-20-2009, 05:55 PM
  5. Please help, need to make my class static.
    By sumak in forum New To Java
    Replies: 1
    Last Post: 04-19-2008, 07:29 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
  •