From 39dc87493fe60b53011739200c2db3a86872cf31 Mon Sep 17 00:00:00 2001 From: satlur02 Date: Thu, 30 Mar 2017 17:09:13 -0400 Subject: [PATCH 1/2] Completed the test Susmita Atluri, susmita.atluri92@gmail.com, Amit Patel --- .../com/elsevier/education/Exercise1.java | 29 ++++++++----------- .../com/elsevier/education/Exercise4.java | 7 +++-- .../com/elsevier/education/Exercise5.java | 12 ++++++-- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/java/src/main/java/com/elsevier/education/Exercise1.java b/java/src/main/java/com/elsevier/education/Exercise1.java index 90622840..b8a9e0d8 100644 --- a/java/src/main/java/com/elsevier/education/Exercise1.java +++ b/java/src/main/java/com/elsevier/education/Exercise1.java @@ -7,36 +7,31 @@ TODO: Make this class immutable. */ -public class Exercise1 { +public final class Exercise1 { - public static class Person { + public static final class Person { - private Set phoneNumbers; - private String firstName; - private String lastName; + private final Set phoneNumbers; + private final String firstName; + private final String lastName; - public Person() { + public Person(Set phoneNumbers, String firstName, String lastName) { + this.phoneNumbers= phoneNumbers; + this.firstName=firstName; + this.lastName=lastName; + } 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/Exercise4.java b/java/src/main/java/com/elsevier/education/Exercise4.java index 87ffbc7a..dd35005d 100644 --- a/java/src/main/java/com/elsevier/education/Exercise4.java +++ b/java/src/main/java/com/elsevier/education/Exercise4.java @@ -5,7 +5,7 @@ TODO Is Counter thread-safe? If so, why, and if not, how can we fix it? */ -public class Exercise4 { +public class Exercise4 { public static class Counter { @@ -24,4 +24,7 @@ public void resetCount() { } } -} \ No newline at end of file +} + +// Counter is not thread safe. If two or more threads try to access Counter class the increment() function +// get corrupted. To fix this we need to either used locks or threadsafe collections like Conurrent hashmap. diff --git a/java/src/main/java/com/elsevier/education/Exercise5.java b/java/src/main/java/com/elsevier/education/Exercise5.java index abd81e3f..f1cfea91 100644 --- a/java/src/main/java/com/elsevier/education/Exercise5.java +++ b/java/src/main/java/com/elsevier/education/Exercise5.java @@ -8,13 +8,21 @@ public class Exercise5 { public static class Singleton { + + private static Singleton st = new Singleton(); + + public static Singleton getObject(){ + return st; + } + public void doSomething() { System.out.println("Doing something...."); } } public static void main(String a[]){ - Singleton st = new Singleton(); - st.doSomeThing(); + + Singleton st= Singleton.getObject(); + st.doSomething(); } } From 80bdb3298ef5ddb2e12a92b941d1d21142287e34 Mon Sep 17 00:00:00 2001 From: satlur02 Date: Thu, 30 Mar 2017 17:11:55 -0400 Subject: [PATCH 2/2] Completed the test Susmita Atluri, susmita.atluri92@gmail.com, Amit Patel --- .../com/elsevier/education/Exercise2.java | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/java/src/main/java/com/elsevier/education/Exercise2.java b/java/src/main/java/com/elsevier/education/Exercise2.java index b7b939e0..beb129ef 100644 --- a/java/src/main/java/com/elsevier/education/Exercise2.java +++ b/java/src/main/java/com/elsevier/education/Exercise2.java @@ -7,23 +7,49 @@ TODO allow use of either a gas engine or electric engine (create an appropriate TODO make sure we have no-op implementations of the gas engine and electric engine */ +interface Engine { + + public void spinWheels (); +} + +interface Working{ + + public void moveForward(); +} + public class Exercise2 { - public static class Car { + public static class Car implements Working { - private GasEngine engine = new GasEngine(); + private Engine wheels; - public Car() { + public Car(Engine wheels) { + this.wheels=wheels; } + @Override public void moveForward() { - engine.spinWheels(); + // implement condition for selecting GasEngine or Electric Engiene + + this.wheels.spinWheels(); } + } - public static class GasEngine { + public static class GasEngine implements Engine { + + @Override public void spinWheels() { // no-op for now } } + + public static class ElectricEngine implements Engine { + + @Override + public void spinWheels() { + // no-op for now + } + + } } \ No newline at end of file