Question

In: Computer Science

A. Add code (see below for details) to the methods "set" and "get" in the following...

A. Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index] = value; } public String get(int index) { return myData[index]; } public static void main(String[] args) { ArrayTen a = new ArrayTen(); a.set(10, "hello world"); System.out.println (a.get(10)); } } The expected output from your successful modification will be: exception java.lang.IndexOutOfBoundsException java.lang.IndexOutOfBoundsException at ArrayTen.set(ArrayTen.java:10) at ArrayTen.main(ArrayTen.java:25) **Adding code means you are not allowed to modify or delete any of the existing code statements. You must add "try" and "catch" and any additional code needed to fulfill the requirements, without modifying the existing statements.

B. Write a program Median.java to read each file whose name is specified in the command-line arguments. That is, for each command-line argument, open it as a file and read it. The file contents are zero or more lines each containing a list of comma-separated integers, such as 1,2,3,4 or 99,120,33. You should parse each of these integers and save them in an ArrayList (if you prefer you may use an array, but an ArrayList is likely to be easier for this assignment). Once you have filled your array list with all the integers in the line, you must compute the median value, that is, the integer for which half the integers in the line are above it, and half are below it. There are two subtleties that you should handle correctly: - each integer may appear more than once: in 1,1,2,2,3, the median is 2 - with an even number of integers, the median may be the average of two of the integers: in 1,2,3,4, the median is 2.5 You must implement and use a method whose header is one of private static double computeMedian(java.util.ArrayList numbers) or private static double computeMedian(Integer[] numbers) or private static double computeMedian(int[] numbers) to compute and return the median of all the numbers. An algorithm to compute the median is as follows: - for each number in the ArrayList (or array), compare it to all the numbers in the array, computing how many are above it, how many are below it, and how many are the same -- at least one (itself) should be the same. - then if (the number below plus the number equal are greater than the number above) AND (the number below is less than the number equal plus the number above) then this number is the median. For example, in 1,1,2,2,3: - for the number 1, there are 0 numbers below, 2 numbers that are equal, and 3 numbers above, so 1 is not the median. - for the number 2, there are 2 numbers below, 2 numbers that are equal, and 1 numbers above, so 2 is the median. - for the number 3, there are 4 numbers below, 1 number that is equal, and 0 numbers above, so 3 is not the median. On the other hand, if (the number below plus the number equal is the same as the number above) OR (the number below is the same as the number equal plus the number above) then this is one of two numbers, the average of which is the median. For example, in 1,2,3,4,5,2, for the number 2, there is one number below and two numbers equal, which is the same as the three number above. For the number 3, there is one number equal and two numbers above, which is the same as the three numbers below. So in this case the median is the average of 2 and 3: mathematically, (2 + 3) / 2 = 2.5 (remember the division must be done using doubles, otherwise the result will be truncated). To correctly implement this part of the computation, you need to have one or more variables declared outside the main loop, to keep track of whether you have already seen one of the two numbers whose average is the mean. In the example above, once you see 3 (after seeing 2), you should be able to immediately return the median 2.5. Be careful -- if you see another 2 (before seeing the 3), as in processing 2,2,1,3,5,4, you must continue your search past the second 2. Your code must use this algorithm to find the median. There exists a much easier way to find the median, which is to sort the array and look in the middle -- you are not allowed to use sort, and instead you must use the algorithm described here. In addition to finding the median, your program must correctly handle all exceptions that may be generated as the result of files not existing, files not containing comma-separated lists of integers, or any other exceptions. Your program must catch and correctly handle every exception, print a sensible message in each case, and continue on to the next file. Although the two problems get equal credit, this second problem is much harder than the first problem, and requires you to practice many of the things we have learned so far in this course.

Solutions

Expert Solution

A)

Code:

public class ArrayTen {

private String myData[] = new String[10];

public void set(int index, String value) {
 try
 {
 myData[index] = value;
 }
 catch(java.lang.ArrayIndexOutOfBoundsException e)
 {

 throw e;//throw exception
 }
 }

public String get(int index) {
 try
 {
 return myData[index];
 }
 catch (java.lang.ArrayIndexOutOfBoundsException e)
   
 {
   

 throw e;//throw exception

 }
 }

public static void main(String[] args) {
 try
 {
 ArrayTen a = new ArrayTen();
 a.set(11, "hello world");
 System.out.println (a.get(10));
 }
 catch( Throwable e)
 {
 e.printStackTrace(); // print stack trace
  
 }
 }

}

