0% found this document useful (0 votes)
18 views7 pages

(Lab-1 Manual) CS-204-DSA - Interfaces, and The ArrayList Class

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

(Lab-1 Manual) CS-204-DSA - Interfaces, and The ArrayList Class

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

GIFT School of Engineering

and Applied Sciences

Fall 2020

CS-204: Data Structures and Algorithms


Lab-1 Manual

Interfaces and the ArrayList Class

v1.0
3/27/2019
Lab-1 Manual 2019

Task #1: Comparable Interface Example


In this task, you are being asked to understand the use of Java Comparable interface.
In this task, you will learn about the Comparable interface of the standard Java library
(java.lang package). The Comparable interface is used to compare two objects. The interface
declares a single compareTo method that has the signature:
int compareTo(Object otherObject);
The call:
a.compareTo(b)
must return a negative number if a should come before b, zero if a and b are the same, and a
positive number if b should come before a.
The Comparable interface has a single method:
public interface Comparable {
int compareTo(Object otherObject);
}//interface

Now open and observe that the class BankAccount implements this interface and provides an
implementation of the compareTo method that compares two account objects based on their
balance. Study this class and understand how the compareTo method is written. Note that we
must cast the BankAccount object from Object as in line # 2 of this method.
Finally, open the UsingBankAcconts class where a test program is written for you that compares
different BankAccount objects.

1. Open the Lab Code folder.


2. Open the BankAccount class. Understand the implementation of the compareTo
method.
3. Open the UsingBankAccounts class. Note the array of BankAccount objects and how
they are compared using the compareTo method.
4. Compile and run the code to see the output.

Page 1
Lab-1 Manual 2019

Task #2: Comparable Interface


In this task, you are being asked to implement the Java Comparable interface and override the
compareTo method to understand how interfaces are used in Java.

NOTE: Write your classes and the main method in separate files.

DO NOT WRITE THE Comparable INTERFACE.

Page 2
Lab-1 Manual 2019

Convert the given UML diagram into classes. Note that bold methods represent overridden
methods in the below given UML diagram.

1. Create all classes having names as per the above diagram, and UsingStudents.java
having the main method.
2. Inside the main method, create 5 Student objects using the overloaded constructor.
Assign appropriate values to the state of all instances.
3. Display the state of all objects.
4. Demonstrate the use of compareTo method and compare these objects. Display the
outputs using appropriate messages.

Page 3
Lab-1 Manual 2019

Task #3: ArrayList Class Example


In this task, you are being asked to understand the use of ArrayList class.
We continue to understand the usage of the ArrayList class (java.util package) as discussed in
the lectures.

The ArrayList class is discussed in Chapter-7 Section 7.13 of your textbook.


Now, study the given ArrayList example paying attention to how to use its various methods,
and write loops to display its contents.

1. Open the Lab Code folder.


2. Open the ArrayListLabDemo class. Understand the use of various methods of this class.
3. Compile and run the code to see the output.

Page 4
Lab-1 Manual 2019

Task #4: ArrayList Class


In this task, you are being asked to use the ArrayList class and write methods that use this class.
NOTE: Write your classes and the main method in separate files.
Convert the given UML diagram into classes.

Page 5
Lab-1 Manual 2019

1. Open the Lab Code folder. Find the Measurable interface and the Student class in that
folder.
2. Complete the implementation of the Student class.
3. Create an UsingStudents class having the main method.
4. Inside the main method, create an ArrayList of at least 5 Student objects using the
overloaded constructor. Assign appropriate values to the state of all instances.
5. Display the state of all objects.
6. Demonstrate the use of all methods as given in the Measurable interface using
appropriate messages.

Page 6

You might also like