Instructions: Answer the following questions one at a time. After answering each question, check your answer (by clicking on the check-mark icon if it is available) before proceeding to the next question.
Getting Ready: Before going any further, you should:
.java
Files: In most IDEs, .java
files
(i.e., source files) can just be copied into the project.
.class
and .jar
Files:
In some IDEs it is easier to use .class
files and in others it is easier to use a .jar
file
that contains the .class
files. Hence, you have been
provided with both.
See the course "Help" page on your IDE for more information.
Resources: In some IDEs, resources (e.g., images, data files) need to be in a special directory whereas in others they need to be in the working directory. See the course "Help" page on your IDE for more information.
HolidayAccount
class and
Driver1
.
Driver1
.
main()
method in
Driver1
:
// Sort the accounts
Arrays.sort(accounts);
// Print the accounts in the sorted order
for (i=0; i < accounts.length; i++)
{
System.out.println(accounts[i].getAccountID()+"\t"+
accounts[i].getHolder());
}
Driver1
.
Arrays.sort(java.lang.Object[])
Object
array
implement?
HolidayAccount
class contain a
compareTo()
method?
HolidayAccount
class
realizes the Comparable
interface. What
change do you need to make to accomplish this?
HolidayAccount
.
HolidayAccount
class not actually implement the
Comparable
interface?
compareTo()
method and casting it to a
HolidayAccount
within the method.
Driver1
.
compareTo(HolidayAccount other)
method. In other words, change it back to:
public int compareTo(HolidayAccount other)
{
int relation;
relation = 0;
if (this.accountNumber < other.accountNumber)
{
relation = -1;
}
else if (this.accountNumber > other.accountNumber)
{
relation = 1;
}
return relation;
}
HolidayAccount
class:
/**
* Compare the account number on this account to the
* account number on a given account (required by Comparable)
*
* @param other The given account
* @return -1/1 if this account comes before/after the given account
*/
public int compareTo(Object other)
{
return compareTo(other);
}
Comparable
interface?
HolidayAccount
.
HolidayAccount
contain any syntax errors?
Driver1
.
Driver1
contain any syntax errors?
Driver1
.
other
in such a way that
the compareTo(Object)
method calls the appropriate
implementation.
HolidayAccount
. Compile and execute
Driver1
.
HolidayAccount
class do you prefer, the one with just the compareTo(Object)
method, or the one with both a compareTo(HolidayAccount)
method and compareTo(Object)
method? Why?
HolidayAccount
class so that
it has an equals()
method that is passed a
Object
object named other
. Your
implementation must call compareTo()
and must not use
an if
statement or loop.
equals()
method in the
Object
equals()
method accordingly.
How did you implement it now?
Copyright 2021