From 66f96c1d87ca420de72310a5aefb42c5ae05a715 Mon Sep 17 00:00:00 2001 From: Phoebe Chuang Date: Mon, 11 Sep 2023 11:50:14 -0400 Subject: [PATCH 1/8] refactored to extract helper method --- .idea/.gitignore | 8 ++++++++ .idea/misc.xml | 6 ++++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ FizzBuzz.iml | 11 +++++++++++ src/FizzBuzz.java | 35 +++++++++++++++++++++-------------- 6 files changed, 60 insertions(+), 14 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 FizzBuzz.iml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..13566b81 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..6ff3c251 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..faf3b05f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/FizzBuzz.iml b/FizzBuzz.iml new file mode 100644 index 00000000..c90834f2 --- /dev/null +++ b/FizzBuzz.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/FizzBuzz.java b/src/FizzBuzz.java index c2fe8f79..5526a141 100644 --- a/src/FizzBuzz.java +++ b/src/FizzBuzz.java @@ -4,31 +4,38 @@ class FizzBuzz { public static void main(String[] args) { + int i = 0; - for (int i = 1; i < 100; i++) { + while (i < 100) { - // Find out which numbers divide i. - boolean divisibleBy3 = i % 3 == 0; - boolean divisibleBy5 = i % 5 == 0; + i = checkingLoop(i); + } + } + + private static int checkingLoop(int i) { + // Find out which numbers divide i. + boolean divisibleBy3 = i % 3 == 0; + boolean divisibleBy5 = i % 5 == 0; - // Print our appropriate result. - if (divisibleBy3 && divisibleBy5) { + // Print our appropriate result. + if (divisibleBy3 && divisibleBy5) { - System.out.println("Fizz Buzz"); + System.out.println("Fizz Buzz"); - } else if (divisibleBy3) { + } else if (divisibleBy3) { - System.out.println("Fizz"); + System.out.println("Fizz"); - } else if (divisibleBy5) { + } else if (divisibleBy5) { - System.out.println("Buzz"); + System.out.println("Buzz"); - } else { + } else { - System.out.println(i); + System.out.println(i); - } } + i++; + return i; } } From 1a63892b8aa2511a15637d96eb96354023edf2cc Mon Sep 17 00:00:00 2001 From: Phoebe Chuang Date: Mon, 11 Sep 2023 12:21:27 -0400 Subject: [PATCH 2/8] refactored to extract helper method --- src/Main.java | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 src/Main.java diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 00000000..70464179 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,2 @@ +public class Main { +} From 79ea7df13afc9fc6000246a40cfe0726813e1526 Mon Sep 17 00:00:00 2001 From: Phoebe Chuang Date: Mon, 11 Sep 2023 12:31:29 -0400 Subject: [PATCH 3/8] refactored to extract helper method --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a1db3975..81be6c34 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ The markdown format is commonly used for things like readme files, as they allow which allows for basic typesetting when viewed while still being a plaintext format. Below is a blank checkbox: -- [ ] Put an X in the [ ] to mark this as done! +- [X] Put an X in the [ ] to mark this as done! You can edit this file directly to check off these checkboxes throughout the lab to mark things as done. Do so now for the checkbox above. @@ -117,7 +117,7 @@ class FizzBuzz { } ``` -- [ ] Open `FizzBuzz.java` in the `src` directory and click the run button in the top left corner. +- [X] Open `FizzBuzz.java` in the `src` directory and click the run button in the top left corner. If you don't see this button, you may need to mark `src` as the `Sources Root` for the project. You can do this by right-clicking the `src` directory in the `Project` tab and near the bottom of the context menu @@ -135,7 +135,7 @@ the code are doing. For example, what's the Java version of Python's `and`? What on with that weird `for` loop? As you do this, you might find it useful to write down your own implementation in Python to help you begin to create a mapping between the two languages. -- [ ] Make note of any specific Java syntax which stands out to you and compare what you +- [X] Make note of any specific Java syntax which stands out to you and compare what you come up with those around you. Try to come up with a list of at least five things that strike you as either similar to or different from Python. @@ -160,7 +160,7 @@ Later in this course, you'll learn what all that mess means, but for now it is e ### Task 1.1: Rewrite this using `while` -- [ ] You've puzzled through how Java `for` and `if` statements work; now rewrite this +- [X] You've puzzled through how Java `for` and `if` statements work; now rewrite this to use a `while` loop instead of a `for` loop. ## How to test this code @@ -179,7 +179,7 @@ line or the closing brace `}` of the loop. 3. Immediately, type the method name you want, maybe something like `doFizzBuzz`. -- [ ] Rerun the program to verify. +- [X] Rerun the program to verify. That's your first big IntelliJ trick! There are lots more. @@ -192,7 +192,7 @@ You'll learn more about the various access modifiers in your Java readings soon. Now that you've made your first edits to your code, you should ask git to save these changes for you. -- [ ] Open the Terminal tab in IntelliJ and type `git status`, it will show you that you have modified `FizzBuzz.java` (and this README too!). +- [X] Open the Terminal tab in IntelliJ and type `git status`, it will show you that you have modified `FizzBuzz.java` (and this README too!). We will save our local changes to `FizzBuzz.java` and then push the changes to your GitHub repository using a sequence of three git commands: @@ -234,12 +234,12 @@ Now, back to exploring the code! To briefly observe what private does, let's create a new class. -- [ ] Right-click on `src` and select `New —> Java Class`. Name it `Main`. This will create a `Main.java` file. +- [X] Right-click on `src` and select `New —> Java Class`. Name it `Main`. This will create a `Main.java` file. We'll write a main method which will attempt to call `FizzBuzz.doFizzBuzz` (or whatever you called your extracted helper method). -- [ ] To quickly generate `main`, you can start typing `psvm` in IntelliJ and then press Enter to accept the +- [X] To quickly generate `main`, you can start typing `psvm` in IntelliJ and then press Enter to accept the autocomplete — it will generate an empty "public static void main" (psvm) method for you. Neat! - [ ] In the body of this main method, type `FizzBuzz.`. You'll see that the private helper method doesn't From 9279d2cb15acdcbf394c13ff4e8728664d31f055 Mon Sep 17 00:00:00 2001 From: Phoebe Chuang Date: Mon, 11 Sep 2023 12:39:27 -0400 Subject: [PATCH 4/8] refactored to extract helper method --- src/FizzBuzz.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/FizzBuzz.java b/src/FizzBuzz.java index 5526a141..398631cf 100644 --- a/src/FizzBuzz.java +++ b/src/FizzBuzz.java @@ -12,7 +12,7 @@ public static void main(String[] args) { } } - private static int checkingLoop(int i) { + public static int checkingLoop(int i) { // Find out which numbers divide i. boolean divisibleBy3 = i % 3 == 0; boolean divisibleBy5 = i % 5 == 0; From 2f516a8c550588e9e2283a7f3b40b53d7a5401fa Mon Sep 17 00:00:00 2001 From: Phoebe Chuang Date: Mon, 11 Sep 2023 12:41:21 -0400 Subject: [PATCH 5/8] ahha --- src/Main.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Main.java b/src/Main.java index 70464179..89431f6c 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,2 +1,5 @@ public class Main { + public static void main(String[] args) { + FizzBuzz.checkingLoop(5); + } } From 4bbf4dd9f9e7a9df35d89026cf2f7f1cc6cd0d40 Mon Sep 17 00:00:00 2001 From: ShihHsin0723 <144292774+ShihHsin0723@users.noreply.github.com> Date: Tue, 12 Sep 2023 00:43:51 +0800 Subject: [PATCH 6/8] Update Main.java --- src/Main.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 89431f6c..c709060b 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,6 @@ public class Main { public static void main(String[] args) { - FizzBuzz.checkingLoop(5); + FizzBuzz.checkingLoop(5 + System.out.println("The change"); } } From 24295628de0d8f2c85f6e45fc772cb1a53553e55 Mon Sep 17 00:00:00 2001 From: Phoebe Chuang Date: Mon, 11 Sep 2023 14:34:09 -0400 Subject: [PATCH 7/8] a new file --- src/Multiples.java | 12 ++++++++++++ src/Reduce.java | 2 ++ 2 files changed, 14 insertions(+) create mode 100644 src/Multiples.java create mode 100644 src/Reduce.java diff --git a/src/Multiples.java b/src/Multiples.java new file mode 100644 index 00000000..67f5cc67 --- /dev/null +++ b/src/Multiples.java @@ -0,0 +1,12 @@ +public class Multiples { + public static void main(String[] args) { + int count = 0; + + for (int i = 0; i < 1000; i++) { + if (i % 3 == 0 || i % 5 == 0) { + count++; + } + } + System.out.println(count); + } +} diff --git a/src/Reduce.java b/src/Reduce.java new file mode 100644 index 00000000..d7947a8f --- /dev/null +++ b/src/Reduce.java @@ -0,0 +1,2 @@ +package PACKAGE_NAME;public class Reduce { +} From b6ee8aeaa04f2dc61891cb89a53363f7f84846ff Mon Sep 17 00:00:00 2001 From: Phoebe Chuang Date: Mon, 11 Sep 2023 14:40:46 -0400 Subject: [PATCH 8/8] reduce file --- src/Reduce.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Reduce.java b/src/Reduce.java index d7947a8f..a33ee1a7 100644 --- a/src/Reduce.java +++ b/src/Reduce.java @@ -1,2 +1,17 @@ -package PACKAGE_NAME;public class Reduce { +public class Reduce { + public static void main(String[] args) { + int steps = 0; + int n = 100; + + while (n > 0) { + if (n % 2 == 0) { + n = n / 2; + } else { + n = n - 1; + } + steps++; + } + + System.out.println(steps); + } }