1.Fix the problem by implementing the Comparable interface in BankAccount. The c
ID: 3816826 • Letter: 1
Question
1.Fix the problem by implementing the Comparable interface in BankAccount. The class header needs to specify that we are implementing Comparable interface.BankAccount already implements one interface.To add another add a comma and the new interface name. Add the required method from the interface, compareTo(Object otherObject), as a stub. Now implement the body of the compareTo method (You will need to cast otherObject to a BankAccount, order by balance, If the balances are equal, order by accountId)
2. To compare doubles
returns a negative integer if d1 is smaller
returns 0 if d1 and d2 are equal
returns a positive integer if d2 is larger
Modify BankAccount compareTo method to use Double.compare.
3.Add the Comparable interface to the Student class. Students are ordered by GPA. If the GPA's are the same, they are ordered by name. Uncomment the code in ComparableTester that creates the ArrayList.
4. In ComparableTester, Student objects are in a Comparable array.
This is valid. Student is-a Comparable uncomment the code for testing and run. Fix the problem by casting
5. Change the Student class from the comparable_interface project used in the last lesson to use the generic Comparable interface.
6. Now we want to sort Rectangles by perimeter. Write a class RecangleComparatorByPerimeter that implements Comparator
Provide the compare method
Write a class RecangleComparatorByPerimeterRunner which creates an array of Rectangles and sorts them by perimeter
Print the array
7.Write a new class RecangleComparatorByPerimeterAnonymous to use an annonymous inner class for the Comparator object
*******BankAccount.java************
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount implements Measurable
{
private double balance;
private String accountId;
/**
Constructs a bank account with a given balance.
@param initialBalance the initial balance
@param id the id for this account
*/
public BankAccount(double initialBalance, String id)
{
balance = initialBalance;
accountId = id;
}
/**
* Gets the id for this account
* @returns the id for this account
*/
public String getAccountId()
{
return accountId;
}
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
{
balance = balance + amount;
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
balance = balance - amount;
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
public double getMeasure()
{
return balance;
}
}
********ComparableTester************
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
public class ComparableTester
{
public static void main(String[] args)
{
// sort bank accounts
BankAccount[] accounts =
{
new BankAccount(2000.0, "xyz789"),
new BankAccount(1000.0, "def333"),
new BankAccount(1000.0, "abc123"),
new BankAccount(1500.0, "pqr456"),
};
Arrays.sort(accounts);
for(BankAccount b : accounts)
{
System.out.println(b.getAccountId());
}
//sort students
// ArrayList<Comparable> students = new ArrayList<Comparable>();
//
// students.add(new Student("Amy", 3.4));
// students.add(new Student("Thong", 2.9));
// students.add(new Student("Perdeep", 3.6));
// students.add(new Student("Carlos", 3.6));
// students.add(new Student("Mica", 3.56));
//
// Collections.sort(students);
//
// for (Comparable s: students)
// {
// System.out.println(s.getName()); //***error. Why
// }
}
}
*********Country.java*************
/**
A country with a name and area.
*/
public class Country implements Measurable
{
private String name;
private double area;
/**
Constructs a country.
@param aName the name of the country
@param anArea the area of the country
*/
public Country(String aName, double anArea)
{
name = aName;
area = anArea;
}
/**
Gets the country name.
@return the name
*/
public String getName()
{
return name;
}
/**
Gets the area of the country.
@return the area
*/
public double getArea()
{
return area;
}
/**
Gets the area of the country.
@return the area
*/
public double getMeasure()
{
return area;
}
}
***********Data.java**************
public class Data
{
/**
Computes the average measure of the given objects.
@param objects an array of Measurable objects
@return the average of the measures
*/
public static double average(Measurable[] objects)
{
double sum = 0;
for (Measurable obj : objects)
{
sum = sum + obj.getMeasure();
}
if (objects.length > 0)
{
return sum / objects.length;
}
else
{
return 0;
}
}
/**
Computes the largest of the given objects.
@param objects an array of Measurable objects
@return the object with the largest measure
*/
public static Measurable largest(Measurable[] objects)
{
if (objects.length == 0)
{
return null;
}
Measurable largestSoFar = objects[0];
for (int i = 1; i < objects.length; i++)
{
Measurable current = objects[i];
if (current.getMeasure() > largestSoFar.getMeasure())
{
largestSoFar = current;
}
}
return largestSoFar;
}
}
*************Student.java***************
/**
* Describes a Student with a name and grade point.
*
*/
public class Student implements Measurable
{
private String name;
private double gpa;
private double QUALIFYING_GPA = 3.54;
/**
* Creates a student
* @param theName the name of this student
* @param theGpa the gpa of this student
*/
public Student(String theName, double theGpa)
{
name = theName;
gpa = theGpa;
}
/**
* Gets the name of this student
* @return the name of this student
*/
public String getName()
{
return name;
}
/**
* Gets the GPA of this student
* @return the GPA of this student
*/
public double getGPA()
{
return gpa;
}
public double getMeasure()
{
return gpa;
}
}
**********Measurable.java*************
//HIDE
/**
Describes any class whose objects can be measured.
*/
public interface Measurable
{
/**
Computes the measure of the object.
@return the measure
*/
double getMeasure();
}
Explanation / Answer
For this below are my assumptions: -
1. accountId is a string so I am using compareTo function here. If it can be double but taken as string for use you can edit the code and parse it to double.
2. For point5 in your question, last project is not provided so I dont know what to change So I am ignoring this.
3. As there is no clear output given,for printing of sorted rectangle array, I am printing int the format (length,breadth,perimeter).
4. I have divided your project into two parts. 1st part contains q 1-6. 2nd part contains Q.7. This is done because Q.7 will change the RecangleComparatorByPerimeterRunner class and you can have the old class also for reference and understanding.
-------------------------------------------------------------Part -1-------------------------------------------------------------------------------
Here is your code: -
BankAccount.java
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount implements Measurable,Comparable<BankAccount>
{
private double balance;
private String accountId;
/**
Constructs a bank account with a given balance.
@param initialBalance the initial balance
@param id the id for this account
*/
public BankAccount(double initialBalance, String id)
{
balance = initialBalance;
accountId = id;
}
/**
* Gets the id for this account
* @returns the id for this account
*/
public String getAccountId()
{
return accountId;
}
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
{
balance = balance + amount;
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
balance = balance - amount;
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
public double getMeasure()
{
return balance;
}
@Override
public int compareTo(BankAccount obj) {
// TODO Auto-generated method stub
if(this.getBalance() != obj.getBalance()) {
return Double.compare(this.getBalance(), obj.getBalance());
} else {
return this.getAccountId().compareTo(obj.getAccountId());
}
}
}
ComparableTester.java
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
public class ComparableTester
{
public static void main(String[] args)
{
// sort bank accounts
BankAccount[] accounts =
{
new BankAccount(2000.0, "xyz789"),
new BankAccount(1000.0, "def333"),
new BankAccount(1000.0, "abc123"),
new BankAccount(1500.0, "pqr456"),
};
Arrays.sort(accounts);
for(BankAccount b : accounts)
{
System.out.println(b.getAccountId());
}
//sort students
ArrayList<Comparable> students = new ArrayList<Comparable>();
students.add(new Student("Amy", 3.4));
students.add(new Student("Thong", 2.9));
students.add(new Student("Perdeep", 3.6));
students.add(new Student("Carlos", 3.6));
students.add(new Student("Mica", 3.56));
Collections.sort(students);
for (Comparable s: students)
{
System.out.println(((Student) s).getName()); //***error. Why
}
}
}
Country.java
/**
A country with a name and area.
*/
public class Country implements Measurable
{
private String name;
private double area;
/**
Constructs a country.
@param aName the name of the country
@param anArea the area of the country
*/
public Country(String aName, double anArea)
{
name = aName;
area = anArea;
}
/**
Gets the country name.
@return the name
*/
public String getName()
{
return name;
}
/**
Gets the area of the country.
@return the area
*/
public double getArea()
{
return area;
}
/**
Gets the area of the country.
@return the area
*/
public double getMeasure()
{
return area;
}
}
Data.java
public class Data
{
/**
Computes the average measure of the given objects.
@param objects an array of Measurable objects
@return the average of the measures
*/
public static double average(Measurable[] objects)
{
double sum = 0;
for (Measurable obj : objects)
{
sum = sum + obj.getMeasure();
}
if (objects.length > 0)
{
return sum / objects.length;
}
else
{
return 0;
}
}
/**
Computes the largest of the given objects.
@param objects an array of Measurable objects
@return the object with the largest measure
*/
public static Measurable largest(Measurable[] objects)
{
if (objects.length == 0)
{
return null;
}
Measurable largestSoFar = objects[0];
for (int i = 1; i < objects.length; i++)
{
Measurable current = objects[i];
if (current.getMeasure() > largestSoFar.getMeasure())
{
largestSoFar = current;
}
}
return largestSoFar;
}
}
Student.java
/**
* Describes a Student with a name and grade point.
*
*/
public class Student implements Measurable,Comparable<Student>
{
private String name;
private double gpa;
private double QUALIFYING_GPA = 3.54;
/**
* Creates a student
* @param theName the name of this student
* @param theGpa the gpa of this student
*/
public Student(String theName, double theGpa)
{
name = theName;
gpa = theGpa;
}
/**
* Gets the name of this student
* @return the name of this student
*/
public String getName()
{
return name;
}
/**
* Gets the GPA of this student
* @return the GPA of this student
*/
public double getGPA()
{
return gpa;
}
public double getMeasure()
{
return gpa;
}
@Override
public int compareTo(Student obj) {
// TODO Auto-generated method stub
if(this.getGPA() != obj.getGPA()) {
return Double.compare(this.getGPA(), obj.getGPA());
} else {
return this.getName().compareTo(obj.getName());
}
}
}
Measurable.java
/**
Describes any class whose objects can be measured.
*/
public interface Measurable
{
/**
Computes the measure of the object.
@return the measure
*/
double getMeasure();
}
RecangleComparatorByPerimeter.java
import java.util.Comparator;
public class RecangleComparatorByPerimeter implements Comparator<RecangleComparatorByPerimeter>{
private double length;
private double breadth;
private double perimeter;
public RecangleComparatorByPerimeter(double len,double bre) {
this.length = len;
this.breadth = bre;
this.perimeter = 2*(len+bre);
}
public RecangleComparatorByPerimeter(){
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getBreadth() {
return breadth;
}
public void setBreadth(double breadth) {
this.breadth = breadth;
}
public double getPerimeter() {
return perimeter;
}
public void setPerimeter(double perimeter) {
this.perimeter = perimeter;
}
@Override
public int compare(RecangleComparatorByPerimeter arg0, RecangleComparatorByPerimeter arg1) {
// TODO Auto-generated method stub
return Double.compare(arg0.getPerimeter(), arg1.getPerimeter());
}
}
RecangleComparatorByPerimeterRunner.java
import java.util.Arrays;
public class RecangleComparatorByPerimeterRunner {
public static void main(String[] args) {
RecangleComparatorByPerimeter[] rectangles =
{
new RecangleComparatorByPerimeter(3.0,4.0),
new RecangleComparatorByPerimeter(3.4,7.4),
new RecangleComparatorByPerimeter(3.2,5.7),
new RecangleComparatorByPerimeter(1.3,2.5),
};
Arrays.sort(rectangles, new RecangleComparatorByPerimeter());
for(RecangleComparatorByPerimeter b : rectangles)
{
System.out.println("("+b.getLength()+","+b.getBreadth()+","+b.getPerimeter()+")");
}
}
}
Outputs: -
abc123
def333
pqr456
xyz789
Thong
Amy
Mica
Carlos
Perdeep
(1.3,2.5,7.6)
(3.0,4.0,14.0)
(3.2,5.7,17.8)
(3.4,7.4,21.6)
-------------------------------------------------Part - 2------------------------------------------------------------------------------------------
RecangleComparatorByPerimeterAnonymous.java
import java.util.Comparator;
public class RecangleComparatorByPerimeterAnonymous {
private double length;
private double breadth;
private double perimeter;
public RecangleComparatorByPerimeterAnonymous(double len,double bre) {
this.length = len;
this.breadth = bre;
this.perimeter = 2*(len+bre);
}
public RecangleComparatorByPerimeterAnonymous(){
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getBreadth() {
return breadth;
}
public void setBreadth(double breadth) {
this.breadth = breadth;
}
public double getPerimeter() {
return perimeter;
}
public void setPerimeter(double perimeter) {
this.perimeter = perimeter;
}
//inner Anonymous class
public static final Comparator<RecangleComparatorByPerimeterAnonymous> AnonymousComparator = new Comparator<RecangleComparatorByPerimeterAnonymous>() {
@Override
public int compare(RecangleComparatorByPerimeterAnonymous arg0, RecangleComparatorByPerimeterAnonymous arg1) {
// TODO Auto-generated method stub
return Double.compare(arg0.getPerimeter(), arg1.getPerimeter());
}
};
}
RecangleComparatorByPerimeterRunner.java
public class RecangleComparatorByPerimeterRunner {
public static void main(String[] args) {
RecangleComparatorByPerimeter[] rectangles =
{
new RecangleComparatorByPerimeter(3.0,4.0),
new RecangleComparatorByPerimeter(3.4,7.4),
new RecangleComparatorByPerimeter(3.2,5.7),
new RecangleComparatorByPerimeter(1.3,2.5),
};
RecangleComparatorByPerimeterAnonymous[] rectanglesAnon =
{
new RecangleComparatorByPerimeterAnonymous(3.0,4.0),
new RecangleComparatorByPerimeterAnonymous(3.4,7.4),
new RecangleComparatorByPerimeterAnonymous(3.2,5.7),
new RecangleComparatorByPerimeterAnonymous(1.3,2.5),
};
System.out.println("Normal sorting");
Arrays.sort(rectangles, new RecangleComparatorByPerimeter());
for(RecangleComparatorByPerimeter b : rectangles)
{
System.out.println("("+b.getLength()+","+b.getBreadth()+","+b.getPerimeter()+")");
}
System.out.println("Anonymous inner class sorting sorting");
Arrays.sort(rectanglesAnon, RecangleComparatorByPerimeterAnonymous.AnonymousComparator);
for(RecangleComparatorByPerimeterAnonymous b : rectanglesAnon)
{
System.out.println("("+b.getLength()+","+b.getBreadth()+","+b.getPerimeter()+")");
}
}
}
Output: -
Normal sorting
(1.3,2.5,7.6)
(3.0,4.0,14.0)
(3.2,5.7,17.8)
(3.4,7.4,21.6)
Anonymous inner class sorting sorting
(1.3,2.5,7.6)
(3.0,4.0,14.0)
(3.2,5.7,17.8)
(3.4,7.4,21.6)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.