i need excet same program Imports: java.util.Scanner Global Variables: Scanner s
ID: 3580059 • Letter: I
Question
i need excet same program
Imports:
java.util.Scanner
Global Variables:
Scanner sc – scanner object to read System.in
Methods:
static void main(String[] args) – process control with loop for multiple runs
static int getCents() – obtain and validate input of change being requested
static void makeChange(int cents) – calculate and display coins given in change
Process:
main(String[] args):
local variables: int cents – variable for change amount being requested
Display welcome message
getCents cents
while loop on cents != 0
makeChange(cents)
getCents cents
Display end message
getCents():
local variables: int c – cents input value to return
do - loop to control input/validation process
Try
How much change? c
c < 0? error message
c > 100? error message
Catch
Illegal input message
Clear input buffer
Set c to sentinel value (to force loop to repeat)
while (c < 0 or c > 100)
return c;
makeChange(int cents):
local variables: int q,d,n,p – for quarters, dimes, nickels, pennies to give
int r – working variable for ‘remaining cents’ to give
set r = cents
Determine Quarters to give:
q = r / 25 (integer division)
adjust r to new remaining value: r = r – (q * 25)
Repeat step 2 for dimes, nickels (in that order)
Remaining r after step 3 is given as pennies
Print results
MakeChange Program Changes for: Part B
Global Variables:
Add: int qoh, doh, noh, and poh for Quarters-on-Hand, Dimes-on-Hand, Nickels-on-Hand, and Pennies-on-Hand
Methods:
Add: int getCoins(String cointype) – to validate and return coins on hand for each ‘cointype’
Specific method changes:
main():
Add logic to request each coin type using getCoins() and place in proper global variable (qoh, doh, etc.)
Int getCoins(String cointype):
Code is virtually the same as getCoins method from prior assignment (calcChange)
makeChange(int cents):
Changes to algorithm: basic process is the same, except after determining each coin count, value must be checked against coins on hand, and adjusted accordingly. So, for prior step 2 you would now have:
Determine Quarters to give:
q = r /25 (integer division)
if q > qoh, adjust q to equal qoh
adjust r as: r = r – (q * 25)
The final step (print results) must also check that r has been worked down to zero: if not, perfect change could not be given from the available set of coins.
Explanation / Answer
Hi, Please find the implementation of Part A.
Please repost second part in separate post.
Please let me know in case of any issue in PART A
import java.util.Scanner;
public class MakeChange {
private static Scanner sc = new Scanner(System.in);
static int getCents(){
int c = -1;
while(c < 0 || c > 100){
try{
System.out.print("How much change? ");
c = sc.nextInt();
if(c < 0 || c >100)
System.out.println("Please enter positive integer value in (0 - 100)");
}catch(Exception e){
sc.nextLine();
c = -1;
}
}
return c;
}
static void makeChange(int cents) {
int q,d,n,p;
int r;
q = cents/25;
r = cents%25;
d = r/10;
r = r%10;
n = r/5;
r = r %5;
p = r;
System.out.println(" Quarters: "+q);
System.out.println("Dimes: "+d);
System.out.println("Nickels: "+n);
System.out.println("Pennies: "+p);
System.out.println();
}
public static void main(String[] args) {
int cents;
System.out.println("Welcome in Cents Change Application ");
cents = getCents();
while(cents != 0){
makeChange(cents);
cents = getCents();
}
System.out.println(" Thanks you using this Application");
}
}
/*
Sample run:
Welcome in Cents Change Application
How much change? 44
Quarters: 1
Dimes: 1
Nickels: 1
Pennies: 4
How much change? 54
Quarters: 2
Dimes: 0
Nickels: 0
Pennies: 4
How much change? -6
Please enter positive integer value in (0 - 100)
How much change? 7
Quarters: 0
Dimes: 0
Nickels: 1
Pennies: 2
How much change? 345
Please enter positive integer value in (0 - 100)
How much change? 99
Quarters: 3
Dimes: 2
Nickels: 0
Pennies: 4
How much change? 0
Thanks you using this Application
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.