Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

It looks for a public static method called main that has exactly that signature: public static void main( String[] args ). However, what exactly is

It looks for a public static method called main that has exactly that signature: public static void main( String[] args ). However, what exactly is this reference args called? First, this is a parameter so it is up to you to select the name of the parameter, you can call it xxx if you want to. I prefer using the name args, which is the customary way of naming this parameter in the Unix operating system. Now, what is it? It is simply a reference to an array of String objects. Have you ever attempted to print its content? Write a new program called Command containing a main method. It will be easier to see what is going on if your program prints some information when it starts its execution and when it ends (the exact content of these print statements is not important so be creative!). In between these two statements, using a loop to traverse the array and print the content of each cell. Your program will be more informative if you print the elements of the array one per line. You might as well print the position of the element within the array. Here is the result of my experiments.

> javac Command.java > java Command bonjour true 1121 "java intro" bravo Start of the program. Argument 0 is bonjour Argument 1 is true Argument 2 is 1121 Argument 3 is java intro Argument 4 is bravo End of the program. > java Command Start of the program. End of the program. > java Command 1 Start of the program. Argument 0 is 1 End of the program. > java Command dude Start of the program. Argument 0 is dude End of the program.

As you can see, the operating system has handed in to your program an array (of Strings) containing all the arguments following the name of the class (here Command) on the command line. Quotes can be used for grouping strings together, e.g. java intro above. The other important concept to understand is that all the elements of the array are all String, although one element is spelled true, this is the String that contains the letters t, r, u, e. Similarly, although 1121 is a number, in the above context, this is the String made of 1, 1, 2, 1.

Question 1

Write the program Command described above

7 Arrays (1)

Lets try something different. We now want to do some simple array manipulations. We are going to write a new program ArrayTool which can do a few things on arrays. We are interested in arrays of double. Specifically, we will use the following array in this program:

double[] valuesArray; valuesArray = new double[]{100.0,34.0,72.0,56.0,82.0,67.0,94.0};

We can base our code on the class Sort provided above. The first step is to create a method printHigherOrLower which has two parameters, one array of double xs, and one value cutOffValue, of type double. So the declaration of the method is as follows:

public static void printHigherOrLower(double[] xs, double cutOffValue){ // code goes here }

This method goes through the values stored in xs, from the first one to the last one, and prints for each value a message stating if that value is lower or higher than the parameter cutOffValue.

For example, with the array valuesArray above, and a cutOffValue of 72.5, the method will print:

The entry 0 (100.0) is higher than 72.5 The entry 1 (34.0) is lower than 72.5 The entry 2 (72.0) is lower than 72.5 The entry 3 (56.0) is lower than 72.5 The entry 4 (82.0) is higher than 72.5 The entry 5 (67.0) is lower than 72.5 The entry 6 (94.0) is higher than 72.5

Question 2

Write the program ArrayTool described above. Test with several values for cutOffValue to ensure that your program works.

8 Average of an array

We now want to add a new method to our class. The method, computeAverage, has one parameter, an array of type double. It returns a value of type double which is the average of the values contained in the array.

From the main of your program, you need to call the method computeAverage and store the returned value in some variable of type double. You then need to print out the result.

When I call it with the array valuesArray as parameter, mine prints out the following:

The average is 72.14285714285714

Question 3

Modify the program ArrayTool to include the method computeAverage described above. In your main, after calling the method computeAverage, call the method printHigherOrLower using the computed average as cutOffValue.

9 Arrays (2)

We now want to dynamically create two new arrays of doubles. The first one, smallerValuesArray, will contain the values that are lower than the average value, while the other one, largerValuesArray, will contain the other values.

To dynamically create an array of double that can accommodate exactly size elements (for some integer value size), we have to first declare a reference variable to an array of doubles as follows:

double[] myArrayOfDouble;

Then, once the size is known (that is, once the variable of type int size has the correct value), we can create the array as follows:

myArrayOfDouble = new double[size];

The first thing we will need is to know how many elements of the array have a value lower than a given value. We will create a method countNumbersLessThan which has two parameters, one array of double xs, and one value cutOffValue, of type double (just as the method printHigherOrLower). the method will return a value of type int which is the number of elements in the array xs that have a value strictly lower than cutOffValue.

Once we have this method working, we can call it passing on the array valuesArray and its average as actual parameters. That will tell us how many elements the array smallerValuesArray will have. We can now dynamically create that array.

From these values we can also deduce the necessary size for the array largerValuesArray. So we can dynamically create that array as well.

Now, we need to create a new method split that has four parameters: three arrays of double double[] xs,double[] low and double[] high and one double value cutOffValue. The goal of this method is to populate the array low with the values of xs that are strictly less than cutOffValue, and the array high with the other values. In the method, we are going to assume that the arrays low and high have the necessary size.

Question 4

Modify the program ArrayTool to include the methods countNumberLessThan and split described above. In your main, use the average found by computeAverage as cutOffValue for both methods. The main should then print off the values of the elements in both arrays low and high.

In my implementation, I see the following output:

The average is 72.14285714285714 The entry 0 (100.0) is higher than 72.14285714285714 The entry 1 (34.0) is lower than 72.14285714285714 The entry 2 (72.0) is lower than 72.14285714285714 The entry 3 (56.0) is lower than 72.14285714285714 The entry 4 (82.0) is higher than 72.14285714285714 The entry 5 (67.0) is lower than 72.14285714285714 The entry 6 (94.0) is higher than 72.14285714285714 Lower values: 34.0, 72.0, 56.0, 67.0 Larger values: 100.0, 82.0, 94.0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security XI Status And Prospects

Authors: T.Y. Lin, Shelly Qian

1st Edition

0412820900, 978-0412820908

More Books

Students also viewed these Databases questions

Question

Explain the global and ethical implications of workplace letters

Answered: 1 week ago