├── .gitignore ├── 000-hello-world ├── 000-hello-world.iml ├── out │ └── production │ │ └── 000-hello-world │ │ └── Hello.class └── src │ └── Hello.java ├── 001-byte-short-int-long ├── 001-byte-short-int-long.iml ├── out │ └── production │ │ └── 001-byte-short-int-long │ │ └── com │ │ └── chezearth │ │ └── Main.class └── src │ └── com │ └── chezearth │ └── Main.java ├── 002-float-and-double ├── 002-float-and-double.iml ├── out │ └── production │ │ └── 002-float-and-double │ │ └── com │ │ └── chezearth │ │ └── Main.class └── src │ └── com │ └── chezearth │ └── Main.java ├── 003-char-and-boolean ├── 003-char-and-boolean.iml ├── out │ └── production │ │ └── 03-char-and-boolean │ │ └── com │ │ └── chezearth │ │ └── Main.class └── src │ └── com │ └── chezearth │ └── Main.java ├── 004-strings ├── 004-strings.iml └── src │ └── com │ └── chezearth │ └── Main.java ├── 005-operators ├── 005-operators.iml ├── out │ └── production │ │ └── 05-operators │ │ └── com │ │ └── chezearth │ │ └── Main.class └── src │ └── com │ └── chezearth │ └── Main.java ├── 006-keywords-and-expressions ├── 006-keywords-and-expressions.iml └── src │ └── com │ └── chezearth │ └── Main.java ├── 007-if-keyword-and-codeblocks ├── 007-if-keyword-and-codeblocks.iml └── src │ └── com │ └── chezearth │ └── Main.java ├── 008-methods ├── 008-methods.iml └── src │ └── com │ └── chezearth │ └── Main.java ├── 009-speed-converter ├── 009-speed-converter.iml └── src │ ├── Main.java │ └── SpeedConverter.java ├── 010-megabytes-converter ├── 010-megabytes-converter.iml └── src │ ├── Main.java │ └── MegaBytesConverter.java ├── 011-barking-dog ├── 011-barking-dog.iml └── src │ ├── BarkingDog.java │ └── Main.java ├── 012-leap-year ├── 012-leap-year.iml └── src │ ├── LeapYear.java │ └── Main.java ├── 013-decimal-comparator ├── 013-decimal-comparator.iml └── src │ ├── DecimalComparator.java │ └── Main.java ├── 014-equal-sum-charger ├── 014-equal-sum-charger.iml └── src │ ├── EqualSumChecker.java │ └── Main.java ├── 015-teen-number-checker ├── 015-teen-number-checker.iml └── src │ ├── Main.java │ └── TeenNumberChecker.java ├── 016-method-overloading ├── 016-method-overloading.iml ├── out │ └── production │ │ └── 016-method-overloading │ │ └── com │ │ └── chezearth │ │ └── Main.class └── src │ └── com │ └── chezearth │ └── Main.java ├── 017-seconds-and-minutes-challenge ├── 017-seconds-and-minutes-challenge.iml └── src │ └── com │ └── chezearth │ └── Main.java ├── 018-area-calculator ├── 018-area-calculator.iml └── src │ ├── AreaCalculator.java │ └── Main.java ├── 019-mintes-to-years-and-days ├── 019-mintes-to-years-and-days.iml └── src │ ├── Main.java │ └── MinutesToYearsDaysCalculator.java ├── 020-equality-printer ├── 020-equality-printer.iml └── src │ ├── IntEqualityPrinter.java │ └── Main.java ├── 021-playing-cat ├── 021-playing-cat.iml └── src │ ├── Main.java │ └── PlayingCat.java ├── 022-switch ├── 022-switch.iml └── src │ └── com │ └── chezearth │ └── Main.java ├── 023-exercises-12-13 ├── 023-exercises-12-13.iml └── src │ ├── Main.java │ ├── NumberInWord.java │ └── NumberOfDaysInMonth.java ├── 024-for-loop ├── 024-for-loop.iml └── src │ └── com │ └── chezearth │ └── Main.java ├── 025-sum-3-and-5-challenge ├── 025-sum-3-and-5-challenge.iml └── src │ └── com │ └── chezearth │ └── Main.java ├── 026-while ├── 026-while.iml └── src │ └── com │ └── chezearth │ └── Main.java ├── 027-digit-sum-challenge ├── 027-digit-sum-challenge.iml └── src │ └── com │ └── chezearth │ └── Main.java ├── 028-exercises-15-23 ├── 028-exercises-15-23.iml └── src │ ├── EvenDigitSum.java │ ├── FactorPrinter.java │ ├── FirstLastDigitSum.java │ ├── GreatestCommonDivisor.java │ ├── LastDigitChecker.java │ ├── Main.java │ ├── NumberPalindrome.java │ ├── NumberToWords.java │ ├── PerfectNumber.java │ └── SharedDigit.java ├── 029-exercises-24-26 ├── 029-exercises-24-26.iml └── src │ ├── DiagonalStar.java │ ├── FlourPacker.java │ ├── LargestPrime.java │ └── Main.java ├── 030-reading-user-input ├── 030-reading-user-input.iml ├── out │ └── production │ │ └── 030-reading-user-input │ │ └── com │ │ └── chezearth │ │ ├── Main.class │ │ ├── MinAndMaxInputChallenge.class │ │ ├── ReadingUserInput.class │ │ └── ReadingUserInputChallenge.class └── src │ └── com │ └── chezearth │ ├── Main.java │ ├── MinAndMaxInputChallenge.java │ ├── ReadingUserInput.java │ └── ReadingUserInputChallenge.java ├── 031-exercises-27-28 ├── 031-exercises-27-28.iml ├── out │ └── production │ │ └── 031-exercises-27-28 │ │ ├── InputCalculator.class │ │ ├── Main.class │ │ └── PaintJob.class └── src │ ├── InputCalculator.java │ ├── Main.java │ └── PaintJob.java ├── 032-classes ├── 032-classes.iml └── src │ └── com │ └── chezearth │ ├── Car.java │ └── Main.java ├── 033-exercises-29-30 ├── 033-exercises-29-30.iml └── src │ ├── Main.java │ ├── Person.java │ └── SimpleCalculator.java ├── 034-constructors ├── 034-constructors.iml └── src │ └── com │ └── chezearth │ ├── BankAccount.java │ ├── Main.java │ └── VipCustomer.java ├── 035-exercises-31-34 ├── 035-exercises-31-34.iml └── src │ ├── Calculator.java │ ├── Carpet.java │ ├── ComplexNumber.java │ ├── Floor.java │ ├── Main.java │ ├── Point.java │ └── Wall.java ├── 036-inheritance ├── 036-inheritance.iml └── src │ └── com │ └── chezearth │ ├── Animal.java │ ├── Car.java │ ├── Dog.java │ ├── LandRover.java │ ├── Main.java │ └── Vehicle.java ├── 037-exercises-35-36 ├── 037-exercises-35-36.iml └── src │ ├── Circle.java │ ├── Cuboid.java │ ├── Cylinder.java │ ├── Main.java │ └── Rectangle.java ├── 038-composition ├── 038-composition.iml └── src │ └── com │ └── chezearth │ ├── Bed.java │ ├── Case.java │ ├── Computer.java │ ├── Cupboard.java │ ├── Dimensions.java │ ├── Lamp.java │ ├── Main.java │ ├── Monitor.java │ ├── Motherboard.java │ ├── Resolution.java │ ├── Room.java │ └── Window.java ├── 039-encapsulation ├── 039-encapsulation.iml └── src │ └── com │ └── chezearth │ ├── Main.java │ └── Printer.java ├── 040-polymorphism ├── 040-polymorphism.iml └── src │ └── com │ └── chezearth │ ├── Car.java │ ├── Ford.java │ ├── Jaguar.java │ ├── LandRover.java │ └── Main.java ├── 041-billsburgers ├── 041-billsburgers.iml ├── out │ └── production │ │ └── 041-billsburgers │ │ └── com │ │ └── chezearth │ │ ├── DeluxeBurger.class │ │ ├── Hamburger.class │ │ ├── HealthyBurger.class │ │ └── Main.class └── src │ └── com │ └── chezearth │ ├── DeluxeBurger.java │ ├── Hamburger.java │ ├── HealthyBurger.java │ └── Main.java ├── 042-arrays ├── 042-arrays.iml └── src │ └── com │ └── chezearth │ └── Main.java ├── 043-references-vs-value-types ├── 043-references-vs-value-types.iml └── src │ └── com │ └── chezearth │ └── Main.java ├── 044-min-element-challenge ├── 044-min-element-challenge.iml └── src │ └── com │ └── chezearth │ └── Main.java ├── 045-reverse-array-challenge ├── 045-reverse-array-challenge.iml └── src │ └── com │ └── chezearth │ └── Main.java ├── 046-list-and-arraylist ├── 046-list-and-arraylist.iml ├── out │ └── production │ │ └── 046-list-and-arraylist │ │ └── com │ │ └── chezearth │ │ ├── GroceryList.class │ │ └── Main.class └── src │ └── com │ └── chezearth │ ├── GroceryList.java │ └── Main.java ├── 047-arraylist-challenge-1 ├── 047-arraylist-challenge-1.iml ├── out │ └── production │ │ └── 047-arraylist-challenge-1 │ │ └── com │ │ └── chezearth │ │ ├── Contact.class │ │ ├── Main.class │ │ └── MobilePhone.class └── src │ └── com │ └── chezearth │ ├── Contact.java │ ├── Main.java │ └── MobilePhone.java └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | .idea 4 | 5 | -------------------------------------------------------------------------------- /000-hello-world/000-hello-world.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /000-hello-world/out/production/000-hello-world/Hello.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chezearth/java-programming-masterclass/c04ef5f57d5421631eed47d489a34ccac92e066a/000-hello-world/out/production/000-hello-world/Hello.class -------------------------------------------------------------------------------- /000-hello-world/src/Hello.java: -------------------------------------------------------------------------------- 1 | public class Hello { 2 | 3 | public static void main(String[] args) { 4 | 5 | System.out.println("Hello, Charles!"); 6 | int myFirstNumber = (10 + 5) + (2 * 10); 7 | int mySecondNumber = 12; 8 | int myThirdNumber = myFirstNumber * 2; 9 | 10 | int myTotal = myFirstNumber + mySecondNumber + myThirdNumber; 11 | 12 | int myDifference = 1000 - myTotal; 13 | 14 | System.out.println(myFirstNumber); 15 | System.out.println(myTotal); 16 | System.out.println(myDifference); 17 | 18 | if(args.length > 0) { 19 | System.out.println("args =" + args[0]); 20 | } else { 21 | System.out.println("No args!"); 22 | } 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /001-byte-short-int-long/001-byte-short-int-long.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /001-byte-short-int-long/out/production/001-byte-short-int-long/com/chezearth/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chezearth/java-programming-masterclass/c04ef5f57d5421631eed47d489a34ccac92e066a/001-byte-short-int-long/out/production/001-byte-short-int-long/com/chezearth/Main.class -------------------------------------------------------------------------------- /001-byte-short-int-long/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | 7 | // int has a width of 32 8 | int myMinIntValue = Integer.MIN_VALUE; //-2_147_483_648; 9 | int myMaxIntValue = Integer.MAX_VALUE; // 2_147_483_647; 10 | 11 | int myIntValue = (myMinIntValue / 2); 12 | System.out.println("myMinIntValue = " + myMinIntValue); 13 | System.out.println("myIntValue = " + myIntValue); 14 | System.out.println("myMaxIntValue = " + myMaxIntValue); 15 | 16 | // byte has a width of 8 17 | byte myMinByteValue = Byte.MIN_VALUE; // -128; 18 | byte myMaxByteValue = Byte.MAX_VALUE; // 127; 19 | 20 | byte myByteValue = (byte) (myMinByteValue / 2); 21 | System.out.println("myMinByteValue = " + myMinByteValue); 22 | System.out.println("myByteValue = " + myByteValue); 23 | System.out.println("myMaxByteValue = " + myMaxByteValue); 24 | 25 | // short has a width of 16 26 | short myMinShortValue = Short.MIN_VALUE; // -32_768; 27 | short myMaxShortValue = Short.MAX_VALUE; //32_767; 28 | 29 | short myShortValue = (short) (myMinShortValue /2); 30 | System.out.println("myMinShortValue = " + myMinShortValue); 31 | System.out.println("myShortValue = " + myShortValue); 32 | System.out.println("myMaxShortValue = " + myMaxShortValue); 33 | 34 | // long has a width of 64 35 | long myMinLongValue = Long.MIN_VALUE; // -9_223_372_036_854_775_808L; 36 | long myMaxLongValue = Long.MAX_VALUE; // 9_223_372_036_854_775_807L; 37 | 38 | long myLongValue = (myMinLongValue /2); 39 | System.out.println("myMinLongValue = " + myMinLongValue); 40 | System.out.println("myShortValue = " + myLongValue); 41 | System.out.println("myMaxLongValue = " + myMaxLongValue); 42 | 43 | 44 | // exercise 45 | byte myByte = 15; 46 | short myShort = 1_256; // 1_271 47 | int myInt = 45_000; // 46_271 48 | long myLong = 50_000L + 10L * (myByte + myShort + myInt); 49 | // 50_000 + 10 * (15 + 1_156 + 45_000) = 50_000 + 10 * (1_271 + 45_000) = 50_000 + 10 * 46_271 = 50 | // 50_000 + 462_710 = 512_710 51 | System.out.println("myLong = " + myLong); 52 | 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /002-float-and-double/002-float-and-double.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /002-float-and-double/out/production/002-float-and-double/com/chezearth/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chezearth/java-programming-masterclass/c04ef5f57d5421631eed47d489a34ccac92e066a/002-float-and-double/out/production/002-float-and-double/com/chezearth/Main.class -------------------------------------------------------------------------------- /002-float-and-double/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | 7 | float myFloat = (float) 5.25; 8 | // width of int is 32 (4 bytes) 9 | int myIntValue = 5 / 2; 10 | // width of float is 32 (4 bytes) 11 | float myFloatValue = 5f / 3f; 12 | // width of double is 64 (8 bytes) 13 | double myDoubleValue = 5d / 3d; 14 | 15 | System.out.println("myIntValue = " + myIntValue); 16 | System.out.println("myFloatValue = " + myFloatValue); 17 | System.out.println("myDoubleValue = " + myDoubleValue); 18 | 19 | 20 | double lbValue = 200d; 21 | double kgValue = lbValue * 0.453_592_37d; 22 | System.out.println(lbValue + " lb converts to " + kgValue + " kg"); 23 | double myKgWeight = 99.6d; 24 | double myLbWeight = myKgWeight / 0.453_592_37d; 25 | System.out.println("My body weight in lbs is " + myLbWeight); 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /003-char-and-boolean/003-char-and-boolean.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /003-char-and-boolean/out/production/03-char-and-boolean/com/chezearth/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chezearth/java-programming-masterclass/c04ef5f57d5421631eed47d489a34ccac92e066a/003-char-and-boolean/out/production/03-char-and-boolean/com/chezearth/Main.class -------------------------------------------------------------------------------- /003-char-and-boolean/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | 7 | char myChar = '\u00A9'; 8 | System.out.println("Unicode copyright symbol is: " + myChar); 9 | 10 | boolean myBool = true; 11 | System.out.println("Boolean inverted = " + !myBool); 12 | 13 | char myRegSymbol = '\u00AE'; 14 | System.out.println("Unicode Registered symbol is: " + myRegSymbol); 15 | 16 | boolean myTrueBoolean = true; 17 | boolean myFalseBoolean = false; 18 | System.out.println(myTrueBoolean); 19 | System.out.println(myFalseBoolean); 20 | 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /004-strings/004-strings.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /004-strings/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | 7 | // byte 8 | // short 9 | // int 10 | // long 11 | // float 12 | // double 13 | // boolean 14 | // char 15 | 16 | String myString = "This is a short string."; 17 | System.out.println("myString is equal to \"" + myString + "\""); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /005-operators/005-operators.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /005-operators/out/production/05-operators/com/chezearth/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chezearth/java-programming-masterclass/c04ef5f57d5421631eed47d489a34ccac92e066a/005-operators/out/production/05-operators/com/chezearth/Main.class -------------------------------------------------------------------------------- /005-operators/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | 7 | int result = 10 * 20; 8 | System.out.println("10 * 20 = " + result); 9 | int previousResult = result; 10 | System.out.println("previousResult = " + previousResult); 11 | result = result * 20; 12 | System.out.println("result * 20 = " + result); 13 | System.out.println("previousResult = " + previousResult); 14 | 15 | result = result / 200; 16 | System.out.println("result / 200 = " + result); 17 | 18 | result = result % 3; 19 | System.out.println("result % 3 = " + result); 20 | 21 | result++; 22 | System.out.println("result++ = " + result); 23 | 24 | result += 3; 25 | System.out.println("result += 3 = " + result); 26 | 27 | result--; 28 | System.out.println("result-- = " + result); 29 | 30 | result *= 10; 31 | System.out.println("result *= 10 = " + result); 32 | 33 | result /= 5; 34 | System.out.println("result /= 5 = " + result); 35 | 36 | result -= 9; 37 | System.out.println("result -= 9 = " + result); 38 | 39 | boolean isAlien = false; 40 | if (!isAlien) { 41 | System.out.println("It is not an alien!"); 42 | } 43 | 44 | int topScore = 100; 45 | if (topScore <= 100) { 46 | System.out.println("Highest Score!"); 47 | } 48 | 49 | int secondHighestScore = 75; 50 | if (topScore > secondHighestScore && topScore > 80) { 51 | System.out.println("Your score was higher than 80 and the second highest score."); 52 | } 53 | 54 | boolean isCar = false; 55 | if (!isCar) { 56 | System.out.println("This is not supposed to happen"); 57 | } 58 | 59 | boolean wasCar = isCar ? true : false; 60 | System.out.println("wasCar is " + wasCar); 61 | 62 | double myFirstVar = 20.00d; 63 | double mySecondVar = 80.00d; 64 | 65 | boolean answer = ((myFirstVar + mySecondVar) * 100.00d % 40.00d == 0) ? true : false; 66 | System.out.println(("Challenge answer is " + answer)); 67 | if (!answer) { 68 | System.out.println("Got some remainder"); 69 | } 70 | 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /006-keywords-and-expressions/006-keywords-and-expressions.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /006-keywords-and-expressions/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | 7 | // In the following code, write down the parts that are expressions 8 | int score = 100; // score = 100 9 | if (score > 99) { // score > 99 10 | System.out.println("You got the high score!"); // "You got the high score!" 11 | score = 0; // score = 0 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /007-if-keyword-and-codeblocks/007-if-keyword-and-codeblocks.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /007-if-keyword-and-codeblocks/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | boolean gameOver = true; 7 | int score = 800; 8 | int levelCompleted = 5; 9 | int bonus = 100; 10 | 11 | if (gameOver) { 12 | int finalScore = score + (levelCompleted * bonus); 13 | System.out.println("Your final score was " + finalScore); 14 | } 15 | 16 | score = 10000; 17 | levelCompleted = 8; 18 | bonus = 200; 19 | 20 | if (gameOver) { 21 | int finalScore = score + (levelCompleted * bonus); 22 | System.out.println("Your final score was " + finalScore); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /008-methods/008-methods.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /008-methods/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | calculateScore(true, 800, 5, 100); 7 | calculateScore(true, 10000, 8, 200); 8 | 9 | displayHighScorePosition("Charles", calculateHighScorePosition(1500)); 10 | displayHighScorePosition("Bob", calculateHighScorePosition(900)); 11 | displayHighScorePosition("Jane", calculateHighScorePosition(400)); 12 | displayHighScorePosition("Fred", calculateHighScorePosition(50)); 13 | } 14 | 15 | public static void calculateScore(boolean gameOver, int score, int levelCompleted, int bonus) { 16 | 17 | if (gameOver) { 18 | int finalScore = score + (levelCompleted * bonus); 19 | System.out.println("Your final score was " + finalScore); 20 | } 21 | 22 | } 23 | 24 | public static void displayHighScorePosition(String name, int position) { 25 | System.out.println(name + " managed to get into position " + position + " on the high score table"); 26 | } 27 | 28 | public static int calculateHighScorePosition(int score) { 29 | 30 | if(score >= 1000) { 31 | return 1; 32 | } else if(score >= 500) { 33 | return 2; 34 | } else if(score >= 100) { 35 | return 3; 36 | } 37 | 38 | return 4; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /009-speed-converter/009-speed-converter.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /009-speed-converter/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | 5 | double miles = SpeedConverter.toMilesPerHour(10.5); 6 | System.out.println("Miles = " + miles); 7 | 8 | SpeedConverter.printConversion(10.5); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /009-speed-converter/src/SpeedConverter.java: -------------------------------------------------------------------------------- 1 | public class SpeedConverter { 2 | 3 | public static long toMilesPerHour(double kilometersPerHour) { 4 | return kilometersPerHour < 0 ? -1 : Math.round(kilometersPerHour / 1.609); 5 | } 6 | 7 | public static void printConversion(double kilometersPerHour) { 8 | System.out.println( 9 | kilometersPerHour < 0 ? 10 | "Invalid Value" : 11 | kilometersPerHour + " km/h = " + toMilesPerHour(kilometersPerHour) + " mi/h" 12 | ); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /010-megabytes-converter/010-megabytes-converter.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /010-megabytes-converter/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | MegaBytesConverter.printMegaBytesAndKiloBytes(2500); 4 | MegaBytesConverter.printMegaBytesAndKiloBytes(5000); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /010-megabytes-converter/src/MegaBytesConverter.java: -------------------------------------------------------------------------------- 1 | public class MegaBytesConverter { 2 | 3 | public static void printMegaBytesAndKiloBytes(int kiloBytes) { 4 | System.out.println( 5 | kiloBytes < 0 ? 6 | "Invalid Value" : 7 | kiloBytes + " KB = " + Math.floorDiv(kiloBytes, 1024) + " MB and " + kiloBytes % 1024 + " KB"); 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /011-barking-dog/011-barking-dog.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /011-barking-dog/src/BarkingDog.java: -------------------------------------------------------------------------------- 1 | public class BarkingDog { 2 | 3 | public static boolean shouldWakeUp(boolean barking, int hourOfDay) { 4 | return barking && hourOfDay >= 0 && hourOfDay < 8 || barking && hourOfDay > 22 && hourOfDay < 24; 5 | } 6 | 7 | } 8 | -------------------------------------------------------------------------------- /011-barking-dog/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | System.out.println("Dogs not barking @ 0 hrs = " + BarkingDog.shouldWakeUp(false, 0)); 4 | System.out.println("Dogs not barking @ 4 hrs = " + BarkingDog.shouldWakeUp(false, 4)); 5 | System.out.println("Dogs barking @ -1 hrs = " + BarkingDog.shouldWakeUp(true, -1)); 6 | System.out.println("Dogs barking @ 0 hrs = " + BarkingDog.shouldWakeUp(true, 0)); 7 | System.out.println("Dogs barking @ 4 hrs = " + BarkingDog.shouldWakeUp(true, 4)); 8 | System.out.println("Dogs barking @ 8 hrs = " + BarkingDog.shouldWakeUp(true, 8)); 9 | System.out.println("Dogs barking @ 9 hrs = " + BarkingDog.shouldWakeUp(true, 9)); 10 | System.out.println("Dogs barking @ 22 hrs = " + BarkingDog.shouldWakeUp(true, 22)); 11 | System.out.println("Dogs barking @ 23 hrs = " + BarkingDog.shouldWakeUp(true, 23)); 12 | System.out.println("Dogs barking @ 24 hrs = " + BarkingDog.shouldWakeUp(true, 24)); 13 | System.out.println("Dogs barking @ 25 hrs = " + BarkingDog.shouldWakeUp(true, 25)); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /012-leap-year/012-leap-year.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /012-leap-year/src/LeapYear.java: -------------------------------------------------------------------------------- 1 | public class LeapYear { 2 | 3 | public static boolean isLeapYear(int year) { 4 | return (year >= 1 && year <= 9999) && (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); 5 | } 6 | 7 | } 8 | -------------------------------------------------------------------------------- /012-leap-year/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | System.out.println("The year 0 is in range? " + LeapYear.isLeapYear(0)); 4 | System.out.println("The year 1700 is a leap year - " + LeapYear.isLeapYear(1700)); 5 | System.out.println("The year 1800 is a leap year - " + LeapYear.isLeapYear(1800)); 6 | System.out.println("The year 1900 is a leap year - " + LeapYear.isLeapYear(1900)); 7 | System.out.println("The year 2100 is a leap year - " + LeapYear.isLeapYear(2100)); 8 | System.out.println("The year 2200 is a leap year - " + LeapYear.isLeapYear(2200)); 9 | System.out.println("The year 2300 is a leap year - " + LeapYear.isLeapYear(2300)); 10 | System.out.println("The year 2500 is a leap year - " + LeapYear.isLeapYear(2500)); 11 | System.out.println("The year 2600 is a leap year - " + LeapYear.isLeapYear(2600)); 12 | System.out.println("The year 1600 is a leap year - " + LeapYear.isLeapYear(1600)); 13 | System.out.println("The year 1996 is a leap year - " + LeapYear.isLeapYear(1996)); 14 | System.out.println("The year 2000 is a leap year - " + LeapYear.isLeapYear(2000)); 15 | System.out.println("The year 2004 is a leap year - " + LeapYear.isLeapYear(2004)); 16 | System.out.println("The year 2400 is a leap year - " + LeapYear.isLeapYear(2400)); 17 | System.out.println("The year 10000 is in range - " + LeapYear.isLeapYear(10000)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /013-decimal-comparator/013-decimal-comparator.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /013-decimal-comparator/src/DecimalComparator.java: -------------------------------------------------------------------------------- 1 | public class DecimalComparator { 2 | 3 | public static boolean areEqualByThreeDecimalPlaces(double num0, double num1) { 4 | return ((int) (num0 * 1000)) == ((int) (num1 * 1000)); 5 | } 6 | 7 | } 8 | -------------------------------------------------------------------------------- /013-decimal-comparator/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | System.out.println("Number -3.1756 is equal to -3.175 up to 3 decimal places - " + DecimalComparator.areEqualByThreeDecimalPlaces(-3.1756, -3.175)); 5 | System.out.println("Number 3.175 is equal to 3.176 up to 3 decimal places - " + DecimalComparator.areEqualByThreeDecimalPlaces(3.175, 3.176)); 6 | System.out.println("Number 3.0 is equal to 3.0 up to 3 decimal places - " + DecimalComparator.areEqualByThreeDecimalPlaces(3.0, 3.0)); 7 | System.out.println("Number -3.123 is equal to 3.123 up to 3 decimal places - " + DecimalComparator.areEqualByThreeDecimalPlaces(-3.123, 3.123)); 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /014-equal-sum-charger/014-equal-sum-charger.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /014-equal-sum-charger/src/EqualSumChecker.java: -------------------------------------------------------------------------------- 1 | public class EqualSumChecker { 2 | 3 | public static boolean hasEqualSum(int num0, int num1, int sum) { 4 | return num0 + num1 == sum; 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /014-equal-sum-charger/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | System.out.println("hasEqualSum(1, 1, 1) is " + EqualSumChecker.hasEqualSum(1, 1, 1)); 4 | System.out.println("hasEqualSum(1, 1, 2) is " + EqualSumChecker.hasEqualSum(1, 1, 2)); 5 | System.out.println("hasEqualSum(1, -1, 0) is " + EqualSumChecker.hasEqualSum(1, -1, 0)); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /015-teen-number-checker/015-teen-number-checker.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /015-teen-number-checker/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | System.out.println("hasTeen(9, 99, 19) is " + TeenNumberChecker.hasTeen(9, 99, 19)); 5 | System.out.println("hasTeen(23, 15, 42) is " + TeenNumberChecker.hasTeen(23, 15, 42)); 6 | System.out.println("hasTeen(22, 23, 34) is " + TeenNumberChecker.hasTeen(22, 23, 34)); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /015-teen-number-checker/src/TeenNumberChecker.java: -------------------------------------------------------------------------------- 1 | public class TeenNumberChecker { 2 | 3 | 4 | public static boolean hasTeen(int num0, int num1, int num2) { 5 | return isTeen(num0) || isTeen(num1) || isTeen(num2); 6 | } 7 | 8 | public static boolean isTeen(int num) { 9 | return num > 12 && num < 20; 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /016-method-overloading/016-method-overloading.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /016-method-overloading/out/production/016-method-overloading/com/chezearth/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chezearth/java-programming-masterclass/c04ef5f57d5421631eed47d489a34ccac92e066a/016-method-overloading/out/production/016-method-overloading/com/chezearth/Main.class -------------------------------------------------------------------------------- /016-method-overloading/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | int newScore = calculateScore("Bob", 500); 7 | System.out.println("New score is " + newScore); 8 | int newNewScore = calculateScore(newScore); 9 | System.out.println("New, new score is " + newNewScore); 10 | int finalScore = calculateScore(); 11 | System.out.println("Final score is " + finalScore); 12 | System.out.println("6' 2\" = " + calcFeetAndInchesToCentimeters(6, 2) + " cm"); 13 | System.out.println("74\" = " + calcFeetAndInchesToCentimeters(74) + " cm"); 14 | } 15 | 16 | public static int calculateScore(String playerName, int score) { 17 | System.out.println("Player " + playerName + " scored " + score); 18 | return score * 1000; 19 | } 20 | 21 | public static int calculateScore(int score) { 22 | System.out.println("Unnamed player scored " + score); 23 | return score * 1000; 24 | } 25 | 26 | public static int calculateScore() { 27 | System.out.println("No player scored "); 28 | return 0; 29 | } 30 | 31 | public static double calcFeetAndInchesToCentimeters(double feet, double inches) { 32 | 33 | if(feet < 0 || inches < 0 || inches > 12) { 34 | return -1; 35 | }; 36 | 37 | return (feet * 12 + inches) * 2.54; 38 | } 39 | 40 | public static double calcFeetAndInchesToCentimeters(double inches) { 41 | 42 | if(inches < 0) { 43 | return -1; 44 | }; 45 | 46 | return calcFeetAndInchesToCentimeters((double) (long) (inches / 12), (double) (inches % 12)); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /017-seconds-and-minutes-challenge/017-seconds-and-minutes-challenge.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /017-seconds-and-minutes-challenge/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Main { 4 | 5 | private static final String INVALID_VALUE = "Invalid value"; 6 | 7 | public static void main(String[] args) { 8 | System.out.println("Hours, minutes and seconds for 217 min 23 sec = " + getDurationString(217, 23)); 9 | System.out.println("Hours, minutes and seconds for 217 min 65 sec = " + getDurationString(217, 65)); 10 | System.out.println("Hours, minutes and seconds for -217 min 23 sec = " + getDurationString(-217, 23)); 11 | System.out.println("Hours, minutes and seconds for 61 min = " + getDurationString(61,0)); 12 | System.out.println("Hours, minutes and seconds for 13043 sec = " + getDurationString(13043)); 13 | System.out.println("Hours, minutes and seconds for -13043 sec = " + getDurationString(-13043)); 14 | } 15 | 16 | private static String getDurationString(long minutes, long seconds) { 17 | 18 | if(minutes < 0 || seconds < 0 || seconds > 59) { 19 | return INVALID_VALUE; 20 | } 21 | 22 | return pad((long) (minutes / 60)) + "h " 23 | + pad(minutes % 60) + "m " 24 | + pad(seconds) + "s"; 25 | } 26 | 27 | private static String getDurationString(long seconds) { 28 | 29 | if(seconds < 0) { 30 | return INVALID_VALUE; 31 | } 32 | 33 | return getDurationString(seconds / 60, seconds % 60); 34 | } 35 | 36 | private static String pad(long num) { 37 | return num < 10 ? "0" + num : "" + num; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /018-area-calculator/018-area-calculator.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /018-area-calculator/src/AreaCalculator.java: -------------------------------------------------------------------------------- 1 | public class AreaCalculator { 2 | 3 | public static double area(double radius) { 4 | if(radius < 0) { 5 | return -1; 6 | } 7 | 8 | return Math.PI * radius * radius; 9 | } 10 | 11 | public static double area(double x, double y) { 12 | if(x < 0 || y < 0) { 13 | return -1; 14 | } 15 | 16 | return x * y; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /018-area-calculator/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | System.out.println("Area(5.0) = " + AreaCalculator.area(5.0)); 4 | System.out.println("Area(-1) = " + AreaCalculator.area(-1)); 5 | System.out.println("Area(5.0, 4.0) = " + AreaCalculator.area(5.0, 4.0)); 6 | System.out.println("Area(-1, 4.0) = " + AreaCalculator.area(-1, 4.0)); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /019-mintes-to-years-and-days/019-mintes-to-years-and-days.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /019-mintes-to-years-and-days/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | MinutesToYearsDaysCalculator.printYearsAndDays(525600); 4 | MinutesToYearsDaysCalculator.printYearsAndDays(1051200); 5 | MinutesToYearsDaysCalculator.printYearsAndDays(561600); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /019-mintes-to-years-and-days/src/MinutesToYearsDaysCalculator.java: -------------------------------------------------------------------------------- 1 | public class MinutesToYearsDaysCalculator { 2 | 3 | private static final String INVALID_VALUE = "Invalid Value"; 4 | 5 | public static void printYearsAndDays(long minutes) { 6 | 7 | if(minutes < 0) { 8 | System.out.println(INVALID_VALUE); 9 | } else { 10 | 11 | System.out.println( 12 | minutes + " min = " 13 | + (long) (minutes / 60 / 24 / 365) + " y and " 14 | + (long) (minutes % (60 * 24 * 365)) / 60 / 24 + " d" 15 | ); 16 | } 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /020-equality-printer/020-equality-printer.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /020-equality-printer/src/IntEqualityPrinter.java: -------------------------------------------------------------------------------- 1 | public class IntEqualityPrinter { 2 | 3 | public static final String INVALID_VALUE = "Invalid Value"; 4 | public static final String ALL_EQUAL = "All numbers are equal"; 5 | public static final String ALL_DIFFERENT = "All numbers are different"; 6 | public static final String NEITHER_ALL_OR_DIFFERENT = "Neither all are equal or different"; 7 | 8 | public static void printEqual(int num0, int num1, int num2) { 9 | 10 | if(num0 < 0 || num1 < 0 || num2 < 0) { 11 | System.out.println(INVALID_VALUE); 12 | } else if(num0 == num1 && num0 == num2) { 13 | System.out.println(ALL_EQUAL); 14 | } else if(num0 != num1 && num1 != num2 && num0 != num2) { 15 | System.out.println(ALL_DIFFERENT); 16 | } else { 17 | System.out.println(NEITHER_ALL_OR_DIFFERENT); 18 | } 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /020-equality-printer/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | IntEqualityPrinter.printEqual(1, 1, 1); 5 | IntEqualityPrinter.printEqual(1, 1, 2); 6 | IntEqualityPrinter.printEqual(-1, -1, -1); 7 | IntEqualityPrinter.printEqual(1, 2, 3); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /021-playing-cat/021-playing-cat.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /021-playing-cat/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | System.out.println(PlayingCat.isCatPlaying(true, 10)); 5 | System.out.println(PlayingCat.isCatPlaying(false, 36)); 6 | System.out.println(PlayingCat.isCatPlaying(false, 35)); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /021-playing-cat/src/PlayingCat.java: -------------------------------------------------------------------------------- 1 | public class PlayingCat { 2 | 3 | public static boolean isCatPlaying(boolean summer, int temperature) { 4 | return ( 5 | summer == false && temperature >= 25 && temperature <= 35 6 | ) || ( 7 | summer == true && temperature >= 25 && temperature <= 45 8 | ); 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /022-switch/022-switch.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /022-switch/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | char charValue = 'D'; 7 | 8 | switch (charValue) { 9 | case 'A': 10 | System.out.println("Letter 'A' found"); 11 | break; 12 | case 'B': 13 | System.out.println("Letter 'B' found"); 14 | break; 15 | case 'C': case 'D': case 'E': 16 | System.out.println("Letter 'C', 'D' or 'E' found"); 17 | break; 18 | default: 19 | System.out.println("Not found"); 20 | break; 21 | } 22 | 23 | printDayOfTheWeek(-1); 24 | printDayOfTheWeek(0); 25 | printDayOfTheWeek(1); 26 | printDayOfTheWeek(3); 27 | printDayOfTheWeek(6); 28 | printDayOfTheWeek(7); 29 | printDayOfTheWeekIf(-1); 30 | printDayOfTheWeekIf(0); 31 | printDayOfTheWeekIf(1); 32 | printDayOfTheWeekIf(3); 33 | printDayOfTheWeekIf(6); 34 | printDayOfTheWeekIf(7); 35 | } 36 | 37 | private static void printDayOfTheWeek(int day) { 38 | switch (day) { 39 | case 0: 40 | System.out.println("Sunday"); 41 | break; 42 | case 1: 43 | System.out.println("Monday"); 44 | break; 45 | case 2: 46 | System.out.println("Tuesday"); 47 | break; 48 | case 3: 49 | System.out.println("Wednesday"); 50 | break; 51 | case 4: 52 | System.out.println("Thursday"); 53 | break; 54 | case 5: 55 | System.out.println("Friday"); 56 | break; 57 | case 6: 58 | System.out.println("Saturday"); 59 | break; 60 | default: 61 | System.out.println("Invalid day"); 62 | break; 63 | } 64 | } 65 | 66 | private static void printDayOfTheWeekIf(int day) { 67 | if(day == 0) { 68 | System.out.println("Sunday"); 69 | } else if(day == 1) { 70 | System.out.println("Monday"); 71 | } else if(day == 2) { 72 | System.out.println("Tuesday"); 73 | } else if(day == 3) { 74 | System.out.println("Wednesday"); 75 | } else if(day == 4) { 76 | System.out.println("Thursday"); 77 | } else if(day == 5) { 78 | System.out.println("Friday"); 79 | } else if(day == 6) { 80 | System.out.println("Saturday"); 81 | } else { 82 | System.out.println("Invalid day"); 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /023-exercises-12-13/023-exercises-12-13.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /023-exercises-12-13/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | NumberInWord.printNumberInWord(-1); 4 | NumberInWord.printNumberInWord(0); 5 | NumberInWord.printNumberInWord(1); 6 | NumberInWord.printNumberInWord(2); 7 | NumberInWord.printNumberInWord(4); 8 | NumberInWord.printNumberInWord(7); 9 | NumberInWord.printNumberInWord(9); 10 | NumberInWord.printNumberInWord(10); 11 | NumberInWord.printNumberInWord(1000); 12 | 13 | System.out.println(NumberOfDaysInMonth.getDaysInMonth(1, 2020)); 14 | System.out.println(NumberOfDaysInMonth.getDaysInMonth(2, 2020)); 15 | System.out.println(NumberOfDaysInMonth.getDaysInMonth(2, 2018)); 16 | System.out.println(NumberOfDaysInMonth.getDaysInMonth(-1, 2020)); 17 | System.out.println(NumberOfDaysInMonth.getDaysInMonth(1, -2020)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /023-exercises-12-13/src/NumberInWord.java: -------------------------------------------------------------------------------- 1 | public class NumberInWord { 2 | 3 | public static void printNumberInWord(int number) { 4 | 5 | switch (number) { 6 | case 0: 7 | System.out.println("ZERO"); 8 | break; 9 | case 1: 10 | System.out.println("ONE"); 11 | break; 12 | case 2: 13 | System.out.println("TWO"); 14 | break; 15 | case 3: 16 | System.out.println("THREE"); 17 | break; 18 | case 4: 19 | System.out.println("FOUR"); 20 | break; 21 | case 5: 22 | System.out.println("FIVE"); 23 | break; 24 | case 6: 25 | System.out.println("SIX"); 26 | break; 27 | case 7: 28 | System.out.println("SEVEN"); 29 | break; 30 | case 8: 31 | System.out.println("EIGHT"); 32 | break; 33 | case 9: 34 | System.out.println("NINE"); 35 | break; 36 | default: 37 | System.out.println("OTHER"); 38 | break; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /023-exercises-12-13/src/NumberOfDaysInMonth.java: -------------------------------------------------------------------------------- 1 | public class NumberOfDaysInMonth { 2 | 3 | private static boolean isLeapYear(int year) { 4 | return (year >= 1 && year <= 9999) && (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); 5 | } 6 | 7 | public static int getDaysInMonth(int month, int year) { 8 | 9 | if(month < 1 || month > 12 || year < 1 || year > 9999) { 10 | return -1; 11 | } 12 | 13 | switch (month) { 14 | case 4: case 6: case 9: case 11: 15 | return 30; 16 | case 2: 17 | return isLeapYear(year) ? 29 : 28; 18 | default: 19 | return 31; 20 | } 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /024-for-loop/024-for-loop.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /024-for-loop/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | 7 | for(long i = 8; i > 1; i--) { 8 | System.out.println("$10 000 at " + i + "% interest = " + String.format( 9 | "%.2f", calculateInterest(10000, 10 | i))); 11 | } 12 | 13 | int primeCount = 0; 14 | for(int i = 1; i <= 1000; i++) { 15 | if(isPrime(i)) { 16 | System.out.println(i); 17 | primeCount++; 18 | if(primeCount == 3) { 19 | break; 20 | } 21 | } 22 | } 23 | // System.out.println("Number of prime numbers between 1 & 1000 inclusive are " + primeCount); 24 | } 25 | 26 | public static double calculateInterest(double amount, double interestRate) { 27 | return amount * interestRate / 100; 28 | } 29 | 30 | public static boolean isPrime(int n) { 31 | 32 | if(n == 1) { 33 | return false; 34 | } 35 | 36 | for(int i = 2; i <= n / 2; i++) { 37 | if(n % i == 0) { 38 | return false; 39 | } 40 | } 41 | 42 | return true; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /025-sum-3-and-5-challenge/025-sum-3-and-5-challenge.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /025-sum-3-and-5-challenge/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | long sum = 0; 7 | int count = 0; 8 | 9 | for(int i = 1; i <= 1000 && count < 5; i++) { 10 | 11 | if(i % 3 == 0 && i % 5 == 0) { 12 | System.out.println(i); 13 | sum += i; 14 | count++; 15 | } 16 | 17 | } 18 | 19 | System.out.println("Sum of first " + count + " numbers divisible by 3 & 5 = " + sum); 20 | 21 | System.out.println(sumOdd(1,100)); 22 | System.out.println(sumOdd(-1, 100)); 23 | System.out.println(sumOdd(100, 100)); 24 | System.out.println(sumOdd(13, 13)); 25 | System.out.println(sumOdd(100, -100)); 26 | System.out.println(sumOdd(100, 1000)); 27 | } 28 | 29 | public static boolean isOdd(int number) { 30 | 31 | if(number <= 0) { 32 | return false; 33 | } 34 | 35 | return number % 2 != 0; 36 | } 37 | 38 | public static int sumOdd(int start, int end) { 39 | 40 | if(end < start || start < 0) { 41 | return -1; 42 | } 43 | 44 | int sum = 0; 45 | 46 | for(int i = start; i <= end; i++) { 47 | 48 | if(isOdd(i)) { 49 | sum += i; 50 | } 51 | 52 | } 53 | 54 | return sum; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /026-while/026-while.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /026-while/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | int number = 4; 7 | int finishNumber = 20; 8 | int total = 0; 9 | int count = 0; 10 | 11 | while (number <= finishNumber) { 12 | 13 | number++; 14 | if(!isEvenNumber(number)) { 15 | continue; 16 | } 17 | 18 | if(count >= 5) { 19 | break; 20 | } 21 | 22 | System.out.println("Even number " + number); 23 | total += number; 24 | // System.out.println(total); 25 | count ++; 26 | // System.out.println(count); 27 | 28 | } 29 | 30 | System.out.println("total = "+ total); 31 | } 32 | 33 | public static boolean isEvenNumber(int num) { 34 | if(num % 2 == 0) return true; 35 | return false; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /027-digit-sum-challenge/027-digit-sum-challenge.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /027-digit-sum-challenge/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | System.out.println(sumDigits(125)); 7 | System.out.println(sumDigits(555)); 8 | System.out.println(sumDigits(10)); 9 | System.out.println(sumDigits(8)); 10 | System.out.println(sumDigits(-3)); 11 | } 12 | 13 | public static int sumDigits(int number) { 14 | 15 | if(number < 10) { 16 | return -1; 17 | } 18 | 19 | int sum = 0; 20 | 21 | while(number > 0) { 22 | sum += number % 10; 23 | number /= 10; 24 | } 25 | 26 | return sum; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /028-exercises-15-23/028-exercises-15-23.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /028-exercises-15-23/src/EvenDigitSum.java: -------------------------------------------------------------------------------- 1 | public class EvenDigitSum { 2 | 3 | public static int getEvenDigitSum(int number) { 4 | 5 | if(number < 0) { 6 | return -1; 7 | } 8 | 9 | int sum = 0; 10 | while(number > 0) { 11 | 12 | if((number % 10) % 2 == 0) { 13 | sum += (number % 10); 14 | } 15 | 16 | number /= 10; 17 | } 18 | 19 | return sum; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /028-exercises-15-23/src/FactorPrinter.java: -------------------------------------------------------------------------------- 1 | public class FactorPrinter { 2 | 3 | public static void printFactors(int number) { 4 | 5 | if (number < 1) { 6 | System.out.println("Invalid Value"); 7 | } 8 | 9 | int counter = 1; 10 | 11 | while (counter <= number) { 12 | if (number % counter == 0) { 13 | System.out.println(counter); 14 | } 15 | 16 | counter++; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /028-exercises-15-23/src/FirstLastDigitSum.java: -------------------------------------------------------------------------------- 1 | public class FirstLastDigitSum { 2 | 3 | public static int sumFirstAndLastDigit(int number) { 4 | 5 | if(number < 0) { 6 | return -1; 7 | } 8 | 9 | int sum = 0; 10 | int count = 0; 11 | 12 | do { 13 | 14 | if(count == 0 && number < 10) { 15 | sum = 2 * number; 16 | } else if(count == 0 || number / 10 == 0) { 17 | sum += number % 10; 18 | } 19 | 20 | count++; 21 | number /= 10; 22 | } while(number > 0); 23 | 24 | return sum; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /028-exercises-15-23/src/GreatestCommonDivisor.java: -------------------------------------------------------------------------------- 1 | public class GreatestCommonDivisor { 2 | 3 | public static int getGreatestCommonDivisor(int first, int second) { 4 | 5 | if (first < 10 || second < 10) { 6 | return -1; 7 | } 8 | 9 | int gcd = 1; 10 | int counter = 1; 11 | 12 | while (counter <= Math.min(first, second)) { 13 | if (first % counter == 0 && second % counter == 0) { 14 | gcd = counter; 15 | } 16 | 17 | counter++; 18 | } 19 | 20 | return gcd; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /028-exercises-15-23/src/LastDigitChecker.java: -------------------------------------------------------------------------------- 1 | public class LastDigitChecker { 2 | 3 | public static boolean hasSameLastDigit(int num0, int num1, int num2) { 4 | return isValid(num0) && 5 | isValid(num1) && 6 | isValid(num2) && 7 | (num0 % 10 == num1 % 10 || num0 % 10 == num2 % 10 || num1 % 10 == num2 % 10); 8 | } 9 | 10 | public static boolean isValid(int num) { 11 | 12 | if(num < 10 || num > 1000) { 13 | return false; 14 | } 15 | 16 | return true; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /028-exercises-15-23/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | System.out.println("isPalindrome(-1221) = " + NumberPalindrome.isPalindrome(-1221)); 5 | System.out.println("isPalindrome(707) = " + NumberPalindrome.isPalindrome(707)); 6 | System.out.println("isPalindrome(11212) = " + NumberPalindrome.isPalindrome(11212)); 7 | System.out.println("--------------"); 8 | System.out.println("sumFirstAndLastDigit(252) = " + FirstLastDigitSum.sumFirstAndLastDigit(252)); 9 | System.out.println("sumFirstAndLastDigit(10) = " + FirstLastDigitSum.sumFirstAndLastDigit(10)); 10 | System.out.println("sumFirstAndLastDigit(0) = " + FirstLastDigitSum.sumFirstAndLastDigit(0)); 11 | System.out.println("sumFirstAndLastDigit(5) = " + FirstLastDigitSum.sumFirstAndLastDigit(5)); 12 | System.out.println("sumFirstAndLastDigit(-10) = " + FirstLastDigitSum.sumFirstAndLastDigit(-10)); 13 | System.out.println("--------------"); 14 | System.out.println("getEvenDigitSum(123456789) = " + EvenDigitSum.getEvenDigitSum(123456789)); 15 | System.out.println("getEvenDigitSum(252) = " + EvenDigitSum.getEvenDigitSum(252)); 16 | System.out.println("getEvenDigitSum(-22) = " + EvenDigitSum.getEvenDigitSum(-22)); 17 | System.out.println("--------------"); 18 | System.out.println("hasSharedDigit(12, 23) = " + SharedDigit.hasSharedDigit(12, 23)); 19 | System.out.println("hasSharedDigit(9, 99) = " + SharedDigit.hasSharedDigit(9, 99)); 20 | System.out.println("hasSharedDigit(15, 55) = " + SharedDigit.hasSharedDigit(15, 55)); 21 | System.out.println("hasSharedDigit(12, 13) = " + SharedDigit.hasSharedDigit(12, 13)); 22 | System.out.println("--------------"); 23 | System.out.println("hasSameLastDigit(41, 22, 71) = " + LastDigitChecker.hasSameLastDigit(41, 24 | 22, 71)); 25 | System.out.println("hasSameLastDigit(23, 32, 42) = " + LastDigitChecker.hasSameLastDigit(23, 26 | 32, 42)); 27 | System.out.println("hasSameLastDigit(9, 99, 999) = " + LastDigitChecker.hasSameLastDigit(9, 28 | 99, 999)); 29 | System.out.println("hasSameLastDigit(15, 23, 37) = " + LastDigitChecker.hasSameLastDigit(15, 30 | 23, 37)); 31 | System.out.println("--------------"); 32 | System.out.println("getGreatestCommonDivisor(25, 15) = " + GreatestCommonDivisor.getGreatestCommonDivisor(25, 15)); 33 | System.out.println("getGreatestCommonDivisor(12, 30) = " + GreatestCommonDivisor.getGreatestCommonDivisor(12, 30)); 34 | System.out.println("getGreatestCommonDivisor(9, 18) = " + GreatestCommonDivisor.getGreatestCommonDivisor(9, 18)); 35 | System.out.println("getGreatestCommonDivisor(81, 153) = " + GreatestCommonDivisor.getGreatestCommonDivisor(81, 153)); 36 | System.out.println("getGreatestCommonDivisor(1010, 10) = " + GreatestCommonDivisor.getGreatestCommonDivisor(1010, 10)); 37 | System.out.println("--------------"); 38 | System.out.println("printFactors(6)"); 39 | FactorPrinter.printFactors(6); 40 | System.out.println("printFactors(32)"); 41 | FactorPrinter.printFactors(32); 42 | System.out.println("printFactors(10)"); 43 | FactorPrinter.printFactors(10); 44 | System.out.println("printFactors(-1)"); 45 | FactorPrinter.printFactors(-1); 46 | System.out.println("--------------"); 47 | System.out.println("isPerfectNumber(6) = " + PerfectNumber.isPerfectNumber(6)); 48 | System.out.println("isPerfectNumber(28) = " + PerfectNumber.isPerfectNumber(28)); 49 | System.out.println("isPerfectNumber(5) = " + PerfectNumber.isPerfectNumber(5)); 50 | System.out.println("isPerfectNumber(-1) = " + PerfectNumber.isPerfectNumber(-1)); 51 | System.out.println("--------------"); 52 | System.out.println("getDigitCount(0) = " + NumberToWords.getDigitCount(0)); 53 | System.out.println("getDigitCount(123) = " + NumberToWords.getDigitCount(123)); 54 | System.out.println("getDigitCount(-12) = " + NumberToWords.getDigitCount(-12)); 55 | System.out.println("getDigitCount(5200) = " + NumberToWords.getDigitCount(5200)); 56 | System.out.println("--------------"); 57 | System.out.println("reverse(-121) = " + NumberToWords.reverse(-121)); 58 | System.out.println("reverse(1212) = " + NumberToWords.reverse(1212)); 59 | System.out.println("reverse(1234) = " + NumberToWords.reverse(1234)); 60 | System.out.println("reverse(100) = " + NumberToWords.reverse(1)); 61 | System.out.println("--------------"); 62 | System.out.println("numberToWords(123)"); 63 | NumberToWords.numberToWords(123); 64 | System.out.println("numberToWords(1010)"); 65 | NumberToWords.numberToWords(1010); 66 | System.out.println("numberToWords(1000)"); 67 | NumberToWords.numberToWords(1000); 68 | System.out.println("numberToWords(-12)"); 69 | NumberToWords.numberToWords(-12); 70 | System.out.println("numberToWords(0)"); 71 | NumberToWords.numberToWords(0); 72 | System.out.println("numberToWords(1682507)"); 73 | NumberToWords.numberToWords(1682507); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /028-exercises-15-23/src/NumberPalindrome.java: -------------------------------------------------------------------------------- 1 | public class NumberPalindrome { 2 | 3 | public static boolean isPalindrome(int number) { 4 | int reverse = 0; 5 | int placeHolder = Math.abs(number); 6 | 7 | while(placeHolder > 0) { 8 | int lastDigit = placeHolder % 10; 9 | reverse *= 10; 10 | reverse += lastDigit; 11 | placeHolder /= 10; 12 | } 13 | 14 | return reverse == Math.abs(number); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /028-exercises-15-23/src/NumberToWords.java: -------------------------------------------------------------------------------- 1 | public class NumberToWords { 2 | 3 | public static void numberToWords(int number) { 4 | 5 | if (number < 0) { 6 | System.out.println("Invalid Value"); 7 | } else { 8 | int digitCount = getDigitCount(number); 9 | int revNumber = reverse(number); 10 | int diffDigitCount = digitCount - getDigitCount(revNumber); 11 | String result = ""; 12 | 13 | do { 14 | int digit = revNumber % 10; 15 | revNumber /= 10; 16 | 17 | if (!result.equals("")) { 18 | result += " "; 19 | } 20 | 21 | switch (digit) { 22 | case 1: 23 | result += "One"; 24 | break; 25 | case 2: 26 | result += "Two"; 27 | break; 28 | case 3: 29 | result += "Three"; 30 | break; 31 | case 4: 32 | result += "Four"; 33 | break; 34 | case 5: 35 | result += "Five"; 36 | break; 37 | case 6: 38 | result += "Six"; 39 | break; 40 | case 7: 41 | result += "Seven"; 42 | break; 43 | case 8: 44 | result += "Eight"; 45 | break; 46 | case 9: 47 | result += "Nine"; 48 | break; 49 | default: 50 | result += "Zero"; 51 | } 52 | 53 | } while (revNumber > 0); 54 | 55 | 56 | while (diffDigitCount > 0) { 57 | result = result + " " + "Zero"; 58 | diffDigitCount--; 59 | } 60 | 61 | System.out.println(result); 62 | } 63 | 64 | } 65 | 66 | public static int reverse(int number) { 67 | int result = 0; 68 | 69 | int count = getDigitCount(Math.abs(number)); 70 | 71 | while (count > 0) { 72 | result += (number % 10) * (int) Math.pow(10.0, count - 1); 73 | count--; 74 | number /= 10; 75 | 76 | } 77 | 78 | return result; 79 | } 80 | 81 | public static int getDigitCount(int number) { 82 | 83 | if(number < 0) { 84 | return -1; 85 | } 86 | 87 | int count = 0; 88 | 89 | do { 90 | number /= 10; 91 | count++; 92 | } while (number > 0); 93 | 94 | return count; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /028-exercises-15-23/src/PerfectNumber.java: -------------------------------------------------------------------------------- 1 | public class PerfectNumber { 2 | 3 | public static boolean isPerfectNumber(int number) { 4 | 5 | if (number < 1) { 6 | return false; 7 | } 8 | 9 | int counter = 1; 10 | int sum = 0; 11 | 12 | while (counter < number) { 13 | 14 | if (number % counter == 0) { 15 | sum += counter; 16 | } 17 | 18 | counter++; 19 | } 20 | 21 | return sum == number; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /028-exercises-15-23/src/SharedDigit.java: -------------------------------------------------------------------------------- 1 | public class SharedDigit { 2 | 3 | public static boolean hasSharedDigit(int num0, int num1) { 4 | 5 | if(num0 < 10 || num1 < 10 || num0 > 99 || num1 > 99) { 6 | return false; 7 | } 8 | 9 | while(num0 > 0) { 10 | int num1Test = num1; 11 | 12 | while(num1Test > 0) { 13 | 14 | if(num0 % 10 == num1Test % 10) { 15 | return true; 16 | } 17 | 18 | num1Test /= 10; 19 | } 20 | 21 | num0 /= 10; 22 | } 23 | 24 | return false; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /029-exercises-24-26/029-exercises-24-26.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /029-exercises-24-26/src/DiagonalStar.java: -------------------------------------------------------------------------------- 1 | public class DiagonalStar { 2 | 3 | public static void printSquareStar(int number) { 4 | 5 | if (number < 5) { 6 | System.out.println("Invalid Value"); 7 | } else { 8 | 9 | for (int i = 0; i < number; i++) { 10 | 11 | for (int j = 0; j < number; j++) { 12 | if ( 13 | i == 0 || 14 | j == 0 || 15 | i == number - 1 || 16 | j == number - 1 || 17 | i == j || 18 | i == number - j - 1 19 | ) { 20 | System.out.print("*");; 21 | } else { 22 | System.out.print(" "); 23 | } 24 | } 25 | System.out.println(); 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /029-exercises-24-26/src/FlourPacker.java: -------------------------------------------------------------------------------- 1 | public class FlourPacker { 2 | 3 | public static boolean canPack(int bigCount, int smallCount, int goal) { 4 | 5 | if (bigCount < 0 || smallCount < 0 || goal < 0) { 6 | return false; 7 | } 8 | 9 | int count = 0; 10 | int rem = 0; 11 | 12 | while (count * 5 <= goal && count <= bigCount) { 13 | rem = goal - count * 5; 14 | count++; 15 | } 16 | 17 | return smallCount >= rem && rem >= 0; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /029-exercises-24-26/src/LargestPrime.java: -------------------------------------------------------------------------------- 1 | public class LargestPrime { 2 | 3 | public static int getLargestPrime(int number) { 4 | 5 | if (number < 2) { 6 | return -1; 7 | } 8 | 9 | int count = 2; 10 | int result = -1; 11 | 12 | while (count <= number) { 13 | 14 | int primeCounter = 2; 15 | boolean primeTest = true; 16 | 17 | while (primeCounter < count) { 18 | primeTest = primeTest && !(count % primeCounter == 0); 19 | primeCounter++; 20 | } 21 | 22 | if (number % count == 0 && primeTest) { 23 | result = count; 24 | } 25 | 26 | count++; 27 | } 28 | 29 | return result; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /029-exercises-24-26/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | System.out.println("canPack(1, 0, 4) = " + FlourPacker.canPack(1, 0, 4)); 5 | System.out.println("canPack(1, 0, 5) = " + FlourPacker.canPack(1, 0, 5)); 6 | System.out.println("canPack(0, 5, 4) = " + FlourPacker.canPack(0, 5, 4)); 7 | System.out.println("canPack(2, 2, 11) = " + FlourPacker.canPack(2, 2, 11)); 8 | System.out.println("canPack(-3, 2, 12) = " + FlourPacker.canPack(-3, 2, 12)); 9 | System.out.println("canPack(0, 5, 6) = " + FlourPacker.canPack(0, 5, 6)); 10 | System.out.println("canPack(2, 1, 5) = " + FlourPacker.canPack(2, 1, 5)); 11 | System.out.println("-----------------"); 12 | System.out.println("getLargestPrime(21) = " + LargestPrime.getLargestPrime(21)); 13 | System.out.println("getLargestPrime(217) = " + LargestPrime.getLargestPrime(217)); 14 | System.out.println("getLargestPrime(0) = " + LargestPrime.getLargestPrime(0)); 15 | System.out.println("getLargestPrime(45) = " + LargestPrime.getLargestPrime(45)); 16 | System.out.println("getLargestPrime(-1) = " + LargestPrime.getLargestPrime(-1)); 17 | System.out.println("getLargestPrime(1) = " + LargestPrime.getLargestPrime(1)); 18 | System.out.println("getLargestPrime(2) = " + LargestPrime.getLargestPrime(2)); 19 | System.out.println("getLargestPrime(3) = " + LargestPrime.getLargestPrime(3)); 20 | System.out.println("getLargestPrime(4) = " + LargestPrime.getLargestPrime(4)); 21 | System.out.println("getLargestPrime(5) = " + LargestPrime.getLargestPrime(5)); 22 | System.out.println("getLargestPrime(6) = " + LargestPrime.getLargestPrime(6)); 23 | System.out.println("getLargestPrime(7) = " + LargestPrime.getLargestPrime(7)); 24 | System.out.println("getLargestPrime(8) = " + LargestPrime.getLargestPrime(8)); 25 | System.out.println("getLargestPrime(9) = " + LargestPrime.getLargestPrime(9)); 26 | System.out.println("getLargestPrime(10) = " + LargestPrime.getLargestPrime(10)); 27 | System.out.println("-----------------"); 28 | DiagonalStar.printSquareStar(5); 29 | DiagonalStar.printSquareStar(8); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /030-reading-user-input/030-reading-user-input.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /030-reading-user-input/out/production/030-reading-user-input/com/chezearth/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chezearth/java-programming-masterclass/c04ef5f57d5421631eed47d489a34ccac92e066a/030-reading-user-input/out/production/030-reading-user-input/com/chezearth/Main.class -------------------------------------------------------------------------------- /030-reading-user-input/out/production/030-reading-user-input/com/chezearth/MinAndMaxInputChallenge.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chezearth/java-programming-masterclass/c04ef5f57d5421631eed47d489a34ccac92e066a/030-reading-user-input/out/production/030-reading-user-input/com/chezearth/MinAndMaxInputChallenge.class -------------------------------------------------------------------------------- /030-reading-user-input/out/production/030-reading-user-input/com/chezearth/ReadingUserInput.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chezearth/java-programming-masterclass/c04ef5f57d5421631eed47d489a34ccac92e066a/030-reading-user-input/out/production/030-reading-user-input/com/chezearth/ReadingUserInput.class -------------------------------------------------------------------------------- /030-reading-user-input/out/production/030-reading-user-input/com/chezearth/ReadingUserInputChallenge.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chezearth/java-programming-masterclass/c04ef5f57d5421631eed47d489a34ccac92e066a/030-reading-user-input/out/production/030-reading-user-input/com/chezearth/ReadingUserInputChallenge.class -------------------------------------------------------------------------------- /030-reading-user-input/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) { 8 | // ReadingUserInput.readUserInput(); 9 | // ReadingUserInputChallenge.readUserInput(); 10 | MinAndMaxInputChallenge.minAndMaxInput(); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /030-reading-user-input/src/com/chezearth/MinAndMaxInputChallenge.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | import java.util.Scanner; 4 | 5 | public class MinAndMaxInputChallenge { 6 | 7 | public static void minAndMaxInput() { 8 | 9 | // int min = 0; 10 | // int max = 0; 11 | int min = Integer.MAX_VALUE; 12 | int max = Integer.MIN_VALUE; 13 | // boolean flag = true; 14 | Scanner scanner = new Scanner(System.in); 15 | 16 | while (true) { 17 | System.out.println("Enter number: "); 18 | boolean isInt = scanner.hasNextInt(); 19 | 20 | if (isInt) { 21 | int number = scanner.nextInt(); 22 | 23 | // if (flag) { 24 | // min = number; 25 | // max = number; 26 | // flag = false; 27 | // } else { 28 | min = Math.min(number, min); 29 | max = Math.max(number, max); 30 | // } 31 | 32 | } else { 33 | 34 | // if(!flag) { 35 | if (!(min == Integer.MAX_VALUE && max == Integer.MIN_VALUE)) { 36 | break; 37 | } 38 | 39 | } 40 | 41 | 42 | scanner.nextLine(); 43 | } 44 | 45 | System.out.println("Minimum number = " + min + " and maximum number = " + max); 46 | scanner.close(); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /030-reading-user-input/src/com/chezearth/ReadingUserInput.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ReadingUserInput { 6 | 7 | public static void readUserInput() { 8 | Scanner scanner = new Scanner(System.in); 9 | System.out.print("Enter your year of birth: "); 10 | boolean hasNextInt = scanner.hasNextInt(); 11 | 12 | if (hasNextInt) { 13 | int yearOfBirth = scanner.nextInt(); 14 | scanner.nextLine(); 15 | 16 | if (yearOfBirth > 2019 - 130 && yearOfBirth < 2019) { 17 | System.out.print("Enter your name: "); 18 | String name = scanner.nextLine(); 19 | int age = 2019 - yearOfBirth; 20 | System.out.println("Your name is " + name + ", and you are " + age + " years old."); 21 | } else { 22 | System.out.println("Invalid year of birth"); 23 | } 24 | 25 | } else { 26 | System.out.println("Unable to parse year of birth"); 27 | } 28 | 29 | scanner.close(); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /030-reading-user-input/src/com/chezearth/ReadingUserInputChallenge.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ReadingUserInputChallenge { 6 | 7 | public static void readUserInput() { 8 | 9 | int counter = 0; 10 | int sum = 0; 11 | Scanner scanner = new Scanner(System.in); 12 | boolean hasNextInt = true; 13 | 14 | while (counter < 10) { 15 | System.out.println("Enter number #" + (counter + 1) + ": "); 16 | hasNextInt = scanner.hasNextInt(); 17 | 18 | if (hasNextInt) { 19 | int number = scanner.nextInt(); 20 | sum += number; 21 | counter++; 22 | } else { 23 | // scanner.nextLine(); 24 | System.out.println("Invalid number entered"); 25 | // hasNextInt = true; 26 | // scanner.reset(); 27 | } 28 | 29 | scanner.nextLine(); 30 | } 31 | 32 | scanner.close(); 33 | System.out.println("Sum of numbers entered = " + sum); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /031-exercises-27-28/031-exercises-27-28.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /031-exercises-27-28/out/production/031-exercises-27-28/InputCalculator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chezearth/java-programming-masterclass/c04ef5f57d5421631eed47d489a34ccac92e066a/031-exercises-27-28/out/production/031-exercises-27-28/InputCalculator.class -------------------------------------------------------------------------------- /031-exercises-27-28/out/production/031-exercises-27-28/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chezearth/java-programming-masterclass/c04ef5f57d5421631eed47d489a34ccac92e066a/031-exercises-27-28/out/production/031-exercises-27-28/Main.class -------------------------------------------------------------------------------- /031-exercises-27-28/out/production/031-exercises-27-28/PaintJob.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chezearth/java-programming-masterclass/c04ef5f57d5421631eed47d489a34ccac92e066a/031-exercises-27-28/out/production/031-exercises-27-28/PaintJob.class -------------------------------------------------------------------------------- /031-exercises-27-28/src/InputCalculator.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class InputCalculator { 4 | 5 | public static void inputThenPrintSumAndAverage() { 6 | Scanner scanner = new Scanner(System.in); 7 | int sum = 0; 8 | int count = 0; 9 | 10 | while (true) { 11 | boolean isInt = scanner.hasNextInt(); 12 | 13 | if(isInt) { 14 | int number = scanner.nextInt(); 15 | sum += number; 16 | count++; 17 | scanner.nextLine(); 18 | } else { 19 | break; 20 | } 21 | } 22 | 23 | if (count == 0) { 24 | count = 1; 25 | } 26 | 27 | long avg = Math.round((double) sum / count); 28 | System.out.println("SUM = " + sum + " AVG = " + avg); 29 | scanner.close(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /031-exercises-27-28/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | // InputCalculator.inputThenPrintSumAndAverage(); 4 | System.out.println("--------------------"); 5 | System.out.println("getBucketCount(-3.4, 2.1, 1.5, 2) = " + PaintJob.getBucketCount(-3.4, 6 | 2.1, 1.5, 2)); 7 | System.out.println("getBucketCount(3.4, 2.1, 1.5, 2) = " + PaintJob.getBucketCount(3.4, 8 | 2.1, 1.5, 2)); 9 | System.out.println("getBucketCount(2.75, 3.25, 2.5, 1) = " + PaintJob.getBucketCount(2.75, 10 | 3.25, 2.5, 1)); 11 | System.out.println("--------------------"); 12 | System.out.println("getBucketCount(-3.4, 2.1, 1.5) = " + PaintJob.getBucketCount(-3.4, 13 | 2.1, 1.5)); 14 | System.out.println("getBucketCount(3.4, 2.1, 1.5) = " + PaintJob.getBucketCount(3.4, 15 | 2.1, 1.5)); 16 | System.out.println("getBucketCount(7.25, 4.3, 2.35) = " + PaintJob.getBucketCount(7.25, 17 | 4.3, 2.35)); 18 | System.out.println("--------------------"); 19 | System.out.println("getBucketCount(3.4, 1.5) = " + PaintJob.getBucketCount(3.4, 1.5)); 20 | System.out.println("getBucketCount(6.26, 2.2) = " + PaintJob.getBucketCount(6.26, 2.2)); 21 | System.out.println("getBucketCount(3.26, 0.75) = " + PaintJob.getBucketCount(3.23, 0.75)); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /031-exercises-27-28/src/PaintJob.java: -------------------------------------------------------------------------------- 1 | public class PaintJob { 2 | 3 | public static int getBucketCount(double width, double height, double areaPerBucket, 4 | int extraBuckets) { 5 | 6 | if (width <= 0 || height <= 0 || areaPerBucket <= 0 || extraBuckets < 0) { 7 | return -1; 8 | } 9 | 10 | return (int) Math.ceil((width * height / areaPerBucket)) - extraBuckets; 11 | } 12 | 13 | public static int getBucketCount(double width, double height, double areaPerBucket) { 14 | 15 | if (width <= 0 || height <= 0 || areaPerBucket <= 0) { 16 | return -1; 17 | } 18 | 19 | return (int) Math.ceil(width * height /areaPerBucket); 20 | } 21 | 22 | public static int getBucketCount(double area, double areaPerBucket) { 23 | 24 | if (area <= 0 || areaPerBucket <= 0) { 25 | return -1; 26 | } 27 | 28 | return (int) Math.ceil(area /areaPerBucket); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /032-classes/032-classes.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /032-classes/src/com/chezearth/Car.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Car { 4 | private int doors; 5 | private int wheels; 6 | private String model; 7 | private String engine; 8 | private String colour; 9 | 10 | public void setModel(String model) { 11 | String validModel = model.toLowerCase(); 12 | 13 | if (validModel.equals("carrera") || validModel.equals("commodore")) { 14 | this.model = model; 15 | } else { 16 | this.model = "Unknown"; 17 | } 18 | 19 | } 20 | 21 | public String getModel() { 22 | return this.model; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /032-classes/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Car porsche = new Car(); 7 | porsche.setModel("Carrera"); 8 | System.out.println("Model is " + porsche.getModel()); 9 | porsche.setModel("Skyline"); 10 | System.out.println("Model is " + porsche.getModel()); 11 | Car holden = new Car(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /033-exercises-29-30/033-exercises-29-30.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /033-exercises-29-30/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | SimpleCalculator calculator = new SimpleCalculator(); 5 | calculator.setFirstNumber(5.0); 6 | calculator.setSecondNumber(4); 7 | System.out.println("add = " + calculator.getAdditionResult()); 8 | System.out.println("subtract = " + calculator.getSubtractionResult()); 9 | calculator.setFirstNumber(5.25); 10 | calculator.setSecondNumber(0); 11 | System.out.println("multiply = " + calculator.getMultiplicationResult()); 12 | System.out.println("divide = " + calculator.getDivisionResult()); 13 | System.out.println("---------------"); 14 | Person person = new Person(); 15 | person.setFirstName(""); 16 | person.setLastName(""); 17 | person.setAge(10); 18 | System.out.println("full name = " + person.getFullName()); 19 | System.out.println("teen = " + person.isTeen()); 20 | person.setFirstName("John"); 21 | person.setAge(18); 22 | System.out.println("full name = " + person.getFullName()); 23 | System.out.println("teen = " + person.isTeen()); 24 | person.setLastName("Smith"); 25 | System.out.println("full name = " + person.getFullName()); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /033-exercises-29-30/src/Person.java: -------------------------------------------------------------------------------- 1 | public class Person { 2 | private String firstName; 3 | private String lastName; 4 | private int age; 5 | 6 | public String getFirstName() { 7 | return firstName; 8 | } 9 | 10 | public String getLastName() { 11 | return lastName; 12 | } 13 | 14 | public int getAge() { 15 | return age; 16 | } 17 | 18 | public void setFirstName(String firstName) { 19 | this.firstName = firstName; 20 | } 21 | 22 | public void setLastName(String lastName) { 23 | this.lastName = lastName; 24 | } 25 | 26 | public void setAge(int age) { 27 | if (age < 0 || age > 100) { 28 | this.age = 0; 29 | } else { 30 | this.age = age; 31 | } 32 | } 33 | 34 | public boolean isTeen() { 35 | return this.age > 12 && this.age < 20; 36 | } 37 | 38 | public String getFullName() { 39 | if (this.firstName.isEmpty() && this.lastName.isEmpty()) { 40 | return ""; 41 | } else if(this.lastName.isEmpty()) { 42 | return this.firstName; 43 | } else if(this.firstName.isEmpty()) { 44 | return this.lastName; 45 | } else { 46 | return this.firstName + " " + this.lastName; 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /033-exercises-29-30/src/SimpleCalculator.java: -------------------------------------------------------------------------------- 1 | public class SimpleCalculator { 2 | private double firstNumber; 3 | private double secondNumber; 4 | 5 | public double getFirstNumber() { 6 | return this.firstNumber; 7 | } 8 | 9 | public double getSecondNumber() { 10 | return this.secondNumber; 11 | } 12 | 13 | public void setFirstNumber(double firstNumber) { 14 | this.firstNumber = firstNumber; 15 | } 16 | 17 | public void setSecondNumber(double secondNumber) { 18 | this.secondNumber = secondNumber; 19 | } 20 | 21 | public double getAdditionResult() { 22 | return firstNumber + secondNumber; 23 | } 24 | 25 | public double getSubtractionResult() { 26 | return firstNumber - secondNumber; 27 | } 28 | 29 | public double getMultiplicationResult() { 30 | return firstNumber * secondNumber; 31 | } 32 | 33 | public double getDivisionResult() { 34 | 35 | if(secondNumber == 0) { 36 | return 0; 37 | } 38 | 39 | return firstNumber / secondNumber; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /034-constructors/034-constructors.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /034-constructors/src/com/chezearth/BankAccount.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class BankAccount { 4 | private String accountNumber; 5 | private String customerName; 6 | private String email; 7 | private String phoneNumber; 8 | private double balance; 9 | 10 | public BankAccount() { 11 | this( 12 | "100000", 13 | "Default Customer", 14 | "default@bank.co.za", 15 | "0211001000", 16 | 0.0D 17 | ); 18 | System.out.println("Empty constructor called"); 19 | } 20 | 21 | public BankAccount( 22 | String accountNumber, 23 | String customerName, 24 | String email, 25 | String phoneNumber, 26 | double balance 27 | ) { 28 | setAccountNumber(accountNumber); 29 | // this.accountNumber = accountNumber; 30 | setCustomerName(customerName); 31 | // this.customerName = customerName; 32 | setEmail(email); 33 | // this.email = email; 34 | setPhoneNumber(phoneNumber); 35 | // this.phoneNumber = phoneNumber; 36 | setBalance(balance); 37 | // this.balance = balance; 38 | } 39 | 40 | public String getAccountNumber() { 41 | return accountNumber; 42 | } 43 | 44 | public double getBalance() { 45 | return (double) (Math.round(this.balance * 100)) / 100; 46 | } 47 | 48 | public String getCustomerName() { 49 | return customerName; 50 | } 51 | 52 | public String getEmail() { 53 | return email; 54 | } 55 | 56 | public String getPhoneNumber() { 57 | return phoneNumber; 58 | } 59 | 60 | public void setAccountNumber(String accountNumber) { 61 | this.accountNumber = accountNumber; 62 | } 63 | 64 | public void setBalance(double balance) { 65 | this.balance = balance; 66 | } 67 | 68 | public void setCustomerName(String customerName) { 69 | this.customerName = customerName; 70 | } 71 | 72 | public void setEmail(String email) { 73 | this.email = email; 74 | } 75 | 76 | public void setPhoneNumber(String phoneNumber) { 77 | this.phoneNumber = phoneNumber; 78 | } 79 | 80 | public double deposit(double depositedAmount) { 81 | this.balance += depositedAmount; 82 | return (double) (Math.round(this.balance * 100)) / 100; 83 | } 84 | 85 | public double withdrawal(double withdrawalAmount) { 86 | 87 | if (withdrawalAmount > this.balance) { 88 | System.out.println("Insufficient Funds"); 89 | return (double) (Math.round(this.balance * 100)) / 100; 90 | } 91 | 92 | this.balance -= withdrawalAmount; 93 | return (double) (Math.round(this.balance * 100)) / 100; 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /034-constructors/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | BankAccount anAccount = new BankAccount(); 7 | System.out.println("Acc number = " + anAccount.getAccountNumber()); 8 | System.out.println("Customer name = " + anAccount.getCustomerName()); 9 | System.out.println("----------"); 10 | BankAccount myAccount = new BankAccount( 11 | "100893", 12 | "CW Rethman", 13 | "me@me.com", 14 | "27711081040", 15 | -25_000.00 16 | ); 17 | System.out.println("Acc number = " + myAccount.getAccountNumber()); 18 | System.out.println("Starting balance = " + myAccount.getBalance()); 19 | System.out.println( 20 | "After deposit, balance = " + 21 | myAccount.deposit(49_899.00) 22 | ); 23 | System.out.println( 24 | "After withdrawal, balance = " + 25 | myAccount.withdrawal(65_000) 26 | ); 27 | System.out.println( 28 | "After withdrawal, balance = " + 29 | myAccount.withdrawal(21_932.69) 30 | ); 31 | System.out.println("Ending balance = " + myAccount.getBalance()); 32 | System.out.println("----------"); 33 | VipCustomer newVip0 = new VipCustomer(); 34 | System.out.println("Customer name = " + newVip0.getName()); 35 | System.out.println("Customer credit limit = " + newVip0.getCreditLimit()); 36 | System.out.println("Customer email = " + newVip0.getEmailAddress()); 37 | VipCustomer newVip1 = new VipCustomer("Bob Jones", "bob@youruncle.com"); 38 | System.out.println("Customer name = " + newVip1.getName()); 39 | System.out.println("Customer credit limit = " + newVip1.getCreditLimit()); 40 | System.out.println("Customer email = " + newVip1.getEmailAddress()); 41 | VipCustomer newVip2 = new VipCustomer( 42 | "John Smith", 43 | 500.0d, 44 | "john@smithsbrewery.co.uk"); 45 | System.out.println("Customer name = " + newVip2.getName()); 46 | System.out.println("Customer credit limit = " + newVip2.getCreditLimit()); 47 | System.out.println("Customer email = " + newVip2.getEmailAddress()); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /034-constructors/src/com/chezearth/VipCustomer.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class VipCustomer { 4 | private String name; 5 | private double creditLimit; 6 | private String emailAddress; 7 | 8 | public VipCustomer() { 9 | this("default-customer", 100.0d, "noreply@fool.com"); 10 | } 11 | 12 | public VipCustomer(String name, String emailAddress) { 13 | this(name, 10.0d, emailAddress); 14 | } 15 | 16 | public VipCustomer(String name, double creditLimit, String emailAddress) { 17 | this.name = name; 18 | this.creditLimit = creditLimit; 19 | this.emailAddress = emailAddress; 20 | } 21 | 22 | public double getCreditLimit() { 23 | return creditLimit; 24 | } 25 | 26 | public String getEmailAddress() { 27 | return emailAddress; 28 | } 29 | 30 | public String getName() { 31 | return name; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /035-exercises-31-34/035-exercises-31-34.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /035-exercises-31-34/src/Calculator.java: -------------------------------------------------------------------------------- 1 | public class Calculator { 2 | private Floor floor; 3 | private Carpet carpet; 4 | 5 | public Calculator(Floor floor, Carpet carpet) { 6 | this.floor = floor; 7 | this.carpet = carpet; 8 | } 9 | 10 | public double getTotalCost() { 11 | return floor.getArea() * carpet.getCost(); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /035-exercises-31-34/src/Carpet.java: -------------------------------------------------------------------------------- 1 | public class Carpet { 2 | private double cost; 3 | 4 | public Carpet(double cost) { 5 | 6 | if (cost < 0) { 7 | this.cost = 0; 8 | } else { 9 | this.cost = cost; 10 | } 11 | 12 | } 13 | 14 | public double getCost() { 15 | return cost; 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /035-exercises-31-34/src/ComplexNumber.java: -------------------------------------------------------------------------------- 1 | public class ComplexNumber { 2 | private double real; 3 | private double imaginary; 4 | 5 | public ComplexNumber(double real, double imaginary) { 6 | this.real = real; 7 | this.imaginary = imaginary; 8 | } 9 | 10 | public double getReal() { 11 | return this.real; 12 | } 13 | 14 | public double getImaginary() { 15 | return this.imaginary; 16 | } 17 | 18 | public void add(double real, double imaginery) { 19 | this.real += real; 20 | this.imaginary += imaginery; 21 | } 22 | 23 | public void add(ComplexNumber complexNumber) { 24 | this.real += complexNumber.real; 25 | this.imaginary += complexNumber.imaginary; 26 | } 27 | 28 | public void subtract(double real, double imaginery) { 29 | this.real -= real; 30 | this.imaginary -= imaginery; 31 | } 32 | 33 | public void subtract(ComplexNumber complexNumber) { 34 | this.real -= complexNumber.real; 35 | this.imaginary -= complexNumber.imaginary; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /035-exercises-31-34/src/Floor.java: -------------------------------------------------------------------------------- 1 | public class Floor { 2 | private double width; 3 | private double length; 4 | 5 | public Floor(double width, double length) { 6 | 7 | if (width < 0) { 8 | this.width = 0; 9 | } else { 10 | this.width = width; 11 | } 12 | 13 | if (length < 0) { 14 | this.length = 0; 15 | } else { 16 | this.length = length; 17 | } 18 | 19 | } 20 | 21 | public double getArea() { 22 | return this.length * this.width; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /035-exercises-31-34/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | Wall wall = new Wall(5, 4); 5 | System.out.println("area= " + wall.getArea()); 6 | wall.setHeight(-1.5); 7 | System.out.println("width= " + wall.getWidth()); 8 | System.out.println("height= " + wall.getHeight()); 9 | System.out.println("area= " + wall.getArea()); 10 | System.out.println("-------------"); 11 | Point first = new Point(6, 5); 12 | Point second = new Point(3,1); 13 | System.out.println("distance(0,0)= " + first.distance()); 14 | System.out.println("distance(second)= " + first.distance(second)); 15 | System.out.println("distance(2,2)= " + first.distance(2, 2)); 16 | Point point = new Point(); 17 | System.out.println("distance()= " + point.distance()); 18 | System.out.println("-------------"); 19 | Carpet carpet = new Carpet(3.5D); 20 | Floor floor = new Floor(2.75D, 4.0D); 21 | Calculator calculator = new Calculator(floor, carpet); 22 | System.out.println("total= " + calculator.getTotalCost()); 23 | carpet = new Carpet(1.5D); 24 | floor = new Floor(5.4D, 4.5D); 25 | calculator = new Calculator(floor, carpet); 26 | System.out.println("total= " + calculator.getTotalCost()); 27 | System.out.println("-------------"); 28 | ComplexNumber one = new ComplexNumber(1.0D, 1.0D); 29 | ComplexNumber number = new ComplexNumber(2.5D, -1.5D); 30 | one.add(1,1); 31 | System.out.println("one.real= " + one.getReal()); 32 | System.out.println("one.imaginery= " + one.getImaginary()); 33 | one.subtract(number); 34 | System.out.println("one.real= " + one.getReal()); 35 | System.out.println("one.imaginery= " + one.getImaginary()); 36 | number.subtract(one); 37 | System.out.println("number.real= " + number.getReal()); 38 | System.out.println("number.imaginery= " + number.getImaginary()); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /035-exercises-31-34/src/Point.java: -------------------------------------------------------------------------------- 1 | public class Point { 2 | private int x; 3 | private int y; 4 | 5 | public Point() { 6 | this(0,0); 7 | } 8 | 9 | public Point(int x, int y) { 10 | setX(x); 11 | setY(y); 12 | } 13 | 14 | public int getX() { 15 | return this.x; 16 | } 17 | 18 | public void setX(int x) { 19 | this.x = x; 20 | } 21 | 22 | public void setY(int y) { 23 | this.y = y; 24 | } 25 | 26 | public int getY() { 27 | return this.y; 28 | } 29 | 30 | public double distance() { 31 | return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); 32 | } 33 | 34 | public double distance(int x, int y) { 35 | return Math.sqrt(Math.pow((this.x - x), 2) + Math.pow((this.y - y), 2)); 36 | } 37 | 38 | public double distance(Point point) { 39 | return Math.sqrt(Math.pow((this.x - point.x), 2) + Math.pow((this.y - point.y), 2)); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /035-exercises-31-34/src/Wall.java: -------------------------------------------------------------------------------- 1 | public class Wall { 2 | private double width; 3 | private double height; 4 | 5 | public Wall() { 6 | this(0,0); 7 | } 8 | 9 | public Wall(double width, double height) { 10 | this.setWidth(width); 11 | this.setHeight(height); 12 | } 13 | 14 | public double getWidth() { 15 | return width; 16 | } 17 | 18 | public double getHeight() { 19 | return height; 20 | } 21 | 22 | public void setWidth(double width) { 23 | 24 | if (width < 0) { 25 | this.width = 0; 26 | } else { 27 | this.width = width; 28 | } 29 | 30 | } 31 | 32 | public void setHeight(double height) { 33 | 34 | if (height < 0) { 35 | this.height = 0; 36 | } else { 37 | this.height = height; 38 | } 39 | 40 | } 41 | 42 | public double getArea() { 43 | return this.width * this.height; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /036-inheritance/036-inheritance.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /036-inheritance/src/com/chezearth/Animal.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Animal { 4 | private String name; 5 | private int brain; 6 | private int body; 7 | private int size; 8 | private int weight; 9 | 10 | public Animal(String name, int brain, int body, int size, int weight) { 11 | this.name = name; 12 | this.brain = brain; 13 | this.body = body; 14 | this.size = size; 15 | this.weight = weight; 16 | } 17 | 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | public int getBrain() { 23 | return brain; 24 | } 25 | 26 | public int getBody() { 27 | return body; 28 | } 29 | 30 | public int getSize() { 31 | return size; 32 | } 33 | 34 | public int getWeight() { 35 | return weight; 36 | } 37 | 38 | public void eat() { 39 | System.out.println("Animal eating."); 40 | } 41 | 42 | public void move(int speed) { 43 | System.out.println("Animal moving at " + speed + " km/h"); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /036-inheritance/src/com/chezearth/Car.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Car extends Vehicle { 4 | private int currentGear; 5 | 6 | public Car(int currentSteering, int currentSpeed, String name, int currentGear) { 7 | super(currentSteering, currentSpeed, 4, name); 8 | this.currentGear = currentGear; 9 | } 10 | 11 | public void setCurrentGear(int currentGear) { 12 | 13 | if (currentGear < 0) { 14 | this.currentGear = 0; 15 | } else { 16 | this.currentGear = currentGear; 17 | } 18 | } 19 | 20 | public int getCurrentGear() { 21 | return currentGear; 22 | } 23 | 24 | @Override 25 | public void accelerate(int extraSpeed) { 26 | 27 | if (currentGear == 0) { 28 | extraSpeed = -getCurrentSpeed(); 29 | } else if (currentGear == 1) { 30 | 31 | if (getCurrentSpeed() + extraSpeed > 10) { 32 | extraSpeed = 10 - getCurrentSpeed(); 33 | // System.out.println("> > extra speed = " + extraSpeed); 34 | } 35 | 36 | } else if (currentGear == 2) { 37 | 38 | if (getCurrentSpeed() + extraSpeed > 30) { 39 | extraSpeed = 30 - getCurrentSpeed(); 40 | // System.out.println("> > extra speed = " + extraSpeed); 41 | } else if (getCurrentSpeed() + extraSpeed < 10) { 42 | extraSpeed = -getCurrentSpeed(); 43 | // System.out.println("> > extra speed = " + extraSpeed); 44 | } 45 | 46 | } else if (currentGear == 3) { 47 | 48 | if (getCurrentSpeed() + extraSpeed > 50) { 49 | extraSpeed = 50 - getCurrentSpeed(); 50 | // System.out.println("> > extra speed = " + extraSpeed); 51 | } else if (getCurrentSpeed() + extraSpeed < 20) { 52 | extraSpeed = -getCurrentSpeed(); 53 | // System.out.println("> > extra speed = " + extraSpeed); 54 | } 55 | 56 | } else { 57 | 58 | if (getCurrentSpeed() + extraSpeed > 80) { 59 | extraSpeed = 80 - getCurrentSpeed(); 60 | // System.out.println("> > extra speed = " + extraSpeed); 61 | } else if (this.getCurrentSpeed() + extraSpeed < 40) { 62 | extraSpeed = -getCurrentSpeed(); 63 | // System.out.println("> > extra speed = " + extraSpeed); 64 | } 65 | 66 | } 67 | 68 | super.accelerate(extraSpeed); 69 | 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /036-inheritance/src/com/chezearth/Dog.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Dog extends Animal { 4 | private int eyes; 5 | private int legs; 6 | private int tail; 7 | private int teeth; 8 | private String coat; 9 | 10 | public Dog( 11 | String name, 12 | int brain, 13 | int body, 14 | int size, 15 | int weight, 16 | int eyes, 17 | int legs, 18 | int tail, 19 | int teeth, 20 | String coat 21 | ) { 22 | super(name, brain, body, size, weight); 23 | this.eyes = eyes; 24 | this.legs = legs; 25 | this.tail = tail; 26 | this.teeth = teeth; 27 | this.coat = coat; 28 | } 29 | 30 | public void chew() { 31 | System.out.println("Dog chewing"); 32 | } 33 | 34 | @Override 35 | public void eat() { 36 | System.out.println("Dog eating"); 37 | chew(); 38 | super.eat(); 39 | } 40 | 41 | public void walk() { 42 | System.out.println("Dog walking"); 43 | move(5); 44 | } 45 | 46 | public void run() { 47 | System.out.println("Dog running"); 48 | move(20); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /036-inheritance/src/com/chezearth/LandRover.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class LandRover extends Car { 4 | private boolean fourWheelDrive; 5 | 6 | public LandRover(int steering, int speed, String name, int gear, boolean fourWheelDrive) { 7 | super(steering, speed, name, gear); 8 | this.fourWheelDrive = fourWheelDrive; 9 | } 10 | 11 | public void setFourWheelDrive(boolean fourWheelDrive) { 12 | this.fourWheelDrive = fourWheelDrive; 13 | } 14 | 15 | public boolean isFourWheelDrive() { 16 | return fourWheelDrive; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /036-inheritance/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | 7 | Dog dog = new Dog("Alsatian", 1, 1, 5, 85, 2, 4, 1,25, "short bristle"); 8 | dog.eat(); 9 | dog.walk(); 10 | dog.run(); 11 | System.out.println("----------"); 12 | System.out.println("ByRef or ByVal?"); 13 | String var1 = "New"; 14 | String var2 = var1; 15 | System.out.println("var1 = " + var1); 16 | System.out.println("var2 = " + var2); 17 | var2 = "Even Newer"; 18 | System.out.println("var1 = " + var1); 19 | System.out.println("var2 = " + var2); 20 | System.out.println("Primitives are ByVal! (objects are ByRef)"); 21 | System.out.println("----------"); 22 | Vehicle bicycle = new Vehicle(0, 10, 2, "bicyle"); 23 | System.out.println(bicycle.getName() + " speed = " + bicycle.getCurrentSpeed()); 24 | System.out.println(bicycle.getName() + " number of wheels = " + bicycle.getWheels()); 25 | System.out.println(bicycle.getName() + " steering direction = " + bicycle.steeringDescriptor()); 26 | bicycle.setCurrentSteering(-10); 27 | System.out.println(bicycle.getName() + " steering direction = " + bicycle.steeringDescriptor()); 28 | bicycle.setCurrentSteering(10); 29 | System.out.println(bicycle.getName() + " steering direction = " + bicycle.steeringDescriptor()); 30 | bicycle.setCurrentSteering(0); 31 | System.out.println(bicycle.getName() + " steering direction = " + bicycle.steeringDescriptor()); 32 | Car bmw = new Car(0,0, "BMW", 0); 33 | System.out.println(bmw.getName() + " speed = " + bmw.getCurrentSpeed()); 34 | System.out.println(bmw.getName() + " number of wheels = " + bmw.getWheels()); 35 | System.out.println(bmw.getName() + " steering direction = " + bmw.steeringDescriptor()); 36 | bmw.accelerate(15); 37 | System.out.println(bmw.getName() + " gear = " + bmw.getCurrentGear() + " speed = " + bmw.getCurrentSpeed()); 38 | bmw.setCurrentGear(2); 39 | bmw.accelerate(10); 40 | System.out.println(bmw.getName() + " gear = " + bmw.getCurrentGear() + " speed = " + bmw.getCurrentSpeed()); 41 | bmw.accelerate(15); 42 | System.out.println(bmw.getName() + " gear = " + bmw.getCurrentGear() + " speed = " + bmw.getCurrentSpeed()); 43 | bmw.accelerate(15); 44 | System.out.println(bmw.getName() + " gear = " + bmw.getCurrentGear() + " speed = " + bmw.getCurrentSpeed()); 45 | bmw.setCurrentGear(3); 46 | bmw.accelerate(15); 47 | System.out.println(bmw.getName() + " gear = " + bmw.getCurrentGear() + " speed = " + bmw.getCurrentSpeed()); 48 | bmw.accelerate(20); 49 | System.out.println(bmw.getName() + " gear = " + bmw.getCurrentGear() + " speed = " + bmw.getCurrentSpeed()); 50 | LandRover defender = new LandRover(0, 0, "Defender", 0, false); 51 | System.out.println(defender.getName() + " gear = " + defender.getCurrentGear() + " speed = " + defender.getCurrentSpeed() + " 4WD engaged" + 52 | " " + defender.isFourWheelDrive()); 53 | defender.setFourWheelDrive(true); 54 | defender.setCurrentGear(1); 55 | defender.accelerate(20); 56 | System.out.println(defender.getName() + " gear = " + defender.getCurrentGear() + " speed " + 57 | "= " + defender.getCurrentSpeed() + " 4WD engaged" + 58 | " " + defender.isFourWheelDrive()); 59 | defender.accelerate(20); 60 | System.out.println(defender.getName() + " gear = " + defender.getCurrentGear() + " speed = " + defender.getCurrentSpeed() + " 4WD engaged" + 61 | " " + defender.isFourWheelDrive()); 62 | defender.setCurrentGear(2); 63 | defender.accelerate(20); 64 | System.out.println(defender.getName() + " gear = " + defender.getCurrentGear() + " speed = " + defender.getCurrentSpeed() + " 4WD engaged" + 65 | " " + defender.isFourWheelDrive()); 66 | defender.setCurrentGear(3); 67 | defender.accelerate(40); 68 | System.out.println(defender.getName() + " gear = " + defender.getCurrentGear() + " speed = " + defender.getCurrentSpeed() + " 4WD engaged" + 69 | " " + defender.isFourWheelDrive()); 70 | defender.accelerate(40); 71 | System.out.println(defender.getName() + " gear = " + defender.getCurrentGear() + " speed = " + defender.getCurrentSpeed() + " 4WD engaged" + 72 | " " + defender.isFourWheelDrive()); 73 | defender.setCurrentGear(4); 74 | defender.accelerate(40); 75 | System.out.println(defender.getName() + " gear = " + defender.getCurrentGear() + " speed = " + defender.getCurrentSpeed() + " 4WD engaged" + 76 | " " + defender.isFourWheelDrive()); 77 | defender.accelerate(-20); 78 | System.out.println(defender.getName() + " gear = " + defender.getCurrentGear() + " speed = " + defender.getCurrentSpeed() + " 4WD " + 79 | "engaged " + defender.isFourWheelDrive()); 80 | defender.accelerate(-30); 81 | System.out.println(defender.getName() + " gear = " + defender.getCurrentGear() + " speed = " + defender.getCurrentSpeed() + " " + 82 | "4WD " + 83 | "engaged " + defender.isFourWheelDrive()); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /036-inheritance/src/com/chezearth/Vehicle.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Vehicle { 4 | private int currentSteering; 5 | private int currentSpeed; 6 | private int wheels; 7 | private String name; 8 | 9 | public Vehicle(int currentSteering, int speed, int wheels, String name) { 10 | this.currentSteering = currentSteering; 11 | this.currentSpeed = speed; 12 | this.wheels = wheels; 13 | this.name = name; 14 | } 15 | 16 | public void setCurrentSteering(int currentSteering) { 17 | this.currentSteering = currentSteering; 18 | } 19 | 20 | public void setCurrentSpeed(int currentSpeed) { 21 | this.currentSpeed = currentSpeed; 22 | } 23 | 24 | public int getCurrentSteering() { 25 | return currentSteering; 26 | } 27 | 28 | public String getName() { 29 | return name; 30 | } 31 | 32 | public int getCurrentSpeed() { 33 | return currentSpeed; 34 | } 35 | 36 | public int getWheels() { 37 | return wheels; 38 | } 39 | 40 | public String steeringDescriptor() { 41 | 42 | if (this.currentSteering < 0) { 43 | return "Turning left"; 44 | } else if (this.currentSteering > 0) { 45 | return "Turning right"; 46 | } else { 47 | return "Straight ahead"; 48 | } 49 | 50 | } 51 | 52 | public void accelerate(int extraSpeed) { 53 | 54 | if (extraSpeed < 0) { 55 | brake(Math.abs(extraSpeed)); 56 | // System.out.println("> accelerate() result speed = " + getCurrentSpeed()); 57 | } else { 58 | this.currentSpeed += extraSpeed; 59 | // System.out.println("> accelerate() result speed = " + getCurrentSpeed()); 60 | } 61 | 62 | } 63 | 64 | public void brake(int reducedSpeed) { 65 | 66 | if (reducedSpeed < 0) { 67 | accelerate(Math.abs(reducedSpeed)); 68 | // System.out.println("> brake() result speed = " + getCurrentSpeed()); 69 | } else { 70 | this.currentSpeed -= reducedSpeed; 71 | // System.out.println("> brake() result speed = " + getCurrentSpeed()); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /037-exercises-35-36/037-exercises-35-36.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /037-exercises-35-36/src/Circle.java: -------------------------------------------------------------------------------- 1 | public class Circle { 2 | private double radius; 3 | 4 | public Circle(double radius) { 5 | 6 | if (radius < 0) { 7 | this.radius = 0; 8 | } else { 9 | this.radius = radius; 10 | } 11 | 12 | } 13 | 14 | public double getRadius() { 15 | return radius; 16 | } 17 | 18 | public double getArea() { 19 | return Math.PI * radius * radius; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /037-exercises-35-36/src/Cuboid.java: -------------------------------------------------------------------------------- 1 | public class Cuboid extends Rectangle { 2 | private double height; 3 | 4 | public Cuboid(double width, double length, double height) { 5 | super(width, length); 6 | this.height = positiveOnly(height); 7 | } 8 | 9 | public double getHeight() { 10 | return height; 11 | } 12 | 13 | public double getVolume() { 14 | return height * getArea(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /037-exercises-35-36/src/Cylinder.java: -------------------------------------------------------------------------------- 1 | public class Cylinder extends Circle { 2 | private double height; 3 | 4 | public Cylinder(double radius, double height) { 5 | super(radius); 6 | 7 | if (height < 0) { 8 | this.height = 0; 9 | } else { 10 | this.height = height; 11 | } 12 | 13 | } 14 | 15 | public double getHeight() { 16 | return height; 17 | } 18 | 19 | public double getVolume() { 20 | return getArea() * height; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /037-exercises-35-36/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | Circle circle = new Circle(3.75); 5 | System.out.println("circle.radius = " + circle.getRadius()); 6 | System.out.println("circle.area = " + circle.getArea()); 7 | Cylinder cylinder = new Cylinder(5.55, 7.25); 8 | System.out.println("cylinder.radius = " + cylinder.getRadius()); 9 | System.out.println("cylinder.height = " + cylinder.getHeight()); 10 | System.out.println("cylinder.area = " + cylinder.getArea()); 11 | System.out.println("cylinder.volume = " + cylinder.getVolume()); 12 | System.out.println("---------------"); 13 | Rectangle rectangle = new Rectangle(5, 10); 14 | System.out.println("rectangle.width = " + rectangle.getWidth()); 15 | System.out.println("rectangle.length = " + rectangle.getLength()); 16 | System.out.println("rectangle.area = " + rectangle.getArea()); 17 | Cuboid cuboid = new Cuboid(5, 10, 5); 18 | System.out.println("cuboid.width = " + cuboid.getWidth()); 19 | System.out.println("cuboid.length = " + cuboid.getLength()); 20 | System.out.println("cuboid.area = " + cuboid.getArea()); 21 | System.out.println("cuboid.length = " + cuboid.getHeight()); 22 | System.out.println("cuboid.area = " + cuboid.getVolume()); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /037-exercises-35-36/src/Rectangle.java: -------------------------------------------------------------------------------- 1 | public class Rectangle { 2 | private double width; 3 | private double length; 4 | 5 | public double positiveOnly(double number) { 6 | 7 | if (number < 0) { 8 | return 0D; 9 | } 10 | 11 | return number; 12 | } 13 | 14 | public Rectangle(double width, double length) { 15 | this.width = positiveOnly(width); 16 | this.length = positiveOnly(length); 17 | } 18 | 19 | public double getWidth() { 20 | return width; 21 | } 22 | 23 | public double getLength() { 24 | return length; 25 | } 26 | 27 | public double getArea() { 28 | return width * length; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /038-composition/038-composition.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /038-composition/src/com/chezearth/Bed.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Bed { 4 | private String size; 5 | private boolean isHidden; 6 | 7 | public Bed(String size, boolean isHidden) { 8 | this.size = size; 9 | this.isHidden = isHidden; 10 | } 11 | 12 | public String getSize() { 13 | return this.size; 14 | } 15 | 16 | public boolean isHidden() { 17 | return isHidden; 18 | } 19 | 20 | public void setHidden(boolean hidden) { 21 | this.isHidden = hidden; 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /038-composition/src/com/chezearth/Case.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Case { 4 | private String model; 5 | private String manufacture; 6 | private String powerSupply; 7 | private Dimensions dimensions; 8 | 9 | public Case(String model, String manufacture, String powerSupply, Dimensions dimensions) { 10 | this.model = model; 11 | this.manufacture = manufacture; 12 | this.powerSupply = powerSupply; 13 | this.dimensions = dimensions; 14 | } 15 | 16 | public void pressPowerButton() { 17 | System.out.println("Power button pressed"); 18 | } 19 | 20 | public String getModel() { 21 | return model; 22 | } 23 | 24 | public String getManufacture() { 25 | return manufacture; 26 | } 27 | 28 | public String getPowerSupply() { 29 | return powerSupply; 30 | } 31 | 32 | public Dimensions getDimensions() { 33 | return dimensions; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /038-composition/src/com/chezearth/Computer.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Computer { 4 | private Case theCase; 5 | private Monitor monitor; 6 | private Motherboard motherboard; 7 | 8 | public Computer(Case theCase, Monitor monitor, Motherboard motherboard) { 9 | this.theCase = theCase; 10 | this.monitor = monitor; 11 | this.motherboard = motherboard; 12 | } 13 | 14 | public void powerUp() { 15 | theCase.pressPowerButton(); 16 | drawLogo(); 17 | } 18 | 19 | public void drawLogo() { 20 | // fancy graphics 21 | monitor.drawPixelAt(1200, 50, "grey"); 22 | } 23 | 24 | // private Case getTheCase() { 25 | // return theCase; 26 | // } 27 | // 28 | // private Monitor getMonitor() { 29 | // return monitor; 30 | // } 31 | // 32 | // private Motherboard getMotherboard() { 33 | // return motherboard; 34 | // } 35 | } 36 | -------------------------------------------------------------------------------- /038-composition/src/com/chezearth/Cupboard.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Cupboard { 4 | 5 | private int doors; 6 | 7 | public Cupboard(int doors) { 8 | this.doors = doors; 9 | } 10 | 11 | public int getDoors() { 12 | return doors; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /038-composition/src/com/chezearth/Dimensions.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Dimensions { 4 | private int width; 5 | private int height; 6 | private int depth; 7 | 8 | public Dimensions(int width, int height, int depth) { 9 | this.width = width; 10 | this.height = height; 11 | this.depth = depth; 12 | } 13 | 14 | public int getWidth() { 15 | return width; 16 | } 17 | 18 | public int getHeight() { 19 | return height; 20 | } 21 | 22 | public int getDepth() { 23 | return depth; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /038-composition/src/com/chezearth/Lamp.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Lamp { 4 | private boolean isOn; 5 | 6 | public Lamp(boolean isOn) { 7 | this.isOn = isOn; 8 | } 9 | 10 | public boolean isOn() { 11 | return isOn; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /038-composition/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Dimensions dimensions = new Dimensions(35, 35, 10); 7 | Case theCase = new Case("Superbia", "Sytem76", "240V ac/15V dc", dimensions); 8 | Monitor theMonitor = new Monitor("Xperienz", "Sony", 27, new Resolution(2540, 1440)); 9 | Motherboard motherboard = new Motherboard("XT500", "Asus", 4, 8, "grub"); 10 | Computer machine = new Computer(theCase, theMonitor, motherboard); 11 | machine.powerUp(); 12 | // machine.getTheCase().pressPowerButton(); 13 | // machine.getMotherboard().loadProgram("Fedora"); 14 | // machine.getMonitor().drawPixelAt(1200, 990, "blue"); 15 | 16 | Room room = new Room( 17 | "Bedroom", 18 | new Bed("Queen", false), 19 | new Cupboard(2), 20 | new Lamp(true), 21 | new Window(false) 22 | ); 23 | System.out.println("Bed size = " + room.getBedSize()); 24 | System.out.println("Is bed hidden? " + room.getBed().isHidden()); 25 | room.hideBed(); 26 | System.out.println("Is bed hidden? " + room.getBed().isHidden()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /038-composition/src/com/chezearth/Monitor.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Monitor { 4 | private String model; 5 | private String manufacturer; 6 | private int size; 7 | private Resolution nativeResolution; 8 | 9 | public Monitor(String model, String manufacturer, int size, Resolution nativeResolution) { 10 | this.model = model; 11 | this.manufacturer = manufacturer; 12 | this.size = size; 13 | this.nativeResolution = nativeResolution; 14 | } 15 | 16 | public void drawPixelAt(int x, int y, String colour) { 17 | System.out.println("Drawing pixel at " + x + ", " + y + " in colour " + colour); 18 | } 19 | 20 | public String getModel() { 21 | return model; 22 | } 23 | 24 | public String getManufacturer() { 25 | return manufacturer; 26 | } 27 | 28 | public int getSize() { 29 | return size; 30 | } 31 | 32 | public Resolution getNativeResolution() { 33 | return nativeResolution; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /038-composition/src/com/chezearth/Motherboard.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Motherboard { 4 | private String model; 5 | private String manufacturer; 6 | private int ramSlots; 7 | private int cardSlots; 8 | private String bios; 9 | 10 | public Motherboard(String model, String manufacturer, int ramSlots, int cardSlots, String bios) { 11 | this.model = model; 12 | this.manufacturer = manufacturer; 13 | this.ramSlots = ramSlots; 14 | this.cardSlots = cardSlots; 15 | this.bios = bios; 16 | } 17 | 18 | public void loadProgram(String programName) { 19 | System.out.println("Program " + programName + " is loading ..."); 20 | } 21 | 22 | public String getModel() { 23 | return model; 24 | } 25 | 26 | public String getManufacturer() { 27 | return manufacturer; 28 | } 29 | 30 | public int getRamSlots() { 31 | return ramSlots; 32 | } 33 | 34 | public int getCardSlots() { 35 | return cardSlots; 36 | } 37 | 38 | public String getBios() { 39 | return bios; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /038-composition/src/com/chezearth/Resolution.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Resolution { 4 | private int width; 5 | private int height; 6 | 7 | public Resolution(int width, int height) { 8 | this.width = width; 9 | this.height = height; 10 | } 11 | 12 | public int getWidth() { 13 | return width; 14 | } 15 | 16 | public int getHeight() { 17 | return height; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /038-composition/src/com/chezearth/Room.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | import java.awt.*; 4 | 5 | public class Room { 6 | private String roomType; 7 | private Bed bed; 8 | private Cupboard cupboard; 9 | private Lamp lamp; 10 | private Window window; 11 | 12 | public Room(String roomType, Bed bed, Cupboard cupboard, Lamp lamp, Window window) { 13 | this.roomType = roomType; 14 | this.bed = bed; 15 | this.cupboard = cupboard; 16 | this.lamp = lamp; 17 | this.window = window; 18 | } 19 | 20 | public Bed getBed() { 21 | return bed; 22 | } 23 | 24 | public String getBedSize() { 25 | return bed.getSize(); 26 | } 27 | 28 | public void hideBed() { 29 | bed.setHidden(true); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /038-composition/src/com/chezearth/Window.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Window { 4 | 5 | private boolean open; 6 | 7 | public Window(boolean open) { 8 | this.open = open; 9 | } 10 | 11 | public boolean getOpen() { 12 | return open; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /039-encapsulation/039-encapsulation.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /039-encapsulation/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Printer printer = new Printer(20, true); 7 | System.out.println("Number of pages printed = " + printer.getNumPagesPrinted()); 8 | System.out.println("Toner level = " + printer.getTonerLevel()); 9 | printer.fillToner(60); 10 | System.out.println("Number of pages printed = " + printer.getNumPagesPrinted()); 11 | System.out.println("Toner level = " + printer.getTonerLevel()); 12 | printer.printPages(30); 13 | System.out.println("Number of pages printed = " + printer.getNumPagesPrinted()); 14 | System.out.println("Toner level = " + printer.getTonerLevel()); 15 | printer.setDuplex(false); 16 | printer.fillToner(80); 17 | System.out.println("Number of pages printed = " + printer.getNumPagesPrinted()); 18 | System.out.println("Toner level = " + printer.getTonerLevel()); 19 | printer.printPages(43); 20 | System.out.println("Number of pages printed = " + printer.getNumPagesPrinted()); 21 | System.out.println("Toner level = " + printer.getTonerLevel()); 22 | printer.printPages(38); 23 | System.out.println("Number of pages printed = " + printer.getNumPagesPrinted()); 24 | System.out.println("Toner level = " + printer.getTonerLevel()); 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /039-encapsulation/src/com/chezearth/Printer.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Printer { 4 | 5 | private int tonerLevel = 100; 6 | private int numPagesPrinted; 7 | private boolean isDuplex; 8 | 9 | public Printer(int tonerLevel, boolean isDuplex) { 10 | 11 | this.tonerLevel = tonerCheck(tonerLevel); 12 | this.numPagesPrinted = 0; 13 | this.isDuplex = isDuplex; 14 | } 15 | 16 | public int getTonerLevel() { 17 | return tonerLevel; 18 | } 19 | 20 | public int getNumPagesPrinted() { 21 | return numPagesPrinted; 22 | } 23 | 24 | public boolean getIsDuplex() { 25 | return isDuplex; 26 | } 27 | 28 | public void printPages(int numPrinted) { 29 | 30 | if (tonerLevel / 2 < numPrinted) { 31 | numPrinted = tonerLevel / 2; 32 | } 33 | 34 | if (this.isDuplex) { 35 | numPrinted /= 2 + numPrinted % 2; 36 | System.out.println("Printing in Duplex Mode"); 37 | } 38 | 39 | this.numPagesPrinted = numPrinted; 40 | this.tonerLevel = tonerCheck(this.tonerLevel - numPrinted * 2); 41 | } 42 | 43 | private int tonerCheck(int tonerLevel) { 44 | 45 | if (tonerLevel > 0 && tonerLevel < 100) { 46 | return tonerLevel; 47 | } else if (tonerLevel >= 100 ){ 48 | return 100; 49 | } 50 | return 0; 51 | 52 | } 53 | 54 | public void fillToner(int tonerAdded) { 55 | this.tonerLevel = tonerCheck(this.tonerLevel + (tonerAdded < 0 ? 0 : tonerAdded)); 56 | } 57 | 58 | public void setDuplex(boolean duplex) { 59 | this.isDuplex = duplex; 60 | System.out.println("Changed to " + (duplex ? "Duplex Mode" : "Non-duplex Mode")); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /040-polymorphism/040-polymorphism.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /040-polymorphism/src/com/chezearth/Car.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Car { 4 | 5 | private String name; 6 | private boolean engine; 7 | private int cylinders; 8 | private int wheels; 9 | private boolean fourWheelDrive; 10 | private int doors; 11 | private boolean engineRunning; 12 | private int speed; 13 | 14 | public Car(String name, int cylinders, boolean fourWheelDrive, int doors) { 15 | this.name = name; 16 | this.engine = true; 17 | this.cylinders = cylinders; 18 | this.wheels = 4; 19 | this.fourWheelDrive = fourWheelDrive; 20 | this.doors = doors; 21 | this.speed = 0; 22 | this.engineRunning = false; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public boolean isEngine() { 30 | return engine; 31 | } 32 | 33 | public int getCylinders() { 34 | return cylinders; 35 | } 36 | 37 | public int getWheels() { 38 | return wheels; 39 | } 40 | 41 | public boolean isFourWheelDrive() { 42 | return fourWheelDrive; 43 | } 44 | 45 | public int getDoors() { 46 | return doors; 47 | } 48 | 49 | public boolean isEngineRunning() { 50 | return engineRunning; 51 | } 52 | 53 | public int getSpeed() { 54 | return speed; 55 | } 56 | 57 | public String describe(String className) { 58 | return className + " name is " + this.name + " and it has " + this.doors + " doors, with " + 59 | "a" + 60 | " " + this.cylinders + "-cylinder engine"; 61 | } 62 | 63 | public void setSpeed(int speed) { 64 | this.speed = speed; 65 | } 66 | 67 | public String startEngine() { 68 | this.speed = 0; 69 | this.engineRunning = true; 70 | return "Engine is now running"; 71 | } 72 | 73 | public String accelerate() { 74 | 75 | if (engineRunning) { 76 | this.speed += 20; 77 | return "Car is speeding up to " + this.getSpeed() + " mph"; 78 | } else { 79 | return "Start the engine first!"; 80 | } 81 | 82 | } 83 | 84 | public String brake() { 85 | 86 | this.speed -= 30; 87 | 88 | if (this.speed < 30) { 89 | this.speed = 0; 90 | return "Car has stopped"; 91 | } 92 | 93 | return "Car is slowing down to " + this.getSpeed() + " mph"; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /040-polymorphism/src/com/chezearth/Ford.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Ford extends Car { 4 | 5 | public Ford(String name, int doors) { 6 | super(name, 8, true, doors); 7 | } 8 | 9 | @Override 10 | public String accelerate() { 11 | if (this.isEngineRunning()) { 12 | this.setSpeed(this.getSpeed() + 25); 13 | 14 | if (this.getSpeed() >= 110) { 15 | this.setSpeed(110); 16 | return "Flooring it! Max speed of 110 mph"; 17 | } 18 | 19 | return getClass().getSimpleName() + " is speeding up to " + this.getSpeed() + " mph"; 20 | } else { 21 | return "Start the engine first!"; 22 | } 23 | } 24 | 25 | @Override 26 | public String brake() { 27 | 28 | if (this.getSpeed() > 55) { 29 | this.setSpeed(this.getSpeed() - 55); 30 | return getClass().getSimpleName() + " is slowing down to " + this.getSpeed() + " mph"; 31 | } else { 32 | this.setSpeed(0); 33 | return getClass().getSimpleName() + " has stopped"; 34 | } 35 | 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /040-polymorphism/src/com/chezearth/Jaguar.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Jaguar extends Car { 4 | 5 | public Jaguar(String name, int doors) { 6 | super(name, 12, true, doors); 7 | } 8 | 9 | @Override 10 | public String accelerate() { 11 | if (this.isEngineRunning()) { 12 | this.setSpeed(this.getSpeed() + 40); 13 | 14 | if (this.getSpeed() >= 200) { 15 | this.setSpeed(200); 16 | return "Flooring it! Max speed of 200 mph"; 17 | } 18 | 19 | return getClass().getSimpleName() + " is speeding up to " + this.getSpeed() + " mph"; 20 | } else { 21 | return "Start the engine first!"; 22 | } 23 | } 24 | 25 | @Override 26 | public String brake() { 27 | 28 | if (this.getSpeed() > 55) { 29 | this.setSpeed(this.getSpeed() - 55); 30 | return getClass().getSimpleName() + " is slowing down to " + this.getSpeed() + " mph"; 31 | } else { 32 | this.setSpeed(0); 33 | return getClass().getSimpleName() + " has stopped"; 34 | } 35 | 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /040-polymorphism/src/com/chezearth/LandRover.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class LandRover extends Car { 4 | 5 | public LandRover(String name, int doors) { 6 | super(name, 4, true, doors); 7 | } 8 | 9 | @Override 10 | public String accelerate() { 11 | if (this.isEngineRunning()) { 12 | this.setSpeed(this.getSpeed() + 10); 13 | 14 | if (this.getSpeed() >= 85) { 15 | this.setSpeed(85); 16 | return "Flooring it! Max speed of 85 mph"; 17 | } 18 | 19 | return getClass().getSimpleName() + " is speeding up to " + this.getSpeed() + " mph"; 20 | } else { 21 | return "Start the engine first!"; 22 | } 23 | } 24 | 25 | @Override 26 | public String brake() { 27 | 28 | if (this.getSpeed() > 15) { 29 | this.setSpeed(this.getSpeed() - 15); 30 | return getClass().getSimpleName() + " is slowing down to " + this.getSpeed() + " mph"; 31 | } else { 32 | this.setSpeed(0); 33 | return getClass().getSimpleName() + " has stopped"; 34 | } 35 | 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /040-polymorphism/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | class Movie { 4 | private String name; 5 | 6 | public Movie(String name) { 7 | this.name = name; 8 | } 9 | 10 | public String getName() { 11 | return name; 12 | } 13 | 14 | public String plot() { 15 | return "No plot here"; 16 | } 17 | } 18 | 19 | class Jaws extends Movie { 20 | 21 | public Jaws() { 22 | super("Jaws"); 23 | } 24 | 25 | public String plot() { 26 | return "A shark eats lots of people"; 27 | } 28 | } 29 | 30 | class IndependenceDay extends Movie { 31 | 32 | public IndependenceDay() { 33 | super("Independence Day"); 34 | } 35 | 36 | @Override 37 | public String plot() { 38 | return "Aliens attempt to take over Earth"; 39 | } 40 | } 41 | 42 | class MazeRunner extends Movie { 43 | 44 | public MazeRunner() { 45 | super("Maze Runner"); 46 | } 47 | 48 | @Override 49 | public String plot() { 50 | return "Kids try escape a maze"; 51 | } 52 | } 53 | 54 | class StarWars extends Movie { 55 | 56 | public StarWars() { 57 | super("Star Wars"); 58 | } 59 | 60 | @Override 61 | public String plot() { 62 | return "Fighting against Imperial Forces taking over the Universe"; 63 | } 64 | } 65 | 66 | class Forgettable extends Movie { 67 | 68 | public Forgettable() { 69 | super("Forgettable"); 70 | } 71 | } 72 | 73 | public class Main { 74 | 75 | public static void main(String[] args) { 76 | 77 | Car car = new Car("Unknown", 4, false, 3); 78 | System.out.println(car.describe(car.getClass().getSimpleName())); 79 | System.out.println(car.accelerate()); 80 | System.out.println(car.brake()); 81 | System.out.println(car.startEngine()); 82 | System.out.println(car.accelerate()); 83 | System.out.println(car.brake()); 84 | System.out.println(car.brake()); 85 | 86 | LandRover landRover = new LandRover("Defender", 5 ); 87 | System.out.println(landRover.describe(landRover.getClass().getSimpleName())); 88 | System.out.println(landRover.accelerate()); 89 | landRover.startEngine(); System.out.println(landRover.accelerate()); System.out.println(landRover.accelerate()); 90 | System.out.println(landRover.brake()); System.out.println(landRover.accelerate()); System.out.println(landRover.brake()); System.out.println(landRover.brake()); 91 | 92 | Jaguar jaguar = new Jaguar("E-Type", 2); 93 | System.out.println(jaguar.describe(jaguar.getClass().getSimpleName())); 94 | System.out.println(jaguar.startEngine()); 95 | System.out.println(jaguar.accelerate()); System.out.println(jaguar.accelerate()); System.out.println(jaguar.accelerate()); System.out.println(jaguar.accelerate()); System.out.println(jaguar.accelerate()); System.out.println(jaguar.accelerate()); 96 | System.out.println(jaguar.brake()); System.out.println(jaguar.accelerate()); System.out.println(jaguar.accelerate()); System.out.println(jaguar.brake()); System.out.println(jaguar.brake()); System.out.println(jaguar.brake()); System.out.println(jaguar.brake()); 97 | /* 98 | System.out.println("Land Rover details:"); 99 | for (int i = 1; i < 11; i++) { 100 | Movie movie = randomMovie(); 101 | System.out.println("Movie #" + i + "; " + movie.getName() + "\n" + 102 | "Plot: " + movie.plot() + "\n" 103 | ); 104 | } */ 105 | } 106 | 107 | public static Movie randomMovie() { 108 | int randomNumber = (int) (Math.random() * 5) + 1; 109 | System.out.println("Random number generated is " + randomNumber); 110 | switch (randomNumber) { 111 | case 1: 112 | return new Jaws(); 113 | case 2: 114 | return new IndependenceDay(); 115 | case 3: 116 | return new MazeRunner(); 117 | case 4: 118 | return new StarWars(); 119 | case 5: 120 | return new Forgettable(); 121 | } 122 | 123 | return null; 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /041-billsburgers/041-billsburgers.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /041-billsburgers/out/production/041-billsburgers/com/chezearth/DeluxeBurger.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chezearth/java-programming-masterclass/c04ef5f57d5421631eed47d489a34ccac92e066a/041-billsburgers/out/production/041-billsburgers/com/chezearth/DeluxeBurger.class -------------------------------------------------------------------------------- /041-billsburgers/out/production/041-billsburgers/com/chezearth/Hamburger.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chezearth/java-programming-masterclass/c04ef5f57d5421631eed47d489a34ccac92e066a/041-billsburgers/out/production/041-billsburgers/com/chezearth/Hamburger.class -------------------------------------------------------------------------------- /041-billsburgers/out/production/041-billsburgers/com/chezearth/HealthyBurger.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chezearth/java-programming-masterclass/c04ef5f57d5421631eed47d489a34ccac92e066a/041-billsburgers/out/production/041-billsburgers/com/chezearth/HealthyBurger.class -------------------------------------------------------------------------------- /041-billsburgers/out/production/041-billsburgers/com/chezearth/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chezearth/java-programming-masterclass/c04ef5f57d5421631eed47d489a34ccac92e066a/041-billsburgers/out/production/041-billsburgers/com/chezearth/Main.class -------------------------------------------------------------------------------- /041-billsburgers/src/com/chezearth/DeluxeBurger.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class DeluxeBurger extends Hamburger { 4 | 5 | public DeluxeBurger(String rollType, String meat, double price) { 6 | super("Deluxe", rollType, meat, price); 7 | super.addAddition1("chips", 0); 8 | super.addAddition2("drink", 0); 9 | } 10 | 11 | @Override 12 | public void addAddition1(String name, double price) { 13 | return; 14 | } 15 | 16 | @Override 17 | public void addAddition2(String name, double price) { 18 | return; 19 | } 20 | 21 | @Override 22 | public void addAddition3(String name, double price) { 23 | return; 24 | } 25 | 26 | @Override 27 | public void addAddition4(String name, double price) { 28 | return; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /041-billsburgers/src/com/chezearth/Hamburger.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Hamburger { 4 | 5 | private String name; 6 | private String breadRollType; 7 | private String meat; 8 | private double basePrice = 39.99; 9 | private String addition1Name; 10 | private double addition1Price; 11 | private String addition2Name; 12 | private double addition2Price; 13 | private String addition3Name; 14 | private double addition3Price; 15 | private String addition4Name; 16 | private double addition4Price; 17 | 18 | public Hamburger(String name, String breadRollType, String meat, double basePrice) { 19 | this.name = name; 20 | this.breadRollType = breadRollType; 21 | this.meat = meat; 22 | this.basePrice = basePrice; 23 | } 24 | 25 | public void addAddition1(String name, double price) { 26 | this.addition1Name = name; 27 | this.addition1Price = price; 28 | } 29 | 30 | public void addAddition2(String name, double price) { 31 | this.addition2Name = name; 32 | this.addition2Price = price; 33 | } 34 | 35 | public void addAddition3(String name, double price) { 36 | // System.out.println(name + " " + price); 37 | this.addition3Name = name; 38 | this.addition3Price = price; 39 | // System.out.println(this.addition3Name + " " + this.addition3Price); 40 | } 41 | 42 | public void addAddition4(String name, double price) { 43 | this.addition4Name = name; 44 | this.addition4Price = price; 45 | } 46 | 47 | public double itemisedHamburger() { 48 | double subTotal = this.basePrice; 49 | 50 | System.out.println("Total price of " 51 | + this.name 52 | + " burger made from " 53 | + this.breadRollType 54 | + " and " 55 | + this.meat 56 | + ":"); 57 | System.out.println("base price: " + this.basePrice); 58 | 59 | if (this.addition1Name != null) { 60 | subTotal += this.addition1Price; 61 | System.out.println("additional " 62 | + this.addition1Name 63 | + ": " 64 | + this.addition1Price 65 | + " subtotal: " 66 | + (double) Math.round(subTotal * 100) / 100 67 | ); 68 | } 69 | 70 | if (this.addition2Name != null) { 71 | subTotal += this.addition2Price; 72 | System.out.println("additional " 73 | + this.addition2Name 74 | + ": " 75 | + this.addition2Price 76 | + " subtotal: " 77 | + (double) Math.round(subTotal * 100) / 100 78 | ); 79 | } 80 | 81 | if (this.addition3Name != null) { 82 | subTotal += this.addition3Price; 83 | System.out.println("additional " 84 | + this.addition3Name 85 | + ": " 86 | + this.addition3Price 87 | + " subtotal: " 88 | + (double) Math.round(subTotal * 100) / 100 89 | ); 90 | } 91 | 92 | if (this.addition4Name != null) { 93 | subTotal += this.addition4Price; 94 | System.out.println("additional " 95 | + this.addition4Name 96 | + ": " 97 | + this.addition4Price 98 | + " subtotal: " 99 | + (double) Math.round(subTotal * 100) / 100 100 | ); 101 | } 102 | 103 | return Math.round(subTotal * 100)/100; 104 | } 105 | } 106 | 107 | 108 | -------------------------------------------------------------------------------- /041-billsburgers/src/com/chezearth/HealthyBurger.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class HealthyBurger extends Hamburger { 4 | 5 | private String addition5Name; 6 | private double addition5Price; 7 | private String addition6Name; 8 | private double addition6Price; 9 | 10 | public HealthyBurger(String meat, double price) { 11 | super( "Healthy","brown rye bread", meat, price); 12 | } 13 | 14 | public void addAddition5(String name, double price) { 15 | this.addition5Name = name; 16 | this.addition5Price = price; 17 | } 18 | 19 | public void addAddition6(String name, double price) { 20 | this.addition6Name = name; 21 | this.addition6Price = price; 22 | } 23 | 24 | @Override 25 | public double itemisedHamburger() { 26 | double subTotal = super.itemisedHamburger(); 27 | 28 | if (this.addition5Name != null) { 29 | subTotal += this.addition5Price; 30 | System.out.println("additional " 31 | + this.addition5Name 32 | + ": " 33 | + this.addition5Price 34 | + " subtotal: " 35 | + (double) Math.round(subTotal * 100) / 100 36 | ); 37 | } 38 | 39 | if (this.addition6Name != null) { 40 | subTotal += this.addition6Price; 41 | System.out.println("additional " 42 | + this.addition6Name 43 | + ": " 44 | + this.addition6Price 45 | + " subtotal: " 46 | + (double) Math.round(subTotal * 100) / 100 47 | ); 48 | } 49 | 50 | return Math.round(subTotal * 100)/100; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /041-billsburgers/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Hamburger hamburger = new Hamburger("Standard","white sesame", "beef", 39.90); 7 | hamburger.addAddition1("lettuce", 5.99); 8 | hamburger.addAddition2("tomato", 4.99); 9 | hamburger.addAddition3("cheese slice", 12.99); 10 | hamburger.itemisedHamburger(); 11 | 12 | HealthyBurger healthyBurger = new HealthyBurger("turkey", 62.49); 13 | healthyBurger.addAddition1("lettuce", 2.99); 14 | healthyBurger.addAddition2("tomato", 1.99); 15 | healthyBurger.addAddition3("feta cheese", 15.99); 16 | healthyBurger.addAddition4("pineapple", 5.99); 17 | healthyBurger.addAddition5("gerkin", 12.99); 18 | healthyBurger.addAddition6("green beans", 3.99); 19 | healthyBurger.itemisedHamburger(); 20 | 21 | DeluxeBurger deluxeBurger = new DeluxeBurger( "sesame", "fatty beef", 119.99); 22 | deluxeBurger.addAddition1("lettuce", 2.99); 23 | deluxeBurger.addAddition2("tomato", 1.99); 24 | deluxeBurger.addAddition3("feta cheese", 15.99); 25 | deluxeBurger.addAddition4("pineapple", 5.99); 26 | deluxeBurger.itemisedHamburger(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /042-arrays/042-arrays.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /042-arrays/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class Main { 7 | 8 | private static Scanner scanner = new Scanner(System.in); 9 | 10 | public static void main(String[] args) { 11 | 12 | int[] myIntegers = getIntegers(5); 13 | 14 | System.out.println("Average is " + getAverage(myIntegers)); 15 | 16 | printIntegers("Sorted array", sortIntegers(myIntegers)); 17 | printIntegers("Original array", myIntegers); 18 | 19 | 20 | } 21 | 22 | public static int[] getIntegers(int number) { 23 | System.out.println("Enter " + number + " or integer values:\r"); 24 | int[] values = new int[number]; 25 | 26 | for (int i = 0; i < values.length; i++) { 27 | values[i] = scanner.nextInt(); 28 | } 29 | 30 | return values; 31 | } 32 | 33 | public static double getAverage(int[] arr) { 34 | int sum = 0; 35 | 36 | if (arr.length > 0) { 37 | 38 | for (int i = 0; i < arr.length; i++) { 39 | sum += arr[i]; 40 | } 41 | 42 | return (double) sum / (double) arr.length; 43 | } 44 | 45 | return sum; 46 | } 47 | 48 | public static void printIntegers(String intro, int[] arr) { 49 | System.out.println(intro + ":"); 50 | for (int i = 0; i < arr.length; i++) { 51 | System.out.println("Element " + i + ", value is " + arr[i]); 52 | } 53 | 54 | } 55 | 56 | public static int[] sortIntegers(int[] arr) { 57 | 58 | if (arr.length > 0) { 59 | int[] result = Arrays.copyOf(arr, arr.length); 60 | 61 | for (int i = 0; i < result.length; i++) { 62 | 63 | for (int j = i + 1; j < result.length; j++) { 64 | 65 | if (result[i] < result[j]) { 66 | int swap = result[j]; 67 | result[j] = result[i]; 68 | result[i] = swap; 69 | } 70 | 71 | } 72 | 73 | } 74 | 75 | return result; 76 | } 77 | 78 | return null; 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /043-references-vs-value-types/043-references-vs-value-types.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /043-references-vs-value-types/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) { 8 | int myIntValue = 10; 9 | int anotherIntValue = myIntValue; 10 | anotherIntValue = 20; 11 | System.out.println("myIntValue = " + myIntValue + ", anotherIntValue = " + anotherIntValue); 12 | 13 | String myStringValue = "hello"; 14 | String anotherStringValue = myStringValue; 15 | myStringValue = "Fred"; 16 | System.out.println("myStringValue = " + myStringValue + ", anotherStringValue = " + anotherStringValue); 17 | 18 | int[] myArr = new int[5]; 19 | int[] anotherArr = myArr; 20 | System.out.println("myArr = " + Arrays.toString(myArr)); 21 | System.out.println("anotherArr = " + Arrays.toString(anotherArr)); 22 | anotherArr[0] = 3; 23 | System.out.println("myArr = " + Arrays.toString(myArr)); 24 | System.out.println("anotherArr = " + Arrays.toString(anotherArr)); 25 | 26 | modifyRef(myArr); 27 | System.out.println("myArr = " + Arrays.toString(myArr)); 28 | System.out.println("anotherArr = " + Arrays.toString(anotherArr)); 29 | 30 | } 31 | 32 | public static void modifyRef(int[] array) { 33 | array[0] = 2; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /044-min-element-challenge/044-min-element-challenge.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /044-min-element-challenge/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | 7 | private static Scanner scanner = new Scanner(System.in); 8 | 9 | public static void main(String[] args) { 10 | if (args.length == 0) { 11 | System.out.println("Please supply the number of integers required"); 12 | args = new String[1]; 13 | args[0] = String.valueOf(readIntegers(1)[0]); 14 | } 15 | int[] array = readIntegers(Integer.parseInt(args[0])); 16 | System.out.println("Min element = " + findMin(array)); 17 | } 18 | 19 | public static int[] readIntegers(int count) { 20 | int[] arr = new int[count]; 21 | System.out.println("Enter " + count + " integers:\r"); 22 | 23 | for (int i = 0; i < count; i++) { 24 | arr[i] = scanner.nextInt(); 25 | } 26 | 27 | return arr; 28 | } 29 | 30 | public static int findMin(int[] arr) { 31 | int min = arr[0]; 32 | 33 | for (int i = 1; i < arr.length; i++) { 34 | 35 | if (min > arr[i]) { 36 | min = arr[i]; 37 | } 38 | 39 | } 40 | 41 | return min; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /045-reverse-array-challenge/045-reverse-array-challenge.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /045-reverse-array-challenge/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) { 8 | int[] array1 = {-20, 5, 63, 2, -1, 0, 4}; 9 | int[] array2 = {-20, 5, 63, 2, -1, 0}; 10 | System.out.println("original array1 = " + Arrays.toString(array1)); 11 | reverse(array1); 12 | System.out.println("reversed array1 = " + Arrays.toString(array1)); 13 | System.out.println("original array2 = " + Arrays.toString(array2)); 14 | reverse(array2); 15 | System.out.println("reversed array2 = " + Arrays.toString(array2)); 16 | } 17 | 18 | public static void reverse(int[] arr) { 19 | for (int i = 0; i < arr.length / 2; i++) { 20 | int swap = arr[i]; 21 | arr[i] = arr[arr.length - i - 1]; 22 | arr[arr.length - i - 1] = swap; 23 | } 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /046-list-and-arraylist/046-list-and-arraylist.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /046-list-and-arraylist/out/production/046-list-and-arraylist/com/chezearth/GroceryList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chezearth/java-programming-masterclass/c04ef5f57d5421631eed47d489a34ccac92e066a/046-list-and-arraylist/out/production/046-list-and-arraylist/com/chezearth/GroceryList.class -------------------------------------------------------------------------------- /046-list-and-arraylist/out/production/046-list-and-arraylist/com/chezearth/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chezearth/java-programming-masterclass/c04ef5f57d5421631eed47d489a34ccac92e066a/046-list-and-arraylist/out/production/046-list-and-arraylist/com/chezearth/Main.class -------------------------------------------------------------------------------- /046-list-and-arraylist/src/com/chezearth/GroceryList.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | import javax.swing.text.StringContent; 4 | import java.lang.reflect.Array; 5 | import java.util.ArrayList; 6 | 7 | public class GroceryList { 8 | private ArrayList groceryList = new ArrayList(); 9 | 10 | public void addGroceryItem(String item) { 11 | groceryList.add(item.toLowerCase()); 12 | } 13 | 14 | public void printGroceryList() { 15 | System.out.println("You have " + groceryList.size() + " items in your grocery list"); 16 | 17 | for (int i = 0; i < groceryList.size(); i++) { 18 | System.out.println(i + 1 + ". " + groceryList.get(i)); 19 | } 20 | } 21 | 22 | public void modifyGroceryItem(String oldItem, String newItem) { 23 | int position = findGroceryItem(oldItem); 24 | 25 | if (position >= 0) { 26 | String currItem = groceryList.get(position); 27 | modifyGroceryItem(position, newItem); 28 | System.out.println("Grocery item " + (position + 1) 29 | + " has been modified from " + currItem 30 | + " to " + groceryList.get(position) 31 | ); 32 | } else { 33 | System.out.println(oldItem + " doesn't exist"); 34 | } 35 | 36 | } 37 | private void modifyGroceryItem(int position, String newItem) { 38 | groceryList.set(position, newItem.toLowerCase()); 39 | } 40 | 41 | public void removeGroceryItem(String item) { 42 | int position = findGroceryItem(item); 43 | 44 | if (position >= 0) { 45 | String currItem = groceryList.get(position); 46 | removeGroceryItem(position); 47 | System.out.println("Grocery item " + (position + 1) 48 | + ", " + currItem 49 | + ", has been removed. " 50 | + "There are now " + groceryList.size() 51 | + " items in your grocery list" 52 | ); 53 | } else { 54 | System.out.println("The item at position " + position + " doesn't exist"); 55 | } 56 | } 57 | private void removeGroceryItem(int position) { 58 | groceryList.remove(position); 59 | } 60 | 61 | public String searchGroceryItem(String searchItem) { 62 | int position = findGroceryItem(searchItem); 63 | if (position >= 0) { 64 | String item = groceryList.get(position); 65 | System.out.println("Found " + item + " in our grocery list"); 66 | return item; 67 | } else { 68 | System.out.println("Item " + searchItem + " is not in the grocery list"); 69 | return null; 70 | } 71 | } 72 | 73 | private int findGroceryItem(String searchItem) { 74 | return groceryList.indexOf(searchItem.toLowerCase()); 75 | } 76 | } 77 | 78 | -------------------------------------------------------------------------------- /046-list-and-arraylist/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | private static Scanner scanner = new Scanner(System.in); 7 | private static GroceryList groceryList = new GroceryList(); 8 | 9 | public static void main(String[] args) { 10 | boolean quit = false; 11 | int choice = 0; 12 | printInstructions(); 13 | while (!quit) { 14 | System.out.println("Enter your choice: "); 15 | choice = scanner.nextInt(); 16 | scanner.nextLine(); 17 | switch (choice) { 18 | case 0: 19 | printInstructions(); 20 | break; 21 | case 1: 22 | groceryList.printGroceryList(); 23 | break; 24 | case 2: 25 | addItem(); 26 | break; 27 | case 3: 28 | modifyItem(); 29 | break; 30 | case 4: 31 | removeItem(); 32 | break; 33 | case 5: 34 | searchForItem(); 35 | break; 36 | case 6: 37 | System.out.println("Bye!"); 38 | quit = true; 39 | } 40 | } 41 | } 42 | 43 | public static void printInstructions() { 44 | System.out.println("\nPress "); 45 | System.out.println("\t 0 - To print choice options"); 46 | System.out.println("\t 1 - To print and list grocery items"); 47 | System.out.println("\t 2 - To add an item to the list"); 48 | System.out.println("\t 3 - To modify an item in the list"); 49 | System.out.println("\t 4 - To remove an item from the list"); 50 | System.out.println("\t 5 - To search for an item in the list"); 51 | System.out.println("\t 6 - To quit the application"); 52 | } 53 | 54 | public static void addItem() { 55 | System.out.print("Please enter your grocery item: "); 56 | groceryList.addGroceryItem(scanner.nextLine()); 57 | } 58 | 59 | public static void modifyItem() { 60 | System.out.print("Please enter your item to be changed: "); 61 | String oldItem = scanner.nextLine(); 62 | System.out.print("Please enter your modification: "); 63 | groceryList.modifyGroceryItem(oldItem, scanner.nextLine()); 64 | } 65 | 66 | public static void removeItem() { 67 | System.out.print("Enter item to remove: "); 68 | String item = scanner.nextLine(); 69 | groceryList.removeGroceryItem(item); 70 | } 71 | 72 | public static void searchForItem() { 73 | System.out.print("Please enter your item to search for: "); 74 | String searchItem = scanner.nextLine(); 75 | groceryList.searchGroceryItem(searchItem); 76 | 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /047-arraylist-challenge-1/047-arraylist-challenge-1.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /047-arraylist-challenge-1/out/production/047-arraylist-challenge-1/com/chezearth/Contact.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chezearth/java-programming-masterclass/c04ef5f57d5421631eed47d489a34ccac92e066a/047-arraylist-challenge-1/out/production/047-arraylist-challenge-1/com/chezearth/Contact.class -------------------------------------------------------------------------------- /047-arraylist-challenge-1/out/production/047-arraylist-challenge-1/com/chezearth/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chezearth/java-programming-masterclass/c04ef5f57d5421631eed47d489a34ccac92e066a/047-arraylist-challenge-1/out/production/047-arraylist-challenge-1/com/chezearth/Main.class -------------------------------------------------------------------------------- /047-arraylist-challenge-1/out/production/047-arraylist-challenge-1/com/chezearth/MobilePhone.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chezearth/java-programming-masterclass/c04ef5f57d5421631eed47d489a34ccac92e066a/047-arraylist-challenge-1/out/production/047-arraylist-challenge-1/com/chezearth/MobilePhone.class -------------------------------------------------------------------------------- /047-arraylist-challenge-1/src/com/chezearth/Contact.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | public class Contact { 4 | private String name; 5 | private String phoneNumber; 6 | 7 | public Contact(String name, String phoneNumber) { 8 | this.name = name; 9 | this.phoneNumber = phoneNumber; 10 | } 11 | 12 | public String getName() { 13 | return name; 14 | } 15 | 16 | public String getPhoneNumber() { 17 | return phoneNumber; 18 | } 19 | 20 | public static Contact createContact(String name, String phoneNumber) { 21 | 22 | return new Contact(name, phoneNumber); 23 | } 24 | 25 | // public void setPhoneNumber(String phoneNumber) { 26 | // this.phoneNumber = phoneNumber; 27 | // } 28 | // 29 | // public void setName(String name) { 30 | // this.name = name; 31 | // } 32 | } 33 | -------------------------------------------------------------------------------- /047-arraylist-challenge-1/src/com/chezearth/Main.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | private static Scanner scanner = new Scanner(System.in); 7 | private static MobilePhone mobilePhone = new MobilePhone(); 8 | 9 | public static void main(String[] args) { 10 | boolean quit = false; 11 | printInstructions(); 12 | 13 | while (!quit) { 14 | System.out.println("Enter your choice: "); 15 | int choice = scanner.nextInt(); 16 | scanner.nextLine(); 17 | 18 | switch (choice) { 19 | case 0: 20 | printInstructions(); 21 | break; 22 | case 1: 23 | addContact(); 24 | break; 25 | case 2: 26 | modifyContact(); 27 | break; 28 | case 3: 29 | removeContact(); 30 | break; 31 | case 4: 32 | queryContacts(); 33 | break; 34 | case 5: 35 | System.out.println("Bye."); 36 | quit = true; 37 | } 38 | 39 | } 40 | 41 | } 42 | 43 | public static void printInstructions() { 44 | System.out.println("\nPress "); 45 | System.out.println("\t 0 - To print choice options"); 46 | System.out.println("\t 1 - To add a contact"); 47 | System.out.println("\t 2 - To modify a contact"); 48 | System.out.println("\t 3 - To remove a contact"); 49 | System.out.println("\t 4 - To query contacts"); 50 | System.out.println("\t 5 - To quit the application"); 51 | } 52 | 53 | public static void addContact() { 54 | System.out.print("Enter contact name: "); 55 | String name = scanner.nextLine(); 56 | if (!mobilePhone.checkForContact(name)) { 57 | System.out.print("Enter contact phone number: "); 58 | String number = scanner.nextLine(); 59 | String result = mobilePhone.addContact(name, number); 60 | 61 | if (result != null) { 62 | System.out.println("Contact added; " + result); 63 | } else { 64 | System.out.println("The contact could not be added"); 65 | } 66 | 67 | } else { 68 | System.out.println("A contact with the name '" + name + "' already exists"); 69 | } 70 | } 71 | 72 | public static void modifyContact() { 73 | System.out.print("Enter contact name to be edited: "); 74 | String name = scanner.nextLine(); 75 | 76 | if (mobilePhone.checkForContact(name)) { 77 | System.out.print("Enter contact new name (leave blank to not change): "); 78 | String newName = scanner.nextLine(); 79 | System.out.print("Enter contact new phone number (leave blank to not change): "); 80 | String newNumber = scanner.nextLine(); 81 | String result = mobilePhone.modifyContact(name, newName , newNumber); 82 | 83 | if (result != null) { 84 | System.out.println("Contact updated, " + result); 85 | return; 86 | } 87 | 88 | } 89 | 90 | System.out.println(notFound(name)); 91 | } 92 | 93 | public static void removeContact() { 94 | System.out.print("Enter contact name to be deleted: "); 95 | String name = scanner.nextLine(); 96 | String result = mobilePhone.removeContact(name); 97 | 98 | if (result != null) { 99 | System.out.println(result); 100 | return; 101 | } 102 | 103 | System.out.println(notFound(name)); 104 | } 105 | 106 | public static void queryContacts() { 107 | System.out.print("Enter contact name to search for (press ENTER to return all contacts):" + 108 | " "); 109 | String name = scanner.nextLine(); 110 | 111 | if (name == null || name.length() == 0) { 112 | String result = mobilePhone.getAllContacts(); 113 | 114 | if (result != null) { 115 | System.out.println("Details for all contacts:\n" + result); 116 | } else { 117 | System.out.println("There are not contacts saved, add some"); 118 | } 119 | 120 | } else { 121 | String result = mobilePhone.getContactDetails(name); 122 | 123 | if (result != null) { 124 | System.out.println("Details for contact:\n" + result); 125 | } else { 126 | System.out.println(notFound(name)); 127 | } 128 | 129 | } 130 | 131 | } 132 | 133 | public static String notFound(String name) { 134 | return "The contact with name '" + name + "' could not be found"; 135 | } 136 | 137 | } 138 | 139 | -------------------------------------------------------------------------------- /047-arraylist-challenge-1/src/com/chezearth/MobilePhone.java: -------------------------------------------------------------------------------- 1 | package com.chezearth; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class MobilePhone { 6 | private ArrayList contactsArr; 7 | 8 | public MobilePhone() { 9 | this.contactsArr = new ArrayList<>(); 10 | } 11 | 12 | public String addContact(String name, String phoneNumber) { 13 | int index = findContact(name); 14 | if (index < 0) { 15 | Contact newContact = new Contact(name, phoneNumber); 16 | contactsArr.add(newContact); 17 | return makeContactDetails(newContact); 18 | } 19 | 20 | return null; 21 | } 22 | 23 | public String modifyContact(String name, String newName, String newNumber) { 24 | int index = findContact(name); 25 | 26 | if (index >= 0) { 27 | 28 | if (newName == null || newName.length() == 0) { 29 | newName = findContact(index).getName(); 30 | } 31 | 32 | if (newNumber == null || newNumber.length() == 0) { 33 | newNumber = findContact(index).getPhoneNumber(); 34 | } 35 | contactsArr.set(index, findContact(index).createContact(newName, newNumber)); 36 | return makeContactDetails(findContact(index)); 37 | } 38 | 39 | return null; 40 | } 41 | 42 | public String removeContact(String name) { 43 | int index = findContact(name); 44 | 45 | if (index >= 0) { 46 | contactsArr.remove(index); 47 | return "Contact with name '" + findContact(index).getName() + "' deleted"; 48 | } 49 | 50 | return null; 51 | } 52 | 53 | public boolean checkForContact(String name) { 54 | return (findContact(name) >= 0); 55 | } 56 | 57 | private int findContact(String name) { 58 | 59 | for (int i = 0; i < contactsArr.size(); i++) { 60 | Contact contact = findContact(i); 61 | 62 | if (contact.getName().trim().toLowerCase().equals(name.trim().toLowerCase())) { 63 | return i; 64 | } 65 | 66 | } 67 | 68 | return -1; 69 | } 70 | 71 | private Contact findContact(int index) { 72 | return contactsArr.get(index); 73 | } 74 | 75 | public String getContactDetails(String name) { 76 | int index = findContact(name); 77 | 78 | if (index < 0) { 79 | return null; 80 | } 81 | 82 | return makeContactDetails(findContact(index)); 83 | } 84 | 85 | public String getAllContacts() { 86 | StringBuilder contactListString = new StringBuilder(); 87 | 88 | if (contactsArr.size() > 0) { 89 | 90 | for (int i = 0; i < contactsArr.size(); i++) { 91 | Contact contact = findContact(i); 92 | contactListString.append(makeContactDetails(contact)).append("\n"); 93 | } 94 | 95 | return contactListString.toString().trim(); 96 | } 97 | 98 | return null; 99 | } 100 | 101 | private String makeContactDetails(Contact contact) { 102 | 103 | if (contact != null) { 104 | return "name: " 105 | + contact.getName() 106 | + ", phone Number: " 107 | + contact.getPhoneNumber(); 108 | } 109 | 110 | return null; 111 | } 112 | 113 | } 114 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java Programming Masterclass for Software Developers 2 | 3 | Course by Tim Buchalka 4 | 5 | ### Project Naming conventions 6 | I haven't followed the convention used by Tim for naming my projects; instead I have started the 7 | project name with a 8 | number and kept my names 9 | in small letters and separated words with a dash `-`, such as `26-digit-sum-challenge`. The 10 | number helps to order the projects logically with the course. 11 | 12 | I have also tried to group the exercises together into single projects (rather than making each 13 | exercise a project) to save space and keep the total number of projects in the course to a 14 | minimum. A group of exercises' project will be named as `[number]-exercise-[from]-[to]`, for 15 | example `30-exercises-27-28`, which is project no. 30, exercises 27 to 28. 16 | --------------------------------------------------------------------------------