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

Exercises Create a Java class named Total, see Hint 1. The class contains a data

ID: 3781595 • Letter: E

Question

Exercises

Create a Java class named Total, see Hint 1. The class contains a data field, a constructor and two methods.

The field is an array of numbers, the array type can be any of the six numeric types of Java, see Hint 2.

The constructor takes an argument to initialize the field.

Method computeTotal( ) computes the sum total of all array elements. The return type is double (see Hint 3) .  

Method compareTotals( ) checks if the computeTotal() return values obtained for two Total objects agree or not; returns true/false accordingly.

Create an Application class having the main method. The working type is determined here.

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

Make the Total class generic, use the type parameter in wherever needed in field declarations and/or method definitions.

Number is a class in the Java library and it serves as a superclass for all numeric wrapper classes.

Number has a method doubleValue( ) implemented by each numeric subclass and the method returns the double type version of the numeric value.

Use Number as a bound for the generic types (notice the analogy to Person in the Fitness project).

See if you need a wildcard in the definition of compareTotals( ). Comment on your experience.

PLEASE ANSWER WITH TEXT NOT PICTURE,

-Thanks in advance!

Explanation / Answer

Find the solution below:

package org.learning.chegg;

public class Total <E extends Number> {

  

   private E [] dataArray;

  

   @SuppressWarnings({"unchecked"})

   public Total(int arraySize) {

       this.dataArray = (E[]) new Object[arraySize];

   }

  

   public Total(E [] dataArray) {

       super();

       this.dataArray = dataArray;

   }

   public E [] getDataArray() {

       return dataArray;

   }

   public void setDataArray(E [] dataArray) {

       this.dataArray = dataArray;

   }

  

  

   public double computeTotal(){

      

       double res = 0;

      

       for(E e: dataArray){

           res += e.doubleValue();

       }

      

       return res;

   }

  

   public static boolean compareTotals(Total<? extends Number> o1, Total<? extends Number> o2){

      

       if(o1.computeTotal() == o2.computeTotal()){

           return true;

       }

      

       return false;

      

   }

  

}

package org.learning;

import org.learning.chegg.Total;

public class Application {

  

   public static void main(String[] args) {

      

       System.out.println("Creating Float Total Object {10f, 20f, 5f, 2f, 8f}");

       Float [] floatArr = new Float[5];

       floatArr[0] = 10f;

       floatArr[1] = 20f;

       floatArr[2] = 5f;

       floatArr[3] = 2f;

       floatArr[4] = 8f;

       Total<Float> floatTotal = new Total<Float>(floatArr);

      

       System.out.println("Creating Int Total Object {10, 2, 15, 25, 8}");

       Integer [] intArr = new Integer [5];

       intArr[0] = 10;

       intArr[1] = 2;

       intArr[2] = 15;

       intArr[3] = 25;

       intArr[4] = 8;

       Total<Integer> intTotal = new Total<Integer>(intArr);

      

       System.out.println("Creating Long Total Object {10L, 2L, 15L, 25L, 8L}");

       Long [] longArr = new Long [5];

       longArr[0] = 10L;

       longArr[1] = 2L;

       longArr[2] = 15L;

       longArr[3] = 25L;

       longArr[4] = 8L;

       Total<Long> longTotal = new Total<Long>(longArr);

      

       System.out.println("Creating Byte Total Object {10, 20, 5, 2, 8} ");

       Byte [] byteArr = new Byte [5];

       byteArr[0] = 10;

       byteArr[1] = 20;

       byteArr[2] = 5;

       byteArr[3] = 2;

       byteArr[4] = 8;

       Total<Byte> byteTotal = new Total<Byte>(byteArr);

      

       System.out.println("Now comparing each pair.... ");

      

       System.out.println("Float and Int: "+ Total.compareTotals(floatTotal, intTotal));

       System.out.println("Float and Long: "+ Total.compareTotals(floatTotal, longTotal));

       System.out.println("Float and Byte: "+ Total.compareTotals(floatTotal, byteTotal));

      

       System.out.println("Int and Long: "+ Total.compareTotals(intTotal, longTotal));

       System.out.println("Int and Byte: "+ Total.compareTotals(intTotal, byteTotal));

      

       System.out.println("Long and Byte: "+ Total.compareTotals(longTotal, byteTotal));

   }

  

  

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote