Computer Science Division 3rd Level Students- CS
Department of Mathematics Course: COMP 301 (Java)
Faculty of Science Date: November 22nd, 2021
Sheet 6
Exercise 1:
Design an abstract class called “Shape” with three abstract methods “getCircum”,
“getArea” and “getVolume”. Try to extend this abstract class to other non-abstract
derived classes according to the following figure:
Be sure your classes have a reasonable constructors, setter and getter methods,
and suitably defined equals and toString methods. Write a program to test all your
methods.
Exercise 2:
The Comparable interface is a predefined Java interface in the java.lang package,
so it is automatically available to your program. The Comparable interface has only the
following method heading that must be overridden in any class implementing the
Comparable interface:
This method is: public int compareTo(Object other);
The method compareTo should return
- a negative number if the calling object “comes before” the parameter other,
- a zero if the calling object “equals” the parameter other, and
- a positive number if the calling object “comes after” the parameter other.
Note:
The “String” class and the wrapper classes “Double”, “Integer”, “Long”. etc.
are already Comparable type. For example:
String s1 = new String (“abc”);
s1.compareTo (“abc”); return 0
s1.compareTo (“xyz”); return -ve value
s1.compareTo (“aaa”); return +ve value
Design a class “GeneralSorting” with a static method “sort” that can sort any array
whose base type implements the Comparable interface.
For example, in the previous labs it is supposed that you built classes “Document” and
“PC” so that you must make these two classes to implement Comparable interface.
Then, test your java code and the static method “sort” on an array of type class
“Document” and another of type class “PC”.
Remark:
You can test your java code and the static method “sort” on an array of type
“String” or “Double” or any other Comparable type class.
Exercise 3:
Make the class type “Shape” to be Comparable type depending on the area of shape that
can be computed using “getArea()” method. Test your java code and the static method
“sort” on an array of type class Shape.