This is java and make simple program. CPSC 1100. Thanks This was one of the prob
ID: 3756824 • Letter: T
Question
This is java and make simple program. CPSC 1100. Thanks
This was one of the problems from the first assignment. Correct Change: Write a program to assist a cashier with determining correct change 3. a. b. c. Create a Change Class. Create 4 integer instance variable, one for quarters, dimes, nickels, pennies. Create a constructor that does not have any parameters and sets each of the instance variables to 0. d. Create 4 integer constant variables. One for quarters (ie. private static final int QUARTER_VALUE 25. Do the same thing for dimes, nickels, and pennies. e. Create a calculateQuattere method. This method should accept a single integer (amount) to serves as the argument as used as input to the method. Calculate the number of quarters by dividing amount by the value of quarters and assign it to the quarters instance variable. Use the QUARTER VALUE variable in your calculations see page 137. Using modulus (see page 138-139 of the text) determine the remainder. The remainder should be returned to the calling program. Create 3 more methods like the calculateQuarters for dimes, nickels and pennies. Create 4 get methods. One to return the number of quarters, one for dimes, one for nickels, and one for pennies. f. g. h. Create a ChangeTester i. Import Scanner j. Create a Scanner object. k. Prompt the user to enter the amount. I. Create an integer variable named change and using Scanner get the input from the user. m. Create an object of type Change. n. Create an integer variable called remainder and set it to 0 o. Call the calculateQuarters method and put the return value in the variable called remainder. Example: remamvQbisst.calculateguaters change) p. Call the calulateDimes method and put the return value in the variable called remainder In this case, when you call alculateDimes use remainder as the parameter Repeat sept p for nickels and pennies. Call and print identify each number printed. Example: Quarters: 2 q. getQuarrecsn, eetRines). EetNickels ), and getPei When printing r.Explanation / Answer
import java.io.*;
import java.util.*;
public class CorrectChange {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int dollars = 0;
int quarters = 0;
int dimes = 0;
int nickels = 0;
int pennies = 0;
System.out.print("How much change do you need? ");
int change = in.nextInt();
System.out.print("Your change is : ");
if (change/100 != 0) { //Check if the change is above 100 cents/$1.
dollars = change/100; //Add the number of dollars to the variable.
change = change%100; //The remainer when divided by 100 is how
//many cents are left.
System.out.print(dollars+" dollars, ");
}
if (change/25 != 0) {
quarters = change/25;
change = change%25;
System.out.print(quarters+" quarters, ");
}
if (change/10 != 0) {
dimes = change/10;
change = change%10;
System.out.print(dimes+" dimes, ");
}
if (change/5 != 0) {
nickels = change/5;
change = change%5;
System.out.print(nickels+" nickels, ");
}
pennies = change;
System.out.println("and "+pennies+" pennies.");
}
}
/* Result:
INPUT: 437
OUTPUT: "Your change is : 4 dollars, 1 quarters, 1 dimes, and 2 pennies."
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.