├── README.md ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── ing │ │ ├── designpattern01 │ │ ├── problem │ │ │ ├── BursluOgrenci.java │ │ │ ├── BursluveCiftAnadalYapanOgrenci.java │ │ │ ├── Main.java │ │ │ ├── Ogrenci.java │ │ │ ├── YarimBursluCiftAnadalOgrenci.java │ │ │ └── YarimBursluOgrenci.java │ │ └── solution │ │ │ ├── Anadallilik.java │ │ │ ├── Bursluluk.java │ │ │ ├── CiftAnadal.java │ │ │ ├── HicBursYok.java │ │ │ ├── Main.java │ │ │ ├── Ogrenci.java │ │ │ ├── TamBurs.java │ │ │ ├── TekAnadal.java │ │ │ └── YariBurslu.java │ │ ├── designpattern02 │ │ ├── problem │ │ │ ├── BolKopukluLatte.java │ │ │ ├── Cay.java │ │ │ ├── CiftShotLatte.java │ │ │ ├── Icecek.java │ │ │ ├── Latte.java │ │ │ └── SutluCay.java │ │ └── solution │ │ │ ├── BolShot.java │ │ │ ├── BolSutlu.java │ │ │ ├── Buzlu.java │ │ │ ├── Cay.java │ │ │ ├── Eklenti.java │ │ │ ├── Icecek.java │ │ │ ├── Latte.java │ │ │ ├── Main.java │ │ │ └── Sut.java │ │ ├── question01 │ │ └── NutritionFacts.java │ │ ├── question02 │ │ ├── Roman.java │ │ └── Sum.java │ │ ├── question03 │ │ ├── Adult.java │ │ └── Room.java │ │ ├── question04 │ │ └── PhoneNumber.java │ │ ├── question05 │ │ └── Sort.java │ │ ├── question06 │ │ └── Complex.java │ │ ├── question07 │ │ ├── Sub.java │ │ └── Super.java │ │ ├── question08 │ │ ├── Figure.java │ │ └── fixed │ │ │ ├── Circle.java │ │ │ ├── Figure.java │ │ │ ├── Main.java │ │ │ ├── Rectangle.java │ │ │ └── Square.java │ │ ├── question09 │ │ ├── App.java │ │ └── Period.java │ │ ├── question10 │ │ ├── Avoid.java │ │ └── Money.java │ │ ├── question11 │ │ ├── ReadAndWriteFile.java │ │ └── ReadFile.java │ │ ├── question12 │ │ ├── Card.java │ │ └── Symptom.java │ │ ├── question13 │ │ └── InstrumentedHashSet.java │ │ ├── question14 │ │ ├── AreaCalculator.java │ │ └── PhysicalConstants.java │ │ ├── question15 │ │ ├── Dessert.java │ │ ├── Main.java │ │ └── Utensil.java │ │ ├── question16 │ │ └── Rawetallica.java │ │ ├── question17 │ │ └── Guess.java │ │ ├── question18 │ │ ├── EmptyStackException.java │ │ └── Stack.java │ │ ├── question19 │ │ └── Combiner.java │ │ ├── question20 │ │ └── Hazardous.java │ │ ├── question21 │ │ ├── Calculator.java │ │ └── Main.java │ │ ├── question22 │ │ ├── Main.java │ │ └── MusicTeam.java │ │ ├── question23 │ │ ├── Main.java │ │ └── PrintedWork.java │ │ ├── question24 │ │ ├── RunTests.java │ │ └── Sample.java │ │ ├── question25 │ │ └── NerfBasic.java │ │ ├── question26 │ │ ├── Freq.java │ │ ├── StockCalculation.java │ │ └── StockInfo.java │ │ ├── question27 │ │ └── BondCalculation.java │ │ ├── question28 │ │ ├── BMW.java │ │ ├── Car.java │ │ ├── CarClassifier.java │ │ ├── Mercedes.java │ │ └── SetList.java │ │ ├── question29 │ │ └── NumberOperation.java │ │ ├── question30 │ │ ├── Cheese.java │ │ ├── CheeseController.java │ │ └── Main.java │ │ ├── question31 │ │ └── Max.java │ │ ├── question32 │ │ └── LordOfTheLoops.java │ │ ├── question33 │ │ └── RandomTest.java │ │ ├── question34 │ │ └── Combine.java │ │ ├── question35 │ │ └── Translate.java │ │ ├── question36 │ │ └── ReadFile.java │ │ ├── question37 │ │ └── Sorting.java │ │ ├── question38 │ │ └── StopThread.java │ │ ├── question39 │ │ └── MailNinja.java │ │ ├── question40 │ │ └── MyLock.java │ │ ├── question41 │ │ └── NetworkUtility.java │ │ ├── question42 │ │ └── Supply.java │ │ ├── question43 │ │ ├── DontDo.java │ │ ├── IllegalArgumentExceptionExample.java │ │ ├── IllegalStateExceptionDemo.java │ │ ├── IndexOutOfBoundsExceptionExample.java │ │ ├── NullPointerExceptionExample.java │ │ ├── User.java │ │ └── unsuportedOperation │ │ │ ├── Duck.java │ │ │ ├── MiniDuckSimulator.java │ │ │ ├── ModelDuck.java │ │ │ └── RedHeadDuck.java │ │ └── question44 │ │ └── PhoneNumber.java └── resource │ ├── question_10.png │ └── question_4.png └── test └── java └── com └── ing └── AppTest.java /README.md: -------------------------------------------------------------------------------- 1 | # BootcampEffectiveJava 2 | 3 | 4 | https://join.slack.com/t/jugistanbul/shared_invite/zt-38ao33ty-AOb_KaQdLcLcvzzaI08cyA 5 | 6 | https://www.cs.virginia.edu/~jh2jf/courses/cs2110/java-pass-by-value.html 7 | 8 | 9 | https://www.goodreads.com/quotes/6341736-any-fool-can-write-code-that-a-computer-can-understand - Martin Fowlers 10 | 11 | https://dev.to/trekhleb/s-o-l-i-d-principles-around-you-1o17 12 | 13 | DRY : https://en.wikipedia.org/wiki/Don%27t_repeat_yourself 14 | KISS : https://en.wikipedia.org/wiki/KISS_principle 15 | 16 | https://en.wikipedia.org/wiki/Java_version_history 17 | 18 | Floating point problem : https://en.wikipedia.org/wiki/IEEE_754 19 | 20 | Pass by ref ile value arasındaki farkı gösteren gif : https://blog.penjee.com/wp-content/uploads/2015/02/pass-by-reference-vs-pass-by-value-animation.gif 21 | 22 | https://en.wikipedia.org/wiki/Poka-yoke 23 | 24 | https://en.wikipedia.org/wiki/Heap_pollution 25 | 26 | https://projectlombok.org/ 27 | 28 | https://refactoring.guru/ 29 | 30 | https://deviq.com/design-patterns/strategy-pattern 31 | https://docs.gitlab.com/ee/development/fe_guide/design_anti_patterns.html -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | com.ing 8 | effective 9 | 1.0-SNAPSHOT 10 | 11 | ing-effective 12 | 13 | http://www.ing.com 14 | 15 | 16 | UTF-8 17 | 1.9 18 | 1.9 19 | 20 | 21 | 22 | 23 | junit 24 | junit 25 | 4.8.2 26 | test 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | maven-clean-plugin 38 | 3.1.0 39 | 40 | 41 | 42 | maven-resources-plugin 43 | 3.0.2 44 | 45 | 46 | maven-compiler-plugin 47 | 3.8.0 48 | 49 | 50 | maven-surefire-plugin 51 | 2.22.1 52 | 53 | 54 | maven-jar-plugin 55 | 3.0.2 56 | 57 | 58 | maven-install-plugin 59 | 2.5.2 60 | 61 | 62 | maven-deploy-plugin 63 | 2.8.2 64 | 65 | 66 | 67 | maven-site-plugin 68 | 3.7.1 69 | 70 | 71 | maven-project-info-reports-plugin 72 | 3.0.0 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern01/problem/BursluOgrenci.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern01.problem; 2 | 3 | public class BursluOgrenci extends Ogrenci{ 4 | 5 | public BursluOgrenci(String ad, String soyad, double not) { 6 | this.ad = ad; 7 | this.soyad = soyad; 8 | this.not = not; 9 | } 10 | 11 | @Override 12 | protected String burslulukOraniniVer() { 13 | return "Burslu "; 14 | } 15 | 16 | @Override 17 | protected String anaDalBilgisiniVer() { 18 | return "Tek anadal"; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern01/problem/BursluveCiftAnadalYapanOgrenci.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern01.problem; 2 | 3 | public class BursluveCiftAnadalYapanOgrenci extends Ogrenci{ 4 | 5 | public BursluveCiftAnadalYapanOgrenci(String ad, String soyad, double not) { 6 | this.ad = ad; 7 | this.soyad = soyad; 8 | this.not = not; 9 | } 10 | 11 | @Override 12 | protected String burslulukOraniniVer() { 13 | return "Burslu "; 14 | } 15 | 16 | @Override 17 | protected String anaDalBilgisiniVer() { 18 | return "Cift anadal"; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern01/problem/Main.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern01.problem; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | YarimBursluCiftAnadalOgrenci yarimBursluCiftAnadalOgrenci = 7 | new YarimBursluCiftAnadalOgrenci("Kemal", "Urkmez", 89); 8 | 9 | System.out.println(yarimBursluCiftAnadalOgrenci.anaDalBilgisiniVer()); 10 | System.out.println(yarimBursluCiftAnadalOgrenci.burslulukOraniniVer()); 11 | 12 | // Burslu degilsin 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern01/problem/Ogrenci.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern01.problem; 2 | 3 | public abstract class Ogrenci { 4 | 5 | protected String ad; 6 | protected String soyad; 7 | protected double not; 8 | 9 | protected double notlariniVer(){ 10 | return not; 11 | } 12 | 13 | protected abstract String burslulukOraniniVer(); 14 | protected abstract String anaDalBilgisiniVer(); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern01/problem/YarimBursluCiftAnadalOgrenci.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern01.problem; 2 | 3 | public class YarimBursluCiftAnadalOgrenci extends Ogrenci{ 4 | 5 | public YarimBursluCiftAnadalOgrenci(String ad, String soyad, double not) { 6 | this.ad = ad; 7 | this.soyad = soyad; 8 | this.not = not; 9 | } 10 | 11 | @Override 12 | protected String burslulukOraniniVer() { 13 | return "Yarim Burslu "; 14 | } 15 | 16 | @Override 17 | protected String anaDalBilgisiniVer() { 18 | return "Cift anadal"; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern01/problem/YarimBursluOgrenci.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern01.problem; 2 | 3 | public class YarimBursluOgrenci extends Ogrenci{ 4 | 5 | public YarimBursluOgrenci(String ad, String soyad, double not) { 6 | this.ad = ad; 7 | this.soyad = soyad; 8 | this.not = not; 9 | } 10 | 11 | @Override 12 | protected String burslulukOraniniVer() { 13 | return "Yarim Burslu "; 14 | } 15 | 16 | @Override 17 | protected String anaDalBilgisiniVer() { 18 | return "Tek anadal"; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern01/solution/Anadallilik.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern01.solution; 2 | 3 | public interface Anadallilik { 4 | public String anaDalBilgisiniSoyle(); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern01/solution/Bursluluk.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern01.solution; 2 | 3 | public interface Bursluluk { 4 | public String bursOraniniVer(); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern01/solution/CiftAnadal.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern01.solution; 2 | 3 | public class CiftAnadal implements Anadallilik{ 4 | @Override 5 | public String anaDalBilgisiniSoyle() { 6 | return "Cift Ana Dal"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern01/solution/HicBursYok.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern01.solution; 2 | 3 | public class HicBursYok implements Bursluluk { 4 | 5 | @Override 6 | public String bursOraniniVer() { 7 | return "Burs Yok"; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern01/solution/Main.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern01.solution; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Ogrenci kemal = new Ogrenci("Kemal", "Urkmez", 89); 6 | kemal.setBursluluk(new TamBurs()); 7 | kemal.setAnadallilik(new TekAnadal()); 8 | System.out.println(kemal); 9 | 10 | kemal.setAnadallilik(new CiftAnadal()); 11 | System.out.println(kemal); 12 | 13 | kemal.setBursluluk(new HicBursYok()); 14 | System.out.println(kemal); 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern01/solution/Ogrenci.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern01.solution; 2 | 3 | public class Ogrenci { 4 | 5 | protected String ad; 6 | protected String soyad; 7 | protected double not; 8 | 9 | public Ogrenci(String ad, String soyad, double not) { 10 | this.ad = ad; 11 | this.soyad = soyad; 12 | this.not = not; 13 | } 14 | 15 | protected Bursluluk bursluluk; 16 | 17 | public void setBursluluk(Bursluluk bursluluk) { 18 | this.bursluluk = bursluluk; 19 | } 20 | 21 | @Override 22 | public String toString() { 23 | return "Ogrenci{" + 24 | "ad='" + ad + '\'' + 25 | ", soyad='" + soyad + '\'' + 26 | ", not=" + not + 27 | ", bursluluk=" + bursluluk.bursOraniniVer() + 28 | ", anadallilik=" + anadallilik.anaDalBilgisiniSoyle() + 29 | '}'; 30 | } 31 | 32 | public void setAnadallilik(Anadallilik anadallilik) { 33 | this.anadallilik = anadallilik; 34 | } 35 | 36 | protected Anadallilik anadallilik; 37 | 38 | protected double notlariniVer(){ 39 | return not; 40 | } 41 | 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern01/solution/TamBurs.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern01.solution; 2 | 3 | public class TamBurs implements Bursluluk { 4 | 5 | @Override 6 | public String bursOraniniVer() { 7 | return "Tam Burslu"; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern01/solution/TekAnadal.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern01.solution; 2 | 3 | public class TekAnadal implements Anadallilik{ 4 | @Override 5 | public String anaDalBilgisiniSoyle() { 6 | return "Tek Ana Dal"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern01/solution/YariBurslu.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern01.solution; 2 | 3 | public class YariBurslu implements Bursluluk { 4 | 5 | @Override 6 | public String bursOraniniVer() { 7 | return "Yari Burslu"; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern02/problem/BolKopukluLatte.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern02.problem; 2 | 3 | public class BolKopukluLatte implements Icecek { 4 | 5 | 6 | @Override 7 | public String detaylariVer() { 8 | return "Bol Kopuklu Latte"; 9 | } 10 | 11 | @Override 12 | public double fiyatHesapla() { 13 | return 24; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern02/problem/Cay.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern02.problem; 2 | 3 | public class Cay implements Icecek{ 4 | @Override 5 | public String detaylariVer() { 6 | return "Cay"; 7 | } 8 | 9 | @Override 10 | public double fiyatHesapla() { 11 | return 1; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern02/problem/CiftShotLatte.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern02.problem; 2 | 3 | public class CiftShotLatte implements Icecek { 4 | 5 | 6 | @Override 7 | public String detaylariVer() { 8 | return "Cift Shot Kopuklu Latte"; 9 | } 10 | 11 | @Override 12 | public double fiyatHesapla() { 13 | return 30; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern02/problem/Icecek.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern02.problem; 2 | 3 | public interface Icecek { 4 | 5 | String detaylariVer(); 6 | double fiyatHesapla(); 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern02/problem/Latte.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern02.problem; 2 | 3 | public class Latte implements Icecek { 4 | 5 | 6 | @Override 7 | public String detaylariVer() { 8 | return "Latte"; 9 | } 10 | 11 | @Override 12 | public double fiyatHesapla() { 13 | return 22; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern02/problem/SutluCay.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern02.problem; 2 | 3 | public class SutluCay implements Icecek { 4 | @Override 5 | public String detaylariVer() { 6 | return "Sutlu Cay"; 7 | } 8 | 9 | @Override 10 | public double fiyatHesapla() { 11 | return 3; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern02/solution/BolShot.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern02.solution; 2 | 3 | public class BolShot extends Eklenti { 4 | 5 | public BolShot(Icecek icecek) { 6 | this.icecek = icecek; 7 | } 8 | 9 | @Override 10 | public String getAciklama() { 11 | return icecek.getAciklama() + " Bol shotlu"; 12 | } 13 | 14 | @Override 15 | double fiyatHesapla() { 16 | return icecek.fiyatHesapla() + 4; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern02/solution/BolSutlu.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern02.solution; 2 | 3 | public class BolSutlu extends Eklenti { 4 | 5 | public BolSutlu(Icecek icecek) { 6 | this.icecek = icecek; 7 | } 8 | 9 | @Override 10 | public String getAciklama() { 11 | return icecek.getAciklama() + " bol sutlu"; 12 | } 13 | 14 | @Override 15 | double fiyatHesapla() { 16 | return icecek.fiyatHesapla() + 3.0; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern02/solution/Buzlu.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern02.solution; 2 | 3 | public class Buzlu extends Eklenti{ 4 | 5 | public Buzlu(Icecek icecek) { 6 | this.icecek = icecek; 7 | } 8 | 9 | @Override 10 | public String getAciklama() { 11 | return icecek.getAciklama() + " Buz eklendi"; 12 | } 13 | 14 | @Override 15 | double fiyatHesapla() { 16 | return icecek.fiyatHesapla() + 1; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern02/solution/Cay.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern02.solution; 2 | 3 | public class Cay extends Icecek{ 4 | public Cay() { 5 | this.aciklama = "Cay"; 6 | } 7 | 8 | @Override 9 | double fiyatHesapla() { 10 | return 1; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern02/solution/Eklenti.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern02.solution; 2 | 3 | public abstract class Eklenti extends Icecek { 4 | 5 | Icecek icecek; 6 | public abstract String getAciklama(); 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern02/solution/Icecek.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern02.solution; 2 | 3 | public abstract class Icecek { 4 | 5 | String aciklama; 6 | 7 | public String getAciklama() { 8 | return aciklama; 9 | } 10 | 11 | abstract double fiyatHesapla(); 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern02/solution/Latte.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern02.solution; 2 | 3 | public class Latte extends Icecek{ 4 | 5 | public Latte() { 6 | this.aciklama = "Latte"; 7 | } 8 | 9 | @Override 10 | double fiyatHesapla() { 11 | return 22; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern02/solution/Main.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern02.solution; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Icecek latte = new Latte(); 7 | System.out.println(latte.fiyatHesapla()); 8 | 9 | latte = new BolSutlu(latte); 10 | System.out.println(latte.getAciklama() + " " + latte.fiyatHesapla()); 11 | 12 | latte = new Buzlu(latte); 13 | System.out.println(latte.getAciklama() + " " + latte.fiyatHesapla()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/ing/designpattern02/solution/Sut.java: -------------------------------------------------------------------------------- 1 | package com.ing.designpattern02.solution; 2 | 3 | public class Sut extends Eklenti { 4 | 5 | public Sut(Icecek icecek) { 6 | this.icecek = icecek; 7 | } 8 | 9 | @Override 10 | public String getAciklama() { 11 | return icecek.getAciklama() + " sutlu"; 12 | } 13 | 14 | @Override 15 | double fiyatHesapla() { 16 | return this.icecek.fiyatHesapla()+ 1.5; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question01/NutritionFacts.java: -------------------------------------------------------------------------------- 1 | package com.ing.question01; 2 | 3 | /** 4 | * Altug Bilgin Altintas 5 | *

