In: Computer Science
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
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////