COMP1210 – Goals: Solved

$ 24.99
Category:

Description

By the end of this activity you should be able to do the following:
 Understand the basics of instantiating arrays and assigning / accessing array elements.  How to iterate through arrays using loops.

Description:
In this activity, you will create a class called Scores that will hold an array of numerical values and provide methods that allow users to interact with the Scores class.

Directions:
Don’t forget to add your Javadoc comments for your classes, constructor, and methods in this activity. Part 1: Scores – instance variable, constructor and method stubs • Create a class called Scores. o Add an instance variable with the name numbers to your class that is an array of int values:

o Add a constructor that has a parameter declared as an array of int values.

• Add method stubs for the following methods. The first one is given; do the rest on your own.

o findEvens: no parameter, returns an array of int (all of the even-valued scores)
o findOdds: no parameter, returns an array of ints (all of the odd-valued scores) o calculateAverage: no parameters; returns a double (the average of all scores) o toString: no parameters; returns a String containing all scores
o toStringInReverse: no parameters; returns a String containing all scores in reverse order

Compile Scores and run the following in interactions. Do not continue until your program compiles and the following code runs without error in interactions.

ϼÏÏScores s = new Scores(null);
ϼÏÏint[] evens = s.findEvens();
ϼÏÏint[] odds = s.findOdds();
ϼÏÏdouble avg = s.calculateAverage();

Part 2: Scores – completing the constructor
• In your constructor, add code that will set the value of numbersIn to numbers. You access the entire array object using its variable name with no brackets.

• Compile Scores. In the interactions pane set up an array of int values using an initializer list and send it to the constructor of scores:
ϼÏÏint [] nums = {2, 5, 8};
ϼÏÏScores s = new Scores(nums);

On the Workbench tab, click the Open New Viewer Canvas button , then drag the array nums and the Scores object s from the Workbench tab onto the canvas window. You should now be able to see the values of each of these items. To change the viewer for a variable on the canvas, select the viewer for the variable, then click the viewer menu button (at upper right of viewer frame), select “Viewer”, then select the desired viewer from the list. You should experiment with different viewers. Below, the “Presentaton – Structure Identifier” viewer has been selected for nums and the “Basic” viewer has been selected for variable s. You should leave the canvas window open for the remainder of the activity since we will be adding other variables.

Canvas with int array nums and Scores s
Change the values in the int array nums using assignment statements in interactions as indicated below and you should see the viewer on the canvas updated as well. It should be obvious why the values in nums changed, but why did the int array field in the Scores object s also change?

ϼÏÏnums[0] = 9;
ϼÏÏnums[1] = 8;
ϼÏÏnums[2] = 7;

Part 3: Scores – toString and toStringInReverse methods
• The toString method will create a local String and then concatenate all of the values of numbers to the String.

• The loop above iterates as the variable i ranges from 0 through the length of numbers – 1 and then terminates (or exits) when i is no longer less than the length of the array numbers. Within the for loop above, add the number at each index to the result:

• Check the toString return in interactions:
ϼÏÏint[] nums = {2, 5, 8};
ϼÏÏScores s = new Scores(nums);
ϼÏÏs ÏÏÏÏ2 5 8 ÏÏÏ

• The toStringInReverse method will be exactly the same as toString, but will iterate from the length of numbers – 1 to 0.

Compile Scores and run the following code in the interactions pane. Do not continue until the following code runs without error in interactions.

ϼÏÏint[] nums = {2, 5, 8};
ϼÏÏScores s = new Scores(nums);
ϼÏÏs.toStringInReverse()
ÏÏÏÏ8 5 2

Part 4: Scores – findEvens and findOdds methods
• There are two parts to the findEvens method. First, count the number of evens in the array:

• You will then need to create an array with the appropriate length to store the number of even numbers.

• Add the even numbers to the evens array. In the following loop, i represents the current index of numbers and count is the current index of evens.

ϼÏÏimport java.util.Arrays;
ϼÏÏint[] nums = {2, 5, 8, 1, 10};
ϼÏÏScores s = new Scores(nums);
ϼÏÏint[] evens = s.findEvens();
ϼÏÏevens // toString output of an array object (will vary)
ÏÏÏÏ[D@5abb7465
ϼÏÏArrays.toString(evens)
ÏÏÏÏ[2, 8, 10]

• Create the findOdds method on your own. It will perform the exact same function as findEvens, but it will find all odd numbers in the array (i.e., the numbers that are not divisible by 2). Hint: the remainder when dividing by 2 is 1 rather than 0.
• Test findOdds in the interactions pane. After the int array odds is created, be sure to drag it from the workbench to the canvas. Do not continue until your output is correct.
ϼÏÏimport java.util.Arrays;
ϼÏÏint[] nums = {1, 5, 8, 3, 10};
ϼÏÏScores s = new Scores(nums);
ϼÏÏint[] odds = s.findOdds();
Arrays.toString(odds)
ÏÏÏÏ[1, 5, 3]

Part 5: Scores – calculateAverage method
• First, find the sum of all values in the numbers array.

in the figure at right.

You should test all methods in the interactions pane with appropriate variables in the canvas window. You should ensure that your methods work with a different set of values for the int array nums than shown above.

Finally, submit your Scores.java file to the grading system.
The canvas after dragging variables onto canvas from the Workbench and invoking the methods findEvens, findOdds, and calculateAverage on the Scores object s.

Reviews

There are no reviews yet.

Be the first to review “COMP1210 – Goals: Solved”

Your email address will not be published. Required fields are marked *