Created
October 28, 2023 22:47
-
-
Save whizyrel/ad6b483457a09dcd9c7512d0c0652cf8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| System.out.println("===Program Started==="); | |
| class TestClass { | |
| public final String name; | |
| public final int age; | |
| public TestClass(String name, int age) { | |
| this.name = name; | |
| this.age = age; | |
| } | |
| public void sayMyName() { | |
| System.out.println("My name is " + this.name); | |
| } | |
| public int getAge() { | |
| return age; | |
| } | |
| } | |
| String myName = "Israel"; | |
| TestClass testClass = new TestClass(myName, 16); | |
| // testClass.sayMyName(); | |
| // INFO IFs | |
| final String personName = "Israel"; | |
| final int personAge = 16; | |
| final ArrayList<Integer> listOfAges = new ArrayList<Integer>(); | |
| listOfAges.add(12); | |
| listOfAges.add(14); | |
| System.out.println("This is the age: " + testClass.getAge()); | |
| if (listOfAges.isEmpty()) { | |
| System.out.println("This is the if block... line 1"); | |
| System.out.println("This is the if block... line 2"); | |
| System.out.println("This is the if block... line 3"); | |
| System.out.println("This is the if block... line 4"); | |
| } else { | |
| // INFO else | |
| System.out.println("Oh no!"); | |
| System.out.println("The list is not empty!"); | |
| System.out.println("Gosh!!!"); | |
| } | |
| // INFO if-else if | |
| if (true) {} | |
| if (false) {} | |
| if (2 == 2) {} | |
| if (testClass.getAge() == 6) { | |
| System.out.println("This is for true"); | |
| } else if (testClass.getAge() == 15) { | |
| System.out.println("This is for false"); | |
| } else if (testClass.getAge() == 14) { | |
| System.out.println("This is for testClass.getAge() == 16"); | |
| } else { | |
| System.out.println("This is for the else block"); | |
| } | |
| // INFO switch | |
| final String switchConditionalVariable = "This" | |
| final int magicNumber = 4; | |
| switch (magicNumber - 6) { | |
| case 2: | |
| System.out.println("This is for case -> 2"); | |
| break; | |
| case 3: | |
| System.out.println("This is for case -> 3"); | |
| break; | |
| case 4: | |
| System.out.println("This is for case -> 4"); | |
| break; | |
| case 5: | |
| System.out.println("This is for case -> 5"); | |
| break; | |
| default: | |
| System.out.println("This is for default"); | |
| break; | |
| } | |
| System.out.println("This is in the global scope and not part of the if block"); | |
| System.out.println("===Program Ended==="); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // for (int i = 1; i < 5; i++) { | |
| // System.out.println("This is a loop: " + i); | |
| // } | |
| // int i; | |
| // for (i = 1; i < 5; i++) { | |
| // System.out.println("This is a loop: " + i); | |
| // } | |
| // int i; | |
| // for (i = 1; i < 5;) { | |
| // System.out.println("This is a loop: " + i); | |
| // i++; | |
| // } | |
| // int i = 1; | |
| // for (; i < 5;) { | |
| // System.out.println("This is a loop: " + i); | |
| // i++; | |
| // } | |
| // int[] arrayOfIntegers = {1,2,3,4,5}; | |
| // for (int i : arrayOfIntegers) { | |
| // System.out.println("item: " + i); | |
| // } | |
| // INFO While loop | |
| // int i = 1; | |
| // while (i < 5) { | |
| // System.out.println("This is a loop: " + i); | |
| // i++; | |
| // } | |
| // INFO do...while | |
| int i = 1; | |
| do { | |
| System.out.println("This is a loop: " + i); | |
| // INFO break | |
| // if (i == 2) { | |
| // // INFO When i equals 2 stop right here | |
| // break; | |
| // } | |
| System.out.println("last print statement"); | |
| // INFO continue | |
| if (i == 2) { | |
| // INFO When i equals 2 skip this iteration | |
| continue; | |
| } | |
| i++; | |
| } while (i < 5); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Write a Java program that takes three numbers from the user and prints the greatest number. | |
| Test Data | |
| Input the 1st number: 25 | |
| Input the 2nd number: 78 | |
| Input the 3rd number: 87 | |
| Expected Output : | |
| The greatest: 87 | |
| */ | |
| // Scanner scanner = new Scanner(System.in); | |
| // System.out.print("Input numOne: "); | |
| // int numOne = scanner.nextInt(); | |
| // System.out.println(); | |
| // System.out.print("Input numTwo: "); | |
| // int numTwo = scanner.nextInt(); | |
| // System.out.println(); | |
| // System.out.print("Input numThree: "); | |
| // int numThree = scanner.nextInt(); | |
| // System.out.println(); | |
| final int numOne = 25; | |
| final int numTwo = 78; | |
| final int numThree = 87; | |
| // INFO Using IFs | |
| if (numOne > numTwo && numOne > numThree) { | |
| System.out.println(numOne + " is the greatest!"); | |
| } | |
| if (numTwo > numOne && numTwo > numThree) { | |
| System.out.println(numTwo + " is the greatest!"); | |
| } | |
| if (numThree > numOne && numThree > numTwo) { | |
| System.out.println(numThree + " is the greatest!"); | |
| } | |
| // INFO IF...ELSE | |
| if (numOne > numTwo && numOne > numThree) { | |
| System.out.println(numOne + " is the greatest!"); | |
| } | |
| if (numTwo > numOne && numTwo > numThree) { | |
| System.out.println(numTwo + " is the greatest!"); | |
| } else { | |
| System.out.println(numThree + " is the greatest!"); | |
| } | |
| // INFO IF...ELSE IF | |
| if (numOne > numTwo && numOne > numThree) { | |
| System.out.println(numOne + " is the greatest!"); | |
| } else if (numTwo > numOne && numTwo > numThree) { | |
| System.out.println(numTwo + " is the greatest!"); | |
| } else if (numThree > numOne && numThree > numTwo) { | |
| System.out.println(numThree + " is the greatest!"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment