Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed the test #120

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions java/src/main/java/com/elsevier/education/Exercise1.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> phoneNumbers;
private String firstName;
private String lastName;
private final Set<String> phoneNumbers;
private final String firstName;
private final String lastName;

public Person() {
public Person(Set<String> phoneNumbers, String firstName, String lastName) {
this.phoneNumbers= phoneNumbers;
this.firstName=firstName;
this.lastName=lastName;

}

public Set<String> getPhoneNumbers() {
return phoneNumbers;
}
public void setPhoneNumbers(Set<String> 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;
}
}
}
36 changes: 31 additions & 5 deletions java/src/main/java/com/elsevier/education/Exercise2.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}
}
7 changes: 5 additions & 2 deletions java/src/main/java/com/elsevier/education/Exercise4.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -24,4 +24,7 @@ public void resetCount() {
}

}
}
}

// 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.
12 changes: 10 additions & 2 deletions java/src/main/java/com/elsevier/education/Exercise5.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}