Skip to content
Snippets Groups Projects
Christopher Bohn's avatar
Christopher Bohn authored
4b6ecdfb
History

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};
  1. As a class, draw the context diagram for numbers.
  2. In your group:
    1. Locate case 1 in Main.java's main() method.
    2. Replace the initial values of foo, bar, and baz with values that will cause these three expressions to evaluate to true:
      • numbers[2] == foo
      • numbers[bar + 2] == 22
      • numbers[numbers[1] - baz] == 9
    3. Run the program, specifying problem 1

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:

  1. Determine the method signature.
    • Locate the comment "/* WRITE convertToBoolean HERE */" and write a stub for convertToBoolean.
  2. Locate case 2 in Main.java's main() method.
    • Uncomment the commented-out code for case 2, and add the call to convertToBoolean on the line that declares convertedValues.
  3. In the convertToBoolean method, design the loop:
    1. Result variable:
    2. Loop variable:
    3. Base case:
    4. Initiation:
    5. Termination condition:
    6. Consecution:
    7. Action:
  4. Write the code to implement your solution.
    • What type of loop did you choose for your implementation? Why?
  5. Run the program, specifying problem 2.
  6. 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 of values 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:

  1. Locate the comment "/* WRITE THE METHOD FOR PROBLEM 3 HERE */" and write a stub for your method.
  2. Locate case 3 in Main.java's main() method. Add code to call your problem 3 method.
  3. Implement your method and test it.

Practice Problem 4

Write a method that that takes two arguments:

  1. An array of doubles. Each value of the array represents the weight of an item to be shipped.
  2. 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).

  1. Locate the comment "/* WRITE THE METHOD FOR PROBLEM 4 HERE */" and write a stub for your method.
  2. Locate case 4 in Main.java's main() method. Add code to call your problem 4 method.
  3. Implement your method and test it.