Step1. Create a Java class named Total, see Hint 1. The class contains a data fi
ID: 3881712 • Letter: S
Question
Step1. Create a Java class named Total, see Hint 1. The class contains a data field, a constructor and two methods.
Step 2. The field is an array of numbers, the array type can be any of the six numeric types of Java, see Hint 2.
Step 3. The constructor takes an argument to initialize the field.
Step 4. Method computeTotal ( ) computes the sum total of all array elements. The return type is double (see Hint 3)
Step5. Method compareTotals( ) checks if the computeTotal() return values obtained for two Total objects agree or not; returns true/false accordingly.
Step 6. Create an Application class having the main method. The concrete types to work with will be determined here.
Step 7. Test the Total class methods running them on four different arrays of types float, int, long and byte; compare each pair and print the results to the console.
Hints and Requirements
1. Make the Total class generic, use the type parameter wherever needed in field declarations and/or method definitions.
2. Number is a class in the Java library and it serves as a superclass for all numeric wrapper classes.
3. T type elements cannot be added. Number has an abstract method doubleValue( ) and each numeric subclass implements the method such that it returns the double type version of the given numeric value.
4. Establish Number as a bound for the generic type (notice the analogy: Number is the same to the classes Integer, Long etc. as Person to Adult, Child etc. in the Fitness project).
5. See if you need a wildcard in the definition of compareTotals ( ). Comment on your experience.
Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Total.java
========
public class Total<T extends Number> {
private T[] nums;
public Total(T[] values)
{
nums =(T[]) new Number[values.length];
for(int i = 0 ; i< values.length; i++)
nums[i] = values[i];
}
public double computeTotal()
{
double sum =0;
for(int i = 0; i < nums.length; i++)
sum += nums[i].doubleValue();
return sum;
}
public boolean compareTotals(Total other)
{
if(computeTotal() == other.computeTotal())
return true;
else
return false;
}
}
Application.java
================
public class Application {
public static void main(String[] args) {
Float[] fArr = {1.5F, 2.5F, 3.5F};
Integer[] iArr = {2, 4, 6, 8, 10};
Long[] lArr = {15L, 20L, 30L, 40L};
Byte[] bArr = {2, 4, 6, 8, 10};
Total<Float> ftotal = new Total<Float>(fArr);
Total<Integer> itotal = new Total<Integer>(iArr);
Total<Long> ltotal = new Total<Long>(lArr);
Total<Byte> btotal = new Total<Byte>(bArr);
System.out.println("Total of Float array: " + ftotal.computeTotal());
System.out.println("Total of Integer array: " + itotal.computeTotal());
System.out.println("Total of Long array: " + ltotal.computeTotal());
System.out.println("Total of Byte array: " + btotal.computeTotal());
if(itotal.compareTotals(ftotal))
System.out.println("Float and Integer types have same total");
else
System.out.println("Float and Integer types have different total");
if(itotal.compareTotals(btotal))
System.out.println("Byte and Integer types have same total");
else
System.out.println("Byte and Integer types have different total");
}
}
output
=======
Total of Float array: 7.5
Total of Integer array: 30.0
Total of Long array: 105.0
Total of Byte array: 30.0
Float and Integer types have different total
Byte and Integer types have same total
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.