COP 2006 Assignment 3 Task Write a Java program that does th

COP 2006 Assignment #3

Task: Write a Java program that does the following:

1) Program shall get one parameter from the command line and parse it into int variable named

numElements. For example:

c:\\> java hw5679 4

executes the Java program “hw5679” with “4” as the parameter.

• Cmd line parameters in Eclipse:

From the \"Run\" menu \"Run Configurations...\" or Debug Configurations if debugging. Select

\"Arguments\" tab. Type in the values for each argument in the \"Program Arguments” text box for

the above example, you would just type in 4 at the text box.

2) If numElements is 0 or negative, program shall print “Array size must be positive” and exit.

Otherwise, program shall declare and instantiate (with new) onedimensional arrays array1 and

array2. Both arrays are double and each has length equal to numElements from step 1.

3) Program shall use a loop to read each element of array1 and of array2 from the user. For each

element with index i, the program shall display the following prompt:

“Please enter a double for array1[<i>] and one for array 2[<i>]:”

Where <i> is the index of each specific element (starting at 0).

4) Program shall use method maxArray() to return the maximum value of all the values in the array

parameter (returns double). Program shall call the method for each array and print the following:

“Array1 max: <value1>, Array2 max: <value2>.”

Where value1 and value2 are the maximum values for the respective array.

Page 2 of 2

5) Program shall use a void method sumDiffArrays() to print the sum of the differences of the two

arrays, elementbyelement. For example, if array1={5.0,4.0} and array2={1.0,2.0}, the sum of the

differences is 6.0 = (51) + (42). Program shall call the method passing the two arrays as

parameters, method prints:

“Sum of the array differences: <sumvalue>.”

6) Program shall handle all of the following exceptions with an appropriate message for each one

(see examples below for messages):

a) missing numbers in command line (ArrayIndexOutOfBoundsException),

b) not an integer in the command line (NumberFormatException).

c) inappropriate values entered for the array elements (not numbers

InputMismatchException),

See examples below for what statement is printed; the program shall exit in any of these cases.

A particular try block should contain only the code that might cause the specific exception; in

other words, you should NOT place all/most of your code in one try block –this will result in

points off.

* Here are a few example runs assuming the program is “hw3” *

C:\\>java hw3 // See (6)a above

Command Line Parameter missing!

C:\\java hw3 hello // See (6)b above

Please enter an integer for the Command Line Parameter!

C:\\java hw3 4.5 // See (6)b above

Please enter an integer for the Command Line Parameter!

c:\\java>java hw3 0

Array size must be positive

c:\\java>java hw3 2

Array size must be positive

C:\\>java hw3 2

Please enter a double value for array1[0] and for array2[0]: a b

Please enter double values! // See (6)c above

C:\\>java hw3 2

Please enter a double value for array1[0] and for array2[0]: 5 1

Please enter a double value for array1[1] and for array2[1]: 4 2

Array1 max: 5.0, Array2 max: 2.0

Sum of the array differences: 6.0

C:\\>java hw3 3

Please enter a double value for array1[0] and for array2[0]: 5 1

Please enter a double value for array1[1] and for array2[1]: 10.2 7

Please enter a double value for array1[2] and for array2[2]: 0 1

Array1 max: 10.2, Array2 max: 7.0

Sum of the array differences: 8.2

Solution

import java.util.*;

public class hw5679{
    public static void main(String []args)
    {
        int numElements;
        try
        {
            numElements = Integer.parseInt(args[0]);
            if(numElements <=0)
            {
                System.out.println(\"Array size must be positive\");
            }
            else
            {
                double[] array1 = new double[numElements];
                double[] array2 = new double[numElements];
                Scanner scan = new Scanner(System.in);
                Scanner scan2 = new Scanner(System.in);
                for(int i = 0; i < numElements; i++)
                {
                    System.out.println(\"Please enter a double for array1[\"+i+\"] and one for array2[\" +i+\"]:\");
                    try
                    {
                        array1[i] = scan.nextDouble();
                        array2[i] = scan2.nextDouble();
                    }
                    catch(InputMismatchException im)
                    {
                        System.out.println(\"Please enter double values! \");
                    }
                }
                double value1 = maxArray(array1);
                double value2 = maxArray(array2);
                System.out.println(\"Array1 max: \" + value1 + \" Array2 max: \" + value2);
                sumDiffArrays(array1, array2);
            }
        }
        catch(ArrayIndexOutOfBoundsException aio)
        {
            System.out.println(\"Command Line Parameter missing!\");
        }
        catch(NumberFormatException nf)
        {
            System.out.println(\"Please enter an integer for the Command Line Parameter!\");
        }
    }
  
    public static double maxArray(double[] array)
    {
        double smallest = array[0];
        double largest = array[0];
             
        for(int i=1; i< array.length; i++)
        {
            if(array[i] > largest)
                largest = array[i];
            else if (array[i] < smallest)
                smallest = array[i];
        }
        return largest;
    }
  
    public static void sumDiffArrays(double[] a, double[] b)
    {
        double c = 0.0;
        for(int i = 0; i< a.length; i++)
        {
            c = c + (Math.abs(a[i] - b[i]));
        }
        System.out.println(\"Sum of the array differences: \" + c);
    }
}

COP 2006 Assignment #3 Task: Write a Java program that does the following: 1) Program shall get one parameter from the command line and parse it into int variab
COP 2006 Assignment #3 Task: Write a Java program that does the following: 1) Program shall get one parameter from the command line and parse it into int variab
COP 2006 Assignment #3 Task: Write a Java program that does the following: 1) Program shall get one parameter from the command line and parse it into int variab
COP 2006 Assignment #3 Task: Write a Java program that does the following: 1) Program shall get one parameter from the command line and parse it into int variab

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site