comp sci 1000 level https://docs.google.com/document/d/1f9TGqV-a-q2ibu13niV-Pxpz
ID: 3673795 • Letter: C
Question
comp sci 1000 level
https://docs.google.com/document/d/1f9TGqV-a-q2ibu13niV-PxpzfCBESA49TD7Yy6z2sAA/edit?usp=sharing
I'm not sure if I should seperate #1 & #2 or not. If so, please ignore #2.
1. 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 a. 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 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 value0f() 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. c. 2. Start with the TemplateLab6.java file. It creates a list of Data objects (both Single and List) using a Datal] array. Take a look a it. Add a loop at the indicated position which will find and print the sum of all seven doubles that appear in the array, using valueof(). It should print the line The sum of everything is 35.8Explanation / Answer
Program plan:
Here as mentioned in the first problem we need define three classes Data,Single and List.three classe must overide the valueOf() class.with three different definitions.firstly we implement these three classes then complete the problem 2.
Note:we cannot make Data as abstract class because abstract class can have only abstract methods and it cannot be instantiated but can be extended.
hence i am creating a normal Data class with mentioned specifications.
Program:
//importing the required packages
import java.lang.*;
//Definition of Data class
class Data
{
double valueOf()
{
return 0.0;
}
}//end of Data class
//defining the Single class which implements the Data class
class Single implements Data
{
double i;
Single(double k)
{
i=k;
}
double valueOf() //overide of valueOf() method
{
return i;
}
}//end of Single class
//defining the List class also implements the Data class
class List implements Data
{
double n[];
List(double[] a)
{
n=a;
}
double valueOf()//override method in List class
{
double sum=0;
for(int i=0;i<n.length();++i)
{
sum=sum+n[i];
}
}
}
//solution for problem 2.
//creating a class which has main method
public class Template
{
public static void main(String args[])
{
Data[] myData = {
new Single(2.4),
new List(new double[] {3.2,6.8}),
new List(new double[] {1.2,7.9,4.5}),
new Single(9.8)
};
double sum=0;
//creating a loop for Data class objects to access the valueOf() method
for(int i=0;i<mydata.length();++i)
{
sum=mydata[i].valueOf();
}
System.out.println("the Sum of Everything is "+sum);
}
}
sample output:
The sum of Evarything is 35.8
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.