LAB #2: ARRAYS, METHODS, FILES


1. Task: Write a Java program using the following methods for a 1-dim array (list):

All methods should take as input parameters the array and its size. Write a driver program to call these methods and get output similar to the sample output below.

 

SAMPLE OUTPUT:

How many elements/list: a

Not an integer! Try again!

How many elements in the list: 3.5

Not an integer! Try again!

How many elements in the list: 16

The list is:

   51    7   33   67   33   61    4  100   27   80    1   91   10   94   62   95

 

Your options are:

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

 1) All even values?

 2) All unique values?

 3) Print min gap between values

 4) Statistics

 5) Print 80% percentile 

 0) EXIT

Please enter your option: 8

 

Your options are:

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

 1) All even values?

 2) All unique values?

 3) Print min gap between values

 4) Statistics

 5) Print 80% percentile 

 0) EXIT

Please enter your option: a

Not an integer! Try again! Please enter your option: 3.5

Not an integer! Try again! Please enter your option: 1

Some values/list are odd.

 

Your options are:

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

 1) All even values?

 2) All unique values?

 3) Print min gap between values

 4) Statistics

 5) Print 80% percentile 

 0) EXIT

Please enter your option: fgfgfd

Not an integer! Try again! Please enter your option: 2

Some values/list appear multiple times

 

Your options are:

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

 1) All even values?

 2) All unique values?

 3) Print min gap between values

 4) Statistics

 5) Print 80% percentile 

 0) EXIT

Please enter your option: 3

The minimum gap between 2 adjacent values is -81

 

Your options are:

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

 1) All even values?

 2) All unique values?

 3) Print min gap between values

 4) Statistics

 5) Print 80% percentile 

 0) EXIT

Please enter your option: 4

   51    7   33   67   33   61    4  100   27   80    1   91   10   94   62   95

The mean for this list is: 51.00

The variance for this list is: 1165.88

The standard deviation for this list is: 34.14

 

Your options are:

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

 1) All even values?

 2) All unique values?

 3) Print min gap between values

 4) Statistics

 5) Print 80% percentile 

 0) EXIT

Please enter your option: 5

The list sorted:

    1    4    7   10   27   33   33   51   61   62   67   80   91   94   95  100

80%-percentile from this list:

  100   95   94

 

Your options are:

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

 1) All even values?

 2) All unique values?

 3) Print min gap between values

 4) Statistics

 5) Print 80% percentile 

 0) EXIT

Please enter your option: 0

Testing completed.

 


2. Task (reuse of code at work!): Rewrite the previous program and make it work when the list is populated with values found in an input file. The name of the file should be input from the user. Handle all possible cases (use try/catch statements). Have a constant for max size and read at most these many values from the file. Your output should be similar to the following samples:

 

SAMPLE OUTPUT #1:

 

Please input the name of the file to be opened: input.tx

 

--- File Not Found! Exit program! ---

SAMPLE OUTPUT #2: For this file holding all values of unwanted types: a nd fdsvfd sdfgvdf 3.5 3.23 sdafs asdasf erqwewr f

 

Please input the name of the file to be opened: input.txt

 

--- The file doesn't contain any integers. Exit program! ---

SAMPLE OUTPUT #3: For this file holding mixed types: 34 a 55 18 47 89 b  45 67 59 abbbb 88 37 20 27 10 78 39 21 n m ghff

 

Please input the name of the file to be opened: input2.txt

 

The list size is: 16

The list is:

   34   55   18   47   89   45   67   59   88   37   20   27   10   78   39   21

 

Your options are:

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

 1) All even values?

 2) All unique values?

 3) Print min gap between values

 4) Statistics

 5) Print 80% percentile 

 0) EXIT

Please enter your option: 3

The minimum gap between 2 adjacent values is -51

 

Your options are:

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

 1) All even values?

 2) All unique values?

 3) Print min gap between values

 4) Statistics

 5) Print 80% percentile 

 0) EXIT

Please enter your option: 2

All values/list are unique values.

 

Your options are:

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

 1) All even values?

 2) All unique values?

 3) Print min gap between values

 4) Statistics

 5) Print 80% percentile 

 0) EXIT

Please enter your option: 1

Some values/list are odd.

 

Your options are:

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

 1) All even values?

 2) All unique values?

 3) Print min gap between values

 4) Statistics

 5) Print 80% percentile 

 0) EXIT

Please enter your option: m

Not an integer! Try again! Please enter your option: 5

The list sorted:

   10   18   20   21   27   34   37   39   45   47   55   59   67   78   88   89

80%-percentile from this list:

   89   88   78

 

Your options are:

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

 1) All even values?

 2) All unique values?

 3) Print min gap between values

 4) Statistics

 5) Print 80% percentile 

 0) EXIT

Please enter your option: wer

Not an integer! Try again! Please enter your option: 4

 

Statistics for this list:

   34   55   18   47   89   45   67   59   88   37   20   27   10   78   39   21

The mean for this list is: 45.88

The variance for this list is: 584.11

The standard deviation for this list is: 24.17

 

Your options are:

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

 1) All even values?

 2) All unique values?

 3) Print min gap between values

 4) Statistics

 5) Print 80% percentile 

 0) EXIT

Please enter your option: 0

Testing completed.


Notes:
A. The lab will NOT be graded, do NOT hand anything in to the instructor.
B. The lab should be completed by the start of the next scheduled lab class. Save the .java files on your disk and e-mail them (attachments) to Vishal Patel at vpatel12@students.towson.edu
Very important: Make sure that you have COSC 237.section, your name, and Lab#2 in the Subject box of your e-mail.
C. In case you have any problems, contact the instructor or the TA for assistance.