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

Removed Scramble code

We had placed Scramble into its own homework assignment.
parent 0c7d312d
No related branches found
No related tags found
No related merge requests found
# Homework #3 - Working with Conditionals # Conditionals Code
**Collaboration Policy**: No collaboration is permitted on this assignment. Starter code for "Working with Conditionals" homework assignment
\ No newline at end of file
**Notes and References**: This is an open-book, open-note assignment.
## Overview
Conditionals play an important role in code by enabling programs to apply
different solutions in different situations. In this assignment you will
practice reading and writing conditional code to solve simple computational
problems.
## Learning Goals
By completing this assignment, you should be able to:
1. Unscramble Java code to create a program that solves a given problem,
2. Write a Java program to solve a simple problem,
3. Write maintainable code, and
4. Run JUnit system tests and collect coverage information.
## Instructions
This assignment is to be completed individually; **no collaboration is
permitted**.
### Clone the Starter Code and Set Up Eclipse
1. In your virtual machine, create a new folder in your `soft160-homework`
folder named `homework3`.
2. Change into the `homework3` directory.
3. Clone
[the starter code](https://git.unl.edu/soft-core/soft-160/conditionals_homework).
4. Change into the `conditionals_homework` directory. Verify that you are in
the correct directory by running the command `pwd`.
5. Remove the Git folder that links this code to the instructors' repository by
running the command `chmod -R u+w .git` (which gives you permission to
remove the folder without extra confirmation prompts) and then the command
`rm -rI .git`. Answer `yes` when asked to confirm the deletion.
6. Verify the Git folder has been removed by running `ls -a` and making sure
that you see folders with names like `src` and `documentation`, but that `.git`
is **not** one of the items listed.
7. Start the Eclipse IDE.
8. Import the starter code into Eclipse.
a. In the Eclipse menu bar, select "File" → "Import…".
<!--
b. Under the "General" folder, select "Existing Projects into Workspace"
and click "Next&nbsp;>".
-->
b. Under the "Maven" folder, select "Existing Maven Projects" and click
"Next&nbsp;>"
c. Using the "Browse" button at the top of the dialog box, locate the
folder `homework3` and click "OK".
d. Click "Finish".
### Part I: Unscramble Code with Conditionals
**Problem:** Compute the median of three integers entered by the user. The median
value is the value at the midpoint of a collection of values; when there are only
three values, it is the value that is neither the maximum nor the minimum.
9. (Optional) Write a solution to this problem in pseudocode.
10. Add a comment to the beginning of the code in `Scramble.java`. Include your
name and a brief natural-language description of the problem your code
solves.
11. The following lines of Java code, when arranged in the proper order, will
solve this problem:
```
System.out.println("The median is " + thirdValue + ".");
System.out.println("The median is " + thirdValue + ".");
System.out.println("The median is " + secondValue + ".");
System.out.println("The median is " + secondValue + ".");
System.out.println("The median is " + firstValue + ".");
System.out.println("The median is " + firstValue + ".");
System.out.print("Enter third value: ");
System.out.print("Enter second value: ");
System.out.print("Enter first value: ");
scanner.close();
Scanner scanner = new java.util.Scanner(System.in);
int thirdValue = Integer.parseInt(scanner.nextLine());
int secondValue = Integer.parseInt(scanner.nextLine());
int firstValue = Integer.parseInt(scanner.nextLine());
if (secondValue > thirdValue) {
if (secondValue < thirdValue) {
if (firstValue > secondValue) {
} else if (firstValue > thirdValue) {
} else if (firstValue < thirdValue) {
} else {
} else {
} else {
}
}
}
```
Copy and paste these lines into the file `Scramble.java` in place of the
comment `// [write code here]` and reorder them to solve the problem. (Do
not add, remove, or modify any of the lines.) You can press control-shift-F
to automatically format the code once you have arranged the lines.
12. Run the JUnit system tests in `ScrambleTest.java` *while collecting
coverage information*. You have completed this part of the assignment
when all tests are passing and the coverage of the `main` method is
100%.
13. Take a screenshot showing your test results and coverage. Instructions for
taking a screenshot on your VM can be found
[here](https://canvas.unl.edu/courses/94598/pages/how-to-take-a-screenshot-on-your-vm).
For full points, your screenshot must include:
- the number of runs, errors, and failures and
- the coverage percentage for the `main` method that you implemented.
Save the image in PNG format as a file named `scramble.png` in the
`documentation` folder.
### Part II: Write a Program with One Conditional
**Problem:** A caucus system should accept valid precinct reports on the day of
a caucus. An electronically-submitted precinct report is valid if it is in the
correct format. A precinct report provided over the telephone is always valid.
Given whether today is a caucus day and the nature of the caucus report, print
the text "Precinct report accepted" followed by a newline if the caucus system
should accept the report. Otherwise, print the text "Precinct report rejected"
followed by a newline.
14. (Optional) Write a solution to this problem in pseudocode using only one
conditional.
15. Add a comment to the beginning of the code in `Caucus.java`. Include
your name and a brief natural-language description of the problem your code
solves.
16. Write code to solve the stated problem using only one conditional in place
of the comment `// [write code here]` in `Caucus.java`. Be sure to use
appropriate variable names and to format your code using the Eclipse
formatter.
17. Run the JUnit system tests in `CaucusTest.java` *while collecting
coverage information*. You have completed this part of the assignment when
all tests are passing and the coverage of the `main` method is 100%.
18. Take a screenshot showing your test results and coverage. For full points,
your screenshot must include:
- the number of runs, errors, and failures and
- the coverage percentage for the `main` method that you implemented.
Save the image in PNG format as a file named `caucus.png` in the
`documentation` folder.
### Part III: Write a Program with Potentially Multiple Conditionals
**Problem:** Unless otherwise marked, a street's speed limit in a particular city
is determined by type of street. A residential street has a speed limit of
40&nbsp;km/h. All other
streets have a speed limit of 70&nbsp;km/h. The exception to this rule is that a
school zone has a speed limit of 25&nbsp;km/h, regardless of the type of street.
Given a car's speed, the type of street, and whether it's in a school zone, print
"Speeding" followed by a newline if the car exceeds the applicable speed limit.
Otherwise, print "Safe" followed by a newline. You may assume that the car's speed
will never be negative.
19. (Optional) Write a solution to this problem in pseudocode.
20. Add a comment to the beginning of the code in `Street.java`. Include
your name and a brief natural-language description of the problem your code
solves.
21. Based on the starter code in `Caucus.java`, write code just before the
comment `// [write code here]` in `SpeedLimit.java` that prompts the user to
answer the following yes/no questions:
- "Is the car on a residential street (yes/no)? "
- "Is the car in a school zone (yes/no)? "
Be sure to match these prompts exactly (including the trailing spaces), or
your tests may fail.
22. Write code to solve the stated problem in place of the comment `// [write
code here]` in `SpeedLimit.java`. Be sure to use appropriate variable names and
to format your code using the Eclipse formatter.
23. Run the JUnit system tests in `SpeedLimitTest.java` *while collecting
coverage information*. You have completed this part of the assignment when
all tests are passing and the coverage of the `main` method is 100%.
24. Take a screenshot showing your test results and coverage. For full points,
your screenshot must include:
* the number of runs, errors, and failures and
* the coverage percentage for the `main` method that you implemented.
Save the image in PNG format as a file named `speedlimit.png` in the
`documentation` folder.
## Homework Submission
When you are ready to save your homework, change into your `soft160-homework`
folder and stage, commit, and push your `homework3` folder to GitLab. Note
that you can (and should) push your changes to the GitLab server as you work to
maintain a backup copy; we will only access your repository to collect the
assignment at the time and date it is due.
## Grading
This assignment is worth **16 points**:
* **1.5 points (0.5 points per problem)** if your code includes a comment
containing your name and a brief restatement of the problem your code solves,
* **3.0 points (1 point per problem)** if your code compiles,
* **1.5 points (0.5 points per problem)** if your code is formatted following
the instructions provided and variables are named well,
* **3.0 points (1 point per problem)** for your test and coverage report
(a screenshot of your test results),
* **6.0 points (2 points per problem)** if your program passes all of the
test cases provided, and
* **1.0 point** if your solution to Part II uses a single conditional.
The assignment is due Wednesday, September&nbsp;30 at 9:00&nbsp;a.m.
**NOTE: You will lose 2 points if you do not follow the file-naming conventions
listed in the instructions.**
package edu.unl.cse.soft160.conditionals;
import java.util.Scanner;
public class Scramble {
public static void main(String... arguments) {
// [write code here]
}
}
package edu.unl.cse.soft160.conditionals;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import static org.junit.Assert.*;
public class ScrambleTest {
protected static String assemble(String... lines) {
return String.join("\n", lines) + "\n";
}
protected static String runMain(String... inputs) {
InputStream in = System.in;
PrintStream out = System.out;
System.setIn(new ByteArrayInputStream(assemble(inputs).getBytes()));
ByteArrayOutputStream collector = new ByteArrayOutputStream();
System.setOut(new PrintStream(collector));
Scramble.main();
System.setIn(in);
System.setOut(out);
return collector.toString();
}
@Test
public void testOrder0() {
assertEquals(
assemble("Enter first value: " + "Enter second value: " + "Enter third value: " + "The median is 1."),
runMain("0", "1", "2"));
}
@Test
public void testOrder1() {
assertEquals(
assemble("Enter first value: " + "Enter second value: " + "Enter third value: " + "The median is 1."),
runMain("0", "2", "1"));
}
@Test
public void testOrder2() {
assertEquals(
assemble("Enter first value: " + "Enter second value: " + "Enter third value: " + "The median is 1."),
runMain("2", "0", "1"));
}
@Test
public void testOrder3() {
assertEquals(
assemble("Enter first value: " + "Enter second value: " + "Enter third value: " + "The median is 1."),
runMain("2", "1", "0"));
}
@Test
public void testOrder4() {
assertEquals(
assemble("Enter first value: " + "Enter second value: " + "Enter third value: " + "The median is 1."),
runMain("1", "2", "0"));
}
@Test
public void testOrder5() {
assertEquals(
assemble("Enter first value: " + "Enter second value: " + "Enter third value: " + "The median is 1."),
runMain("1", "0", "2"));
}
@Test
public void testOrder6WithAHighTie() {
assertEquals(
assemble("Enter first value: " + "Enter second value: " + "Enter third value: " + "The median is 1."),
runMain("0", "1", "1"));
}
@Test
public void testOrder7WithAHighTie() {
assertEquals(
assemble("Enter first value: " + "Enter second value: " + "Enter third value: " + "The median is 1."),
runMain("1", "0", "1"));
}
@Test
public void testOrder8WithAHighTie() {
assertEquals(
assemble("Enter first value: " + "Enter second value: " + "Enter third value: " + "The median is 1."),
runMain("1", "1", "0"));
}
@Test
public void testOrder9WithALowTie() {
assertEquals(
assemble("Enter first value: " + "Enter second value: " + "Enter third value: " + "The median is 0."),
runMain("0", "0", "1"));
}
@Test
public void testOrder10WithALowTie() {
assertEquals(
assemble("Enter first value: " + "Enter second value: " + "Enter third value: " + "The median is 0."),
runMain("1", "0", "0"));
}
@Test
public void testOrder11WithALowTie() {
assertEquals(
assemble("Enter first value: " + "Enter second value: " + "Enter third value: " + "The median is 0."),
runMain("0", "1", "0"));
}
@Test
public void testAllTied() {
assertEquals(
assemble("Enter first value: " + "Enter second value: " + "Enter third value: " + "The median is 0."),
runMain("0", "0", "0"));
}
@Test
public void testNegatives() {
assertEquals(
assemble("Enter first value: " + "Enter second value: " + "Enter third value: " + "The median is -2."),
runMain("-1", "-2", "-3"));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment