Question

In: Computer Science

Copy the program Stats.java to your computer and implement the TBI (To Be Implemented) stats() and...

Copy the program Stats.java to your computer and implement the TBI (To Be Implemented) stats() and checkIfSorted() methods following the instructions given in their method comment blocks.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Stats.java

import java.util.*;

/*
 * This Java application calculates some statistics
 * given an array of integers.
 *
 * @creator gdt
 * @created 02017.12.15
 * @updated 02019.01.21  morphed the assignment
 */

interface StatsConstants {
   // used by print() to format output...
   String PRINT_BEGIN = "[";
   String PRINT_END = "]";
   String PRINT_SEPARATOR = ",";

   // checkIfSorted() return values...
   int UNSORTED = 0;
   int ASCENDING = 1;
   int DESCENDING = 2;
   int SKIP = 3;
}

public class Stats {

   public static void main(String[] argv) {

      int[][] data = { 
         null,
         { },
         { 0 },
         { 1, 2 },
         { 1, 1 },
         { 1, 3, 2 },
         { 5, 5, 5, 5, 5 },
         { 2, 0, 5, 4, 8, 5 },
         { 1, 5, 6, 6, 6, 7, 9 },
         { -7, 0, 0, 3, 3, 3, 4, 4 },
         { -2, -2, 0, 1, 1, 2, 2, 2 },
         { -4, -2, 42, 12, 12, 3, -2 },
      };
      for (int i = 0; i < data.length; i++) {
         switch (checkIfSorted(data[i])) {
            case StatsConstants.SKIP:
               if (data[i] == null)
                  System.out.println("null element\n");
               else if (data[i].length == 0) 
                  System.out.println("empty array\n");
               else
                  System.out.println("???\n");
               continue;
            case StatsConstants.UNSORTED:
               Arrays.sort(data[i]);
               break;
            case StatsConstants.DESCENDING:
               reverseArray(data[i]);
               break;
         }
         printResults(stats(data[i]));
      }
   }

   private static void reverseArray(int[] x) {
      for (int i = 0, j = x.length - 1 ; i < j; i++, j--) {
         int k = x[i];
         x[i] = x[j];
         x[j] = k;
      }
   }

   private static void printArray(int[] x, boolean nl) {
      System.out.print(StatsConstants.PRINT_BEGIN);
      for (int i = 0, j = x.length - 1; i < j; i++)
         System.out.print(x[i] + StatsConstants.PRINT_SEPARATOR);
      System.out.print(x[x.length - 1] + StatsConstants.PRINT_END);
      if (nl) System.out.println();
   }

   private static void printResults(Results r) {
      printArray(r.data, true);
      StringBuffer sb = new StringBuffer("...mean: ");
      sb.append(r.mean).append("; median: "). append(r.median).
         append("; mode: "). append(r.nomode ? "modeless" : r.mode).
         append("; cardinality: ").append(r.cardinality).
         append("; range: ").append(r.range);
      System.out.println(sb);
      System.out.println();
   }

   static class Results {
      public int[] data; 
      public int cardinality;
      public int range;
      public double mean;
      public double median;
      public int mode;
      public boolean nomode;
   }

   /*
    * TBI (To Be Implemented)...
    *
    * Instantiates and returns a Results object that
    * contains the calculations (statistics) for the 
    * int[] parameter.  
    *
    * Note: 'mode' is a single solitary number repeated 
    * more often than any other number. The Results object
    * nomode variable is set to true if there is no mode.
    *
    * The stats() method assumes the int[] is sorted
    * in ascending order.
    *
    * @param data  an array if ints
    * @return a Results object
    */
   private static Results stats(int[] data) {
      
   }

   /*
    * TBI (To Be Implemented)...
    *
    * Checks if the contents of the int[] parameter is sorted.
    * The method returns values defined in interface StatsConstants.
    *
    * @param  data  an array of integers
    * @return SKIP if data is null or the array is empty;
    *         else UNSORTED or DESCENDING or ASCENDING
    */
   private static int checkIfSorted(int[] data) {

   }

}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The program output must match the following. Note: Every output line that starts with ... is followed by a blank line:

null element

empty array

