public class chapt9Tester the tester is complete just run
public class chapt9Tester { // the tester is complete, just run it after you finish the rest of the code
public static void main(String[] args) {
// create two bankaccount objects a1 using constructor without parameters, and object a2 using constructor with parameter sending it the amount 2000.50
BankAccount a1 = new BankAccount();
BankAccount a2 = new BankAccount(2000.50);
// create two coin objects c1 using constructor without parameters and c2 using constructor and sending 0.25 and \"Quarter\"
Coin c1 = new Coin();
Coin c2 = new Coin(0.25, \"Quarter\");
// now create an array of 4 elements of type Measurable and load it with the 4 objects that you just created
Measurable marr[] = { a1, a2, c1, c2 };
for ( int i=0; i< marr.length; i++) {
double r = marr[i].getMeasure(); // getMeasure() will behave differently depending on the object that called it
System.out.println(\"Return \" + r);
}
// notice that a variable of type Measure marr can refer to BankAccount object or Coin object without casting.
// however it can only call the methods that are declared in the Measure interface
}
}
// This is the Interface complete the missing code, (if you use Eclipse: click on File -> New -> Interface )
public _______________ Measurable {
double getMeasure();
}
// BankAccount class will use the interface
public class BankAccount ___________________ Measurable{
private double balance;
public BankAccount(double amt) { //constructor with parameter
balance = amt;
}
public BankAccount() { //constructor without parameter, set balance to 0
____________________
}
public void deposit(double amt){
balance = balance + amt;
}
public void withdraw(double amt){
balance = balance - amt;
}
@Override
public double getMeasure() {
return balance;
}
}
// Coin class will use the same interface
public class Coin ________________ ____________________ {
private double value = 1;
private String name = \"Dollar\";
public Coin(double v, String n){ // constructor with two parameters
value = v;
name = n;
}
public _______________ { // constructor without parameters
value = 1;
_____________________;
}
// implement the interface method getMeasure here
public _________ ___________________ {
return value;
}
}
--------------------------------------------------------------------------
Solution
// Measurable.java interface
public interface Measurable {
double getMeasure();
}
// BankAccount.java BankAccount class will use the interface
public class BankAccount implements Measurable{
private double balance;
public BankAccount(double amt) {
//constructor with parameter
balance = amt;
}
public BankAccount() {
//constructor without parameter, set balance to 0
balance = 0.0;
}
public void deposit(double amt){
balance = balance + amt;
}
public void withdraw(double amt){
balance = balance - amt;
}
@Override
public double getMeasure() {
return balance;
}
}
// Coin.java, Coin class will use the same interface
public class Coin implements Measurable {
private double value = 1;
private String name = \"Dollar\";
public Coin(double v, String n){
// constructor with two parameters
value = v;
name = n;
}
public Coin() {
// constructor without parameters
value = 1;
name = \"NULL\";
}
// implement the interface method getMeasure here
public double getMeasure() {
return value;
}
}
// chapt9Tester.java
public class chapt9Tester {
// the tester is complete, just run it after you finish the rest of the code
public static void main(String[] args) {
// create two bankaccount objects a1 using constructor without parameters, and object a2 using constructor with parameter sending it the amount 2000.50
BankAccount a1 = new BankAccount();
BankAccount a2 = new BankAccount(2000.50);
// create two coin objects c1 using constructor without parameters and c2 using constructor and sending 0.25 and \"Quarter\"
Coin c1 = new Coin();
Coin c2 = new Coin(0.25, \"Quarter\");
// now create an array of 4 elements of type Measurable and load it with the 4 objects that you just created
Measurable marr[] = { a1, a2, c1, c2 };
for ( int i=0; i< marr.length; i++) {
double r = marr[i].getMeasure();
// getMeasure() will behave differently depending on the object that called it
System.out.println(\"Return \" + r);
}
}
}
/*
output:
Return 0.0
Return 2000.5
Return 1.0
Return 0.25
*/
![public class chapt9Tester { // the tester is complete, just run it after you finish the rest of the code public static void main(String[] args) { // create two public class chapt9Tester { // the tester is complete, just run it after you finish the rest of the code public static void main(String[] args) { // create two](/WebImages/8/public-class-chapt9tester-the-tester-is-complete-just-run-994981-1761512033-0.webp)
![public class chapt9Tester { // the tester is complete, just run it after you finish the rest of the code public static void main(String[] args) { // create two public class chapt9Tester { // the tester is complete, just run it after you finish the rest of the code public static void main(String[] args) { // create two](/WebImages/8/public-class-chapt9tester-the-tester-is-complete-just-run-994981-1761512033-1.webp)
![public class chapt9Tester { // the tester is complete, just run it after you finish the rest of the code public static void main(String[] args) { // create two public class chapt9Tester { // the tester is complete, just run it after you finish the rest of the code public static void main(String[] args) { // create two](/WebImages/8/public-class-chapt9tester-the-tester-is-complete-just-run-994981-1761512033-2.webp)
![public class chapt9Tester { // the tester is complete, just run it after you finish the rest of the code public static void main(String[] args) { // create two public class chapt9Tester { // the tester is complete, just run it after you finish the rest of the code public static void main(String[] args) { // create two](/WebImages/8/public-class-chapt9tester-the-tester-is-complete-just-run-994981-1761512033-3.webp)