JAVA Please follow the instructions exactly - no more and no less. Pick the best
ID: 3674959 • Letter: J
Question
JAVA
Please follow the instructions exactly - no more and no less. Pick the best data types and follow all Java conventions regarding naming and the use of public and private modifiers. You may assume the user enters an appropriate value when prompted (i.e., you do not have to check that a user entered an integer if that is what you asked for).
1. Create a class PiggyBank. It is to keep track of the number of quarter, half-dollar and dollar coins inside of it. It should have instance variables for the count of each type of coin. It should have a single constructor that takes no parameters. It should have an accessor method, countMoney that takes no parameters and returns the total value (in cents) of the PiggyBank. It should have mutator methods addQuarters, addHalfDollars and addDollars to add quantities of each type of coin ( methods like addQuarters(numPennies) ). The PiggyBank should *not* ask the user for any information. That should be done by the PiggyBankTester program.
2. Create a class PiggyBankTester that is to have 1 method named main. It should contain a loop that in each execution of the loop asks the user for the type of coin and the number of that type of coin to add. That numbers of coins should then be added to the PiggyBank. If the user enters an ‘X’ or 'x', the loop should terminate. After the loop is complete, the program should display the value of the PiggyBank. Do not worry about testing for valid input, but the user may enter the type of coin in either upper or lower case (e.g., 'Q' or 'q').
Here is a sample run of the program:
Welcome to the PiggyBankTester!
What type of coin to add (Q, H, D or X to exit)?
Q
How many do you wish to add?
3
What type of coin to add (Q, H, D or X to exit)?
D
How many do you wish to add?
2
What type of coin to add (Q, H, D or X to exit)?
H
How many do you wish to add?
2
What type of coin to add (Q, H, D or X to exit)?
D
How many do you wish to add?
1
What type of coin to add (Q, H, D or X to exit)?
X
The PiggyBank now contains 475 cents.
Goodbye!
Explanation / Answer
import java.util.Scanner;
public class PiggyBank {
int quater;
int halfDollar;
int dollar;
public PiggyBank() {
}
public void addQuater(int numPennies){
quater+=numPennies;
}
public void addHalfDollar(int numPennies){
halfDollar+=numPennies;
}
public void addDollar(int numPennies){
dollar+=numPennies;
}
public int countMoney(){
return (quater*25) + (halfDollar*50) + (dollar*100);
}
}
class PiggyBankTester{
public static void main(String[] args) {
char c;
int num;
PiggyBank piggyBank = new PiggyBank();
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to the PiggyBankTester!");
while(true){
System.out.println("What type of coin to add (Q, H, D or X to exit)?");
//converting into lower case
c = Character.toLowerCase(sc.next().charAt(0));
if(c=='x')
break;
System.out.println("How many do you wish to add?");
num = sc.nextInt();
switch (c) {
case 'q':
piggyBank.addQuater(num);
break;
case 'h':
piggyBank.addHalfDollar(num);
break;
case 'd':
piggyBank.addDollar(num);
break;
default:
System.out.println("invalid option");
}
}
System.out.println("The PiggyBank now contains "+piggyBank.countMoney()+"cents.");
System.out.println("Goodbye!");
}
}
/*
Output
Welcome to the PiggyBankTester!
What type of coin to add (Q, H, D or X to exit)?
Q
How many do you wish to add?
3
What type of coin to add (Q, H, D or X to exit)?
D
How many do you wish to add?
2
What type of coin to add (Q, H, D or X to exit)?
H
How many do you wish to add?
2
What type of coin to add (Q, H, D or X to exit)?
D
How many do you wish to add?
1
What type of coin to add (Q, H, D or X to exit)?
X
The PiggyBank now contains 475cents.
Goodbye!
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.