[0]
...mean: 0.0; median: 0.0; mode: 0; cardinality: 1; range: 0

[1,2]
...mean: 1.5; median: 1.5; mode: modeless; cardinality: 2; range: 1

[1,1]
...mean: 1.0; median: 1.0; mode: 1; cardinality: 2; range: 0

[1,2,3]
...mean: 2.0; median: 2.0; mode: modeless; cardinality: 3; range: 2

[5,5,5,5,5]
...mean: 5.0; median: 5.0; mode: 5; cardinality: 5; range: 0

[0,2,4,5,5,8]
...mean: 4.0; median: 4.5; mode: 5; cardinality: 6; range: 8

[1,5,6,6,6,7,9]
...mean: 5.714285714285714; median: 6.0; mode: 6; cardinality: 7; range: 8

[-7,0,0,3,3,3,4,4]
...mean: 1.25; median: 3.0; mode: 3; cardinality: 8; range: 11

[-2,-2,0,1,1,2,2,2]
...mean: 0.5; median: 1.0; mode: 2; cardinality: 8; range: 4

[-4,-2,-2,3,12,12,42]
...mean: 8.714285714285714; median: 3.0; mode: modeless; cardinality: 7; range: 46

Solutions

Expert Solution

Answer:

Progam:

import java.util.*;


interface StatsConstants {
    String PRINT_BEGIN = "[";
    String PRINT_END = "]";
    String PRINT_SEPARATOR = ",";

    int UNSORTED = 0;
    int ASCENDING = 1;
    int DESCENDING = 2;
    int SKIP = 3;
}

public class Stats {

    public static void main(String[] argv) {
        int[][] data = {
                null,
                { },
                { 0 },
                { 1, 2 },
                { 1, 1 },
                { 1, 3, 2 },
                { 5, 5, 5, 5, 5 },
                { 2, 0, 5, 4, 8, 5 },
                { 1, 5, 6, 6, 6, 7, 9 },
                { -7, 0, 0, 3, 3, 3, 4, 4 },
                { -2, -2, 0, 1, 1, 2, 2, 2 },
                { -4, -2, 42, 12, 12, 3, -2 },
        };

        for (int i = 0; i < data.length; i++) {
            switch (checkIfSorted(data[i])) {
                case StatsConstants.SKIP:
                    if (data[i] == null)
                        System.out.println("null element\n");
                    else if (data[i].length == 0)
                        System.out.println("empty array\n");
                    else
                        System.out.println("???\n");
                    continue;
                case StatsConstants.UNSORTED:
                    Arrays.sort(data[i]);
                    break;
                case StatsConstants.DESCENDING:
                    reverseArray(data[i]);
                    break;
            }
            printResults(stats(data[i]));
        }
    }

    private static void reverseArray(int[] x) {
        for (int i = 0, j = x.length - 1 ; i < j; i++, j--) {
            int k = x[i];
            x[i] = x[j];
            x[j] = k;
        }
    }

    private static void printArray(int[] x, boolean nl) {
        System.out.print(StatsConstants.PRINT_BEGIN);
        for (int i = 0, j = x.length - 1; i < j; i++)
            System.out.print(x[i] + StatsConstants.PRINT_SEPARATOR);
        System.out.print(x[x.length - 1] + StatsConstants.PRINT_END);
        if (nl) System.out.println();
    }

    private static void printResults(Results r) {
        printArray(r.data, true);
        StringBuffer sb = new StringBuffer("...mean: ");
        sb.append(r.mean).append("; median: "). append(r.median).
                append("; mode: "). append(r.nomode ? "modeless" : r.mode).
                append("; cardinality: ").append(r.cardinality).
                append("; range: ").append(r.range);
        System.out.println(sb);
        System.out.println();
    }

    static class Results {
        public int[] data;
        public int cardinality;
        public int range;
        public double mean;
        public double median;
        public int mode;
        public boolean nomode;
    }

