VendingMachine Help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: VendingMachine Help

  1. #1
    Join Date
    Oct 2002
    Posts
    1

    VendingMachine Help

    I took a JAVA class two years ago and haven't used it since. Now my brother is taking a JAVA class and he is asking me questions on how to troubleshoot his assignments. I can help him with VB/ORACLE/SQL but I am still new to JAVA. I was hoping someone here could help me figure out his problem so I can help him. I swear I am not cheating and I am not taking this class. Thank you.

    Here is what he sent me:

    Last time I e-mailed you I probably didn't give you enough enough for someone there to solve the problem...so I'm sending the whole thing!

    First off I have this .java file that came with our assignment...it should enter in the values for me automatically and doesn't need to be changed, but here is the code anyway.

    public class VendingMachineDriver {
    public static void main(String args[]) {
    VendingMachine v = new VendingMachine();
    v.setPrice(27);
    v.insertMoney(75);
    v.printReceipt();
    v.setPrice(173);
    v.insertMoney(75);
    v.insertMoney(50);
    v.insertMoney(200);
    System.out.println("Price = " + v.getPrice());
    System.out.println("Money paid = " + v.getMoneyPaid());
    System.out.println();

    v.setPrice(451);
    v.insertMoney(200);
    v.insertMoney(600);
    v.cancelPurchase();
    v.setPrice(267);
    v.insertMoney(120);
    v.insertMoney(650);
    v.printReceipt();

    System.out.println("Total collected = " + v.getTotalCollected());
    System.out.println();
    }
    }


    Next is the file I've made from scratch....had those comments in there telling where to put stuff when I started it...


    /**
    * Write a description of class VendingMachine here.
    *
    * @author (your name)
    * @version (a version number or a date)
    */
    public class VendingMachine {
    // instance variables - replace the example below with your own
    private int price;
    private int paid;
    private int total;


    /**
    * Constructor for objects of class VendingMachine
    */
    public VendingMachine() {

    // initialise instance variables
    price = 0;
    paid = 0;
    total = 0;
    }
    public VendingMachine(int priceOfItem, int amountPaid) {

    price = priceOfItem;
    paid = amountPaid;
    total = 0;

    }

    /**
    * An example of a method - replace this comment with your own
    *
    * @param y a sample parameter for a method
    * @return the sum of x and y
    */
    public void setPrice(int amount) {

    price = amount;
    }

    public void insertMoney(int amount) {

    paid = paid + amount;

    }
    public int getPrice() {

    return price;

    }

    public int getMoneyPaid() {

    return paid;

    }

    public int getTotalCollected() {

    return total;

    }

    public void reset() {

    price = 0;
    paid = 0;
    total = 0;

    }

    public void cancelPurchase() {

    int cancelPurchase;

    }

    pubic void printReceipt() {

    paid();
    price();
    total = paid - price;
    quarters = total / 25;
    total = total % 25;
    dimes = total / 10;
    total = total % 10;
    nickels = total / 5;
    total = total % 5
    pennies = total;
    }
    }

    Now I'm not positive, but I think that last one won't compile because its not sure where to show the info at, or possibly it doesn't recognize quarters, dimes, etc. Help is needed! Thanks, talk to you later.


    ------------------------
    It looks as if he need to declare and initialize the DIMES, QUARTERS, ETC. But would he do that in printReceipt or in the Main?

  2. #2
    Join Date
    Sep 2002
    Posts
    78
    i fixed the syntax errors now your bro only has to clean up on the logic there are a couple of mistakes .. i think from what i believe each variable name means, maybe the assignment asks for them like that i'm not sure.
    Attached Files Attached Files
    ¿Red Baron?
    [code] /* dont forget code tags! */ [/code]
    Come visit the Café!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center