Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Comparable Programming . Suppose you have a pre-existing class called BankAccount that represents users' accounts and money deposited at a given bank. The class has

Comparable Programming. Suppose you have a pre-existing class called BankAccount that represents users' accounts and money deposited at a given bank. The class has the following data and behavior:

Field/Constructor/Method

Description

private String name

name of the person using the account

private int id

ID number of this account

private double balance

amount of money currently in the account

public BankAccount(String name, int id)

makes account with given name/ID and $0.00

public void deposit(double amount)

adds given amount to account's balance

public int getID()

returns the account's ID number

public String getName()

returns the account's name

public double getBalance()

returns the account's balance

public String toString()

returns String such as "Ed Smith: $12.56"

public void withdraw(double amount)

subtracts given amount from account's balance

Make BankAccount objects comparable to each other using the Comparable interface.

use the following rules to implement the compareTo method

  • The bank wants to sort by who has the most money, so accounts are compared by balance.
  • One with a lower balance is considered to be "less than" one with a higher balance.
  • If two accounts have the same balance, they are compared by ID.
  • The one with the lower ID is considered to be "less than" the one with the higher ID.
  • If two accounts have the same balance and ID, they are considered to be "equal." Your method should not modify any account's state. You may assume the parameter passed is not null.

solution

public class BankAccount implements Comparable {

 public int compareTo(Object o) { BankAccount other = (BankAccount)o; if (balance > other.getBalance()) { return 1; } else if (balance < other.getBalance()) { return -1; } else if (id > other.getID()) { return 1; } else if (id < other.getID()) { return -1; } else { return 0; } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Shamkant B. Navathe

7th Edition Global Edition

1292097612, 978-1292097619

Students also viewed these Databases questions

Question

How do Dimensional Database Models differ from Relational Models?

Answered: 1 week ago

Question

What type of processing do Relational Databases support?

Answered: 1 week ago

Question

Describe several aggregation operators.

Answered: 1 week ago