    //Result of stats
    private static Results stats(int[] data) {
        Results result = new Results();
        int sum = data[0];
        int maxCount = 0, count = 0;

        result.data = data;
        result.cardinality = data.length;
        result.range = data[data.length-1] - data[0];
        result.mode = data[0];
        result.nomode = data.length != 1;

        if (data.length % 2 == 0)
            result.median = (data[data.length/2] + data[(data.length/2) - 1]) / 2.0;
        else
            result.median = data[data.length/2];

        for (int i = 1; i < data.length; i++) {
            sum += data[i];

            if (data[i] == data[i-1]) {
                count++;

                if (count > maxCount) {
                    result.nomode = false;
                    result.mode = data[i];
                    maxCount = count;
                } else if (count == maxCount) {
                    result.nomode = true;
                }
            } else {
                count = 0;
            }
        }

        result.mean = sum / (double) data.length;

        return result;
    }

    // Checks if sorted method

    private static int checkIfSorted(int[] data) {
        if (data == null || data.length == 0)
            return StatsConstants.SKIP;

        if (data[0] < data[data.length - 1]) {
            for (int i = 0; i < data.length-1; i++) {
                if (data[i] > data[i + 1])
                    return StatsConstants.UNSORTED;
            }

            return StatsConstants.ASCENDING;
        } else if (data[0] > data[data.length - 1]) {
            for (int i = 0; i < data.length-1; i++) {
                if (data[i] < data[i + 1])
                    return StatsConstants.UNSORTED;
            }

            return StatsConstants.DESCENDING;
        }

        return StatsConstants.UNSORTED;
    }
}

Out put:


Related Solutions

For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write and implement an interactive C++ program to find...
For this computer assignment, you are to write and implement an interactive C++ program to find and print all prime numbers, which are less than or equal to a given value of n, using the algorithm known as the Sieve of Eratosthenes. A prime number p is an integer greater than 1 that is divisible only by 1 and p (itself). The algorithm begins by initializing a set container to contain all the integers in the range 2 to n....
Describe how you implemented your intervention such as the steps you took to implement your intervention...
Describe how you implemented your intervention such as the steps you took to implement your intervention for the pamphlet on substance abuse resources
Challenge: Number Stats 2 Description: Extend the program you wrote for Number Stats to determine the...
Challenge: Number Stats 2 Description: Extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. Purpose: The purpose of this challenge is to provide experience working with numerical data in a file and generating summary information including the determination of median and mode. It also provides experience adding new functionality to an existing program. Requirements: Extend the program you wrote for Number Stats to determine the median and mode...
Java program to implement circular linked list. NO COPY PASTE ANSWERS plz follow the given template......
Java program to implement circular linked list. NO COPY PASTE ANSWERS plz follow the given template... public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last()...
Counting and Looping The program in LoveCS.java prints “I love Computer Science!!” 10 times. Copy it...
Counting and Looping The program in LoveCS.java prints “I love Computer Science!!” 10 times. Copy it to your directory and compile and run it to see how it works. Then modify it as follows: // **************************************************************** // LoveCS.java // // Use a while loop to print many messages declaring your // passion for computer science // **************************************************************** public class LoveCS { public static void main(String[] args) { final int LIMIT = 10; int count = 1; while (count <= LIMIT){...
In this Java program you will implement your own doubly linked lists. Implement the following operations...
In this Java program you will implement your own doubly linked lists. Implement the following operations that Java7 LinkedLists have. 1. public DList() This creates the empty list 2. public void addFirst(String element) adds the element to the front of the list 3. public void addLast(String element) adds the element to the end of the list 4. public String getFirst() 5. public String getLast() 6. public String removeLast() removes & returns the last element of the list. 7. public String...
You are to implement a program to play “Guess an Animal.”  Your program should construct a tree...
You are to implement a program to play “Guess an Animal.”  Your program should construct a tree with a string at each node.  This tree stores the knowledge that your program currently knows.  At leaf nodes, the strings should contain the names of animals, while interior nodes should contain yes/no questions.  The program should start at the root and ask the question stored at the root.  If the user answers the question no, the program traverses the left branch of the tree, while if the...
Write and test a C program to implement Bubble Sort. . In your C program, you...
Write and test a C program to implement Bubble Sort. . In your C program, you should do: Implement the array use an integer pointer, get the size of the array from standard input and use the malloc function to allocate the required memory for it. Read the array elements from standard input. Print out the sorted array, and don’t forget to free the memory. Debug your program using Eclipse C/C++ CDT.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT