Results 1 to 12 of 12
- 07-08-2009, 11:37 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 5
- Rep Power
- 0
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
}
-
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; } }
-
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
-
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
- 07-09-2009, 12:08 AM #5
Member
- Join Date
- Jul 2009
- Posts
- 5
- Rep Power
- 0
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. "
-
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.
- 07-09-2009, 12:39 AM #7
Member
- Join Date
- Jul 2009
- Posts
- 5
- Rep Power
- 0
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.
- 07-09-2009, 12:58 AM #8
You made a few mistakes, but your learning. If you already knew how to do it right you wouldn't be taking the class.I feel like I'm doing everything wrong.
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.If I was able to use more than one class, just out of curiosity, how would I go about doing that exactly?
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);
- 07-09-2009, 01:15 AM #9
Member
- Join Date
- Jul 2009
- Posts
- 5
- Rep Power
- 0
- 07-09-2009, 01:41 AM #10
Member
- Join Date
- Jul 2009
- Posts
- 5
- Rep Power
- 0
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.
-
Just like any new intellectual endeavor, the more effort exerted the more you practice the easier it will become.
- 07-09-2009, 05:31 AM #12
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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)
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.public class restockProduct(String product, int quantity)
{
if (quantity <= maxperslot)
{System.out.println("This is the " + quantity);}
}
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 BoyoIf 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
-
how do i make java external/internal?
By zoon81 in forum New To JavaReplies: 2Last Post: 06-29-2009, 09:59 AM -
E:\IT 215 Java Programming\public class Inventory.java:39: class, interface, or enum
By tlouvierre in forum New To JavaReplies: 14Last Post: 05-28-2009, 05:44 AM -
Can anybody make this on java ???
By eliCanzee in forum AWT / SwingReplies: 9Last Post: 04-30-2009, 01:14 AM -
Would this be easy to make in java?
By tooner in forum Java AppletsReplies: 3Last Post: 03-20-2009, 05:55 PM -
Please help, need to make my class static.
By sumak in forum New To JavaReplies: 1Last Post: 04-19-2008, 07:29 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks