Question

In: Computer Science

Write a Java program 1-)write a program that simulates a vending machine, takes input from the...

Write a Java program

1-)write a program that simulates a vending machine, takes input from the user (money), and returns the change to the user.

The change means

Quarter =   25 cent

Dime = 10 cent

Nickels = 5 cent

Pinneies = 1 cent

2-) What is the output produced by the following code?

  • int result = 11;
  • result /= 2;
  • System.out.println("resu lt is " + result);

Solutions

Expert Solution

1.

Since you have not provided what options does the vending machine should have, I have taken the liberty to use my own set of examples.

Code:

import java.util.*;
import java.io.*;
class Main{
public static void main(String[] args) {
    int Food = runMenu();
    int Price = retrievePrice(Food);
    int change = moneyInserted(Price);
    changeOut(change);
}

public static int runMenu(){
    Scanner keyboard = new Scanner(System.in);
    int choice = 0 ;
    System.out.println("\n\nPlease enter your selection:"
                + "\n1: Snickers \t 125"
                + "\n2: Reeses Cup \t 130"
                + "\n3: Doritoes \t 150"
                + "\n4: Pepsi \t\t 185"
                + "\n5: Exit ");
    choice = keyboard.nextInt();
    return choice;        
}

public static int retrievePrice(int menuChoice){
    if (menuChoice == 1)
        return 125;
    if (menuChoice == 2)
        return 130;
    if (menuChoice == 3)
        return 150;
    if (menuChoice == 4)
        return 185;
    else return 0;
}

public static int moneyInserted(int Price){
    Scanner keyboard = new Scanner(System.in);
    int money = 0;
    System.out.println("Your item costs: " + Price + "\nPlease enter the amount of money:");
    money = keyboard.nextInt();
    while (money < Price){
        System.out.println("Please insert sufficient funds");
        money = money + keyboard.nextInt();
    }
    return money - Price ;
}
public static void changeOut(int change){
    int quarters = 0;
    int dimes = 0;
    int nickels = 0;
    while (change >= 25){
        quarters = quarters + 1;
        change = change - 25;
    }
    while (change >= 10){
        dimes = dimes + 1;
        change = change - 10;
    }
    while (change >= 5){
        nickels = nickels + 1;
        change = change - 5;
    }

    System.out.printf("\nHere's your change:\n%d quarters, %d dimes, %d nickels and %d pennies!",
        quarters, dimes, nickels, change);
}
}

2.

The result will be 5 as a is an integer type variable so its division only gives the quotient value.


Related Solutions

3. Write a program that simulates a vending machine.   The user will be prompted to enter...
3. Write a program that simulates a vending machine.   The user will be prompted to enter a number then a letter. As part of the prompt you must display a message to indicate the number and letter, along with the possible choices. That is, what selections will give user which item.   If the user enters a number or a letter that is not valid, then display the message “Bad Entry” and end the program. (100 pts) Selections with messages. 1a...
Write a program that simulates a vending machine. The machine holds six snack items labeled them...
Write a program that simulates a vending machine. The machine holds six snack items labeled them 1 through 6. The program should initially display a menu of items along with their prices: Vending Machine 1. Roasted Almonds --> $1.25 2. Pretzels --> $1.75 3. Chewing Gum --> $0.90 4. Mints --> $0.75 5. Chocolate bar --> $1.50 6. Cookies --> $2.00 The program then should ask the user to enter the item to purchase along with a sum of money....
Write a java console application,. It simulates the vending machine and ask two questions. When you...
Write a java console application,. It simulates the vending machine and ask two questions. When you run your code, it will do following: Computer output: What item you want? User input: Soda If user input is Soda Computer output: How many cans do you want? User input:            3 Computer output: Please pay $3.00. END The vending machine has 3 items for sale: Soda the price is $1.50/can. Computer should ask “How many cans do you want?” Chips, the price is $1.20/bag....
Using a (GUI interface), write a Java program that simulates an ATM machine with the following...
Using a (GUI interface), write a Java program that simulates an ATM machine with the following options menu: "Welcome" 1. Deposit to account 2. Withdraw 3. Exit
Must be written in JAVA Code Write a program that takes a whole number input from...
Must be written in JAVA Code Write a program that takes a whole number input from a user and determines whether it’s prime. If the number is not prime, display its unique prime factors. Remember that a prime number’s factors are only 1 and the prime number itself. Every number that’s not prime has a unique prime factorization. For example, consider the number 54. The prime factors of 54 are 2, 3, 3 and 3. When the values are multiplied...
Write a java program that simulates thousands of games and then calculate the probabilities from the...
Write a java program that simulates thousands of games and then calculate the probabilities from the simulation results. Specifically in the game, throw two dice, the possible summations of the results are: 2, 3, ..., 12. You need to use arrays to count the occurrence and store the probabilities of all possible summations. Try to simulate rolling two dice 100, 1000, 10,0000 times, or more if needed. Choose one simulation number so that the probabilities you calculated is within 1%...
in Java, write a program that takes an input of 4 numbers and splits them into...
in Java, write a program that takes an input of 4 numbers and splits them into 4 separate lines. EXAMPLE: so if input is 1994 the output should be 1 9 9 4
Write a program that determines the change to be dispensed from a vending machine. An item...
Write a program that determines the change to be dispensed from a vending machine. An item in the machine can cost between 25 cents and 1 dollar, in 5-cents increments (25, 30, 35, . . . 90, 95, or 100), and the machine accepts only a single dollar bill to pay for the item. (Explain with algorithms if you can please!)
In Java please: 5.23 LAB: Seasons Write a program that takes a date as input and...
In Java please: 5.23 LAB: Seasons Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day. Ex: If the input is: April 11 the output is: Spring In addition, check if the string and int are valid (an actual month and day). Ex: If the input is: Blue 65 the output is: Invalid The dates for each season are: Spring:...
Write a Java program that uses printf, and takes user input to find the name of...
Write a Java program that uses printf, and takes user input to find the name of the File. FileCompare (20) Write a program that compares to files line by line, and counts the number of lines that are different. You can use file1.txt and file2.txt when testing your program, but you must ask for the file names in the input. main Interactively requests the names of the two files, after creating the Scanner for the keyboard. Creates a Scanner for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT