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())) {
                set2.add(temp);
            }

        }
        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
        set.add("03");
        set.add("25");
        set.add("33");
        set.add("21");
        set.add("55");
        set.add("43");
        set.add("78");
        set.add("31");
        set.add("33");
        set.add("75");
        set.add("43");
        set.add("11");
        set.add("36");
        set.add("04");
        set.add("10");
        set.add("99");
        set.add("A");
        set.add("B");
// print input set
        System.out.println("Set: " + set);

}

Homework Answers

Answer #1

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

PLEASE WAIT ADDING THE COMPLETE EXPLANATION AND OUTPUT PICS

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()) {
               set2.add(temp);
           }

       }
       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
       set.add("03");
       set.add("25");
       set.add("25");
       set.add("33");
       set.add("21");
       set.add("55");
       set.add("43");
       set.add("78");
       set.add("31");
       set.add("33");
       set.add("75");
       set.add("43");
       set.add("11");
       set.add("36");
       set.add("04");
       set.add("10");
       set.add("99");
       set.add("A");
       set.add("B");
// 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())
               x.add(Integer.parseInt(s));
       }
       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) {
                    y.add(String.valueOf(maxValue));
                    maxValue = x.get(i);
                }
            }
            y.add(String.valueOf(maxValue));
            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()) {
               set2.add(temp);
           }

       }
       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
       set.add("03");
       set.add("25");
       set.add("25");
       set.add("33");
       set.add("21");
       set.add("55");
       set.add("43");
       set.add("78");
       set.add("31");
       set.add("33");
       set.add("75");
       set.add("43");
       set.add("11");
       set.add("36");
       set.add("04");
       set.add("10");
       set.add("99");
       set.add("A");
       set.add("B");
// 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())
               x.add(Integer.parseInt(s));
       }
       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) {
               y.add(String.valueOf(maxValue));
               maxValue = x.get(i);
           }
       }
       y.add(String.valueOf(maxValue));
       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
               x.add(Integer.parseInt(s)); // ADD IT INT LIST
       }
       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
                    y.add(String.valueOf(maxValue)); // ADD THE NUMBER INTO THE SET
                    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
               x.add(Integer.parseInt(s)); // ADD IT INT LIST
       }
       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
                    y.add(String.valueOf(maxValue)); // ADD THE NUMBER INTO THE SET
                    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

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

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

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
A data set of 50 comprehension scores is included below. Please answer the following questions and/or...
A data set of 50 comprehension scores is included below. Please answer the following questions and/or complete the following tasks. 12, 15, 11, 16, 21, 25, 21, 8, 6, 2, 22, 26, 27, 36, 34, 33, 38, 42, 44, 47, 54, 55, 45, 56, 53, 57, 45, 45, 45, 47, 43, 31, 12, 14, 15, 16, 22, 29, 54, 56, 57, 59, 54, 56, 43, 44, 41, 42, 7, 29 1. Create a frequency distribution for the data set using...
Data Set Height Weight Age Shoe Size Waist Size Pocket Change 64 180 39 7 36...
Data Set Height Weight Age Shoe Size Waist Size Pocket Change 64 180 39 7 36 18 66 140 31 9 30 125 69 130 31 9 25 151 63 125 36 7 25 11 68 155 24 8 31 151 62 129 42 6 32 214 63 173 30 8 34 138 60 102 26 6 25 67 66 180 33 8 30 285 66 130 31 9 30 50 63 125 32 8 26 32 68 145 33...
The data collection is conducted by randomly selecting 51 persons whose ages are between 25-30 and...
The data collection is conducted by randomly selecting 51 persons whose ages are between 25-30 and interviewing the average time they spend on Instagram in a day. What is the probability that people ages between 25-30 spend time using Instagram for more than one hour in a day? I need a PMF equation The collected data: Number of Person Time Number of Person Time Number of Person Time Number of Person Time 1 120 14 90 26 120 39 70...
Using the accompanying Student Grades​ data, construct a scatter chart for midterm versus final exam grades...
Using the accompanying Student Grades​ data, construct a scatter chart for midterm versus final exam grades and add a linear trendline. What is the​ model? If a student scores 7878 on the​ midterm, what would you predict her grade on the final exam to​ be? Student Midterm Final Exam 1 75 64 2 85 91 3 80 68 4 88 83 5 76 60 6 67 80 7 78 74 8 95 94 9 67 61 10 93 87 11...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
have a java application need to create an application which is able to do some analysis...
have a java application need to create an application which is able to do some analysis on temperature data stored in a data file. You will be given the “temperatures.dat” data file which contains the data you must analyze. The analysis you’ll need to do is: Total number of data points Find coldest temperature Find warmest temperature Find average temperature Find the frequency of each temperature Find the most frequent temperature Find the least frequent temperature All classes must be...
Student Grades Student Test Grade 1 76 62 2 84 90 3 79 68 4 88...
Student Grades Student Test Grade 1 76 62 2 84 90 3 79 68 4 88 84 5 76 58 6 66 79 7 75 73 8 94 93 9 66 65 10 92 86 11 80 53 12 87 83 13 86 49 14 63 72 15 92 87 16 75 89 17 69 81 18 92 94 19 79 78 20 60 71 21 68 84 22 71 74 23 61 74 24 68 54 25 76 97...
TODO 1: Constructor. It assigns the songList member variable to a new instance of an ArrayList...
TODO 1: Constructor. It assigns the songList member variable to a new instance of an ArrayList that stores Song objects.. TODO 2: Implement the isEmpty method. Takes no parameters. Returns true if there are no songs on the list, false otherwise. TODO 3: Implement the addSong method. This method takes a Song object as a parameter and does not return a value. It adds the song to the songList. TODO 4: Implement the getSongListAsString method. This method takes no parameters....
using System; public static class Lab5 { public static void Main() { // declare variables int...
using System; public static class Lab5 { public static void Main() { // declare variables int inpMark; string lastName; char grade = ' '; // enter the student's last name Console.Write("Enter the last name of the student => "); lastName = Console.ReadLine(); // enter (and validate) the mark do { Console.Write("Enter a mark between 0 and 100 => "); inpMark = Convert.ToInt32(Console.ReadLine()); } while (inpMark < 0 || inpMark > 100); // Use the method to convert the mark into...
Do the TODOs in SongFileAccessor.java. It inherits from FileAccessor class. TODO 1: Implement the processLine method....
Do the TODOs in SongFileAccessor.java. It inherits from FileAccessor class. TODO 1: Implement the processLine method. When the text file is processed, each line of text will be passed to processLine . Each line contains 4 fields: title, album, artist, and play time. The album field is optional. Each field is separated by a comma. TODO 2: Implement the songToCSVString method. This method takes a Song object as a parameter and returns a String which is the csv representation of...