6 | * Effective Java Workshop 7 | */ 8 | 9 | /* 10 | TODO: 11 | 1 - Initialize (create) the class according to the fields below 12 | 2 - Instructor will show the solutions 13 | 3 - Refactor and ask questions 14 | */ 15 | public class NutritionFacts { 16 | 17 | private final int servingSize; // must 18 | private final int servings; // must 19 | private final int calories; // optional 20 | private final int fat; // optional 21 | private final int sodium; // optional 22 | private final int carbohydrate; // optional 23 | 24 | private NutritionFacts(Builder builder) { 25 | servingSize = builder.servingSize; 26 | servings = builder.servings; 27 | calories = builder.calories; 28 | fat = builder.fat; 29 | sodium = builder.sodium; 30 | carbohydrate = builder.carbohydrate; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "NutritionFacts{" + 36 | "servingSize=" + servingSize + 37 | ", servings=" + servings + 38 | ", calories=" + calories + 39 | ", fat=" + fat + 40 | ", sodium=" + sodium + 41 | ", carbohydrate=" + carbohydrate + 42 | '}'; 43 | } 44 | 45 | public static class Builder { 46 | // Required parameters 47 | private final int servingSize; 48 | private final int servings; 49 | 50 | // Optional parameters - initialized to default values 51 | private int calories = 0; 52 | private int fat = 0; 53 | private int carbohydrate = 0; 54 | private int sodium = 0; 55 | 56 | public Builder(int servingSize, int servings) { 57 | this.servingSize = servingSize; 58 | this.servings = servings; 59 | } 60 | 61 | public Builder calories(int val) { 62 | calories = val; 63 | return this; 64 | } 65 | 66 | public Builder fat(int val) { 67 | fat = val; 68 | return this; 69 | } 70 | 71 | public Builder carbohydrate(int val) { 72 | carbohydrate = val; 73 | return this; 74 | } 75 | 76 | public Builder sodium(int val) { 77 | sodium = val; 78 | return this; 79 | } 80 | 81 | public NutritionFacts build() { 82 | return new NutritionFacts(this); 83 | } 84 | 85 | 86 | } 87 | 88 | public static void main(String[] args) { 89 | 90 | NutritionFacts cocaCola = new Builder(23, 45).calories(50).fat(100).build(); 91 | 92 | 93 | System.out.println(cocaCola); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question02/Roman.java: -------------------------------------------------------------------------------- 1 | package com.ing.question02; 2 | 3 | 4 | /** 5 | * Altug Bilgin Altintas 6 | * 7 | * Effective Java Workshop 8 | * 9 | */ 10 | 11 | import java.util.concurrent.TimeUnit; 12 | import java.util.regex.Pattern; 13 | 14 | /**TODO 15 | * 1- Measure the performance of the main() method in MICROSECONDS 16 | * 2 - Can you make isRomanNumeral() method faster ? 17 | */ 18 | public class Roman { 19 | 20 | 21 | private static final Pattern ROMAN = Pattern.compile( 22 | "^(?=.)M*(C[MD]|D?C{0,3})" 23 | + "(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$"); 24 | 25 | static boolean isRomanNumeral(String s) { 26 | return ROMAN.matcher(s).matches(); 27 | } 28 | 29 | 30 | public static void main(String[] args) { 31 | long t1 = System.nanoTime(); 32 | System.out.println(isRomanNumeral("MCMLXXVI")); 33 | long d = System.nanoTime() - t1; 34 | System.out.println("time " + TimeUnit.MICROSECONDS.convert(d, TimeUnit.NANOSECONDS)); 35 | 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question02/Sum.java: -------------------------------------------------------------------------------- 1 | package com.ing.question02; 2 | 3 | 4 | import java.util.concurrent.TimeUnit; 5 | 6 | /** 7 | * Altug Bilgin Altintas 8 | * 9 | * Effective Java Workshop 10 | * 11 | */ 12 | 13 | /* 14 | TODO 15 | 1 - Measure the performance of the main() method in MILLISECONDS 16 | 2 - Discussion : Did you feel any abnormality about the performance ? 17 | 3 - Instructor will show the solutions 18 | 4 - Refactor 19 | */ 20 | public class Sum { 21 | private static long sum() { 22 | long sum = 0L; 23 | // int , double, long, float, short, boolean, char, byte 24 | // Integer, Double Long, Float, Short, Boolean, Char, Byte 25 | 26 | for (long i = 0; i <= Integer.MAX_VALUE; i++) 27 | sum += i; // autoboxing 28 | return sum; 29 | } 30 | 31 | public static void main(String[] args) { 32 | 33 | long start = System.nanoTime(); // always use nanotTime for accuracy 34 | sum(); 35 | long end = System.nanoTime(); 36 | System.out.println( TimeUnit.MILLISECONDS.convert( (end - start), TimeUnit.NANOSECONDS) + " ms.."); 37 | 38 | 39 | 40 | } 41 | } -------------------------------------------------------------------------------- /src/main/java/com/ing/question03/Adult.java: -------------------------------------------------------------------------------- 1 | package com.ing.question03; 2 | 3 | /** 4 | * Altug Bilgin Altintas 5 | * 6 | * Effective Java Workshop 7 | * 8 | * Twitter : @altugaltintas 9 | */ 10 | 11 | 12 | /* 13 | TODO: 14 | 1 - Story : Let’s assume that rooms must be cleaned before they are reclaimed. 15 | 2 - How do you clean the Room - Create Room and clean numJunkPiles - 5 mins 16 | 3 - Instructor will show the solutions 17 | 4 - Refactor and ask questions 18 | */ 19 | 20 | public class Adult { 21 | public static void main(String[] args) { 22 | 23 | try(Room room = new Room(10)){ 24 | System.out.println("Temizlik basliyorrrr"); // clean numJunkPiles to zero 25 | }; 26 | 27 | 28 | //Clean the room automagically 29 | 30 | 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question03/Room.java: -------------------------------------------------------------------------------- 1 | package com.ing.question03; 2 | 3 | 4 | /** 5 | * Altug Bilgin Altintas 6 | * 7 | * Effective Java Workshop 8 | * 9 | */ 10 | 11 | public final class Room implements AutoCloseable{ 12 | 13 | int numJunkPiles; // Number of junk piles in this room 14 | 15 | public Room(int numJunkPiles) { 16 | this.numJunkPiles = numJunkPiles; 17 | } 18 | 19 | 20 | @Override 21 | public void close() { 22 | this.numJunkPiles = 0; 23 | System.out.println(" **** Number of Junk : s" + this.numJunkPiles); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question04/PhoneNumber.java: -------------------------------------------------------------------------------- 1 | package com.ing.question04; 2 | 3 | import java.util.*; 4 | 5 | /** 6 | * Altug Bilgin Altintas 7 | * 8 | * Effective Java Workshop 9 | * 10 | */ 11 | 12 | /* 13 | TODO: 14 | 1 - Run the app. 15 | 2 - Any abnormality ? If yes try fix it - 5 mins 16 | 3 - Instructor will show the solutions 17 | 4 - Refactor and ask questions 18 | */ 19 | public final class PhoneNumber { 20 | private final short areaCode, prefix, lineNum; 21 | 22 | public PhoneNumber(int areaCode, int prefix, int lineNum) { 23 | this.areaCode = rangeCheck(areaCode, 999, "area code"); 24 | this.prefix = rangeCheck(prefix, 999, "prefix"); 25 | this.lineNum = rangeCheck(lineNum, 9999, "line num"); 26 | } 27 | 28 | private static short rangeCheck(int val, int max, String arg) { 29 | if (val < 0 || val > max) 30 | throw new IllegalArgumentException(arg + ": " + val); 31 | return (short) val; 32 | } 33 | 34 | @Override 35 | public int hashCode() { 36 | return 42; 37 | } 38 | 39 | @Override public boolean equals(Object o) { 40 | if (o == this) 41 | return true; 42 | if (!(o instanceof PhoneNumber)) 43 | return false; 44 | PhoneNumber pn = (PhoneNumber)o; 45 | return pn.lineNum == lineNum && pn.prefix == prefix 46 | && pn.areaCode == areaCode; 47 | } 48 | 49 | 50 | 51 | public static void main(String[] args) { 52 | 53 | 54 | Map m = new HashMap<>(); 55 | m.put(new PhoneNumber(707, 867, 5309), "Jenny"); 56 | System.out.println(m.get(new PhoneNumber(707, 867, 5309))); 57 | } 58 | } -------------------------------------------------------------------------------- /src/main/java/com/ing/question05/Sort.java: -------------------------------------------------------------------------------- 1 | package com.ing.question05; 2 | 3 | import java.util.Arrays; 4 | import java.util.Collections; 5 | import java.util.Comparator; 6 | import java.util.List; 7 | 8 | import static java.util.Comparator.comparingInt; 9 | 10 | /** 11 | * Altug Bilgin Altintas 12 | *

13 | * Effective Java Workshop 14 | */ 15 | 16 | /* 17 | TODO: 18 | 1 - Run the app like this ex: java Sort Istanbul Tokyo 19 | 2 - Sort the words according to length() of the String 20 | 3 - Instructor will show the solutions 21 | 4 - Refactor and ask questions 22 | */ 23 | public class Sort { 24 | 25 | // Sort according to lenght of the String 26 | public static void main(String[] args) { 27 | List words = Arrays.asList(args); 28 | System.out.println(words); 29 | 30 | // Anonymous class instance as a function object - obsolete! 31 | /* 32 | Collections.sort(words, new Comparator() { 33 | public int compare(String s1, String s2) { 34 | return Integer.compare(s1.length(), s2.length()); 35 | } 36 | });*/ 37 | 38 | 39 | // Lambda expression as function object (replaces anonymous class) 40 | //Collections.sort(words, 41 | // (s1, s2) -> Integer.compare(s1.length(), s2.length())); 42 | 43 | 44 | // Collections.sort(words, comparingInt(String::length)); 45 | 46 | words.sort(comparingInt(String::length)); 47 | 48 | System.out.println(words); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question06/Complex.java: -------------------------------------------------------------------------------- 1 | package com.ing.question06; 2 | 3 | 4 | /** 5 | * Altug Bilgin Altintas 6 | * 7 | * Effective Java 8 | */ 9 | 10 | 11 | /* 12 | TODO: 13 | 1 - Implement subtract(..) method 14 | 2 - Instructor will show the solutions 15 | 3 - Refactor and ask questions 16 | */ 17 | public final class Complex { 18 | private final double re; 19 | private final double im; 20 | 21 | private Complex(double re, double im) { 22 | this.re = re; 23 | this.im = im; 24 | } 25 | 26 | public static Complex valueOf(double re, double im) { 27 | return new Complex(re, im); 28 | } 29 | 30 | public static Complex valueOfPolar(double r, double theta) { 31 | return new Complex(r * Math.cos(theta), r * Math.sin(theta)); 32 | } 33 | 34 | public static final Complex ZERO = new Complex(0, 0); 35 | public static final Complex ONE = new Complex(1, 0); 36 | public static final Complex I = new Complex(0, 1); 37 | 38 | // Accessors with no corresponding mutators 39 | public double realPart() { 40 | return re; 41 | } 42 | 43 | public double imaginaryPart() { 44 | return im; 45 | } 46 | 47 | public Complex add(Complex c) { 48 | return new Complex(re + c.re, im + c.im); 49 | } 50 | 51 | // TODO implement subtract 52 | 53 | 54 | public Complex multiply(Complex c) { 55 | return new Complex(re * c.re - im * c.im, re * c.im + im * c.re); 56 | } 57 | 58 | public Complex divide(Complex c) { 59 | double tmp = c.re * c.re + c.im * c.im; 60 | return new Complex((re * c.re + im * c.im) / tmp, (im * c.re - re 61 | * c.im) 62 | / tmp); 63 | } 64 | 65 | @Override 66 | public boolean equals(Object o) { 67 | if (o == this) 68 | return true; 69 | if (!(o instanceof Complex)) 70 | return false; 71 | Complex c = (Complex) o; 72 | 73 | 74 | return Double.compare(re, c.re) == 0 && Double.compare(im, c.im) == 0; 75 | } 76 | 77 | @Override 78 | public int hashCode() { 79 | int result = 17 + hashDouble(re); 80 | result = 31 * result + hashDouble(im); 81 | return result; 82 | } 83 | 84 | private int hashDouble(double val) { 85 | long longBits = Double.doubleToLongBits(re); 86 | return (int) (longBits ^ (longBits >>> 32)); 87 | } 88 | 89 | @Override 90 | public String toString() { 91 | return "(" + re + " + " + im + "i)"; 92 | } 93 | } -------------------------------------------------------------------------------- /src/main/java/com/ing/question07/Sub.java: -------------------------------------------------------------------------------- 1 | package com.ing.question07; 2 | 3 | 4 | /** 5 | * Altug Bilgin Altintas 6 | * 7 | * Effective Java Workshop 8 | * 9 | */ 10 | 11 | 12 | /* 13 | TODO: 14 | 1 - Run the app. 15 | 2 - Any abnormality ? If yes try fix it - 5 mins 16 | 3 - Instructor will show the solutions 17 | 4 - Refactor and ask questions 18 | */ 19 | import java.math.BigDecimal; 20 | import java.time.Instant; 21 | 22 | public final class Sub extends Super { 23 | 24 | // Blank final, set by constructor 25 | private final Instant instant ; 26 | 27 | Sub() { 28 | instant = Instant.now(); 29 | } 30 | 31 | // Overriding method invoked by superclass constructor 32 | @Override 33 | public void overrideMe() { 34 | System.out.println(instant); 35 | } 36 | 37 | public static void main(String[] args) { 38 | Sub sub = new Sub(); 39 | sub.overrideMe(); 40 | 41 | } 42 | } -------------------------------------------------------------------------------- /src/main/java/com/ing/question07/Super.java: -------------------------------------------------------------------------------- 1 | package com.ing.question07; 2 | 3 | 4 | /** 5 | * Altug Bilgin Altintas 6 | 7 | * Effective Java 8 | */ 9 | 10 | public abstract class Super { 11 | 12 | public Super() { 13 | overrideMe(); 14 | } 15 | public abstract void overrideMe() ; 16 | } -------------------------------------------------------------------------------- /src/main/java/com/ing/question08/Figure.java: -------------------------------------------------------------------------------- 1 | package com.ing.question08; 2 | 3 | /** 4 | * Altug Bilgin Altintas 5 | * 6 | * Effective Java Workshop 7 | * 8 | * Twitter : @altugaltintas 9 | */ 10 | 11 | 12 | /* 13 | TODO: 14 | 1 - Calculate SQUARE's area in the same code - 5 min 15 | 2 - Is this class compatible with SOLID principles ? 16 | 3 - Try to refactor according to SOLID principles - 5 mins 17 | 4 - Instructor will show the solutions 18 | 5 - Refactor and ask questions 19 | */ 20 | class Figure { 21 | enum Shape { RECTANGLE, CIRCLE, SQUARE, UCGEN }; 22 | 23 | // Tag field - the shape of this figure 24 | final Shape shape; 25 | 26 | // These fields are used only if shape is RECTANGLE 27 | double length; 28 | double width; 29 | 30 | // This field is used only if shape is CIRCLE 31 | double radius; 32 | 33 | // Constructor for circle 34 | Figure(double radius) { 35 | shape = Shape.CIRCLE; 36 | this.radius = radius; 37 | } 38 | 39 | // Constructor for rectangle 40 | Figure(double length, double width) { 41 | shape = Shape.RECTANGLE; 42 | this.length = length; 43 | this.width = width; 44 | } 45 | 46 | Figure(double length, boolean isSquare) { 47 | shape = Shape.SQUARE; 48 | this.length = length; 49 | //this.width = length; 50 | } 51 | 52 | double area() { 53 | switch(shape) { 54 | case RECTANGLE: 55 | return length * width; 56 | case CIRCLE: 57 | return Math.PI * (radius * radius); 58 | case SQUARE: 59 | return length * length; 60 | 61 | default: 62 | throw new AssertionError(shape); 63 | } 64 | } 65 | 66 | public static void main(String[] args) { 67 | Figure figure = new Figure(3, false); 68 | System.out.println(figure.area()); 69 | 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question08/fixed/Circle.java: -------------------------------------------------------------------------------- 1 | package com.ing.question08.fixed; 2 | 3 | class Circle extends Figure implements AutoCloseable { 4 | final double radius; 5 | 6 | Circle(double radius) { 7 | this.radius = radius; 8 | } 9 | 10 | @Override 11 | public String toString() { 12 | return "Circle{" + 13 | "radius=" + radius + 14 | '}'; 15 | } 16 | 17 | double area() { 18 | return Math.PI * (radius * radius); 19 | } 20 | 21 | @Override 22 | public void close() throws Exception { 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question08/fixed/Figure.java: -------------------------------------------------------------------------------- 1 | package com.ing.question08.fixed; 2 | 3 | abstract class Figure { 4 | abstract double area(); 5 | 6 | 7 | 8 | public void calculate() { 9 | System.out.println("hesaplaniyor..."); 10 | } 11 | } -------------------------------------------------------------------------------- /src/main/java/com/ing/question08/fixed/Main.java: -------------------------------------------------------------------------------- 1 | package com.ing.question08.fixed; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Figure figure = new Circle(2); 7 | System.out.println(" Area : " + figure.area()); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question08/fixed/Rectangle.java: -------------------------------------------------------------------------------- 1 | package com.ing.question08.fixed; 2 | 3 | class Rectangle extends Figure { 4 | final double length; 5 | final double width; 6 | 7 | Rectangle(double length, double width) { 8 | this.length = length; 9 | this.width = width; 10 | } 11 | 12 | @Override 13 | public String toString() { 14 | return super.toString(); 15 | } 16 | 17 | double area() { 18 | return length * width; 19 | } 20 | } -------------------------------------------------------------------------------- /src/main/java/com/ing/question08/fixed/Square.java: -------------------------------------------------------------------------------- 1 | package com.ing.question08.fixed; 2 | 3 | class Square extends Rectangle { 4 | Square(double side) { 5 | super(side, side); 6 | } 7 | 8 | @Override 9 | public String toString() { 10 | return "Square{" + 11 | "length=" + length + 12 | ", width=" + width + 13 | '}'; 14 | } 15 | } -------------------------------------------------------------------------------- /src/main/java/com/ing/question09/App.java: -------------------------------------------------------------------------------- 1 | package com.ing.question09; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * Altug Bilgin Altintas 7 | * 8 | * Effective Java Workshop 9 | * 10 | * Twitter : @altugaltintas 11 | */ 12 | 13 | 14 | /* 15 | TODO 16 | 1 - Run the app. 17 | 2 - Any abnormality ? If yes try fix it - 5 mins 18 | 3 - Instructor will show the solutions 19 | 4 - Refactor and ask questions 20 | */ 21 | public class App 22 | { 23 | public static void main( String[] args ) 24 | { 25 | Date start = new Date(); 26 | Date end = new Date(); // 2021 27 | Period p = new Period(start, end); // 2021 28 | System.out.println(p.end()); // 2021 29 | end.setYear(78); // Modifies internals of p! 30 | System.out.println(p.end()); // 1978 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question09/Period.java: -------------------------------------------------------------------------------- 1 | package com.ing.question09; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * Altug Bilgin Altintas 7 | * 8 | * Effective Java Workshop 9 | * 10 | * Twitter : @altugaltintas 11 | */ 12 | 13 | public final class Period { 14 | private final Date start; 15 | private final Date end; 16 | 17 | /** 18 | * @param start the beginning of the period 19 | * @param end the end of the period; must not precede start 20 | * @throws IllegalArgumentException if start is after end 21 | * @throws NullPointerException if start or end is null 22 | */ 23 | public Period(Date start, Date end) { 24 | 25 | if (start.compareTo(end) > 0) 26 | throw new IllegalArgumentException( 27 | start + " after " + end); 28 | this.start = new Date(start.getTime()); 29 | this.end = new Date(end.getTime()); 30 | 31 | 32 | 33 | } 34 | 35 | public Date start() { 36 | return start; 37 | } 38 | 39 | public Date end() { 40 | return end; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question10/Avoid.java: -------------------------------------------------------------------------------- 1 | package com.ing.question10; 2 | 3 | /** 4 | * Altug Bilgin Altintas 5 | * 6 | * Effective Java 7 | * 8 | * 9 | */ 10 | public class Avoid { 11 | 12 | public static void main(String[] args) { 13 | 14 | System.out.println(1.03 - 0.42); 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question10/Money.java: -------------------------------------------------------------------------------- 1 | package com.ing.question10; 2 | 3 | 4 | import java.math.BigDecimal; 5 | 6 | /** 7 | * Altug Bilgin Altintas 8 | * 9 | * Effective Java Workshop 10 | * 11 | * Twitter : @altugaltintas 12 | */ 13 | 14 | /* 15 | TODO: 16 | 1 - Show presentation in order to understand the question well - http://tiny.cc/cnrccz 17 | 2 - Run the app. 18 | 3 - Any abnormality ? If yes try fix it - 5 mins 19 | 4 - Instructor will show the solutions 20 | 5 - Refactor and ask questions 21 | */ 22 | 23 | public class Money { 24 | 25 | public static void main(String[] args) { 26 | BigDecimal funds = new BigDecimal("1.00"); 27 | BigDecimal NOTZERO = new BigDecimal("0.10"); 28 | int itemsBought = 0; 29 | for (BigDecimal price = NOTZERO; funds.compareTo(price) >= 0; 30 | price= price.add(NOTZERO) ) { 31 | funds = funds.subtract(price) ; 32 | itemsBought++; 33 | } 34 | System.out.println(itemsBought + " items bought."); 35 | System.out.println("Change: $" + funds); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question11/ReadAndWriteFile.java: -------------------------------------------------------------------------------- 1 | package com.ing.question11; 2 | 3 | import java.io.*; 4 | 5 | /** 6 | * Altug Bilgin Altintas 7 | * 8 | * Effective Java Workshop 9 | * 10 | * 11 | * Twitter : @altugaltintas 12 | */ 13 | 14 | /* 15 | TODO: 16 | 1 - Simply the code - 5 mins 17 | 2 - Instructor will show the solutions 18 | 3 - Refactor and ask questions 19 | */ 20 | public class ReadAndWriteFile { 21 | 22 | private static final int BUFFER_SIZE = 8 * 1024; 23 | 24 | static void copy(String src, String dst) throws IOException { 25 | InputStream in = new FileInputStream(src); 26 | try { 27 | OutputStream out = new FileOutputStream(dst); 28 | try { 29 | byte[] buf = new byte[BUFFER_SIZE]; 30 | int n; 31 | while ((n = in.read(buf)) >= 0) 32 | out.write(buf, 0, n); 33 | } finally { 34 | out.close(); 35 | } 36 | } finally { 37 | in.close(); 38 | } 39 | } 40 | 41 | public static void main(String[] args) throws IOException { 42 | String src = args[0]; 43 | String dst = args[1]; 44 | copy(src, dst); 45 | } 46 | } -------------------------------------------------------------------------------- /src/main/java/com/ing/question11/ReadFile.java: -------------------------------------------------------------------------------- 1 | package com.ing.question11; 2 | 3 | import java.io.*; 4 | 5 | 6 | /** 7 | * Altug Bilgin Altintas 8 | * 9 | * Effective Java Workshop 10 | * 11 | * 12 | * Twitter : @altugaltintas 13 | */ 14 | 15 | 16 | /* 17 | TODO: 18 | 1 - Ugly code please simplify the code - 5 mins 19 | 2 - Instructor will show the solutions 20 | 3 - Refactor and ask questions 21 | */ 22 | public class ReadFile { 23 | 24 | static String firstLineOfFile(String path) throws IOException { 25 | BufferedReader br = new BufferedReader(new FileReader(path)); 26 | try { 27 | return br.readLine(); 28 | } finally { 29 | br.close(); 30 | } 31 | } 32 | 33 | public static void main(String[] args) throws IOException { 34 | String path = args[0]; 35 | System.out.println(firstLineOfFile(path)); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question12/Card.java: -------------------------------------------------------------------------------- 1 | package com.ing.question12; 2 | 3 | /** 4 | * Altug Bilgin Altintas 5 | * 6 | * Effective Java Workshop 7 | * 8 | * 9 | * Twitter : @altugaltintas 10 | */ 11 | 12 | public class Card { 13 | 14 | 15 | enum Suit { CLUB, DIAMOND, HEART, SPADE } 16 | enum Rank { ACE, DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, 17 | NINE, TEN, JACK, QUEEN, KING } 18 | 19 | 20 | private final Suit suit ; 21 | private final Rank rank ; 22 | 23 | 24 | public Card(Suit suit, Rank rank ) { 25 | this.suit = suit; 26 | this.rank = rank; 27 | } 28 | 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question12/Symptom.java: -------------------------------------------------------------------------------- 1 | package com.ing.question12; 2 | 3 | 4 | import java.util.*; 5 | 6 | 7 | /** 8 | * Altug Bilgin Altintas 9 | * 10 | * Effective Java Workshop 11 | * 12 | * 13 | * Twitter : @altugaltintas 14 | */ 15 | 16 | /* 17 | TODO: 18 | 1 - Run the app. 19 | 2 - Any abnormality ? If yes try fix it - 5 mins 20 | 3 - Instructor will show the solutions 21 | 4 - Refactor and ask questions 22 | */ 23 | public class Symptom { 24 | 25 | static Collection suits = Arrays.asList(Card.Suit.values()); 26 | static Collection ranks = Arrays.asList(Card.Rank.values()); 27 | static List deck = new ArrayList<>(); 28 | 29 | public static void main(String[] args) { 30 | 31 | 32 | List deck = new ArrayList<>(); 33 | 34 | for (Iterator i = suits.iterator(); i.hasNext(); ) 35 | for (Iterator j = ranks.iterator(); j.hasNext(); ) 36 | deck.add(new Card(i.next(), j.next())); 37 | 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question13/InstrumentedHashSet.java: -------------------------------------------------------------------------------- 1 | package com.ing.question13; 2 | 3 | import java.util.Collection; 4 | import java.util.HashSet; 5 | import java.util.List; 6 | 7 | /* 8 | TODO 9 | 1 - Run the app. 10 | 2 - Any abnormality ? If yes try fix it - 5 mins 11 | 3 - Instructor will show the solutions 12 | 4 - Refactor and ask questions 13 | */ 14 | 15 | public class InstrumentedHashSet extends HashSet { 16 | // The number of attempted element insertions 17 | private int addCount = 0; 18 | 19 | public InstrumentedHashSet() { 20 | } 21 | 22 | public InstrumentedHashSet(int initCap, float loadFactor) { 23 | super(initCap, loadFactor); 24 | } 25 | 26 | @Override public boolean add(E e) { 27 | addCount++; 28 | return super.add(e); 29 | } 30 | 31 | @Override public boolean addAll(Collection c) { 32 | addCount += c.size(); 33 | return super.addAll(c); 34 | } 35 | 36 | public int getAddCount() { 37 | return addCount; 38 | } 39 | 40 | public static void main(String[] args) { 41 | InstrumentedHashSet s = new InstrumentedHashSet<>(); 42 | s.addAll(List.of("Snap", "Crackle", "Pop")); 43 | System.out.println(s.getAddCount()); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question14/AreaCalculator.java: -------------------------------------------------------------------------------- 1 | package com.ing.question14; 2 | 3 | 4 | 5 | /* 6 | TODO 7 | 1 - Calculate the area of a circle (A = π x r2 ) using PhysicalConstants.PI. 8 | 2 - What is not good is that process ? 9 | 3 - Instructor will show the solutions 10 | 4 - Calculate the energy (J) in 5 KG uranium ? 11 | Formula = E = MxC^2 12 | C = speed of light (m/s) = 3.0x10^8 m/s 13 | */ 14 | 15 | public class AreaCalculator { 16 | 17 | 18 | 19 | public static void main(String[] args) { 20 | 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question14/PhysicalConstants.java: -------------------------------------------------------------------------------- 1 | package com.ing.question14; 2 | 3 | 4 | public interface PhysicalConstants { 5 | 6 | static final double PI = 3.14159 ; 7 | 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question15/Dessert.java: -------------------------------------------------------------------------------- 1 | package com.ing.question15; 2 | 3 | /* 4 | class Utensil { 5 | static final String NAME = "pot"; 6 | } 7 | 8 | class Dessert { 9 | static final String NAME = "pie"; 10 | } 11 | 12 | */ 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question15/Main.java: -------------------------------------------------------------------------------- 1 | package com.ing.question15; 2 | 3 | 4 | /* 5 | TODO 6 | 1 - Go to Dessert.java and remove the comment characters 7 | 2 - Just compile Dessert.java and Main.java and try to Run Main class 8 | 3 - Just compile Utensil.java and Main.java and try to Run Main class 9 | 4 - What is the lesson here ? 10 | */ 11 | public class Main { 12 | public static void main(String[] args) { 13 | System.out.println(Utensil.NAME + Dessert.NAME); 14 | } 15 | 16 | 17 | 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question15/Utensil.java: -------------------------------------------------------------------------------- 1 | package com.ing.question15; 2 | 3 | class Utensil { 4 | static final String NAME = "pan"; 5 | } 6 | 7 | class Dessert { 8 | static final String NAME = "cake"; 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question16/Rawetallica.java: -------------------------------------------------------------------------------- 1 | package com.ing.question16; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | 7 | /* 8 | TODO 9 | 1 - Run the Rawetallica app 10 | 2 - Any abnormality ? Please try to fix it 11 | 3 - Instructor will show the solutions 12 | 4 - What is the lesson ? 13 | */ 14 | 15 | public class Rawetallica { 16 | 17 | public static void main(String[] args) { 18 | List strings = new ArrayList<>(); 19 | add(strings, 2.45); 20 | double s = strings.get(0); // Has compiler-generated cast 21 | System.out.println(s + " sss "); 22 | } 23 | 24 | private static void add(List list, T o) { 25 | list.add(o); 26 | } 27 | 28 | 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question17/Guess.java: -------------------------------------------------------------------------------- 1 | package com.ing.question17; 2 | 3 | import java.util.Collection; 4 | import java.util.List; 5 | import java.util.Random; 6 | import java.util.concurrent.ThreadLocalRandom; 7 | 8 | 9 | /* 10 | TODO 11 | 1 - Run the Guess app 12 | 2 - Any abnormality ? Please try to fix it 13 | 3 - Instructor will show the solutions 14 | 4 - What is the lesson ? 15 | */ 16 | 17 | public class Guess { 18 | private final Object[] choiceArray; 19 | 20 | public Guess(Collection choices) { 21 | choiceArray = choices.toArray(); 22 | } 23 | public Object choose() { 24 | Random rnd = ThreadLocalRandom.current(); 25 | return choiceArray[rnd.nextInt(choiceArray.length)]; 26 | 27 | } 28 | 29 | public static void main(String[] args) { 30 | List intList = List.of(1, 2, 3, 4, 5, 6); 31 | 32 | List stringList = List.of("one", "two", "three", "four", "five", "six"); 33 | 34 | Guess guess = new Guess(intList); 35 | 36 | for (int i = 0; i < 10; i++) { 37 | String choice = (String) guess.choose(); 38 | System.out.println(choice); 39 | } 40 | 41 | guess = new Guess(stringList); 42 | 43 | for (int i = 0; i < 10; i++) { 44 | String choice = (String) guess.choose(); 45 | System.out.println(choice); 46 | } 47 | 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question18/EmptyStackException.java: -------------------------------------------------------------------------------- 1 | package com.ing.question18; 2 | 3 | public class EmptyStackException extends RuntimeException { 4 | } 5 | 6 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question18/Stack.java: -------------------------------------------------------------------------------- 1 | package com.ing.question18; 2 | 3 | 4 | import java.util.Arrays; 5 | 6 | /* 7 | TODO 8 | 1 - Convert Stack.java into type safe mode - Generics 9 | 2 - What is the lesson ? 10 | */ 11 | 12 | public class Stack { 13 | public Object[] elements; 14 | private int size = 0; 15 | private static final int DEFAULT_INITIAL_CAPACITY = 16; 16 | 17 | 18 | public Stack() { 19 | elements = new Object[DEFAULT_INITIAL_CAPACITY]; 20 | } 21 | 22 | public void push(Object e) { 23 | ensureCapacity(); 24 | elements[size++] = e; 25 | } 26 | 27 | public Object pop() { 28 | if (size == 0) { 29 | throw new EmptyStackException(); 30 | } 31 | 32 | 33 | Object result = elements[--size]; 34 | elements[size] = null; 35 | return result; 36 | } 37 | 38 | /** 39 | * Ensure space for at least one more element, roughly doubling the capacity 40 | * each time the array needs to grow. 41 | */ 42 | private void ensureCapacity() { 43 | if (elements.length == size) { 44 | elements = Arrays.copyOf(elements, 2 * size + 1); 45 | } 46 | } 47 | 48 | 49 | public static void main(String[] args) { 50 | Stack stack = new Stack(); 51 | stack.push("hello"); // write 12 and run again 52 | 53 | String result = (String) stack.pop(); 54 | System.out.println(result.toUpperCase()); 55 | 56 | } 57 | } -------------------------------------------------------------------------------- /src/main/java/com/ing/question19/Combiner.java: -------------------------------------------------------------------------------- 1 | package com.ing.question19; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | 7 | /* 8 | TODO 9 | 1 - Compile Combiner.java 10 | 2 - There are 2 warnings, can you spot the warnings ? 11 | 3 - try to fix those 2 warnings. 12 | */ 13 | public class Combiner { 14 | 15 | public static Set combine(Set s1, Set s2) { 16 | Set result = new HashSet(s1); 17 | result.addAll(s2); 18 | return result; 19 | } 20 | 21 | // Simple program to exercise generic method 22 | public static void main(String[] args) { 23 | Set guys = Set.of("Tom", "Dick", "Harry"); 24 | Set ages = Set.of(44, 34, 21); 25 | Set aflCio = combine(guys, ages); 26 | System.out.println(aflCio); 27 | } 28 | } -------------------------------------------------------------------------------- /src/main/java/com/ing/question20/Hazardous.java: -------------------------------------------------------------------------------- 1 | package com.ing.question20; 2 | 3 | import java.util.List; 4 | 5 | 6 | /* 7 | TODO 8 | 1 - Compile Hazardous.java 9 | 2 - There is a 1 warning, can you spot the warning ? 10 | 3 - try to fix this 1 warning. 11 | */ 12 | public class Hazardous { 13 | 14 | public static void calculate(int... values) { 15 | System.out.println(values.length); 16 | } 17 | 18 | @SafeVarargs 19 | static void hazardous(List... stringLists) { 20 | List intList = List.of(42); 21 | Object[] objects = stringLists; 22 | objects[0] = intList; // Heap pollution 23 | Integer s = (Integer)stringLists[0].get(0); // ClassCastException 24 | 25 | } 26 | 27 | public static void main(String[] args) { 28 | hazardous(List.of("I love JUG Istanbul and Java Day Istanbul")); 29 | calculate(2,4,5,6,7,7); 30 | } 31 | } -------------------------------------------------------------------------------- /src/main/java/com/ing/question21/Calculator.java: -------------------------------------------------------------------------------- 1 | package com.ing.question21; 2 | 3 | 4 | public class Calculator { 5 | 6 | public static final double MERCURY_MASS = 3.302e+23; 7 | public static final double VENUS_MASS = 4.869e+24; 8 | public static final double EARTH_MASS = 5.975e+24; 9 | public static final double MARS_MASS = 6.419e+23; 10 | public static final double JUPITER_MASS = 1.899e+27; 11 | public static final double SATURN_MASS = 5.685e+26; 12 | public static final double URANUS_MASS = 8.683e+25; 13 | public static final double NEPTUNE_MASS = 1.024e+26; 14 | 15 | 16 | public static final double MERCURY_REDIUS = 2.439e6; 17 | public static final double VENUS_REDIUS = 6.052e6; 18 | public static final double EARTH_REDIUS = 6.378e6; 19 | public static final double MARS_REDIUS = 3.393e6; 20 | public static final double JUPITER_REDIUS = 7.149e7; 21 | public static final double SATURN_REDIUS = 6.027e7; 22 | public static final double URANUS_REDIUS = 2.556e7; 23 | public static final double NEPTUNE_REDIUS = 2.477e7; 24 | 25 | 26 | // Universal gravitational constant in m^3 / kg s^2 27 | private static final double G = 6.67300E-11; 28 | 29 | 30 | private static double convertToMass(double wightOfPerson) { 31 | 32 | double surfaceGravityOfEarth = G * EARTH_MASS / (EARTH_REDIUS * EARTH_REDIUS); 33 | return wightOfPerson / surfaceGravityOfEarth; 34 | } 35 | 36 | /** 37 | * 38 | * @param radius In meters 39 | * @param mass In kilograms 40 | * @return 41 | */ 42 | public static double calculateSurfaceWeight(double radius, double mass, double wightOfPerson) { 43 | 44 | wightOfPerson = convertToMass(wightOfPerson); 45 | 46 | double surfaceGravity; // In m / s^2 47 | surfaceGravity = G * mass / (radius * radius); 48 | return wightOfPerson * surfaceGravity; // F = ma 49 | } 50 | 51 | 52 | 53 | 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question21/Main.java: -------------------------------------------------------------------------------- 1 | package com.ing.question21; 2 | 3 | 4 | /** 5 | * TODO 6 | * 1 - How to calculate your weight on all eight planets - 5 minutes 7 | * 2 - Instructor will show the solutions 8 | * 3 - What is the lesson ? 9 | */ 10 | public class Main { 11 | 12 | public static void main(String[] args) { 13 | 14 | /** 15 | * If your Weight is 80 KG on Earth then what will be on Mars? Or Uranus ? ... 16 | */ 17 | double earthWeight = Double.parseDouble(args[0]); 18 | 19 | double result = Calculator. 20 | calculateSurfaceWeight(Calculator.JUPITER_REDIUS, 21 | Calculator.JUPITER_MASS, earthWeight); 22 | System.out.println("Weight on EARTH = " + result); 23 | 24 | /** 25 | * for (Planets p ...) { 26 | * ....... 27 | * System.out.printf ... 28 | * } 29 | */ 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question22/Main.java: -------------------------------------------------------------------------------- 1 | package com.ing.question22; 2 | 3 | 4 | /** 5 | * TODO 6 | * 1 - Run the Main method , can you see "Complex algorithm is working" ? 7 | * 2 - Add new element (ex : GUITARMAN) to MusicTeam, add at the beginning 8 | * 3 - Run the Main method , can you see "Complex algorithm is working" ? 9 | * 4 - How can we sure about the position of the elements ? 10 | */ 11 | public class Main { 12 | 13 | public static void main(String[] args) { 14 | System.out.println( MusicTeam.DUET.numberOfMusicians()); 15 | 16 | if (MusicTeam.DUET.numberOfMusicians() == 2) { 17 | System.out.println("Complex algorithm is working"); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question22/MusicTeam.java: -------------------------------------------------------------------------------- 1 | package com.ing.question22; 2 | 3 | public enum MusicTeam { 4 | SOLO, DUET, TRIO, QUARTET, QUINTET, 5 | SEXTET, SEPTET, OCTET, NONET, DECTET; 6 | public int numberOfMusicians() { return ordinal() + 1; } 7 | } -------------------------------------------------------------------------------- /src/main/java/com/ing/question23/Main.java: -------------------------------------------------------------------------------- 1 | package com.ing.question23; 2 | 3 | import static com.ing.question23.PrintedWork.STYLE_BOLD; 4 | 5 | 6 | /** 7 | * TODO 8 | * 1 - Can you apply STYLE_BOLD and STYLE_STRIKETHROUGH at the same time ? 9 | * 2 - What are the alternative options other than working with INTEGERS ? 10 | */ 11 | 12 | public class Main { 13 | 14 | public static void main(String[] args) { 15 | PrintedWork printedWork = new PrintedWork(); 16 | printedWork.applyStyles(STYLE_BOLD); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question23/PrintedWork.java: -------------------------------------------------------------------------------- 1 | package com.ing.question23; 2 | 3 | public class PrintedWork { 4 | 5 | public static final int STYLE_BOLD = 1 ; 6 | public static final int STYLE_ITALIC = 2 ; 7 | public static final int STYLE_UNDERLINE = 4 ; 8 | public static final int STYLE_STRIKETHROUGH = 8 ; 9 | // Parameter is bitwise OR of zero or more STYLE_ constants 10 | public void applyStyles(int styles) { 11 | System.out.println( styles + " are applied "); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question24/RunTests.java: -------------------------------------------------------------------------------- 1 | package com.ing.question24; 2 | 3 | 4 | 5 | import java.lang.reflect.InvocationTargetException; 6 | import java.lang.reflect.Method; 7 | 8 | 9 | /* 10 | TODO 11 | 12 | 1 - Run RunTests.java 13 | 2 - Expected to see 3 methods should pass 14 | 3 - Any abnormality ? 15 | 16 | */ 17 | public class RunTests { 18 | public static void main(String[] args) throws Exception { 19 | int tests = 0; 20 | int passed = 0; 21 | Class testClass = Sample.class ; 22 | 23 | for (Method m : testClass.getDeclaredMethods()) { 24 | if (m.getName().startsWith("test")) { 25 | tests++; 26 | try { 27 | m.invoke(null); 28 | passed++; 29 | } catch (InvocationTargetException wrappedExc) { 30 | Throwable exc = wrappedExc.getCause(); 31 | System.out.println(m + " failed: " + exc); 32 | } catch (Exception exc) { 33 | System.out.println("Invalid @Test: " + m); 34 | } 35 | } 36 | } 37 | System.out.printf("Passed: %d, Failed: %d%n", 38 | passed, tests - passed); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question24/Sample.java: -------------------------------------------------------------------------------- 1 | package com.ing.question24; 2 | 3 | 4 | public class Sample { 5 | 6 | public static void testM1() { } // Test should pass 7 | 8 | public static void tetsM2() { } // Test should pass 9 | 10 | public static void testM3() { } // Test should pass 11 | 12 | } -------------------------------------------------------------------------------- /src/main/java/com/ing/question25/NerfBasic.java: -------------------------------------------------------------------------------- 1 | package com.ing.question25; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | import java.util.Objects; 6 | 7 | /* 8 | TODO 9 | 10 | 1 - Run NerfBasic.java 11 | 2 - Any abnormality ? 12 | */ 13 | 14 | public class NerfBasic { 15 | 16 | 17 | protected double range; 18 | protected int dartInRows; 19 | 20 | public NerfBasic(double range, int dartInRows) { 21 | this.range = range; 22 | this.dartInRows = dartInRows; 23 | } 24 | 25 | 26 | public boolean equals(NerfBasic nerfBasic) { 27 | if (this == nerfBasic) return true; 28 | 29 | return Double.compare(nerfBasic.range, range) == 0 && 30 | dartInRows == nerfBasic.dartInRows; 31 | } 32 | 33 | 34 | 35 | public int hashCode() { 36 | return Objects.hashCode(range * dartInRows); 37 | } 38 | 39 | @Override 40 | public String toString() { 41 | return "NerfBasic{" + 42 | "range=" + range + 43 | ", dartInRows=" + dartInRows + 44 | '}'; 45 | } 46 | 47 | public static void main(String[] args) { 48 | Map m = new HashMap<>(); 49 | 50 | NerfBasic nerfBasic = new NerfBasic(4d, 6); 51 | NerfBasic nerfElite = new NerfBasic(6d, 3); 52 | NerfBasic nerfUltra = new NerfBasic(8d, 5); 53 | 54 | m.put(nerfBasic, "Nerf Basic"); 55 | m.put(nerfElite, "Nerf Elite"); 56 | m.put(nerfUltra, "Nerf Ultra"); 57 | System.out.println(m); 58 | 59 | System.out.println(" --> " + m.get(new NerfBasic(4d, 6))); 60 | 61 | } 62 | 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question26/Freq.java: -------------------------------------------------------------------------------- 1 | package com.ing.question26; 2 | 3 | import java.math.BigInteger; 4 | import java.util.stream.Stream; 5 | 6 | import static java.math.BigInteger.ONE; 7 | import static java.math.BigInteger.TWO; 8 | 9 | 10 | /* 11 | TODO 12 | 13 | 1 - Measure working time 14 | 2 - Can you make this app faster ? 15 | 16 | 17 | */ 18 | public class Freq { 19 | 20 | public static void main(String[] args) { 21 | 22 | primes().map(p -> TWO.pow(p.intValueExact()).subtract(ONE)) 23 | .filter(mersenne -> mersenne.isProbablePrime(50)) 24 | .limit(20) 25 | .forEach(System.out::println); 26 | 27 | } 28 | 29 | static Stream primes() { 30 | return Stream.iterate(BigInteger.TWO, BigInteger::nextProbablePrime); 31 | } 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question26/StockCalculation.java: -------------------------------------------------------------------------------- 1 | package com.ing.question26; 2 | 3 | import java.math.BigDecimal; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | import java.util.stream.Stream; 7 | 8 | import static java.util.stream.Collectors.toList; 9 | 10 | 11 | /* 12 | TODO 13 | 14 | 1 - Measure working time 15 | 2 - Can you make this app faster ? 16 | 17 | */ 18 | 19 | public class StockCalculation { 20 | 21 | 22 | private List getStockInfo(Stream symbols) { 23 | return symbols 24 | .map(this::getStock) //slow network operation 25 | .collect(toList()); 26 | } 27 | 28 | private StockInfo getStock(String s) { 29 | 30 | //Network Operation Simulation 31 | try { 32 | Thread.sleep(10); 33 | } catch (InterruptedException e) { 34 | e.printStackTrace(); 35 | } 36 | 37 | StockInfo stockInfo = new StockInfo(s, new BigDecimal(Math.random()* 100000)); 38 | return stockInfo; 39 | 40 | } 41 | 42 | public static void main(String[] args) { 43 | StockCalculation stockCalculation = new StockCalculation(); 44 | 45 | List stockNames = new ArrayList() ; 46 | for (int i = 0; i <1000 ; i++) { 47 | stockNames.add("A" + i) ; 48 | } 49 | List stockInfos = stockCalculation.getStockInfo(stockNames.stream()); 50 | System.out.println(stockInfos); 51 | 52 | } 53 | 54 | 55 | 56 | 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question26/StockInfo.java: -------------------------------------------------------------------------------- 1 | package com.ing.question26; 2 | 3 | import java.math.BigDecimal; 4 | 5 | public final class StockInfo { 6 | 7 | private final String name ; 8 | private final BigDecimal value ; 9 | 10 | public StockInfo(String name, BigDecimal value) { 11 | this.name = name; 12 | this.value = value; 13 | } 14 | 15 | public String getName() { 16 | return name; 17 | } 18 | 19 | public BigDecimal getValue() { 20 | return value; 21 | } 22 | 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question27/BondCalculation.java: -------------------------------------------------------------------------------- 1 | package com.ing.question27; 2 | 3 | 4 | import java.math.BigDecimal; 5 | 6 | 7 | /* 8 | TODO 9 | 1 - Should we check if m is null or not ? If Yes how ? 10 | 11 | */ 12 | public class BondCalculation { 13 | 14 | /** 15 | * Returns a BigDecimal whose value is (this mod m). This method 16 | * always returns a non-negative BigDecimal. 17 | * 18 | * 19 | * @param m the modulus, which must be positive 20 | * @return Calculated Money Value 21 | * @throws ArithmeticException if m is less than or equal to 0 22 | */ 23 | public BigDecimal calculateTimeValueOfMoney(BigDecimal m) { 24 | 25 | /* 26 | Should we check if m is null or not ? If Yes how ? 27 | */ 28 | 29 | if (m.signum() <= 0) 30 | throw new ArithmeticException("Money Input <= 0: " + m); 31 | 32 | // calculation 33 | return new BigDecimal(1); 34 | 35 | } 36 | 37 | public static void main(String[] args) { 38 | 39 | BondCalculation bondCalculation = new BondCalculation(); 40 | 41 | bondCalculation.calculateTimeValueOfMoney(null); 42 | } 43 | 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question28/BMW.java: -------------------------------------------------------------------------------- 1 | package com.ing.question28; 2 | 3 | public class BMW extends Car { 4 | 5 | @Override 6 | public String sayModel() { 7 | return "BMW"; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question28/Car.java: -------------------------------------------------------------------------------- 1 | package com.ing.question28; 2 | 3 | public class Car { 4 | 5 | public String sayModel() { 6 | return "Just a Car"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question28/CarClassifier.java: -------------------------------------------------------------------------------- 1 | package com.ing.question28; 2 | 3 | 4 | import java.util.List; 5 | 6 | /** 7 | * 8 | * TODO 9 | * 10 | * 1 - Run CarClassifier.java 11 | * 2 - What does this program print? 12 | * 3 - How can you print "Car - BMW - Mercedes" ? 13 | */ 14 | public class CarClassifier { 15 | 16 | public String classify(Car car) { 17 | return "Car"; 18 | } 19 | 20 | 21 | public String classify(BMW bmw) { 22 | return "BMW"; 23 | } 24 | 25 | 26 | public String classify(Mercedes mercedes) { 27 | return "Mercedes"; 28 | } 29 | 30 | public static void main(String[] args) { 31 | List collections = List.of( 32 | new Car(), 33 | new Mercedes(), 34 | new BMW() 35 | ); 36 | 37 | CarClassifier carClassifier = new CarClassifier(); 38 | 39 | for (Car car : collections) 40 | System.out.println(carClassifier.classify(car)); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question28/Mercedes.java: -------------------------------------------------------------------------------- 1 | package com.ing.question28; 2 | 3 | public class Mercedes extends Car { 4 | 5 | @Override 6 | public String sayModel() { 7 | return "Mercedes"; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question28/SetList.java: -------------------------------------------------------------------------------- 1 | package com.ing.question28; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.Set; 6 | import java.util.TreeSet; 7 | 8 | /** 9 | * 10 | * TODO 11 | * 12 | * 1 - What does this program print? 13 | * 2 - How can you fix it ? 14 | * 15 | */ 16 | 17 | public class SetList { 18 | public static void main(String[] args) { 19 | Set set = new TreeSet<>(); 20 | List list = new ArrayList<>(); 21 | 22 | for (int i = -3; i < 3; i++) { 23 | set.add(i); 24 | list.add(i); 25 | } 26 | for (int i = 0; i < 3; i++) { 27 | set.remove(i); 28 | list.remove(i); 29 | } 30 | System.out.println(set + " " + list); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question29/NumberOperation.java: -------------------------------------------------------------------------------- 1 | package com.ing.question29; 2 | 3 | 4 | import java.util.List; 5 | 6 | /* 7 | TODO 8 | 1 - Run the NumberOperation.java 9 | 2 - Is this the best way for sum() and min() methods? 10 | 3 - Instructor will show the solutions 11 | 4 - What is the lesson ? 12 | */ 13 | public class NumberOperation { 14 | 15 | static int sum(List intList) { 16 | int sum = 0; 17 | for (int arg : intList) 18 | sum += arg; 19 | return sum; 20 | } 21 | 22 | 23 | static int min(Integer[] args) { 24 | if (args.length == 0) 25 | throw new IllegalArgumentException("Too few arguments"); 26 | int min = args[0]; 27 | for (int i = 1; i < args.length; i++) 28 | if (args[i] < min) 29 | min = args[i]; 30 | return min; 31 | } 32 | 33 | 34 | public static void main(String[] args) { 35 | System.out.println(sum(List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))); 36 | System.out.println(min(new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question30/Cheese.java: -------------------------------------------------------------------------------- 1 | package com.ing.question30; 2 | 3 | public class Cheese { 4 | 5 | enum Name { 6 | STILTON(1), 7 | EZINE(2), 8 | ERZINCAN(3); 9 | 10 | private int id; 11 | 12 | public int getId() { 13 | return id; 14 | } 15 | 16 | Name(int id) { 17 | this.id = id; 18 | } 19 | } 20 | 21 | 22 | private Long id; 23 | private String name; 24 | 25 | public Cheese(Long id, String name) { 26 | this.id = id; 27 | this.name = name; 28 | } 29 | 30 | public Long getId() { 31 | return id; 32 | } 33 | 34 | public String getName() { 35 | return name; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question30/CheeseController.java: -------------------------------------------------------------------------------- 1 | package com.ing.question30; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class CheeseController { 7 | 8 | 9 | private final List cheesesInStock = new ArrayList<>(); 10 | /** 11 | * @return a list containing all of the cheeses in the shop, 12 | * or null if no cheeses are available for purchase. 13 | */ 14 | public List getCheeses() { 15 | return cheesesInStock.isEmpty() ? null 16 | : cheesesInStock; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question30/Main.java: -------------------------------------------------------------------------------- 1 | package com.ing.question30; 2 | 3 | import java.util.List; 4 | 5 | /* 6 | TODO 7 | 8 | 1 - What improvements can be made to this class ? 9 | 2 - What is the lesson ? 10 | */ 11 | public class Main { 12 | 13 | public static void main(String[] args) { 14 | 15 | CheeseController shop = new CheeseController(); 16 | 17 | List cheeses = shop.getCheeses(); 18 | if (cheeses != null && cheeses.contains(Cheese.Name.STILTON)) 19 | System.out.println("Jolly good, just the thing."); 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question31/Max.java: -------------------------------------------------------------------------------- 1 | package com.ing.question31; 2 | 3 | import java.util.*; 4 | 5 | /* 6 | TODO 7 | 1 - Run the Max.java 8 | 2 - Is this the best way for max() method? 9 | 3 - Instructor will show the solutions 10 | 4 - What is the lesson ? 11 | */ 12 | public class Max { 13 | 14 | 15 | // Returns maximum value in collection - throws exception if empty 16 | public static String max(Collection collection) { 17 | if (collection == null || collection.isEmpty()) 18 | throw new IllegalArgumentException("Empty collection"); 19 | 20 | String result = null; 21 | for (String e : collection) { 22 | if (result == null || result.compareTo(e) > 0) 23 | result = Objects.requireNonNull(e); 24 | } 25 | 26 | 27 | return result; 28 | } 29 | 30 | 31 | 32 | public static void main(String[] args) { 33 | List words = Arrays.asList("100", "20", "5"); 34 | 35 | System.out.println(max(words)); 36 | 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question32/LordOfTheLoops.java: -------------------------------------------------------------------------------- 1 | package com.ing.question32; 2 | 3 | import java.util.Iterator; 4 | import java.util.List; 5 | 6 | /* 7 | TODO 8 | 1 - Run the LordOfTheLoops.java 9 | 2 - Did you spot the bug? 10 | */ 11 | public class LordOfTheLoops { 12 | 13 | public static void exec(List n, List a) { 14 | Iterator i = n.iterator(); 15 | while (i.hasNext()) { 16 | System.out.println(" --> " + i.next() ); 17 | } 18 | 19 | Iterator i2 = a.iterator(); 20 | while (i.hasNext()) { 21 | System.out.println(" --> " + i2.next()); 22 | } 23 | } 24 | 25 | public static void main(String[] args) { 26 | 27 | List names = List.of("Lurtz", "Saruman" , "Gollum" , "Boromir" , "Legolas"); 28 | List ages = List.of(40, 50, 160, 65, 1040); 29 | 30 | exec(names, ages); 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question33/RandomTest.java: -------------------------------------------------------------------------------- 1 | package com.ing.question33; 2 | 3 | import java.util.Random; 4 | 5 | /* 6 | TODO 7 | 1 - Run the RandomTest.java 8 | 2 - Did you spot the bug? 9 | */ 10 | public class RandomTest { 11 | 12 | public static void main(String[] args) { 13 | int sum = 0; 14 | Random rand = new Random(100); 15 | for(int x = 1; x < 100; x++){ 16 | int num = (rand.nextInt(x)) ; 17 | sum += num; 18 | System.out.println("Random number:" + num); 19 | } 20 | //value never changes with repeated program executions. 21 | System.out.println("Sum: " + sum); 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question34/Combine.java: -------------------------------------------------------------------------------- 1 | package com.ing.question34; 2 | 3 | 4 | /* 5 | TODO 6 | 1 - Measure time in microseconds of String concatenation operation 7 | 2 - What are the alternative approaches for String concatenation operation? 8 | */ 9 | 10 | public class Combine { 11 | 12 | 13 | public static void main(String[] args) { 14 | int count = 1000; 15 | String str = ""; 16 | 17 | for (int i = 0; i < count; i++) { 18 | str = str + "core"; // String concatenation 19 | } 20 | 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question35/Translate.java: -------------------------------------------------------------------------------- 1 | package com.ing.question35; 2 | 3 | 4 | /* 5 | TODO 6 | 1 - What is wrong in this class ? 7 | 8 | */ 9 | public class Translate { 10 | 11 | public static String processStrings(String[] strings) { 12 | 13 | int i = 0; 14 | String result = ""; 15 | try { 16 | while (true) { 17 | result = result.concat(processSingleString(strings[i])); 18 | i++; 19 | } 20 | } catch (ArrayIndexOutOfBoundsException e) { 21 | // Ignore, we're done 22 | } 23 | return result; 24 | } 25 | 26 | private static String processSingleString(String string) { 27 | 28 | return string + "*"; 29 | } 30 | 31 | public static void main(String[] args) { 32 | 33 | System.out.println(processStrings(args)); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question36/ReadFile.java: -------------------------------------------------------------------------------- 1 | package com.ing.question36; 2 | 3 | import java.io.*; 4 | 5 | 6 | /** 7 | * Altug Bilgin Altintas 8 | *

9 | * Effective Java Workshop 10 | *

11 | 12 | * Twitter : @altugaltintas 13 | */ 14 | 15 | 16 | /* 17 | TODO: 18 | 1 - What are the alternatives for copy() method 19 | */ 20 | public class ReadFile { 21 | 22 | private static final int BUFFER_SIZE = 8 * 1024; 23 | 24 | 25 | static void copy(String src, String dst) throws IOException { 26 | try (InputStream in = new FileInputStream(src); 27 | OutputStream out = new FileOutputStream(dst)) { 28 | 29 | byte[] buf = new byte[BUFFER_SIZE]; 30 | int n; 31 | while ((n = in.read(buf)) >= 0) { 32 | out.write(buf, 0, n); 33 | } 34 | 35 | } catch (IOException ex) { 36 | System.err.println(ex); 37 | throw ex; 38 | } 39 | 40 | 41 | } 42 | 43 | public static void main(String[] args) throws IOException{ 44 | String src = "a.txt"; // always gives error 45 | String dst = "b.txt"; // always gives error 46 | copy(src, dst); 47 | 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question37/Sorting.java: -------------------------------------------------------------------------------- 1 | package com.ing.question37; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.ListIterator; 6 | 7 | /** 8 | * 1 - How do you transform current Exception in to IndexOutOfBoundsException - 9 | * Exception Translation : 10 | * Convert LowerLevelException to HigherLevelException 11 | * 12 | * @param Type of the Element 13 | */ 14 | public class Sorting { 15 | 16 | private static List myList = new ArrayList(); 17 | 18 | public E get(int index) { 19 | ListIterator i = myList.listIterator(); 20 | return i.next(); 21 | } 22 | 23 | public static void main(String[] args) { 24 | Sorting sorting = new Sorting(); 25 | System.out.println(sorting.get(9)); 26 | } 27 | 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question38/StopThread.java: -------------------------------------------------------------------------------- 1 | package com.ing.question38; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | /** TODO 6 | * How long would you expect this program to run? 7 | */ 8 | public class StopThread { 9 | private static boolean stopRequested; 10 | 11 | private static synchronized void requestStop() { 12 | stopRequested = true; 13 | } 14 | 15 | private static synchronized boolean stopRequested() { 16 | return stopRequested; 17 | } 18 | 19 | public static void main(String[] args) 20 | throws InterruptedException { 21 | 22 | Thread backgroundThread = new Thread(() -> { 23 | int i = 0; 24 | while (!stopRequested()) 25 | i++; 26 | System.out.println("Finito"); 27 | }); 28 | backgroundThread.start(); 29 | 30 | TimeUnit.SECONDS.sleep(1); 31 | requestStop() ; 32 | 33 | } 34 | } -------------------------------------------------------------------------------- /src/main/java/com/ing/question39/MailNinja.java: -------------------------------------------------------------------------------- 1 | package com.ing.question39; 2 | 3 | 4 | /* 5 | TODO: 6 | - Run the app 7 | - Problem : Speed up the application, sendMail() method is too slow 8 | - call sendMail method in an async way with Thread 9 | */ 10 | public class MailNinja { 11 | 12 | public static void main(String[] args) { 13 | 14 | for (int i = 0; i < 10; i++) { 15 | // call sendMail method in an async way with Thread 16 | sendMail(i); 17 | } 18 | 19 | } 20 | 21 | public static void sendMail(int counter) { 22 | try { 23 | Thread.sleep(1000); 24 | System.out.println(" Mail send ... " + counter); 25 | } catch (InterruptedException e) { 26 | e.printStackTrace(); 27 | } 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question40/MyLock.java: -------------------------------------------------------------------------------- 1 | package com.ing.question40; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.concurrent.ExecutorService; 6 | import java.util.concurrent.Executors; 7 | 8 | 9 | /* 10 | TODO: 11 | - Run the app 12 | - Any problem ? 13 | */ 14 | public class MyLock { 15 | 16 | static ExecutorService exec = Executors.newFixedThreadPool(10); 17 | 18 | private final List myList = new ArrayList(); 19 | 20 | { 21 | myList.add("Hello"); 22 | myList.add("Africa"); 23 | 24 | } 25 | 26 | 27 | private final Object key1 = new Object(); 28 | private final Object key2 = new Object(); 29 | 30 | 31 | public String get(int index) { 32 | 33 | synchronized (key1) { 34 | try { 35 | Thread.sleep(100); // IO 36 | } catch (InterruptedException e) { 37 | e.printStackTrace(); 38 | } 39 | for (String element : myList) { 40 | System.out.println(" Here is the list " + element ); 41 | } 42 | 43 | 44 | synchronized (key2) { 45 | return myList.get(index); 46 | } 47 | } 48 | } 49 | 50 | public void set(String value) { 51 | synchronized (key2) { 52 | synchronized (key1) { 53 | myList.add(value); 54 | } 55 | } 56 | } 57 | 58 | public static void main(String[] args) throws InterruptedException { 59 | MyLock myLock = new MyLock(); 60 | Runnable taskForSet = () -> myLock.set("Canada"); 61 | Runnable taskForGet = () -> System.out.println(myLock.get(2)); 62 | exec.submit(taskForSet); 63 | exec.submit(taskForGet); 64 | 65 | exec.shutdown(); 66 | System.out.println("Finito"); 67 | 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question41/NetworkUtility.java: -------------------------------------------------------------------------------- 1 | package com.ing.question41; 2 | 3 | import java.util.concurrent.*; 4 | 5 | public class NetworkUtility { 6 | 7 | private static ExecutorService exec = Executors.newFixedThreadPool(10); 8 | private static int DEFAULT_VALUE=4; 9 | 10 | 11 | // An empty catch block defeats the purpose of exceptions 12 | public void await() { 13 | try { 14 | // ... 15 | } catch (Exception e) { 16 | 17 | } 18 | } 19 | 20 | /** If you choose 21 | to ignore an exception, the catch block should contain a comment explaining 22 | why it is appropriate to do so, and the variable should be named ignored: 23 | */ 24 | public void doWork() { 25 | 26 | Future f = exec.submit(new Callable() { 27 | @Override 28 | public Integer call() throws Exception { 29 | return Integer.MAX_VALUE; 30 | } 31 | }); 32 | 33 | int numColors = DEFAULT_VALUE; // Default; guaranteed sufficient for any map 34 | try { 35 | numColors = f.get(1L, TimeUnit.SECONDS); 36 | } catch (TimeoutException | ExecutionException | InterruptedException ignored) { 37 | // Use default: minimal coloring is desirable, not required 38 | } 39 | } 40 | 41 | 42 | } -------------------------------------------------------------------------------- /src/main/java/com/ing/question42/Supply.java: -------------------------------------------------------------------------------- 1 | package com.ing.question42; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.FileReader; 5 | import java.io.IOException; 6 | 7 | /**TODO: 8 | - Throwing IOException is okay? 9 | */ 10 | public class Supply { 11 | 12 | static String firstLineOfFile(String path) throws IOException { 13 | 14 | try (BufferedReader br = new BufferedReader(new FileReader(path));) { 15 | return br.readLine(); 16 | } 17 | } 18 | 19 | public static void main(String[] args) throws IOException { 20 | String path = args[0]; 21 | System.out.println(firstLineOfFile(path)); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question43/DontDo.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | package com.ing.question43; 4 | 5 | public class DontDo { 6 | 7 | public void calculate() { 8 | try { 9 | // risky code 10 | } catch (Exception ex ) { // don't do this. 11 | 12 | } 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question43/IllegalArgumentExceptionExample.java: -------------------------------------------------------------------------------- 1 | package com.ing.question43; 2 | 3 | import java.io.File; 4 | 5 | public class IllegalArgumentExceptionExample { 6 | 7 | /** 8 | * 9 | * @param parent, The path of the parent node. 10 | * @param filename, The filename of the current node. 11 | * @return The relative path to the current node, starting from the parent node. 12 | * @throws IllegalArgumentException in case of null 13 | */ 14 | public static String createRelativePath(String parent, String filename) { 15 | if(parent == null) 16 | throw new IllegalArgumentException("The parent path cannot be null!"); 17 | 18 | if(filename == null) 19 | throw new IllegalArgumentException("The filename cannot be null!"); 20 | 21 | return parent + File.separator + filename; 22 | } 23 | 24 | public static void main(String[] args) { 25 | // The following command will be successfully executed. 26 | System.out.println(IllegalArgumentExceptionExample.createRelativePath("dir1", "file1")); 27 | System.out.println(); 28 | 29 | // The following command throws an IllegalArgumentException. 30 | System.out.println(IllegalArgumentExceptionExample.createRelativePath(null, "file1")); 31 | } 32 | } -------------------------------------------------------------------------------- /src/main/java/com/ing/question43/IllegalStateExceptionDemo.java: -------------------------------------------------------------------------------- 1 | package com.ing.question43; 2 | 3 | 4 | public class IllegalStateExceptionDemo { 5 | 6 | private boolean initted; 7 | 8 | public void initialise() { 9 | // ... 10 | initted = true; 11 | } 12 | 13 | /** 14 | * @throws IllegalStateException if initted is not initialise 15 | */ 16 | public void doSomething() { 17 | if (!initted) 18 | throw new IllegalStateException("Object not initialised"); 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question43/IndexOutOfBoundsExceptionExample.java: -------------------------------------------------------------------------------- 1 | package com.ing.question43; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class IndexOutOfBoundsExceptionExample { 7 | 8 | List allUsers = new ArrayList<>(); 9 | 10 | /** 11 | * 12 | * @param index location index 13 | * @return User : founder User 14 | * @throws IndexOutOfBoundsException if index bigger or equal to size of the User list 15 | */ 16 | public User getUser(int index) { 17 | if(index >= allUsers.size()){ 18 | throw new IndexOutOfBoundsException("User doesn't exist"); 19 | } 20 | return allUsers.get(index); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question43/NullPointerExceptionExample.java: -------------------------------------------------------------------------------- 1 | package com.ing.question43; 2 | 3 | public class NullPointerExceptionExample { 4 | 5 | public static void main(String[] args) { 6 | if(args==null) { 7 | // In the case of a null argument IllegalArgumentException might be the more appropriate exception to throw. 8 | throw new NullPointerException("args is null"); // No need to do this 9 | } 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question43/User.java: -------------------------------------------------------------------------------- 1 | package com.ing.question43; 2 | 3 | public class User { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question43/unsuportedOperation/Duck.java: -------------------------------------------------------------------------------- 1 | package com.ing.question43.unsuportedOperation; 2 | 3 | public abstract class Duck { 4 | 5 | abstract void display(); 6 | abstract void fly() throws UnsupportedOperationException; 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question43/unsuportedOperation/MiniDuckSimulator.java: -------------------------------------------------------------------------------- 1 | package com.ing.question43.unsuportedOperation; 2 | 3 | public class MiniDuckSimulator { 4 | 5 | public static void main(String[] args) { 6 | Duck duck = new ModelDuck(); 7 | duck.display(); 8 | duck.fly(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question43/unsuportedOperation/ModelDuck.java: -------------------------------------------------------------------------------- 1 | package com.ing.question43.unsuportedOperation; 2 | 3 | public class ModelDuck extends Duck{ 4 | @Override 5 | void display() { 6 | System.out.println("I am Model Duck"); 7 | } 8 | 9 | 10 | @Override 11 | void fly() { 12 | throw new UnsupportedOperationException("I am Model Duck"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question43/unsuportedOperation/RedHeadDuck.java: -------------------------------------------------------------------------------- 1 | package com.ing.question43.unsuportedOperation; 2 | 3 | public class RedHeadDuck extends Duck{ 4 | @Override 5 | void display() { 6 | System.out.println(" I'm RedHeadDuck .."); 7 | } 8 | 9 | @Override 10 | void fly() { 11 | System.out.println(" I'm flying ... "); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/ing/question44/PhoneNumber.java: -------------------------------------------------------------------------------- 1 | package com.ing.question44; 2 | 3 | public final class PhoneNumber { 4 | private final short areaCode, prefix, lineNum; 5 | 6 | public PhoneNumber(int areaCode, int prefix, int lineNum) { 7 | this.areaCode = rangeCheck(areaCode, 999, "area code"); 8 | this.prefix = rangeCheck(prefix, 999, "prefix"); 9 | this.lineNum = rangeCheck(lineNum, 9999, "line num"); 10 | } 11 | 12 | private static short rangeCheck(int val, int max, String arg) { 13 | if (val < 0 || val > max) 14 | throw new IllegalArgumentException(arg + ": " + val); 15 | return (short) val; 16 | } 17 | 18 | @Override public boolean equals(Object o) { 19 | if (o == this) 20 | return true; 21 | if (!(o instanceof PhoneNumber)) 22 | return false; 23 | PhoneNumber pn = (PhoneNumber)o; 24 | return pn.lineNum == lineNum && pn.prefix == prefix 25 | && pn.areaCode == areaCode; 26 | } 27 | 28 | @Override public int hashCode() { 29 | int result = Short.hashCode(areaCode); 30 | result = 31 * result + Short.hashCode(prefix); 31 | result = 31 * result + Short.hashCode(lineNum); 32 | return result; 33 | } 34 | 35 | /** 36 | * Returns the string representation of this phone number. 37 | * The string consists of twelve characters whose format is 38 | * "XXX-YYY-ZZZZ", where XXX is the area code, YYY is the 39 | * prefix, and ZZZZ is the line number. Each of the capital 40 | * letters represents a single decimal digit. 41 | * 42 | * If any of the three parts of this phone number is too small 43 | * to fill up its field, the field is padded with leading zeros. 44 | * For example, if the value of the line number is 123, the last 45 | * four characters of the string representation will be "0123". 46 | */ 47 | // @Override public String toString() { 48 | // return String.format("%03d-%03d-%04d", 49 | // areaCode, prefix, lineNum); 50 | // } 51 | 52 | public static void main(String[] args) { 53 | PhoneNumber jenny = new PhoneNumber(707, 867, 5309); 54 | System.out.println("Jenny's number: " + jenny); 55 | } 56 | } -------------------------------------------------------------------------------- /src/main/resource/question_10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/altuga/BootcampEffectiveJava/306f878afe5d96378f3f7fe331e86dd3076471ec/src/main/resource/question_10.png -------------------------------------------------------------------------------- /src/main/resource/question_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/altuga/BootcampEffectiveJava/306f878afe5d96378f3f7fe331e86dd3076471ec/src/main/resource/question_4.png -------------------------------------------------------------------------------- /src/test/java/com/ing/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.ing; 2 | 3 | import static org.junit.Assert.assertTrue; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | { 12 | /** 13 | * Rigorous Test :-) 14 | */ 15 | @Test 16 | public void shouldAnswerWithTrue() 17 | { 18 | assertTrue( true ); 19 | } 20 | } 21 | --------------------------------------------------------------------------------