Practice with Arrays and Loops
git clone git@git.unl.edu:soft-core/soft-160/practice-with-arrays-and-loops.git
Practice Problem 1
Given the array
int[] numbers = {6, 9, 21, 22, 14, 3};
- As a class, draw the context diagram for
numbers
. - In your group:
- Locate
case 1
in Main.java'smain()
method. - Replace the initial values of
foo
,bar
, andbaz
with values that will cause these three expressions to evaluate totrue
:numbers[2] == foo
numbers[bar + 2] == 22
numbers[numbers[1] - baz] == 9
- Run the program, specifying problem 1
- Locate
Practice Problem 2
Given an array of integer values as the input, design and implement a method, convertToBoolean
, that returns an array of boolean values, where a value of true
at a given index in the boolean array indicates the value at the corresponding index in the input array is even and false
otherwise.
Example: if the input array is {3, 4, 5}, the output array should be {false, true, false}
In your group:
- Determine the method signature.
- Locate the comment "/* WRITE convertToBoolean HERE */" and write a stub for
convertToBoolean
.
- Locate the comment "/* WRITE convertToBoolean HERE */" and write a stub for
- Locate
case 2
in Main.java'smain()
method.- Uncomment the commented-out code for case 2, and add the call to
convertToBoolean
on the line that declaresconvertedValues
.
- Uncomment the commented-out code for case 2, and add the call to
- In the
convertToBoolean
method, design the loop:- Result variable:
- Loop variable:
- Base case:
- Initiation:
- Termination condition:
- Consecution:
- Action:
- Write the code to implement your solution.
- What type of loop did you choose for your implementation? Why?
- Run the program, specifying problem 2.
- Using the Category-Partition Method, create a set of test frames for your method based on the specification above.
- Think about good categories for an array of integers, especially one in which the evenness of its elements is important
- In the
case 2
block, change the initialization ofvalues
to correspond to one of your test frames. Do this a few times, for different test frames.
Practice Problem 3
Write a method with no return value that prints a grid of characters. The inputs to your method are the width and height of the grid and the character that is to be printed. For example, given a width of 3 and height of 4 and the character 'x', your method should print:
xxx
xxx
xxx
xxx
In your group:
- Locate the comment "/* WRITE THE METHOD FOR PROBLEM 3 HERE */" and write a stub for your method.
- Locate
case 3
in Main.java'smain()
method. Add code to call your problem 3 method. - Implement your method and test it.
Practice Problem 4
Write a method that that takes two arguments:
- An array of doubles. Each value of the array represents the weight of an item to be shipped.
- A value of type double that represents the maximum weight that the shipping container can hold.
The return value indicates how many items from the list can be shipped in the container. Note that the items in the array are in priority order for shipping (i.e., you are not attempting to find the maximum number of items to ship in the container).
For example, if the items weights are {12.0, 28.5, 15.4, 22.1} and the maximum weight that the shipping container can hold is 50.0, then the method will return 2 (12.0 + 28.5 < 50.0, but 12.0 + 28.5 + 15.4 > 50).
- Locate the comment "/* WRITE THE METHOD FOR PROBLEM 4 HERE */" and write a stub for your method.
- Locate
case 4
in Main.java'smain()
method. Add code to call your problem 4 method. - Implement your method and test it.