Activity_08_pics - COMP 1210 Activity 8: Arrays Due:...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: COMP 1210 Activity 8: Arrays Due: Activity (in-lab) Monday, March 5, 2012 by the end of lab Goals: 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: Part 1: Scores - method stubs • Create a class called Scores. • Add method stubs for the following methods. The first two are given; do the rest on your own. o The constructor has a parameter that references an array of int values o findEvens: no parameter, returns an array of ints (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 toReverseString: no parameters; returns a String (all scores in reverse order) o toString: no parameters; returns a String (all scores) 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 e = s.findEvens(); ϼÏÏint o = s.findOdds(); ϼÏÏdouble avg = s.calculateAverage(); COMP 1210 | Spring 2012 1 COMP 1210 Activity 8: Arrays Part 2: Scores - instance variable and constructor • Add an instance variable with the name numbers to your class that is an array of int values: • In your constructor, add code that will set the value of numbers to numbersIn. 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); Open the Scores object s on the Workbench in the upper left corner and open the instance variable numbers. You should be able to see your values. Open a viewer on s by dragging the blue square from the debug tab. Part 3: Scores – toString and toReverseString methods • The toString method will create a local String and then concatenate all of the values of numbers to the String. • The variable i iterates from 0 the length of numbers - 1. Within the for loop, get the number at each index and add it to the output: • Check the toString return in interactions: ϼÏÏint nums = {2, 5, 8}; ϼÏÏScores s = new Scores(nums); ϼÏÏs ÏÏÏÏ2 5 8 COMP 1210 | Spring 2012 2 COMP 1210 • Activity 8: Arrays The toReverseString 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.toReverseString() ÏÏÏÏ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. COMP 1210 | Spring 2012 3 COMP 1210 • Activity 8: Arrays Compile Scores and test the return of findEvens. The array return does not have a toString representation including the value at each index, so you will use a method from the Arrays class. ϼÏÏ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) ÏÏÏÏ[[email protected] ϼÏÏArrays.toString(evens) ÏÏÏÏ[2, 8, 10] • Create the findOdds method. It will perform the exact same function as findEvens, but it will find all odd numbers in the array (numbers that are not divisible by 2). • Test findOdds in the interactions pane. Do not continue until your output is correct. ϼÏÏimport java.util.Arrays; ϼÏÏint nums = {1, 5, 8, 3, 10}; ϼÏÏScores s = new Scores(nums); ϼÏÏArrays.toString(s.findOdds()) ÏÏÏÏ[1, 5, 3] Part 5: Scores - calculateAverage method • First, find the sum of all values in the numbers array. • Return the sum divided by the number of elements in the array. Remember that sum and arrays.length are both integers. return ____________ / ____________; Compile NumberOperations and run the following code in the interactions pane. ϼÏÏint nums = {2, 5, 8, 7, 19}; ϼÏÏScores s = new Scores(nums); ϼÏÏs.calculateAverage() ÏÏÏÏ8.2 Your GTA will ask you to demonstrate all methods in the interactions pane with a different set of values than shown above. COMP 1210 | Spring 2012 4 ...
View Full Document

This note was uploaded on 03/02/2012 for the course COMP 1210 taught by Professor Cross during the Winter '07 term at Auburn University.

Ask a homework question - tutors are online