From b69870c184091098894015ea830101ee475e8bed Mon Sep 17 00:00:00 2001 From: jreimMBP Date: Thu, 23 May 2019 00:47:54 -0400 Subject: [PATCH] submitting exercises --- java/build.gradle | 2 + .../com/elsevier/education/Exercise1.java | 43 +++++++++++-------- .../com/elsevier/education/Exercise2.java | 33 +++++++++++--- .../com/elsevier/education/Exercise3.java | 4 +- .../com/elsevier/education/Exercise4.java | 15 +++++-- .../com/elsevier/education/Exercise5.java | 19 ++++++-- 6 files changed, 84 insertions(+), 32 deletions(-) diff --git a/java/build.gradle b/java/build.gradle index 63e5da37..d79f87ee 100644 --- a/java/build.gradle +++ b/java/build.gradle @@ -12,6 +12,8 @@ repositories { dependencies { testCompile 'junit:junit:4.12' + //added for Exercise2 + compile 'javax.inject:javax.inject:1' } task wrapper(type: Wrapper) { diff --git a/java/src/main/java/com/elsevier/education/Exercise1.java b/java/src/main/java/com/elsevier/education/Exercise1.java index 90622840..31a25a75 100644 --- a/java/src/main/java/com/elsevier/education/Exercise1.java +++ b/java/src/main/java/com/elsevier/education/Exercise1.java @@ -9,34 +9,43 @@ */ public class Exercise1 { - public static class Person { - - private Set phoneNumbers; - private String firstName; - private String lastName; - - public Person() { + /** + * the class must be set to final so it can not be extended. + */ + public static final class Person { + + /** + * all members must be set to final. + */ + private final Set phoneNumbers; + private final String firstName; + private final String lastName; + + /** + * constructor must take all args so that they can be set. + * @param phoneNumbers + * @param firstName + * @param lastName + */ + public Person(Set phoneNumbers,String firstName, String lastName) { + this.phoneNumbers = phoneNumbers; + this.firstName = firstName; + this.lastName = lastName; } + /** + * all setters removed. + */ public Set getPhoneNumbers() { return phoneNumbers; } - public void setPhoneNumbers(Set newPhoneNumbers) { - phoneNumbers = newPhoneNumbers; - } - + public String getFirstName() { return firstName; } - public void setFirstName(String newName) { - firstName = newName; - } public String getLastName() { return lastName; } - public void setLastName(String newName) { - lastName = newName; } - } } \ No newline at end of file diff --git a/java/src/main/java/com/elsevier/education/Exercise2.java b/java/src/main/java/com/elsevier/education/Exercise2.java index b7b939e0..fb850af2 100644 --- a/java/src/main/java/com/elsevier/education/Exercise2.java +++ b/java/src/main/java/com/elsevier/education/Exercise2.java @@ -1,19 +1,27 @@ package com.elsevier.education; + +import javax.inject.Inject; + /** TODO refactor the Car to use dependency injection of the engine + I used javax.injection and added it as a dependency. TODO allow use of either a gas engine or electric engine (create an appropriate abstraction) + //made an abstract Engine class that both electric and gas extend TODO make sure we have no-op implementations of the gas engine and electric engine + //by declaring the method abstract , the extending classes will have to implement. */ public class Exercise2 { public static class Car { - - private GasEngine engine = new GasEngine(); - - public Car() { + + + private Engine engine; + @Inject + public Car(Engine engine) { + this.engine = engine; } public void moveForward() { @@ -21,9 +29,22 @@ public void moveForward() { } } - public static class GasEngine { + public static abstract class Engine { + public abstract void spinWheels(); + } + public static class GasEngine extends Engine { + + @Override public void spinWheels() { - // no-op for now + } } + + public static class ElectricEngine extends Engine { + @Override + public void spinWheels() { + + } + } + } \ No newline at end of file diff --git a/java/src/main/java/com/elsevier/education/Exercise3.java b/java/src/main/java/com/elsevier/education/Exercise3.java index 6b208652..910dfb83 100644 --- a/java/src/main/java/com/elsevier/education/Exercise3.java +++ b/java/src/main/java/com/elsevier/education/Exercise3.java @@ -13,7 +13,6 @@ public class Exercise3 { public static class Person { - private static Random generator = new java.util.Random(); private Integer id; public Person(int newId) { @@ -21,7 +20,8 @@ public Person(int newId) { } public int hashCode() { - return id * generator.nextInt(); + //removed the random number as it would cause uniqe hash values which is why it was getting added twice. + return id ; } public boolean equals(Object other) { diff --git a/java/src/main/java/com/elsevier/education/Exercise4.java b/java/src/main/java/com/elsevier/education/Exercise4.java index 87ffbc7a..20bab5f6 100644 --- a/java/src/main/java/com/elsevier/education/Exercise4.java +++ b/java/src/main/java/com/elsevier/education/Exercise4.java @@ -1,26 +1,33 @@ package com.elsevier.education; +import java.util.concurrent.atomic.AtomicInteger; + /** TODO Is Counter thread-safe? If so, why, and if not, how can we fix it? + no, objects live on the heap while methods live on the stack, + the counter is static so one instance will be shared among threads. + + we can fix this by using the java concurrent packate and swtich it to an AtomicInteger + we also could have used synchronized blocks. */ public class Exercise4 { public static class Counter { - private int count = 0; + private AtomicInteger count = new AtomicInteger(0); public int increment() { - return ++count; + return count.incrementAndGet(); } public int getCount() { - return count; + return count.get(); } public void resetCount() { - count = 0; + count.set(0); } } diff --git a/java/src/main/java/com/elsevier/education/Exercise5.java b/java/src/main/java/com/elsevier/education/Exercise5.java index abd81e3f..7df585ac 100644 --- a/java/src/main/java/com/elsevier/education/Exercise5.java +++ b/java/src/main/java/com/elsevier/education/Exercise5.java @@ -3,18 +3,31 @@ /** TODO: Turn the "Singleton" class into an actual singleton. The main() method should still call .doSomething(). + //There is a really cool library out there called lombok https://projectlombok.org/ which uses an annotation for this + //by actually adding the byte code to the class file but not muddying up the java. + //I did assume that you wanted to see the implemtation tho. */ public class Exercise5 { - + + public static class Singleton { - public void doSomething() { + + private static Singleton singleton; + private Singleton(){} + public static Singleton getInstance(){ + if(singleton == null){ + singleton = new Singleton(); + } + return singleton; + } + public void doSomeThing() { System.out.println("Doing something...."); } } public static void main(String a[]){ - Singleton st = new Singleton(); + Singleton st = Singleton.getInstance(); st.doSomeThing(); } }