Name Task 1The Comparable Interface In todays class we show
Name: ___________________________
Task 1—The Comparable Interface
In today’s class, we showed how to compare two circles by implementing the Comparable interface. Now, we want to compare two BankAccount objects by implementing the same interface.
Download the BankAccount.java and implement the Comparable interface by modifying the class header first:
public class BankAccount implements Comparable <BankAccount>
Implement the method required by the Comparable interface within the BankAcount Class. Skeleton of the method is provided.
public int compareTo(BankAccount other)
{
//Replace this return with your own implementation. //Return 0, -1, 1 respectively for different situations
return 0;
}
Now we should be able to compare two BankAccount objects. Create a driver program that instantiates two bank accounts with different balances. Invoke the compareTo method to compare them. Output which account has a larger balance.
Task 2—Implement Our Own Interface
In the previous task, since Comparable interface is already implemented in the Java API, we do not need to declare it but use it directly. In this task, we will create our own interface, which is not provided in Java library. Let’s start with a simple example.
Say we have a collection of animal classes, with each class represents a kind of animal. They all share a common ability of “speak”. Dog speak in the way of “Woof! Woof!”, cats speak in the way of “Meow! Meow!”, etc. In order to capture this common characteristic, we need an interface named Speakable:
public interface Speakable
// put the method header here
Think about what method is needed for Speakable interface? (what method can describe the speakable feature? )
We simply need one method speak() to print out the way that the animal “speaks”. Put the method header in the interface body. This method does not take any parameter and does not return any value.
Do you need define the body part of this method here? No, you will implement the details in each animal class later.
BE NOTED: Adding an interface is different from adding a class. You need to click File tabàNewàInterface, to add an interface file to the project.
Now download the Dog.java and Cat.java. Have them implement the Speakable interface by
Modifying the header of each class to implement the interface.
Implementing the speak() method in each class. Take the Dog class for example:
public void speak()
{
System.out.println(\"Woof! Woof!\");
}
Write a tester class to create a Dog object and a Cat object. Invoke the speak method and check the output.
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount
{
private double balance;
//constructor
public BankAccount(double initialBalance)
{
balance = initialBalance;
}
public void deposit(double amount)
{
double newBalance = balance + amount;
balance = newBalance;
}
public void withdraw(double amount)
{
double newBalance = balance - amount;
balance = newBalance;
}
public double getBalance()
{
return balance;
}
/**
Compares two bank accounts.
return 1 if this bank account has a greater balance than the other one,
-1 if this bank account is has a smaller balance than the other one,
and 0 if both bank accounts have the same balance
*/
public int compareTo(BankAccount other)
{
//Replace this return with your own implementation
return 0;
}
}
------------------------------
//Created by James Vanderhyde, 12 April 2011
// Copied from Horstmann Lab Manual
public class Cat
{
private String name;
public Cat(String name)
{
this.name = name;
}
public String toString()
{
return \"Cat: \" + name;
}
}
------------------------
//Created by James Vanderhyde, 12 April 2011
// Copied from Horstmann Lab Manual
public class Dog
{
private String name;
public Dog(String name)
{
this.name = name;
}
public String toString()
{
return \"Dog: \" + name;
}
}
| Name: ___________________________ |
Solution
Hi Friend, I have answered first question.
Please repost second question in separate post.
Please let me know in case of any issue.
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount
{
private double balance;
//constructor
public BankAccount(double initialBalance)
{
balance = initialBalance;
}
public void deposit(double amount)
{
double newBalance = balance + amount;
balance = newBalance;
}
public void withdraw(double amount)
{
double newBalance = balance - amount;
balance = newBalance;
}
public double getBalance()
{
return balance;
}
/**
Compares two bank accounts.
return 1 if this bank account has a greater balance than the other one,
-1 if this bank account is has a smaller balance than the other one,
and 0 if both bank accounts have the same balance
*/
public int compareTo(BankAccount other)
{
if(this.balance < other.balance)
return -1;
else if(this.balance > other.balance)
return 1;
else
return 0;
}
}
public class BankAccountTest {
public static void main(String[] args) {
// creating two BankAccount object
BankAccount ba1 = new BankAccount(456.76);
BankAccount ba2 = new BankAccount(700);
int status = ba1.compareTo(ba2);
if(status == -1)
System.out.println(\"Account1 is < than Account2\");
else if(status == 1)
System.out.println(\"Account1 is > than Account2\");
else
System.out.println(\"Account1 is = than Account2\");
}
}
/*
Sample Output:
Account1 is < than Account2
*/




