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/README.md b/README.md index ee2e6265..cc09f42e 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: @@ -241,12 +241,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 diff --git a/src/FizzBuzz.java b/src/FizzBuzz.java index c2fe8f79..398631cf 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); + } + } + + public 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; } } diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 00000000..c709060b --- /dev/null +++ b/src/Main.java @@ -0,0 +1,6 @@ +public class Main { + public static void main(String[] args) { + FizzBuzz.checkingLoop(5 + System.out.println("The change"); + } +} 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..a33ee1a7 --- /dev/null +++ b/src/Reduce.java @@ -0,0 +1,17 @@ +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); + } +}