From c5a3d7ee1d73ed110f6fb6342c0851c31b0a95a1 Mon Sep 17 00:00:00 2001 From: Forest Date: Thu, 15 Feb 2024 11:38:07 -0600 Subject: [PATCH] add questions as a custom package --- ComputerScience42SAP.iml | 3 + src/mainpackage/MainClass.java | 17 +++--- src/testing/Tester.java | 4 +- .../units/APUnit01PrimitiveTypes.java | 4 +- src/testing/forest/TestCode.java | 33 +++++++++++ src/testing/forest/questions/Question1.java | 17 ++++++ src/testing/forest/questions/Question2.java | 25 ++++++++ src/testing/forest/questions/Question3.java | 58 +++++++++++++++++++ 8 files changed, 149 insertions(+), 12 deletions(-) create mode 100644 src/testing/forest/TestCode.java create mode 100644 src/testing/forest/questions/Question1.java create mode 100644 src/testing/forest/questions/Question2.java create mode 100644 src/testing/forest/questions/Question3.java diff --git a/ComputerScience42SAP.iml b/ComputerScience42SAP.iml index c90834f..6bd3fd5 100644 --- a/ComputerScience42SAP.iml +++ b/ComputerScience42SAP.iml @@ -4,6 +4,9 @@ + + + diff --git a/src/mainpackage/MainClass.java b/src/mainpackage/MainClass.java index 3bea563..39050fe 100644 --- a/src/mainpackage/MainClass.java +++ b/src/mainpackage/MainClass.java @@ -1,10 +1,13 @@ -/** Required package class namespace */ +/** + * Required package class namespace + */ package mainpackage; /** Required API imports */ -import testing.Tester; +import testing.Tester; +import testing.forest.TestCode; /** * MainClass.java - the main class for this project @@ -12,18 +15,18 @@ * @author Mr. Wachs * @since June 2023 */ -public class MainClass -{ +public class MainClass { /** * Main method for the project - * + * * @param args the command line arguments */ public static void main(String[] args) { - new Tester(); + //new Tester(); + new TestCode(); // Forest's code } - + } // diff --git a/src/testing/Tester.java b/src/testing/Tester.java index 472aa1b..8d9acdc 100644 --- a/src/testing/Tester.java +++ b/src/testing/Tester.java @@ -23,9 +23,9 @@ public class Tester */ public Tester() { Simulator.header("Mr. Wachs Computer Science testing started..."); - new PreCourseContent(); + //new PreCourseContent(); new ComputerScience42SAP(); - new PostCourseContent(); + //new PostCourseContent(); Simulator.saveOutput(); Simulator.header("Mr. Wachs Computer Science testing complete!"); } diff --git a/src/testing/cs42sap/curriculum/units/APUnit01PrimitiveTypes.java b/src/testing/cs42sap/curriculum/units/APUnit01PrimitiveTypes.java index 5a026aa..d47d689 100644 --- a/src/testing/cs42sap/curriculum/units/APUnit01PrimitiveTypes.java +++ b/src/testing/cs42sap/curriculum/units/APUnit01PrimitiveTypes.java @@ -104,9 +104,7 @@ public APUnit01PrimitiveTypes() { System.out.println("specified type of data. However, the data "); System.out.print("itself can change (or \"vary\") after the initial "); System.out.println("declaration. Examples of this are below..."); - - int age = 17; - final String name = "Forest"; + // Variables declared and assigned initial ("starting") values: boolean a = true; int b = 0; diff --git a/src/testing/forest/TestCode.java b/src/testing/forest/TestCode.java new file mode 100644 index 0000000..5c33927 --- /dev/null +++ b/src/testing/forest/TestCode.java @@ -0,0 +1,33 @@ +package testing.forest; + +import testing.forest.questions.*; + +import java.lang.reflect.Constructor; + +/* + * TestCode.java - Running some code for testing purposes + * + * @Author Forest + */ +public class TestCode { + public TestCode() { + initializeQuestions(); + } + + private void initializeQuestions() { + try { + Class[] questionClasses = { + Question1.class, + Question2.class, + Question3.class + }; + + for (Class questionClass : questionClasses) { + Constructor constructor = questionClass.getDeclaredConstructor(); + constructor.newInstance(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/testing/forest/questions/Question1.java b/src/testing/forest/questions/Question1.java new file mode 100644 index 0000000..d4058af --- /dev/null +++ b/src/testing/forest/questions/Question1.java @@ -0,0 +1,17 @@ +package testing.forest.questions; + +import io.Simulator; + +public class Question1 { + public Question1() { + Simulator.header("Question 1"); + int a = 5; + int b = 2; + double c = 3.0; + Simulator.code("int a = 5;"); + Simulator.code("int b = 2;"); + Simulator.code("double c = 3.0;"); + Simulator.code("System.out.println(5 + a / b * c -1);"); + System.out.println(5 + a / b * c -1); + } +} diff --git a/src/testing/forest/questions/Question2.java b/src/testing/forest/questions/Question2.java new file mode 100644 index 0000000..e73d822 --- /dev/null +++ b/src/testing/forest/questions/Question2.java @@ -0,0 +1,25 @@ +package testing.forest.questions; + +import io.Simulator; + +import java.util.Random; + +public class Question2 { + Random random = new Random(); + public Question2() { + Simulator.header("Question 2"); + + String[] words = {"hello", "world", "java", "programming", "language", "computer", "science", "software", "engineering"}; + for (int i = 0; i < 5; i++) { + processWords(words[random.nextInt(words.length)], words[random.nextInt(words.length)]); + } + } + + public static void processWords(String word1, String word2) { + String str1 = word1.substring(0, 2); + String str2 = word2.substring(word2.length() - 1); + String result = str2 + str1; + int index = result.indexOf(str2); + System.out.println(result.indexOf(str2)); + } +} diff --git a/src/testing/forest/questions/Question3.java b/src/testing/forest/questions/Question3.java new file mode 100644 index 0000000..c1f27c9 --- /dev/null +++ b/src/testing/forest/questions/Question3.java @@ -0,0 +1,58 @@ +package testing.forest.questions; + +import io.Simulator; + +import java.util.ArrayList; +import java.util.List; + +public class Question3 { + + List rn1List = new ArrayList<>(); + List rn2List = new ArrayList<>(); + List rn3List = new ArrayList<>(); + List rn4List = new ArrayList<>(); + List rn5List = new ArrayList<>(); + + public Question3() { + Simulator.header("Question 3"); + Simulator.comment("Witch of the following statments assigns a random int between 25 and 60, inclusive, to `rn`?"); + + Simulator.code("int rn = (int) (Math.random() * 25) + 36;"); + Simulator.code("int rn = (int) (Math.random() * 25) + 60;"); + Simulator.code("int rn = (int) (Math.random() * 26) + 60;"); + Simulator.code("int rn = (int) (Math.random() * 36) + 25;"); + Simulator.code("int rn = (int) (Math.random() * 60) + 25;"); + + Simulator.comment("Run each 100 times."); + + for (int i = 0; i < 100; i++) { + int rn1 = (int) (Math.random() * 25) + 36; + int rn2 = (int) (Math.random() * 25) + 60; + int rn3 = (int) (Math.random() * 26) + 60; + int rn4 = (int) (Math.random() * 36) + 25; + int rn5 = (int) (Math.random() * 60) + 25; + + rn1List.add(rn1); + rn2List.add(rn2); + rn3List.add(rn3); + rn4List.add(rn4); + rn5List.add(rn5); + } + + // Check if the results are within the range + boolean rn1InRange = rn1List.stream().allMatch(num -> num >= 25 && num <= 60); + boolean rn2InRange = rn2List.stream().allMatch(num -> num >= 25 && num <= 60); + boolean rn3InRange = rn3List.stream().allMatch(num -> num >= 25 && num <= 60); + boolean rn4InRange = rn4List.stream().allMatch(num -> num >= 25 && num <= 60); + boolean rn5InRange = rn5List.stream().allMatch(num -> num >= 25 && num <= 60); + + // Output the results + Simulator.output("rn1List: " + rn1List + ", all in range: " + rn1InRange, true); + Simulator.output("rn2List: " + rn2List + ", all in range: " + rn2InRange, true); + Simulator.output("rn3List: " + rn3List + ", all in range: " + rn3InRange, true); + Simulator.output("rn4List: " + rn4List + ", all in range: " + rn4InRange, true); + Simulator.output("rn5List: " + rn5List + ", all in range: " + rn5InRange, true); + + + } +}