Review:
CSE148 Object Oriented Programming Homework Set 09
1. Abstract method and Abstract class
2. Interface, Java build-in interfaces: Comparable, Cloneable
a. Overriding compareTo()
b. Shallow copy and deep copy; Override clone() method to perform deep-copy
Write a Java program to:
1. Design an interface MaxMinNum which has 2 methods: int getMaximum();
int getMinimum();
2. Design a class MyNumArray:
(1) The class has the following data field:
int [] data;
(2) The class implements MaxMinNum, Comparable, and Cloneable interfaces (3) Methods:
• Constructors:
no-arg constructor (generate data array of size 16) constructor(int size) (generate data array of size ‘size’)
• int getNumber(int index): return number from data at location index
• void setNumber(int number, int index): set value to array at location index;
• Override equals() method: return true if (1) 2 MyNumArray instances have the array of same size; and (2) the numbers in both arrays are the same (same number at same location);
• Override toString() method, return information of: the size of array, maximum and minimum numbers in array;
• Implement interface Comparable
Compare ‘data’ array in ‘this’ object with ‘data’ in input object:
– compare the maximum numbers in both arrays first, if they are same, then compare the minimum numbers. If both maximums and minimums are the same, return 0;
• Implement interface Cloneable
Perform deep copy (array “data” is a reference)
SCCC CSE148, Spring 2021
•
Implement interface MaxMinNum:
implement methods declared in interface MaxMinNum, return maximum and
minimum numbers in ‘data’ array; (4) Test
• • •
• •
Create 3 MyNumArray objects, add some numbers to each object; Add above objects to ArrayList myArrayNumList;
Display myArrayNumList (iterate over array list and display information of each element in the list)
Sort myArrayNumList, display myArrayNumList again Save below information to a text file:
Number of MyNumArray objects;
Data in each MyNumArray object
Maximum and minimum numbers in each MyNumArray object
(For example, after information in above objects has been successfully written to the file, the file should have the follow text message:
Number of MyNumArray instances: 3
Instance: data: 1, 5, 3, 8, 0; maximum number: 8; minimum number: 0 Instance: data: 12, 31, 80; maximum number: 80; minimum number: 12 Instance: data: 2, 100, 7; maximum number: 100; minimum number: 2
Read from the text file and display information.
•
– – –
SCCC CSE148, Spring 2021