B)

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class Median {


    private static double computeMedian(ArrayList<Integer> numbers) {

        int mediansFound = 0;
        int firstMedianValue = 0;
        int secondtMedianValue = 0;

        for (Integer num : numbers) {
            int aboveCount = 0;
            int belowCount = 0;

            for (Integer n : numbers) {

                if (num == n) continue;
                if (num > n) aboveCount += 1;
                else if (num < n) belowCount += 1;
            }

            if (aboveCount == belowCount) {

                if (mediansFound == 0) {
                    firstMedianValue = num;
                    mediansFound+=1;
                } else if (mediansFound == 1 && firstMedianValue != num) {
                    secondtMedianValue = num;
                    mediansFound += 1;
                    break;
                }
            }
        }

        if (mediansFound == 1) return firstMedianValue * 1.0;
        else return (firstMedianValue + secondtMedianValue) / 2.0;


    }

    public static void main(String[] args) {


        ArrayList<Integer> numbers = new ArrayList<>();

        for (String file : args) {


            try {
                Scanner fileReader = new Scanner(new File(file));
                while (fileReader.hasNextLine()) {
                    String line = fileReader.nextLine();
                    String[] nums = line.split(",");
                    for (String num : nums) {
                        try {
                            numbers.add(Integer.valueOf(num));
                        } catch (Exception e) {

                        }

                    }
                }
                fileReader.close();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        }


        double median = computeMedian(numbers);

        System.out.println("Median Value: " + median);
    }
}
Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

Related Solutions

A. Add code (see below for details) to the methods "set" and "get" in the following...
A. Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index]...
Add code (see below for details) to the methods "set" and "get" in the following class,...
Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index] =...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
How do i get the code below to add an element before another without making the...
How do i get the code below to add an element before another without making the array off one element? (javascript) public void addBefore(double element) { itemCount++; double data[] = new double[this.data.length]; if(currentIndex <= itemCount) { if(currentIndex != 0) { for(int index = currentIndex; index >= itemCount; index ++) { data[index] = this.data[index]; } currentIndex--; data[currentIndex] = element; } if(currentIndex == 0) { data[0] = element; currentIndex = 0; } } }
java code Add the following methods to the LinkedQueue class, and create a test driver for...
java code Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging...
code the following methods: one returning the average weekly hours and a method to add a...
code the following methods: one returning the average weekly hours and a method to add a weeks hours to the array of hours (this means creating a larger array).
Answer IN R CODE to get the following. Using the data below, Create a scatterplot of...
Answer IN R CODE to get the following. Using the data below, Create a scatterplot of y vs x Fit a simple linear regression model using y as the response and plot the regression line (with the data) Test whether x is a significant predictor and create a 95% CI around the slope coefficient. Report and interpret the coefficient of determination. For x=20, create a CI for E(Y|X=20). For x=150, can you use the model to estimate E(Y|X=150)? Discuss. Does...
Add the following methods to the singly list implementation below. int size(); // Returns the number...
Add the following methods to the singly list implementation below. int size(); // Returns the number of nodes in the linked list bool search(string query); // Returns if the query is present in the list void add(List& l); // // Adds elements of input list to front of "this" list (the list that calls the add method) ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- // slist.cpp #include <string> #include "slist.h" using namespace std; Node::Node(string element) : data{element}, next{nullptr} {} List::List() : first{nullptr} {} // Adds to...
Add the following methods to the singly list implementation below. int size(); // Returns the number...
Add the following methods to the singly list implementation below. int size(); // Returns the number of nodes in the linked list bool search(string query); // Returns if the query is present in the list void add(List& l); // // Adds elements of input list to front of "this" list (the list that calls the add method) #include <string> #include "slist.h" using namespace std; Node::Node(string element) : data{element}, next{nullptr} {} List::List() : first{nullptr} {} // Adds to the front of...
We see that this computes the product of two matrices. Add a new kernel code, called...
We see that this computes the product of two matrices. Add a new kernel code, called sum, to compute the sum of the two matrices. #include <stdio.h> #include <math.h> #include <sys/time.h> #define TILE_WIDTH 2 #define WIDTH 6 // Kernel function execute by the device (GPU) __global__ void product (float *d_a, float *d_b, float *d_c, const int n) {    int col = blockIdx.x * blockDim.x + threadIdx.x ;    int row = blockIdx.y * blockDim.y + threadIdx.y ;    float...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT