Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create three classes Data, Single, and List, as follows: Create an abstract clas

ID: 3852083 • Letter: C

Question

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.0 Create a class Single which is a subclass of Data, and which will store one double value. 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[] array. 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 TemplateLab7.java file. It creates a list of Data objects (both 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 at appears in a Single or in a List. using valueof(). It should print the line The sum of everything is 35.8

Explanation / Answer

Solution 1:

Please find below the class implementations as directed:

Data.java

public abstract class Data {

double valueOf(){
return 0.0;
}

}

Single.java

public class Single extends Data {

double value;

public Single(double value) {
this.value = value;
}

double valueOf(){
return value;
}
}

List.java

public class List extends Data{

double[] a;

public List(double[] a) {
this.a = a;
}

double valueOf(){
double sumOfValuesInArray =0.0;
for(int i =0; i<a.length;i++){
sumOfValuesInArray+=a[i];
}
return sumOfValuesInArray;
}
}

---------------------------------------------------------------------

Solution 2:

Please provide the required template for TemplateLab7.java so that the required implementation ( adding loop at 'indicated position') can be completed