diff --git a/homework5/.gitignore b/homework5/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..9dbe7d11c1294e17b42776f8336b4394281d223c --- /dev/null +++ b/homework5/.gitignore @@ -0,0 +1,54 @@ +# Mac file finder metadata +.DS_Store +# Windows file metadata +._* +# Thumbnail image caches +Thumbs.db +ethumbs.db +# MS Office temporary file +~* +# Emacs backup file +*~ + +# Common +[Bb]in/ +[Bb]uild/ +[Oo]bj/ +[Oo]ut/ +[Tt]mp/ +[Xx]86/ +[Ii][Aa]32/ +[Xx]64/ +[Xx]86_64/ +[Xx]86-64/ +[Aa]rm +[Aa]32 +[Tt]32 +[Aa]64 +*.tmp +*.bak +*.bk +*.swp + +# Java files +*.class +javadoc/ + +# Maven +target/ + +# JetBrains (IntelliJ IDEA, PyCharm, etc) files +.idea/ +cmake-build-*/ +*.iml +*.iws +*.ipr + +# Eclipse files +.settings/ +.project +.classpath +.buildpath +.loadpath +.factorypath +local.properties diff --git a/homework5/decomposition-and-conditionals-homework/.gitignore b/homework5/decomposition-and-conditionals-homework/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..9dbe7d11c1294e17b42776f8336b4394281d223c --- /dev/null +++ b/homework5/decomposition-and-conditionals-homework/.gitignore @@ -0,0 +1,54 @@ +# Mac file finder metadata +.DS_Store +# Windows file metadata +._* +# Thumbnail image caches +Thumbs.db +ethumbs.db +# MS Office temporary file +~* +# Emacs backup file +*~ + +# Common +[Bb]in/ +[Bb]uild/ +[Oo]bj/ +[Oo]ut/ +[Tt]mp/ +[Xx]86/ +[Ii][Aa]32/ +[Xx]64/ +[Xx]86_64/ +[Xx]86-64/ +[Aa]rm +[Aa]32 +[Tt]32 +[Aa]64 +*.tmp +*.bak +*.bk +*.swp + +# Java files +*.class +javadoc/ + +# Maven +target/ + +# JetBrains (IntelliJ IDEA, PyCharm, etc) files +.idea/ +cmake-build-*/ +*.iml +*.iws +*.ipr + +# Eclipse files +.settings/ +.project +.classpath +.buildpath +.loadpath +.factorypath +local.properties diff --git a/homework5/decomposition-and-conditionals-homework/README.md b/homework5/decomposition-and-conditionals-homework/README.md new file mode 100644 index 0000000000000000000000000000000000000000..81e10a03547da4be7f691a0726b99527562889fd --- /dev/null +++ b/homework5/decomposition-and-conditionals-homework/README.md @@ -0,0 +1,57 @@ +# Decomposition and Conditionals Homework + +**Collaboration Policy**: No collaboration is permitted on this assignment. + +**Notes and References**: This is an open-book, open-note assignment. + +## Overview + +Proper problem and solution decomposition is an important software engineering +technique for writing readable and maintainable code. In this assignment you +will practice decomposition to solve two small computational problems whose +solutions may not be obvious from their descriptions. You will also practice +writing code that uses an enumerated type, string concatenation, and exception +handlers. + +## Learning Goals + +By completing this assignment, you should be able to: + +1. Decompose a solution to a problem into multiple methods in a Java program, + +2. Use an enumerated type to represent values from a limited space, + +3. Use string concatenation to format text output, and + +4. Use exception handlers to catch and handle input errors. + +## 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 `homework5`. + +2. Change into the `homework5` directory. + +3. Clone + [the starter code](https://git.unl.edu/soft-core/soft-160/decomposition-and-conditionals-homework). + +4. Change into the `decomposition-and-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 as a Maven project. diff --git a/homework5/decomposition-and-conditionals-homework/documentation/.gitkeep b/homework5/decomposition-and-conditionals-homework/documentation/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/homework5/decomposition-and-conditionals-homework/pom.xml b/homework5/decomposition-and-conditionals-homework/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..fd501b2c7216b387114834beb2899eaccd72fa2a --- /dev/null +++ b/homework5/decomposition-and-conditionals-homework/pom.xml @@ -0,0 +1,36 @@ +<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/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>edu.unl.cse.soft160.decomposition_and_conditionals</groupId> + <artifactId>decomposition_and_conditionals</artifactId> + <packaging>jar</packaging> + <version>1.0-SNAPSHOT</version> + <name>decomposition_and_conditionals</name> + <url>http://maven.apache.org</url> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <build> + <plugins> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.8.1</version> + <configuration> + <source>8</source> + <target>8</target> + </configuration> + </plugin> + </plugins> + </build> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.13</version> + <scope>test</scope> + </dependency> + </dependencies> +</project> diff --git a/homework5/decomposition-and-conditionals-homework/src/main/java/edu/unl/cse/soft160/decomposition_and_conditionals/BooleanOperator.java b/homework5/decomposition-and-conditionals-homework/src/main/java/edu/unl/cse/soft160/decomposition_and_conditionals/BooleanOperator.java new file mode 100755 index 0000000000000000000000000000000000000000..4e0b5cf51d0f9238eb9ba3053a1f030ec1f345af --- /dev/null +++ b/homework5/decomposition-and-conditionals-homework/src/main/java/edu/unl/cse/soft160/decomposition_and_conditionals/BooleanOperator.java @@ -0,0 +1,11 @@ +package edu.unl.cse.soft160.decomposition_and_conditionals; + +public class BooleanOperator { + private static enum TruthValue { + TRUE, FALSE, UNKNOWN, + } + + public static void main(String... arguments) { + // [write code here] + } +} diff --git a/homework5/decomposition-and-conditionals-homework/src/main/java/edu/unl/cse/soft160/decomposition_and_conditionals/ProcessInput.java b/homework5/decomposition-and-conditionals-homework/src/main/java/edu/unl/cse/soft160/decomposition_and_conditionals/ProcessInput.java new file mode 100755 index 0000000000000000000000000000000000000000..42892798acc12a1cb1e14a81e4e1c3d065c8f363 --- /dev/null +++ b/homework5/decomposition-and-conditionals-homework/src/main/java/edu/unl/cse/soft160/decomposition_and_conditionals/ProcessInput.java @@ -0,0 +1,15 @@ +package edu.unl.cse.soft160.decomposition_and_conditionals; + +public class ProcessInput { + private static enum State { + EMPTY, DECIMAL, NUMERIC, + } + + private static enum Classification { + FLOAT, INTEGER, NAN, + } + + public static void main(String... arguments) { + // [write code here] + } +} diff --git a/homework5/decomposition-and-conditionals-homework/src/test/java/edu/unl/cse/soft160/decomposition_and_conditionals/BooleanOperatorTest.java b/homework5/decomposition-and-conditionals-homework/src/test/java/edu/unl/cse/soft160/decomposition_and_conditionals/BooleanOperatorTest.java new file mode 100644 index 0000000000000000000000000000000000000000..d8aadc8e6eba3f8f68db26165d5bbc0330ceb96e --- /dev/null +++ b/homework5/decomposition-and-conditionals-homework/src/test/java/edu/unl/cse/soft160/decomposition_and_conditionals/BooleanOperatorTest.java @@ -0,0 +1,107 @@ +package edu.unl.cse.soft160.decomposition_and_conditionals; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.PrintStream; + +public class BooleanOperatorTest { + + 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)); + BooleanOperator.main(); + System.setIn(in); + System.setOut(out); + return collector.toString(); + } + + @Test + public void testTrueTrue() { + assertEquals(assemble( + "Enter truth value of operand 1: Enter truth value of operand 2: The NOR value of TRUE and TRUE is FALSE."), + runMain("TRUE", "TRUE")); + } + + @Test + public void testTrueFalse() { + assertEquals(assemble( + "Enter truth value of operand 1: Enter truth value of operand 2: The NOR value of TRUE and FALSE is FALSE."), + runMain("TRUE", "FALSE")); + } + + @Test + public void testFalseTrue() { + assertEquals(assemble( + "Enter truth value of operand 1: Enter truth value of operand 2: The NOR value of FALSE and TRUE is FALSE."), + runMain("FALSE", "TRUE")); + } + + @Test + public void testFalseFalse() { + assertEquals(assemble( + "Enter truth value of operand 1: Enter truth value of operand 2: The NOR value of FALSE and FALSE is TRUE."), + runMain("FALSE", "FALSE")); + } + + @Test + public void testFalseUnknown() { + assertEquals(assemble( + "Enter truth value of operand 1: Enter truth value of operand 2: The NOR value of FALSE and UNKNOWN is UNKNOWN."), + runMain("FALSE", "UNKNOWN")); + } + + @Test + public void testUnknownFalse() { + assertEquals(assemble( + "Enter truth value of operand 1: Enter truth value of operand 2: The NOR value of UNKNOWN and FALSE is UNKNOWN."), + runMain("UNKNOWN", "FALSE")); + } + + @Test + public void testUnknownUnknown() { + assertEquals(assemble( + "Enter truth value of operand 1: Enter truth value of operand 2: The NOR value of UNKNOWN and UNKNOWN is UNKNOWN."), + runMain("UNKNOWN", "UNKNOWN")); + } + + @Test + public void testTrueUnknown() { + assertEquals(assemble( + "Enter truth value of operand 1: Enter truth value of operand 2: The NOR value of TRUE and UNKNOWN is FALSE."), + runMain("TRUE", "UNKNOWN")); + } + + @Test + public void testUnknownTrue() { + assertEquals(assemble( + "Enter truth value of operand 1: Enter truth value of operand 2: The NOR value of UNKNOWN and TRUE is FALSE."), + runMain("UNKNOWN", "TRUE")); + } + + @Test + public void testExceptionInOp1() { + assertEquals(assemble( + "Enter truth value of operand 1: Enter truth value of operand 2: Truth value must be one of TRUE, FALSE, or UNKNOWN."), + runMain("FOO", "TRUE")); + } + + @Test + public void testExceptionInOp2() { + assertEquals(assemble( + "Enter truth value of operand 1: Enter truth value of operand 2: Truth value must be one of TRUE, FALSE, or UNKNOWN."), + runMain("TRUE", "BAR")); + } + +} diff --git a/homework5/decomposition-and-conditionals-homework/src/test/java/edu/unl/cse/soft160/decomposition_and_conditionals/ProcessInputTest.java b/homework5/decomposition-and-conditionals-homework/src/test/java/edu/unl/cse/soft160/decomposition_and_conditionals/ProcessInputTest.java new file mode 100644 index 0000000000000000000000000000000000000000..443be183205bc414fa912917d54c0b19cc183697 --- /dev/null +++ b/homework5/decomposition-and-conditionals-homework/src/test/java/edu/unl/cse/soft160/decomposition_and_conditionals/ProcessInputTest.java @@ -0,0 +1,365 @@ +package edu.unl.cse.soft160.decomposition_and_conditionals; + +import static org.junit.Assert.*; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.PrintStream; + +import org.junit.Test; + +public class ProcessInputTest { + + 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)); + ProcessInput.main(); + System.setIn(in); + System.setOut(out); + return collector.toString(); + } + + @Test + public void testEmptyL() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: NAN"), + runMain("empty","L")); + } + + @Test + public void testEmpty0() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: INTEGER"), + runMain("empty","0")); + } + + @Test + public void testEmpty1() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: INTEGER"), + runMain("empty","1")); + } + + @Test + public void testEmpty2() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: INTEGER"), + runMain("empty","2")); + } + + @Test + public void testEmpty3() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: INTEGER"), + runMain("empty","3")); + } + + @Test + public void testEmpty4() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: INTEGER"), + runMain("empty","4")); + } + + @Test + public void testEmpty5() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: INTEGER"), + runMain("empty","5")); + } + + @Test + public void testEmpty6() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: INTEGER"), + runMain("empty","6")); + } + + @Test + public void testEmpty7() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: INTEGER"), + runMain("empty","7")); + } + + @Test + public void testEmpty8() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: INTEGER"), + runMain("empty","8")); + } + + @Test + public void testEmpty9() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: INTEGER"), + runMain("empty","9")); + } + + @Test + public void testEmptyPeriod() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: FLOAT"), + runMain("empty",".")); + } + + @Test + public void testEmptyF() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: NAN"), + runMain("empty","f")); + } + + @Test + public void testEmptyD() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: NAN"), + runMain("empty","d")); + } + + @Test + public void testDecimalL() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: NAN"), + runMain("decimal","L")); + } + + @Test + public void testDecimal0() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: FLOAT"), + runMain("decimal","0")); + } + + @Test + public void testDecimal1() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: FLOAT"), + runMain("decimal","1")); + } + + @Test + public void testDecimal2() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: FLOAT"), + runMain("decimal","2")); + } + + @Test + public void testDecimal3() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: FLOAT"), + runMain("decimal","3")); + } + + @Test + public void testDecimal4() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: FLOAT"), + runMain("decimal","4")); + } + + @Test + public void testDecimal5() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: FLOAT"), + runMain("decimal","5")); + } + + @Test + public void testDecimal6() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: FLOAT"), + runMain("decimal","6")); + } + + @Test + public void testDecimal7() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: FLOAT"), + runMain("decimal","7")); + } + + @Test + public void testDecimal8() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: FLOAT"), + runMain("decimal","8")); + } + + @Test + public void testDecimal9() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: FLOAT"), + runMain("decimal","9")); + } + + @Test + public void testDecimalPeriod() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: NAN"), + runMain("decimal",".")); + } + + @Test + public void testDecimalF() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: FLOAT"), + runMain("decimal","f")); + } + + @Test + public void testDecimalD() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: FLOAT"), + runMain("decimal","d")); + } + + @Test + public void testNumericL() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: INTEGER"), + runMain("numeric","L")); + } + + @Test + public void testNumeric0() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: INTEGER"), + runMain("numeric","0")); + } + + @Test + public void testNumeric1() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: INTEGER"), + runMain("numeric","1")); + } + + @Test + public void testNumeric2() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: INTEGER"), + runMain("numeric","2")); + } + + @Test + public void testNumeric3() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: INTEGER"), + runMain("numeric","3")); + } + + @Test + public void testNumeric4() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: INTEGER"), + runMain("numeric","4")); + } + + @Test + public void testNumeric5() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: INTEGER"), + runMain("numeric","5")); + } + + @Test + public void testNumeric6() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: INTEGER"), + runMain("numeric","6")); + } + + @Test + public void testNumeric7() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: INTEGER"), + runMain("numeric","7")); + } + + @Test + public void testNumeric8() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: INTEGER"), + runMain("numeric","8")); + } + + @Test + public void testNumeric9() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: INTEGER"), + runMain("numeric","9")); + } + + @Test + public void testNumericPeriod() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: FLOAT"), + runMain("numeric",".")); + } + + @Test + public void testNumericF() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: FLOAT"), + runMain("numeric","f")); + } + + @Test + public void testNumericD() { + assertEquals( + assemble("Enter the current state: Enter the next character: " + + "Classification: FLOAT"), + runMain("numeric","d")); + } +}