diff --git a/_sources/Unit2-Using-Objects/topic-2-4-methods-with-params.rst b/_sources/Unit2-Using-Objects/topic-2-4-methods-with-params.rst index bbcb4b57..a59115fb 100644 --- a/_sources/Unit2-Using-Objects/topic-2-4-methods-with-params.rst +++ b/_sources/Unit2-Using-Objects/topic-2-4-methods-with-params.rst @@ -340,70 +340,72 @@ Use the Code Lens button or this |Java Visualizer| to step through the code. } ==== - import static org.junit.Assert.*; - - import org.junit.*; - - import java.io.*; - - public class RunestoneTests extends CodeTestHelper - { - public String expected = - "Old MacDonald had a farm\n" - + "E-I-E-I-O\n" - + "And on that farm he had a cow\n" - + "E-I-E-I-O\n" - + "With a moo moo here,\n" - + "And a moo moo there,\n" - + "Old MacDonald had a farm\n" - + "E-I-E-I-O\n" - + "Old MacDonald had a farm\n" - + "E-I-E-I-O\n" - + "And on that farm he had a duck\n" - + "E-I-E-I-O\n" - + "With a quack quack here,\n" - + "And a quack quack there,\n" - + "Old MacDonald had a farm\n" - + "E-I-E-I-O"; - - public RunestoneTests() - { - super("Song"); - } - - @Test - public void test1() - { - String output = getMethodOutput("main"); - - boolean passed = output.contains(expected); - - passed = getResults(expected, output, "Still have the old output", passed); - assertTrue(passed); - } - - @Test - public void test2() - { - String output = getMethodOutput("main"); - - boolean passed = output.contains(expected) && !output.equals(expected); - - passed = getResults(expected, output, "Verse added", passed); - assertTrue(passed); - } - - @Test - public void test3() - { - String code = getCode(); - int numVerses = countOccurences(code, "verse("); - boolean passed = numVerses >= 4; - // + 1 because of verse method definition - passed = getResults("3 or more", "" + numVerses, "Number of verses", passed); - assertTrue(passed); - } - } + // Test for 2.4.4 Song + import static org.junit.Assert.*; + import org.junit.*;; + import java.io.*; + + public class RunestoneTests extends CodeTestHelper + { + public String verse1 = "Old MacDonald had a farm\nE-I-E-I-O\nAnd on that farm he had a cow\nE-I-E-I-O\nWith a moo moo here,\nAnd a moo moo there,\nOld MacDonald had a farm\nE-I-E-I-O"; + public String verse2 = "Old MacDonald had a farm\nE-I-E-I-O\nAnd on that farm he had a duck\nE-I-E-I-O\nWith a quack quack here,\nAnd a quack quack there,\nOld MacDonald had a farm\nE-I-E-I-O"; + + public String verse3 = "Old MacDonald had a farm\nE-I-E-I-O\nAnd on that farm he had a ...\nE-I-E-I-O\nWith a ... ... here,\nAnd a ... ... there,\nOld MacDonald had a farm\nE-I-E-I-O"; + + public RunestoneTests() { + super("Song"); + } + + @Test + public void test1() + { + String output = getMethodOutput("main"); + output = output.replace(verse1, "").trim(); + output = output.replace(verse2, "").trim(); + + boolean passed = output.length() > 0; + + passed = getResults(verse3, output, "Contains new verse", passed); + assertTrue(passed); + } + + @Test + public void test2() + { + String output = getMethodOutput("main"); + + boolean passed1 = output.contains(verse1); + boolean passed2 = output.contains(verse2); + boolean passed = passed1 && passed2; + + String exp = "Verse 1: true\nVerse 2: true"; + String act = "Verse 1: " + passed1 + "\nVerse 2: " + passed2; + + passed = getResults(exp, act, "Contains original verses", passed); + assertTrue(passed); + } + + @Test + public void testCode1() { + String[] lines = getCode().split("\n"); + String expect = "s.verse("; + String output = ""; + int count = 0; + + for (int i = 0; i < lines.length; i++) { + if (lines[i].contains(expect)) { + output += lines[i].trim() + "\n"; + count++; + } + } + + String expected = "s.verse(\"cow\", \"moo\");\ns.verse(\"duck\",\"quack\");\ns.verse(\"...\", \"...\");"; + + boolean passed = count >= 3; + passed = getResults(expected, output, "Added third call to verse", passed); + assertTrue(passed); + } + } |Exercise| **Check your understanding** diff --git a/_sources/Unit2-Using-Objects/topic-2-9-Math.rst b/_sources/Unit2-Using-Objects/topic-2-9-Math.rst index 5c494047..fde43eda 100644 --- a/_sources/Unit2-Using-Objects/topic-2-9-Math.rst +++ b/_sources/Unit2-Using-Objects/topic-2-9-Math.rst @@ -620,61 +620,62 @@ Now what about the combination lock for this challenge? You will need to spin th ==== import static org.junit.Assert.*; - import org.junit.*; - import java.io.*; - - public class RunestoneTests extends CodeTestHelper - { + + public class RunestoneTests extends CodeTestHelper { @Test - public void test1() - { + public void test1() { String output = getMethodOutput("main"); String[] lines = output.split("\\s+"); - + boolean passed = lines.length >= 2; - - passed = - getResults( - "2+ lines of output", - lines.length + " lines of output", - "Expected output", - passed); + + passed = getResults( + "2+ lines of output", + lines.length + " lines of output", + "Expected output", + passed); assertTrue(passed); } - + @Test - public void test2() - { + public void test2() { String output = getMethodOutput("main"); boolean passed = output.contains("64000"); - passed = getResults("true", "" + passed, "Prints 40^3", passed); + passed = getResults("true", "" + passed, "Prints result of 40^3", passed); assertTrue(passed); } - + @Test - public void test3() - { - String code = getCode(); - int num = countOccurences(code, "(int)(Math.random()*40"); - + public void test3() { + String[] code = getCode().split("\n"); + String expected = "Possible answers:\n(int) (Math.random() * 40)\n(int) (40 * Math.random())"; + String actual = ""; + int num = 0; + + for (int i = 0; i < code.length; i++) { + if (code[i].contains("Math.random()") && code[i].contains("40")) { + actual += code[i].trim() + "\n"; + if (code[i].contains("(int)")) + num++; + } + } + boolean passed = num >= 3; - passed = - getResults( - "3", - "" + num, - "Calls to Math.random() for a random number from 0 up to 40", - passed); + passed = getResults( + expected, + actual, + "Creates 3 random numbers from 0 to 40 (not inclusive)", + passed); assertTrue(passed); } - + @Test - public void test4() - { + public void test4() { String code = getCode(); int num = countOccurences(code, "Math.pow("); - + boolean passed = num >= 1; passed = getResults("1 or more", "" + num, "Calls to Math.pow(...)", passed); assertTrue(passed); diff --git a/_sources/Unit3-If-Statements/topic-3-4-else-ifs.rst b/_sources/Unit3-If-Statements/topic-3-4-else-ifs.rst index 038d2cfb..842d16b7 100644 --- a/_sources/Unit3-If-Statements/topic-3-4-else-ifs.rst +++ b/_sources/Unit3-If-Statements/topic-3-4-else-ifs.rst @@ -230,41 +230,79 @@ Here is a flowchart for a conditional with 3 options like in the code above. ==== // Test Code for Lesson 3.4 - lccbIfDebug import static org.junit.Assert.*; - + import org.junit.After; + import org.junit.Before; import org.junit.Test; - + import java.io.*; - - public class RunestoneTests extends CodeTestHelper - { - @Test - public void testMainCorrectOutput() throws IOException - { - String output = getMethodOutput("main"); - String expected = "A\n"; - boolean passed = getResults(expected, output, "Expected output from main"); - assertTrue(passed); + + public class RunestoneTests extends CodeTestHelper { + public RunestoneTests() { + super("IfDebug"); } - + @Test - public void testCodeContainsFourElses() - { + public void testCodeContainsFourElses() { String code = getCode(); - String[] tokens = code.split("\\s+"); - int expectedElseCount = 4; - int actualElseCount = 0; - for (int i = 0; i < tokens.length; i++) - { - if (tokens[i].equals("else")) - { - actualElseCount++; - } - } - boolean passed = - getResults(expectedElseCount, actualElseCount, "Expected number of else's"); + int actualElseCount = countOccurences(code, "else"); + + boolean passed = getResults("" + expectedElseCount, "" + actualElseCount, "Expected number of else's"); + assertTrue(passed); + + } + + private int[] grades = { 100, 95, 83, 79, 65, 50 }; + String[] outs = { "A", "A", "B", "C", "D", "F" }; + + @Test + public void testGrades0() throws Exception { + changeAndTestCode(0); + } + + @Test + public void testGrades1() throws Exception { + changeAndTestCode(1); + } + + @Test + public void testGrades2() throws Exception { + changeAndTestCode(2); + } + + @Test + public void testGrades3() throws Exception { + changeAndTestCode(3); + } + + @Test + public void testGrades4() throws Exception { + changeAndTestCode(4); + } + + @Test + public void testGrades5() throws Exception { + changeAndTestCode(5); + } + + public void changeAndTestCode(int i) throws Exception { + String output = getOutputChangedCode(grades[i]); + + String expected = outs[i]; + boolean passed = output.contains(expected); + getResults(expected, output, "Checking output for grade = " + grades[i], passed); assertTrue(passed); } + + public String getOutputChangedCode(int newVal) throws Exception { + String className = "Test" + newVal; + + String program = getCode(); + program = program.replace("IfDebug", className).replace("public class", "class"); + program = program.replaceAll("int score\\s*=\\s*\\d+", "int score = " + newVal); + + return getMethodOutputChangedCode(program, className, "main"); + } } .. activecode:: ifelseifBattery