Q3 Complete the following code and run to demonstrate the us
Q3. Complete the following code, and run to demonstrate the use of Interface. (10 points)
The main class tester is given to you, complete the code in the Interface and the two Classes.
Notice when coding Interface using Eclipse choose New Interface, NOT New Class
Here is a sample Run:
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 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
Solution
//creating interface
public interface Measurable {
public double getMeasure();
}
//BankAccount class will use the same interface
public class BankAccount implements Measurable {
private double balance;
public BankAccount(double amt) {
this.balance = amt;
}
// constructor without parameter, set balance to 0
public BankAccount() {
balance = 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 implements Measurable {
private double value = 1;
private String name = \"Dollar\";
public Coin(double v, String n) { // constructor with two parameters
value = v;
name = n;
}
// constructor with no arguments
public Coin() {
value = 1;
}
@Override
public double getMeasure() {
return value;
}
}
//tester class
public class Chapter9Tester {
public static void main(String[] args) {
// creating the objects of bank account
BankAccount a1 = new BankAccount();
BankAccount a2 = new BankAccount(2000.50);
// creating the objects of coin
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


