Question

In: Computer Science

Suppose you have a set of data as shown below: {3, 25, 33, 21, 55, 43,...

Suppose you have a set of data as shown below:

{3, 25, 33, 21, 55, 43, 78, 31, 33, 75, 43, 11, 36, 4, 10, 99, A, B, C}

Write a Java class called "DataAnalysis" that has the following methods:

Data_max: this method computes and returns the maximum of the numerical data;
Data_average: this method computes and returns the average of the numerical data

Solutions

Expert Solution

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

NOTE: WHILE USING SETS WE DON'T GET DPLICATE ELEMENTS HENCE AVERAGE CHANGES

I GAVE BOTH THE CODES WHICH USES SET AND LIST , THE FIRST CODE IS USING SET AND THE SECOND ONE IS USING LIST

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

CODE TO COPY : USING SET

import java.util.HashSet;
import java.util.Set;

public class DataAnalysis {

   public static void main(String[] args) {
       Set<String> s = new HashSet<String>();
       s.add("3");
       s.add("25");
       s.add("33");
       s.add("21");
       s.add("55");
       s.add("43");
       s.add("78");
       s.add("31");
       s.add("33");
       s.add("75");
       s.add("43");
       s.add("11");
       s.add("36");
       s.add("4");
       s.add("10");
       s.add("99");
       s.add("A");
       s.add("B");
       s.add("C");

       System.out.println(s.toString());
       int max = Data_max(s);
       System.out.println("Max number from the set is: " + max);
       double average = Data_average(s);
       System.out.println("Average of numerical set is: " + average);
   }

   private static double Data_average(Set<String> s) {
       int sum = 0;
       int n, count = 0;
       for (String element : s) {
           try {

               n = Integer.parseInt(element);
               sum += n;
               count++;
           } catch (Exception e) {
           }
       }
       return sum / count;
   }

   private static int Data_max(Set<String> s) {
       int max = -999999999;
       int n;
       for (String element : s) {
           try {

               n = Integer.parseInt(element);
               if (n > max) {
                   max = n;
               }
           } catch (Exception e) {
           }
       }
       return max;
   }

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

IF YOU WANT TO ALLOW DUPLICATES YOU CAN USE THE BELOW PROGRAMME

CODE TO COPY: USING LIST

import java.util.ArrayList;

public class DataAnalysis {

   public static void main(String[] args) {
       ArrayList<String> s = new ArrayList<String>();
       s.add("3");
       s.add("25");
       s.add("33");
       s.add("21");
       s.add("55");
       s.add("43");
       s.add("78");
       s.add("31");
       s.add("33");
       s.add("75");
       s.add("43");
       s.add("11");
       s.add("36");
       s.add("4");
       s.add("10");
       s.add("99");
       s.add("A");
       s.add("B");
       s.add("C");

       System.out.println(s.toString());
       int max = Data_max(s);
       System.out.println("Max number from the set is: " + max);
       double average = Data_average(s);
       System.out.println("Average of numerical set is: " + average);
   }

   private static double Data_average(ArrayList<String> s) {
       int sum = 0;
       int n, count = 0;
       for (String element : s) {
           try {

               n = Integer.parseInt(element);
               sum += n;
               count++;
           } catch (Exception e) {
           }
       }
       return sum / count;
   }

   private static int Data_max(ArrayList<String> s) {
       int max = -999999999;
       int n;
       for (String element : s) {
           try {

               n = Integer.parseInt(element);
               if (n > max) {
                   max = n;
               }
           } catch (Exception e) {
           }
       }
       return max;
   }

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

EXPLANATION : FOR BOTH THE CODES METHOD EXPLANATION IS SAME

NOTE: USE ONLY SET OR LIST.... YOU CANNOT USE SET IN METHOD WHILE USING LIST AND VISEVERSA

    private static double Data_average(ArrayList<String> s) { // creating a method named Data_average which takes Array List in this method

   private static double Data_average(Set<String> s) { // creating a method named Data_average which takes SET in this method
       int sum = 0; // set sum as 0
       int n, count = 0; // creating count and n
       for (String element : s) { // for each element
           try { // try block to convert the string input into number

               n = Integer.parseInt(element); // change the string into number
               sum += n; // increase sum
               count++; // increase number of count
           } catch (Exception e) {
           }
       }
       return sum / count; // return the average
   }

   private static int Data_max(ArrayList<String> s) { // this method declaration uses list as input

   private static int Data_max(Set<String> s) { // this method uses set as input
       int max = -999999999; // set max as some min number
       int n; // take n
       for (String element : s) { // take each string element from the set or list
           try {

               n = Integer.parseInt(element); //change the string to number
               if (n > max) {// if the number n is greater than max
                   max = n; //set max as n
               }
           } catch (Exception e) {
           }
       }
       return max; //return the max
   }

public class DataAnalysis { //creating a class DataAnalysis
   public static void main(String[] args) { //creating a main method

       Set<String> s = new HashSet<String>(); // creating a set of strings.. in order to use sets use this
       ArrayList<String> s = new ArrayList<String>(); // creating a array list of string ... to allow duplicates use this

//adding elements in the set or list is same
       s.add("3");
       s.add("25");
       s.add("33");
       s.add("21");
       s.add("55");
       s.add("43");
       s.add("78");
       s.add("31");
       s.add("33");
       s.add("75");
       s.add("43");
       s.add("11");
       s.add("36");
       s.add("4");
       s.add("10");
       s.add("99");
       s.add("A");
       s.add("B");
       s.add("C");

       System.out.println(s.toString()); //printing the list or set
       int max = Data_max(s); // get the max
       System.out.println("Max number from the set is: " + max); // print this statement and max
       double average = Data_average(s); // pass the set or list and get the average
       System.out.println("Average of numerical set is: " + average); // print the average
   }

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

OUTPUT

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

IF YOU HAVE ANY PROBLEM REGARDING THE SOLUTION PLEASE COMMENT BELOW I WILL HELP YOU

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Related Solutions

Suppose you have a set of data as shown below: {3, 25, 33, 21, 55, 43,...
Suppose you have a set of data as shown below: {3, 25, 33, 21, 55, 43, 78, 31, 33, 75, 43, 11, 36, 4, 10, 99, A, B, C} Write a Java class called "DataAnalysis" that has the following methods: Data_NaN: this method filters nonnumerical data, for example, A, B, and C will be filtered out by this method; Data_min: this method computes and returns the minimum of the numerical data;
Suppose you have a set of data as shown below: {3, 25, 33, 21, 55, 43,...
Suppose you have a set of data as shown below: {3, 25, 33, 21, 55, 43, 78, 31, 33, 75, 43, 11, 36, 4, 10, 99, A, B, C} Write a Java class called "DataAnalysis" that has the following methods: Data_media: this method computes and returns the median of the numerical data; Data_mode: this method computes and returns the mode of the numerical data; Data_SortedArray: this method rearranges and returns the data in the increasing order (i.e., smallest to largest).
Here is a set of sample data. 3 87 7 57 15 90 25 55 68...
Here is a set of sample data. 3 87 7 57 15 90 25 55 68 70 89 20 26 93 9 5 86 23 37 52 96 4 91 Base on the data above, identify the 5 number summary _ , _ ,_ , _ , _
Consider the following data. 21, 21, 37, 33, 38, 21, 37 (a) If you were to...
Consider the following data. 21, 21, 37, 33, 38, 21, 37 (a) If you were to construct a normal probability plot by hand for the above data what are the numerical values (in order) of the first five numbers that would go on the y-axis? Separate your answers with a comma. (use Z table)
For the data set shown below, complete parts (a) through (d) below. X 3 4 5...
For the data set shown below, complete parts (a) through (d) below. X 3 4 5 7 8 Y 3 5 8 12 13 (a) Find the estimates of Bo and B1. Bo=bo= _____ (Round to three decimal places as needed.) B1=b1= ______(Round to four decimal places as needed.) (b) Compute the standard error the point estimate for se= ____ (c) Assuming the residuals are normally distributed, determine Sb1=____ (Round to four decimal places as needed.) (d) Assuming the residuals...
For the data set shown below, complete parts (a) through (d) below. X 3 4 5...
For the data set shown below, complete parts (a) through (d) below. X 3 4 5 7 8 Y 4 7 6 12 15 (a) Find the estimates of Bo and B1. Bo=bo= _____ (Round to three decimal places as needed.) B1=b1= ______(Round to four decimal places as needed.) (b) Compute the standard error the point estimate for se= ____ (c) Assuming the residuals are normally distributed, determine Sb1=____ (Round to four decimal places as needed.) (d) Assuming the residuals...
the data set shown​ below, complete parts​ (a) through​ (d) below. x 3 4 5 7...
the data set shown​ below, complete parts​ (a) through​ (d) below. x 3 4 5 7 8 y 5 7 8 12 13 ​(a)  Find the estimates of beta 0 and beta 1. beta 0almost equalsb 0equals nothing ​(Round to three decimal places as​ needed.) beta 1almost equalsb 1equals nothing ​(Round to three decimal places as​ needed.)
the data set shown​ below, complete parts​ (a) through​ (d) below. x 3 4 5 7...
the data set shown​ below, complete parts​ (a) through​ (d) below. x 3 4 5 7 8 y 5 7 6 12 13 ​(a)  Find the estimates of beta 0 and beta 1. beta 0almost equalsb 0equals nothing ​(Round to three decimal places as​ needed.) beta 1almost equalsb 1equals nothing ​(Round to three decimal places as​ needed.)(a)  Find the estimates of beta 0 and beta 1. beta 0almost equalsb 0equals ??​(Round to three decimal places as​ needed.) beta 1almost equalsb...
Consider the following data: 43 54 55 63 67 68 69 77 85 Suppose that the...
Consider the following data: 43 54 55 63 67 68 69 77 85 Suppose that the last value is actually 115 instead of 85. What effect would this new maximum have on the median of the data? increase the value of the median decrease the value of the median no effect Approximately, what z-score divides the lower 75% of the data from the upper 25%? z = 0.75   z = 0.675   z = - 0.675   z = -0.25   none of...
Consider the following set of ordered pairs. x 33 11 55 44 y 44 33 44...
Consider the following set of ordered pairs. x 33 11 55 44 y 44 33 44 44 ​a) Calculate the slope and​ y-intercept for these data. ​b) Calculate the total sum of squares​ (SST). ​c) Partition the sum of squares into the SSR and SSE.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT