- Inheritance, polymorphism, instanceof, casting
Notes:
- The three exercises are cumulative each builds on the previous one.
- Only one of the three exercises is required.
- Try to complete the Bronze and Silver exercises. The Gold exercise is trickier, as usual.
Creating a class hierarchy
- Create three classes Data, Single, and List, as follows:
- Create an abstract class Data, which will contain no instance variables, no constructor, and only one method: double valueOf() which returns 0.
- Create a class Single which is a subclass of Data, and which will store one double Provide a constructor to initialize the value. Override the valueOf() method so that it returns this value.
- Create a class List which is a subclass of Data, and which will store a double[] Provide a constructor List(double[] a) which will initialize this array. Override the valueOf() method so that it returns the sum of all the doubles in the array. Note that this will always be a full array, not a partially full array. There will be no separate length variable.
- Start with the java file. It creates a list of Data objects (a mixture of Single and List) using a Data[] myData array. Take a look at it. Add a loop at the indicated position which will find and print the sum of every number that appears in myData, whether it appears in a Single or in a List, using valueOf(). It should print the line
The sum of everything is 35.8
- Add a length() method to the List class which returns the size of the list stored in the object.
You are not allowed to add a length() method to the Data or Single classes. (This would be a logical thing to do, but it would destroy the purpose of the question.)
- Add more code to the java file at the indicated position which will find and print the total number of values that appear anywhere in myData. It should print the line There are 7 values in total. as well as the output line from the Bronze exercise.
- Look at the file java. Note that the Data[] array has been replaced by an Object[] array instead. This Object array contains Single and List objects, but it also contains Strings and other types as well.
- Paste your code from the Bronze and Silver exercises into this file at the places shown they remain unchanged.
- Complete the public static Data[] convert(Object[] objects) method, which will convert the Object[] array objects into a Data[] array as follows:
- The two arrays should have the same length.
- Any Single or List objects should be placed unchanged into the new array. (Use only a shallow copy.)
- Any String should become a List object containing all of the numbers that can be found (as separate tokens) in the String. Use a Scanner to scan the String for numbers. Non-numbers should be ignored. [Note: any number will give true for hasNextDouble().] Assume that the resulting List will always have from 0 to MAX_LIST_SIZE values in it. Remember that a List object must always be a full list, not a partially-full list.
- Any other kind of object should be changed into a List object containing a length-0 array.
- The new result should be:
The sum of everything is 45.4 There are 11 values in total.
Reviews
There are no reviews yet.