Skip to content
Snippets Groups Projects
Verified Commit 4b6ecdfb authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
# 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.
pom.xml 0 → 100644
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.unl.cse.soft160</groupId>
<artifactId>practice-with-arrays-and-loops</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
\ No newline at end of file
package edu.unl.cse.soft160;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
static int numberOfProblems = 4;
private static int getProblemNumber(Scanner scanner) {
int choice = -1;
while (choice < 1 || choice > numberOfProblems) {
System.out.print("Please enter the problem you're working on (1..4): ");
try {
choice = scanner.nextInt();
if (choice < 1 || choice > numberOfProblems) {
System.out.println("Your selection must be between 1 and " + numberOfProblems + ".");
}
} catch (InputMismatchException ignored) {
System.out.println("Your selection must be an integer between 1 and " + numberOfProblems + ".");
} finally {
scanner.nextLine(); // consume the newline
}
}
return choice;
}
public static void printExpressionValues(int foo, int bar, int baz) {
int[] numbers = {6, 9, 21, 22, 14, 3};
String fooExpression = "numbers[2] == foo";
String barExpression = "numbers[bar + 2] == 22";
String bazExpression = "numbers[numbers[1] - baz] == 9";
System.out.println(fooExpression + " evaluates to " + (numbers[2] == foo));
System.out.println(barExpression + " evaluates to " + (numbers[bar + 2] == 22));
System.out.println(bazExpression + " evaluates to " + (numbers[numbers[1] - baz] == 9));
}
/* WRITE convertToBoolean HERE */
/* WRITE THE METHOD FOR PROBLEM 3 HERE */
/* WRITE THE METHOD FOR PROBLEM 3 HERE */
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice = getProblemNumber(scanner);
switch (choice) {
case 1:
int foo = Integer.MIN_VALUE;
int bar = Integer.MIN_VALUE;
int baz = Integer.MIN_VALUE;
printExpressionValues(foo, bar, baz);
break;
case 2:
// UNCOMMENT THIS CODE, AND ADD THE CALL TO convertToBoolean
// int[] values = {3, 4, 5};
// System.out.println(Arrays.toString(values));
// boolean[] convertedValues = ...
// System.out.println(Arrays.toString(convertedValues));
// break;
case 3:
// WRITE CODE TO CALL YOUR PROBLEM 3 METHOD HERE. BE SURE TO ADD A break STATEMENT AT THE END OF THIS CASE.
case 4:
// WRITE CODE TO CALL YOUR PROBLEM 4 METHOD HERE. BE SURE TO ADD A break STATEMENT AT THE END OF THIS CASE.
default:
System.out.println("Not ready for problem " + choice);
}
scanner.close();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment