Skip to content

Commit 6534327

Browse files
committed
big refactor on new KataQuestions for Course 1 and 2 and 3
1 parent 40c5d5a commit 6534327

14 files changed

+207
-126
lines changed

src/main/java/org/teachingextensions/approvals/lite/util/NumberUtils.java

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,64 @@
55
/**
66
* A static class of convenience functions for Manipulating numbers
77
*/
8-
public class NumberUtils {
9-
public static Random RANDOM = new Random();
10-
11-
/**
12-
* Loads an int from a String.
13-
*
14-
* @param i a string with an integer in it
15-
* @param defaultValue value to use when no integer can be found in the string
16-
* @return the integer found in the string
17-
*/
18-
public static int load(String i, int defaultValue) {
19-
return load(i, defaultValue, true);
8+
public class NumberUtils
9+
{
10+
public static Random RANDOM = new Random();
11+
/**
12+
* Loads an int from a String.
13+
*
14+
* @param i a string with an integer in it
15+
* @param defaultValue value to use when no integer can be found in the string
16+
* @return the integer found in the string
17+
*/
18+
public static int load(String i, int defaultValue)
19+
{
20+
return load(i, defaultValue, true);
21+
}
22+
/**
23+
* Loads an int from a String.
24+
*
25+
* @param i a string with an integer in it
26+
* @param defaultValue value to use when no integer can be found in the string
27+
* @param stripNonNumeric true if non-numeric characters should be removed from the string
28+
* @return the integer found in the string
29+
*/
30+
public static int load(String i, int defaultValue, boolean stripNonNumeric)
31+
{
32+
try
33+
{
34+
i = stripNonNumeric ? StringUtils.stripNonNumeric(i, true, true) : i;
35+
defaultValue = Integer.parseInt(i);
2036
}
21-
22-
/**
23-
* Loads an int from a String.
24-
*
25-
* @param i a string with an integer in it
26-
* @param defaultValue value to use when no integer can be found in the string
27-
* @param stripNonNumeric true if non-numeric characters should be removed from the string
28-
* @return the integer found in the string
29-
*/
30-
public static int load(String i, int defaultValue, boolean stripNonNumeric) {
31-
try {
32-
i = stripNonNumeric ? StringUtils.stripNonNumeric(i, true, true)
33-
: i;
34-
defaultValue = Integer.parseInt(i);
35-
} catch (Exception ignored) {
36-
}
37-
return defaultValue;
37+
catch (Exception ignored)
38+
{
3839
}
39-
40-
public static boolean equals(double one, double two, double delta) {
41-
double actualDelta = one - two;
42-
return (-delta < actualDelta) && (actualDelta < delta);
40+
return defaultValue;
41+
}
42+
public static boolean equals(double one, double two, double delta)
43+
{
44+
double actualDelta = one - two;
45+
return (-delta < actualDelta) && (actualDelta < delta);
46+
}
47+
/**
48+
* randomly chooses a number between the minimum and maximum
49+
* <div><b>Example:</b>
50+
* {@code int grade = NumberUtils.getRandomInt(1,100);} </div>
51+
*
52+
* @param minimum The lowest possible value (inclusive)
53+
* @param maximum The highest possible value (inclusive)
54+
* @return the random number
55+
*/
56+
public static int getRandomInt(int minimum, int maximum)
57+
{
58+
int diff = maximum - minimum;
59+
if (diff == 0)
60+
{
61+
return maximum;
4362
}
44-
45-
/**
46-
* randomly chooses a number between the minimum and maximum
47-
* <div><b>Example:</b>
48-
* {@code int grade = NumberUtils.getRandomInt(1,100);} </div>
49-
*
50-
* @param minimum The lowest possible value (inclusive)
51-
* @param maximum The highest possible value (inclusive)
52-
* @return the random number
53-
*/
54-
public static int getRandomInt(int minimum, int maximum) {
55-
int diff = maximum - minimum;
56-
if (diff == 0) {
57-
return maximum;
58-
} else {
59-
return RANDOM.nextInt(diff) + minimum;
60-
}
63+
else
64+
{
65+
return RANDOM.nextInt(diff) + minimum;
6166
}
67+
}
6268
}

src/main/java/org/teachingkidsprogramming/recipes/completed/section01forloops/KataQuestions/SquareToIncreasingThickHexagonMultiColor_05.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// How would you make a hexagon...
88
// with 20px thick sides...
99
// and with randomly colored lines
10-
// and with side lengths of 25 that increase by 2x?
10+
// and with side lengths of 25 that increase by 2x each time?
1111
//
1212
// Write out the steps in English
1313
// Then translate the steps into code
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.teachingkidsprogramming.recipes.completed.section01forloops.KataQuestions;
2+
3+
import org.teachingextensions.logo.Tortoise;
4+
import org.teachingextensions.logo.utils.ColorUtils.PenColors;
5+
6+
//------------Kata Question---------------//
7+
// How would you make a hexagon...
8+
// with 20px thick sides...
9+
// and with randomly colored lines
10+
// and with side lengths of 25 that increase by 2x each time?
11+
//
12+
public class SquareToIncreasingThickHexagonMultiColor_ANSWER_06
13+
{
14+
public static void main(String[] args) throws Exception
15+
{
16+
Tortoise.show();
17+
Tortoise.setSpeed(10);
18+
Tortoise.setPenWidth(20);
19+
int sides = 7;
20+
for (int i = 0; i < sides; i++)
21+
{
22+
Tortoise.setPenColor(PenColors.getRandomColor());
23+
Tortoise.move(25 * i);
24+
Tortoise.turn(360 / sides);
25+
}
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.teachingkidsprogramming.recipes.completed.section02methods.KataQuestions;
2+
3+
import org.teachingextensions.logo.Tortoise;
4+
import org.teachingextensions.logo.utils.ColorUtils.PenColors;
5+
6+
//------------Kata Question---------------//
7+
// How would you make a pointy roof
8+
// and how would make a slanted roof
9+
// and change the color of the houses?
10+
//
11+
public class HousesToAColorfulPointyAndASlantedRoof_ANSWER_05
12+
{
13+
public static void main(String[] args)
14+
{
15+
Tortoise.show();
16+
Tortoise.setSpeed(10);
17+
Tortoise.setX(200);
18+
int height = 40;
19+
drawSlantedRoofHouse(height);
20+
drawPointyRoofHouse(120);
21+
drawSlantedRoofHouse(90);
22+
drawPointyRoofHouse(20);
23+
}
24+
public static void drawSlantedRoofHouse(int height)
25+
{
26+
Tortoise.setPenColor(PenColors.Grays.Black);
27+
Tortoise.move(height);
28+
drawSlantedRoof();
29+
Tortoise.move(height);
30+
Tortoise.turn(-90);
31+
Tortoise.move(20);
32+
Tortoise.turn(-90);
33+
}
34+
private static void drawSlantedRoof()
35+
{
36+
Tortoise.turn(45);
37+
Tortoise.move(60);
38+
Tortoise.turn(135);
39+
Tortoise.move(45);
40+
}
41+
public static void drawPointyRoofHouse(int height)
42+
{
43+
Tortoise.setPenColor(PenColors.Grays.LightGray);
44+
Tortoise.move(height);
45+
drawPointyRoof();
46+
Tortoise.move(height);
47+
Tortoise.turn(-90);
48+
Tortoise.move(20);
49+
Tortoise.turn(-90);
50+
}
51+
private static void drawPointyRoof()
52+
{
53+
Tortoise.turn(45);
54+
Tortoise.move(30);
55+
Tortoise.turn(90);
56+
Tortoise.move(30);
57+
Tortoise.turn(45);
58+
}
59+
}

src/main/java/org/teachingkidsprogramming/recipes/completed/section02methods/KataQuestions/PyramidsOfGizaRemoveEvenMoreDuplication_03.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import org.teachingextensions.logo.utils.ColorUtils.PenColors;
55

66
//------------Kata Question---------------//
7-
//Where do you see duplicate lines of code?
8-
// and how can you refactor to make this code more readable?
9-
//Write out the steps in English
10-
//Then translate the steps into code
11-
//Make sure to run after each line
7+
// Where do you see duplicate lines of code?
8+
// and how can you re-factor to make this code more readable?
9+
// Write out the steps in English
10+
// Then translate the steps into code
11+
// Make sure to run after each line
1212
//
1313
public class PyramidsOfGizaRemoveEvenMoreDuplication_03
1414
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.teachingkidsprogramming.recipes.completed.section02methods.KataQuestions;
2+
3+
import org.teachingextensions.logo.Tortoise;
4+
import org.teachingextensions.logo.utils.ColorUtils.PenColors;
5+
6+
//------------Kata Question---------------//
7+
// Where do you see duplicate lines of code?
8+
// and how can you re-factor to make this code more readable?
9+
//
10+
// Note: This answer is more advanced than the student's knowledge at this point
11+
// The answer is for teacher preparation (for future lessons)
12+
// It is also somewhat academic to illustrate 'too much re-factoring'
13+
// Which results in reduced human readability
14+
//
15+
public class PyramidsOfGizaRemoveEvenMoreDuplication_ANSWER_04
16+
{
17+
public static void main(String[] args) throws Exception
18+
{
19+
setUpPyramidLand();
20+
int[][] degreesAndLength = {{-90, 220},
21+
{135, 100},
22+
{90, 100},
23+
{-90, 100},
24+
{90, 100},
25+
{-90, 100},
26+
{90, 100},
27+
{135, 210}};
28+
for (int[] i : degreesAndLength)
29+
{
30+
Tortoise.turn(i[0]);
31+
Tortoise.move(i[1]);
32+
}
33+
}
34+
private static void setUpPyramidLand()
35+
{
36+
Tortoise.show();
37+
Tortoise.setSpeed(10);
38+
Tortoise.getBackgroundWindow().setBackground(PenColors.Blues.AliceBlue);
39+
Tortoise.setPenColor(PenColors.Yellows.DarkGoldenrod);
40+
Tortoise.setPenWidth(2);
41+
Tortoise.hide();
42+
}
43+
}

src/main/java/org/teachingkidsprogramming/recipes/completed/section03ifs/KataQuestions/HiLowToNoZeroNoNegativeAndKeepPlaying_02.java renamed to src/main/java/org/teachingkidsprogramming/recipes/completed/section03ifs/KataQuestions/HiLoNoZeroNoNegativeAndPlayOn_02.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// Then translate the steps into code
1414
// Make sure to run after each line
1515
//
16-
public class HiLowToNoZeroNoNegativeAndKeepPlaying_02
16+
public class HiLoNoZeroNoNegativeAndPlayOn_02
1717
{
1818
public static void main(String[] args)
1919
{

src/main/java/org/teachingkidsprogramming/recipes/completed/section03ifs/KataQuestions/HiLowToNoZeroNoNegativePlayOnAndAddUpperBound_03.java renamed to src/main/java/org/teachingkidsprogramming/recipes/completed/section03ifs/KataQuestions/HiLoNoZeroOr101AndNoMinusPlayOn_03.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// Then translate the steps into code
1616
// Make sure to run after each line
1717
//
18-
public class HiLowToNoZeroNoNegativePlayOnAndAddUpperBound_03
18+
public class HiLoNoZeroOr101AndNoMinusPlayOn_03
1919
{
2020
public static void main(String[] args)
2121
{

src/main/java/org/teachingkidsprogramming/recipes/completed/section03ifs/KataQuestions/HiLowNoZero_01.java renamed to src/main/java/org/teachingkidsprogramming/recipes/completed/section03ifs/KataQuestions/HiLoNoZero_01.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// Then translate the steps into code
1212
// Make sure to run after each line
1313
//
14-
public class HiLowNoZero_01
14+
public class HiLoNoZero_01
1515
{
1616
public static void main(String[] args)
1717
{

src/main/java/org/teachingkidsprogramming/recipes/completed/section03ifs/KataQuestions/HiLowFixBug_04.java renamed to src/main/java/org/teachingkidsprogramming/recipes/completed/section03ifs/KataQuestions/HiLoSeeBugInfo_04.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
import org.teachingextensions.logo.utils.EventUtils.MessageBox;
66

77
//------------Kata Question---------------//
8-
// What happens when you enter a decimal and why?
9-
// What should happen?
10-
// Can you fix the bug?
8+
// What happens when you enter letters (i.e. 'abc') as your guess?
9+
// How can you see what your guess is exactly?
1110
// Write out the steps in English
1211
// Then translate the steps into code
1312
// Make sure to run after each line
1413
//
15-
public class HiLowFixBug_04
14+
public class HiLoSeeBugInfo_04
1615
{
1716
public static void main(String[] args)
1817
{
@@ -21,6 +20,7 @@ public static void main(String[] args)
2120
for (int i = 0; i < 8; i++)
2221
{
2322
int guess = MessageBox.askForNumericalInput("Can you guess the random number between 1 and 100?");
23+
2424
if (guess == 0)
2525
{
2626
MessageBox.showMessage("No Zero allowed, you lose!");

0 commit comments

Comments
 (0)