diff --git a/java/src/main/java/com/elsevier/education/Exercise1.java b/java/src/main/java/com/elsevier/education/Exercise1.java index 90622840..2844d393 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 { + //Declare class as final so it cant be extended. + public static final class Person { - private Set phoneNumbers; - private String firstName; - private String lastName; + //Make mutable fields final so its value can be assigned only once. + private final Set phoneNumbers; + private final String firstName; + private final String lastName; - public Person() { - } + //Initialize the field via constructor + public Person(Set phone, String fName, String lName) { + this.firstName = fName; + this.lastName = lName; + set tempSet = new TreeSet<>(phone) + phoneNumbers = tempSet; + } - public Set getPhoneNumbers() { - return phoneNumbers; - } - public void setPhoneNumbers(Set newPhoneNumbers) { + public Set getPhoneNumbers() { + return (Set) ((TreeSet)phoneNumbers).clone(); + } + + //no setter methods + /*public void setPhoneNumbers(Set newPhoneNumbers) { phoneNumbers = newPhoneNumbers; - } + }*/ public String getFirstName() { return firstName; } - public void setFirstName(String newName) { + /*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..102d3c79 100644 --- a/java/src/main/java/com/elsevier/education/Exercise2.java +++ b/java/src/main/java/com/elsevier/education/Exercise2.java @@ -11,7 +11,8 @@ public class Exercise2 { public static class Car { - private GasEngine engine = new GasEngine(); + @Autowired + private GasEngine engine public Car() { } @@ -21,9 +22,15 @@ public void moveForward() { } } - public static class GasEngine { - public void spinWheels() { - // no-op for now + public static abstract class Engine{ + public final void spinWheels(){ + // TODO Auto-generated method stub } } +public static class GasEngine extendts Engine { + + } + public static class ElectricEngine extendts Engine { + + } } \ 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..d82cdd3f 100644 --- a/java/src/main/java/com/elsevier/education/Exercise3.java +++ b/java/src/main/java/com/elsevier/education/Exercise3.java @@ -21,7 +21,10 @@ public Person(int newId) { } public int hashCode() { - return id * generator.nextInt(); + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; } 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..d6f80d87 100644 --- a/java/src/main/java/com/elsevier/education/Exercise4.java +++ b/java/src/main/java/com/elsevier/education/Exercise4.java @@ -9,18 +9,23 @@ public class Exercise4 { public static class Counter { - private int count = 0; + + private AtomicInteger count = new AtomicInteger(0); + + public Counter(AtomicInteger count) { + this.count = count; + } - public int increment() { - return ++count; + public void increment() { + count.incrementAndGet(); } - public int getCount() { - return count; + public AtomicInteger getCount() { + return count; } 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..75a2be8f 100644 --- a/java/src/main/java/com/elsevier/education/Exercise5.java +++ b/java/src/main/java/com/elsevier/education/Exercise5.java @@ -8,13 +8,33 @@ public class Exercise5 { public static class Singleton { + + //private static variable of the same class + private static Singleton myObj; + + //private Constructor to restrict instantiation from other classes + private MySingleton() { + } + public void doSomething() { System.out.println("Doing something...."); } } + + //create static method to get an instance + public static MySingleton getInstance(){ + if(myObj == null){ + synchronized(MySingleton.class){ + if(myObj == null){ + myObj = new MySingleTon(); + } + } + } + return myObj; + } public static void main(String a[]){ - Singleton st = new Singleton(); + Singleton st = Singleton.getInstance(); st.doSomeThing(); } }