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...
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...
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...
1) Given the data set below 25 52 67 23 78 89 57 90 32 77...
1) Given the data set below 25 52 67 23 78 89 57 90 32 77 45 48 62 54 94 69 46 79 40 33 21 57 84 54 23 34 68 63 61 76 87 78 39 50 70 60 32 65 73 45 28 82 66 79 71 80 46 66 24 90 a) Construct a Frequency Distribution with 8 classes and a class width of 10 b) Construct a Relative Frequency distribution from the frequency distribution...
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...
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....
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...
The data below are daily carbon monoxide emissions, in parts per million (ppm). 45, 30, 38,...
The data below are daily carbon monoxide emissions, in parts per million (ppm). 45, 30, 38, 42, 63, 43, 102, 86, 99, 63, 58, 34, 37, 55, 58, 153, 75, 58, 36, 59, 43, 102, 52, 30, 21, 40, 141, 85, 161, 86, 71, 12.5, 20, 4, 20, 25, 170, 15, 20, 15. Data from DASL http://lib.stat.cmu.edu/DASL/Datafiles/Refinery.html. We wish to know if these data suggest that the overall average daily emission is more than 45 ppm. (a) Report an appropriate...
6. Using the class data (see Blackboard file - "Class Survey Data - PSS-10 and GAS")...
6. Using the class data (see Blackboard file - "Class Survey Data - PSS-10 and GAS") calculate the correlation between the GAS – Goal disengagement and the GAS – Goal reengagement scores. Write the results in a statistical statement. 7. Using the class data calculate the correlation between the GAS – Goal disengagement and the PSS-10 scores. Write the results in a statistical statement. 8. Using the class data calculate the correlation between the GAS – Goal reengagement scores and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT