Question
Questions 6-9 refer to the following incomplete definition of the PiggyBank class. public class PiggyBank { /** Constructor- initial izes nickels, dimes, and quarters to
Questions 6-9 refer to the following incomplete definition of the PiggyBank class.
public class PiggyBank {
/** Constructor- initial izes nickels, dimes, and quarters to 0. */
public PiggyBank() {. . }
/** Adds nickels to the bank. param count the number of nickels to add */
public void addNickels(int count) { . . }
/** Adds dimes to the bank. param count the number of dimes to add */
public void addDimes(int count) {. .}
/** Adds quarters to the bank. param count the number of quarters to add */
public void addQuarters(int count) { . . }
/** Returns total value of all the coins in the bank. return the total amount of money in the bank */
public double getTotal(){ ... }
/** Takes count dimes from the bank. param count the number of dimes to take from the bank */
public void subtractDimes(int count) {
// Code goes here
}
private static final double NICKEL_VALUE = 0.05;
private static final double DIME_VALUE = 0.1;
private static final double QUARTER_VALUE = 0.25;
private int nickels; // Number of nickels in piggy bank private int dimes;
// Number of dimes in piggy bank private int quarters;
// Number of quarters in piggy bank
6. The code for method subtractDimes should be:
a. dimes = dimes - DIME_VALUE * count;
b. dimes = dimes - count;
c. double amount dimes - count; return amount;
d. int amount = dimes - 1; return amount;
e. int amount = dimes - DIME VALUE * count; return amount;
7. A client program is to print the total value of the coins in the PiggyBank object myBank. Which of the following code segments will complete the intended task?
I. double total Money = myBank.getTotal(); System.out.println(totalMoney);
II. double totalMoney = nickels * NICKEL_VALUE + dimes * DIME_VALUE + quarters * QUARTER_VALUE; System.out.println(totalMoney);
III. double totalMoney = nickels * 0.05 + dimes * 0.10 + quarters * 0.25; System.out.println(totalMoney);
a. I only
b. II only
c. III only
d. I and III only
e. II and III only
8. After the following code is executed, what are the values of c and d?
PiggyBank p = new PiggyBank();
PiggyBank q = p;
p.addNickels(5);
q.addNickels(3);
double c = p.getTotal();
double d = q.getTotal();
a. C = 5 d = 3
b. c = 0.25 d = 0.15
c. c = 0.25 d = 0.25
d. c = 0.15 d = 0.15
e. c = 0.4 d = 0.4
9. Consider the following code segment
1 int numberOfCoins = 5;
2 double taxRate = 4;
3 double number= 3.0 ;
4 PiggyBank yourBank;
5 PiggyBank myMoney = new PiggyBank() ;
6 taxRate = numberOfCoins;
7 numberOfCoins = number;
Which of the following statements is true?
a. A compile-time error occurs in line 2 because 4 is an int and not a double.
b. A compile-time error occurs in line 4 because an object is not being created with new.
c. An error occurs in line 6 because numberOfCoins and taxRate are not compatible types.
d. An error occurs in line 7 because a double cannot be assigned to an int.
e. The program will compile without errors.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started