Create three classes Data, Single, and List, as follows: a. Create an abstract c
ID: 3852105 • Letter: C
Question
Create three classes Data, Single, and List, as follows: a. Create an abstract class Data, which will contain no instance variables, no constructor, and only one method: double valueof () which returns 0.0. b. 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. c. 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.8Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
abstract class Data{
abstract double valueOf();
}
class Single extends Data
{
double val;
public Single()
{
val=2.3;
}
double valueOf()
{
return val;
}
}
class List extends Data
{
double sum=0.0;
public List(double[] a)
{
for(int i=0;i<a.length;i++)
{
sum+=a[i];
}
}
double valueOf()
{
return sum;
}
}
class main
{
public static void main (String[] args) throws java.lang.Exception
{
Single obj=new Single();
System.out.println("The value is "+obj.valueOf());
double[] arrayList = new double[2];
arrayList[0]=4.8;
arrayList[1]=5.8;
List objList=new List(arrayList);
System.out.println("The sum value is "+objList.valueOf());
}
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.