Question

# 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).
```
```package projectone;
import java.util.*;

public class DataAnalysis {

static Set<String> Data_NaN(Set<String> set) {
Set<String> set2 = new HashSet<String>();
for (String temp : set) {
temp = temp.replaceAll("[^0-9]", "");

if (!(set.isEmpty())) {
}

}
return set2;

}

public static void main(String args[]) {
// create empty set
Set<String> set = new HashSet<String>();
// {3, 25, 33, 21, 55, 43, 78, 31, 33, 75, 43, 11, 36, 4, 10, 99, A, B, C}
// add values one by one in set
// print input set
System.out.println("Set: " + set);```

}

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

NOTE: A SET DOES NOT STORE DUPLICATE VALUES, HENCE IF WE WANT TO FIND MODE WE HAVE TO USE ARRAYLIST , THE CODE FOR MODE FROM A ARRAY LIST IS ALSO GIVEN

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

CODE TO COPY : IT DOES NOT CONTAIN DUPLICATES AND OUTPUT VARIES FOR MODE AND MEDIAN ALSO

import java.util.*;

public class DataAnalysis {

static Set<String> Data_NaN(Set<String> set) {
Set<String> set2 = new HashSet<String>();
for (String temp : set) {
temp = temp.replaceAll("[^0-9]", "");

if (!(set.isEmpty()) && !temp.isBlank()) {
}

}
return set2;

}

public static void main(String args[]) {
// create empty set
Set<String> set = new HashSet<String>();
// {3, 25, 33, 21, 55, 43, 78, 31, 33, 75, 43, 11, 36, 4, 10, 99, A, B, C}
// add values one by one in set
// print input set
System.out.println("Set: " + set);
set = Data_NaN(set);
System.out.println("using Data_Nan method: " + set);
double median = Data_media(set);
System.out.println("Median of the set: " + median);
Set<String> mode = Data_mode(set);
System.out.println("Mode of the set is:" + mode);
List<Integer> sorted = Data_SortedArray(set);
System.out.println("Sorted set is: " + sorted);
}

private static List<Integer> Data_SortedArray(Set<String> set) {
Set<String> y = new HashSet<String>();
List<Integer> x = new ArrayList<Integer>();
for (String s : set) {
if (!s.isBlank())
}
Collections.sort(x);
return x;
}

private static Set<String> Data_mode(Set<String> set) {
Set<String> y = new HashSet<String>();
List<Integer> x = Data_SortedArray(set);
int maxCount = 0;
Integer maxValue = null;
for (int i = 0; i < x.size(); ++i) {
int cnt = 0;
for (int j = 0; j < x.size(); ++j) {
if (x.get(j) == x.get(i))
++cnt;
}

if (cnt > maxCount) {
maxCount = cnt;
maxValue = x.get(i);
}
}

for (int i = 0; i < x.size(); ++i) {
int cnt = 0;
for (int j = 0; j < x.size(); ++j) {
if (x.get(j) == x.get(i))
++cnt;
}

// System.out.println("c :"+cnt+" max:"+maxCount+" : "+maxValue);
if (cnt == maxCount && maxCount!=1) {
maxValue = x.get(i);
}
}
return y;
}

private static double Data_media(Set<String> set) {
Set<String> x = new HashSet<String>(set);
List<Integer> sorted = Data_SortedArray(x);
if(sorted.size()%2==0)
{
return sorted.get(sorted.size()/2 -1) + sorted.get(sorted.size()/2 +1) / 2;
}else
{
return sorted.get(sorted.size()/2);
}
}
}

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

CODE : USING ARRAY LIST TO ALLOW DUPLICATES INORDER TO CAL MODE, MEDIAN

import java.util.*;

public class DataAnalysis {

static ArrayList<String> Data_NaN(ArrayList<String> set) {
ArrayList<String> set2 = new ArrayList<String>();
for (String temp : set) {
temp = temp.replaceAll("[^0-9]", "");

if (!(set.isEmpty()) && !temp.isBlank()) {
}

}
return set2;

}

public static void main(String args[]) {
// create empty set
ArrayList<String> set = new ArrayList<String>();
// {3, 25, 33, 21, 55, 43, 78, 31, 33, 75, 43, 11, 36, 4, 10, 99, A, B, C}
// add values one by one in set
// print input set

System.out.println("Set: " + set);
set = Data_NaN(set);
System.out.println();

System.out.println("using Data_Nan method: " + set);
double median = Data_media(set);
System.out.println();

System.out.println("Median of the set: " + median);
List<Integer> sorted = Data_SortedArray(set);
System.out.println();

System.out.println("Sorted set is: " + sorted);
Set<String> mode = Data_mode(set);
System.out.println();

System.out.println("Mode of the set is:" + mode);

}

private static List<Integer> Data_SortedArray(ArrayList<String> set) {
ArrayList<String> y = new ArrayList<String>();
List<Integer> x = new ArrayList<Integer>();
for (String s : set) {
if (!s.isBlank())
}
Collections.sort(x);
return x;
}

private static Set<String> Data_mode(ArrayList<String> set) {
Set<String> y = new HashSet<String>();
List<Integer> x = Data_SortedArray(set);
int maxCount = 0;
Integer maxValue = null;
for (int i = 0; i < x.size(); ++i) {
int cnt = 0;
for (int j = 0; j < x.size(); ++j) {
if (x.get(j) == x.get(i))
++cnt;
}

if (cnt > maxCount) {
maxCount = cnt;
maxValue = x.get(i);
}
}

for (int i = 0; i < x.size(); ++i) {
int cnt = 0;
for (int j = 0; j < x.size(); ++j) {
if (x.get(j) == x.get(i))
++cnt;
}

// System.out.println("c :"+cnt+" max:"+maxCount+" : "+maxValue);
if (cnt == maxCount && maxCount!=1) {
maxValue = x.get(i);
}
}
return y;
}

private static double Data_media(ArrayList<String> set) {
List<Integer> sorted = Data_SortedArray(set);
if (sorted.size() % 2 == 0) {
return sorted.get(sorted.size() / 2 - 1) + sorted.get(sorted.size() / 2 + 1) / 2;
} else {
return sorted.get(sorted.size() / 2);
}
}
}

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

EXPLANATION

// FOR SET's

private static List<Integer> Data_SortedArray(Set<String> set) { //CREATING A METHOD WHICH TAKES SET AND RETURNS LIST WHICH IS SORTED, BECAUSE A SET ELEMENTS CANNOT BE SORTED
Set<String> y = new HashSet<String>(); // CRREATING A SET OF STRING
List<Integer> x = new ArrayList<Integer>(); // CREATING A SET OF INT
for (String s : set) { // FOR EVERY STRING S
if (!s.isBlank()) // IF S IS NOT EMPTY
}
Collections.sort(x); // SORT THE LIST
return x; //RETURN THE LIST
}

=================================================================================

//FOR SET'S IT RETURNS MODE, SINCE THERE CAN BE MORE THAN ONE MODE IT RETURNS A SET

*** NOTE: SINCE A SET DOES NOT HAVE DUPLICATES IT RETURNS FIRST SMALLEST ELEMENT ***

private static Set<String> Data_mode(Set<String> set) { // TAKES IN SET AS PARAMETER
Set<String> y = new HashSet<String>(); // TO STORE SET
List<Integer> x = Data_SortedArray(set); // CONVERTING SET TO INT
int maxCount = 0; // TO KEEP MAX COUNT
Integer maxValue = null; // TO STORE MAC VALUE
for (int i = 0; i < x.size(); ++i) { // FOR LOOP TO FIND MAX NUMBER AND ITS COUNT
int cnt = 0; // SET COUNT AS 0
for (int j = 0; j < x.size(); ++j) { // FOR LOOP TO CHECK FOR FREQUENCY
if (x.get(j) == x.get(i)) // IF THE NUMBER IS SAME
++cnt; //INCREASE COUNT
}

if (cnt > maxCount) { // IF COUNT IS GREATER THAN MAX
maxCount = cnt; //SET MAX COUNT
maxValue = x.get(i); // SET MAX VALUE
}
}

// FOR LOOP TO LOOK FOR SAME FREQUENCY NUMBERS AND STORING IN SET

for (int i = 0; i < x.size(); ++i) {
int cnt = 0;
for (int j = 0; j < x.size(); ++j) {
if (x.get(j) == x.get(i)) // IF THE NUMBER IS SAME INCREASE COUNT
++cnt;
}

// System.out.println("c :"+cnt+" max:"+maxCount+" : "+maxValue);
if (cnt == maxCount && maxCount!=1) { // IF FREQUENCY IS GREATER THAN 1 AND SAME
maxValue = x.get(i);
}
}
y.add(String.valueOf(maxValue)); // CHANGE NUMBER TO STRING
return y; //RETURN THE SET
}

=================================================================================

//RETURNS THE MEDIAN
private static double Data_media(Set<String> set) {
Set<String> x = new HashSet<String>(set); // CREATING A SET
List<Integer> sorted = Data_SortedArray(x); //TAKE THE SORTED LIST AND ASSIGN IT AS SORTED
if(sorted.size()%2==0) // IF THERE ARE EVEN NUMBER OF ELEMENTS
{
return sorted.get(sorted.size()/2 -1) + sorted.get(sorted.size()/2 +1) / 2;// RETURN THE MEAN OF THE NUMBERS
}else
{
return sorted.get(sorted.size()/2); // ELSE RETURN THE ELEMENT IN THE MIDDLE
}
}

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

FOR LIST

private static List<Integer> Data_SortedArray(ArrayList<String> set) {//CREATING A METHOD WHICH TAKES LIST AND RETURNS LIST WHICH IS SORTED, BECAUSE A ArrayList ELEMENTS CANNOT BE SORTED
ArrayList<String> y = new ArrayList<String>(); // CREATING A ARRAY LIST
List<Integer> x = new ArrayList<Integer>(); // CREATING A list OF INT
for (String s : set) { // FOR EVERY STRING S
if (!s.isBlank()) // IF S IS NOT EMPTY
}
Collections.sort(x); // SORT THE LIST
return x; //RETURN THE LIST
}

============================================================================
//FOR list'S IT RETURNS MODE, SINCE THERE CAN BE MORE THAN ONE MODE IT RETURNS A SET

private static Set<String> Data_mode(ArrayList<String> set) {// TAKES IN Arraylist AS PARAMETER
Set<String> y = new HashSet<String>(); // TO STORE SET
List<Integer> x = Data_SortedArray(set); // CONVERTING SET TO INT LIST
int maxCount = 0; // TO KEEP MAX COUNT
Integer maxValue = null; // TO STORE MAC VALUE
for (int i = 0; i < x.size(); ++i) { // FOR LOOP TO FIND MAX NUMBER AND ITS COUNT
int cnt = 0; // SET COUNT AS 0
for (int j = 0; j < x.size(); ++j) { // FOR LOOP TO CHECK FOR FREQUENCY
if (x.get(j) == x.get(i)) // IF THE NUMBER IS SAME
++cnt; //INCREASE COUNT
}

if (cnt > maxCount) { // IF COUNT IS GREATER THAN MAX
maxCount = cnt; //SET MAX COUNT
maxValue = x.get(i); // SET MAX VALUE
}
}

// FOR LOOP TO LOOK FOR SAME FREQUENCY NUMBERS AND STORING IN SET

for (int i = 0; i < x.size(); ++i) {
int cnt = 0;
for (int j = 0; j < x.size(); ++j) {
if (x.get(j) == x.get(i)) // IF THE NUMBER IS SAME INCREASE COUNT
++cnt;
}

// System.out.println("c :"+cnt+" max:"+maxCount+" : "+maxValue);
if (cnt == maxCount && maxCount!=1) { // IF FREQUENCY IS GREATER THAN 1 AND SAME
maxValue = x.get(i);
}
}
y.add(String.valueOf(maxValue)); // CHANGE NUMBER TO STRING
return y; //RETURN THE SET
}

==============================================================================================

//RETURNS THE MEDIAN
private static double Data_media(ArrayList<String> set) {
List<Integer> sorted = Data_SortedArray(set); // CREATING A list using the set
if(sorted.size()%2==0) // IF THERE ARE EVEN NUMBER OF ELEMENTS
{
return sorted.get(sorted.size()/2 -1) + sorted.get(sorted.size()/2 +1) / 2;// RETURN THE MEAN OF THE NUMBERS
}else
{
return sorted.get(sorted.size()/2); // ELSE RETURN THE ELEMENT IN THE MIDDLE
}
}

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

OUTPUT

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

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

#### Earn Coins

Coins can be redeemed for fabulous gifts.

##### Need Online Homework Help?

Most questions answered within 1 hours.