VendingMachine - System.out.println ("Please enter the...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
/* @author: * Class: ITP 109 * Lab#: 3 * Date modified: 02/03/2010 * Program description: This program will determine the change to be dispensed from a vending * machine. An item in the machine can cost between 25 cents and 1 dollar, and the machine * accepts only a single dollar bill to pay for the item. */ import java.util.Scanner; i public class VendingMachine { p public static void main(String[] args) { //declare all variables that will be used int price=0, change=0, modChange=0, numQuarters=0, numDimes=0, numNickles=0, numPennies=0; n //Constants final int PENNIE = 1; //in cents final int NICKLE = 5; //in cents final int DIME = 10; //in cents final int QUARTER = 25; //in cents final int DOLLAR = 100; //in cents f Scanner keyboard = new Scanner(; S //Getting inputs from the user System.out.println("Vending Machine!");
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: System.out.println ("Please enter the price of items in cents (25-100): "); price = keyboard.nextInt(); p //Calculate the change first, and then I used % to find the remainder change = DOLLAR - price; numQuarters = change/QUARTER; modChange = change%QUARTER; numDimes = modChange/DIME; modChange = modChange%DIME; numNickles = modChange/NICKLE; modChange = modChange%NICKLE; numPennies = modChange/PENNIE; n //Print the results System.out.println("You bought an item for " + price + " cents and gave me a dollar."); System.out.println("Your change (" + change + " cents) is "); System.out.println("Quarters: " + numQuarters); System.out.println("Dimes: " + numDimes); System.out.println("Nickles: " + numNickles); System.out.println("Pennies: " + numPennies); S } }...
View Full Document

This note was uploaded on 03/15/2010 for the course ITP 109x taught by Professor Trinagregory during the Spring '08 term at USC.

Ask a homework question - tutors are online