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

Interview Exercises #137

Open
wants to merge 1 commit 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
40 changes: 24 additions & 16 deletions java/src/main/java/com/elsevier/education/Exercise1.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,50 @@
package com.elsevier.education;

import java.util.Set;
import java.util.TreeSet;

/**
1. Both outter class Exercise1 and the static nested class Person are made final
so that they cannot be extended.

2. The setters in the static nested class Person were removed so that fields in
Person cannot be set.

TODO: Make this class immutable.
3. The fields in the static nested class Person are initialized when the
Exercise1.Person object is instantiated. Since the phoneNumbers field is
mutable, it is initialized with a copy of the phoneNumbers argument in the
constructor so that any update in the phoneNumbers argument would not affect the
phoneNumbers field.

*/
public class Exercise1 {

public static class Person {
*/
public final class Exercise1 {
public final static class Person {

private Set<String> phoneNumbers;
private String firstName;
private String lastName;

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

/**
Returns a copy of phoneNumbers so that update in the returned
phoneNumbers object would not effect the phoneNumbers field.
*/
public Set<String> getPhoneNumbers() {
return phoneNumbers;
}
public void setPhoneNumbers(Set<String> newPhoneNumbers) {
phoneNumbers = newPhoneNumbers;
return new TreeSet(phoneNumbers);
}

public String getFirstName() {
return firstName;
}
public void setFirstName(String newName) {
firstName = newName;
}

public String getLastName() {
return lastName;
}
public void setLastName(String newName) {
lastName = newName;
}
}
}
22 changes: 19 additions & 3 deletions java/src/main/java/com/elsevier/education/Exercise2.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,33 @@ public class Exercise2 {

public static class Car {

private GasEngine engine = new GasEngine();
private Engine engine;

public Car() {
public Car(Engine engine) {
this.engine = engine;
}

public void moveForward() {
engine.spinWheels();
}
}

public static class GasEngine {
/**
Interface to be implemented by GasEngine class and EletricEngine class.
*/
public static interface Engine {
public void spinWheels();
}

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
}
Expand Down
14 changes: 10 additions & 4 deletions java/src/main/java/com/elsevier/education/Exercise3.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,30 @@
We should be able to call people.add() twice but end with only one object in it.
We can test with "gradlew test"

1. The hashCode() method is overrode incorrectly with returning a pseudo-random
number. For two object to be equal, their values of hashCode must be equal.

*/
public class Exercise3 {
public final class Exercise3 {

public static class Person {

private static Random generator = new java.util.Random();
private Integer id;

public Person(int newId) {
id = newId;
}

public int hashCode() {
return id * generator.nextInt();
return id;
}

public boolean equals(Object other) {
return id.equals(((Person)other).id);
if (this == other) return true;
if (other == null) return false;
if (!(other instanceof Person)) return false;
Person otherPerson = (Person) other;
return (id == otherPerson.id) || (id != null && id.equals(otherPerson.id));
}
}
}
9 changes: 6 additions & 3 deletions java/src/main/java/com/elsevier/education/Exercise4.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@

TODO Is Counter thread-safe? If so, why, and if not, how can we fix it?

Counter is not thread-safe since two or more threads can access a single instance
of Counter. One way to fix it is to make the methods synchronized.

*/
public class Exercise4 {

public static class Counter {

private int count = 0;

public int increment() {
public synchronized int increment() {
return ++count;
}

public int getCount() {
public synchronized int getCount() {
return count;
}

public void resetCount() {
public synchronized void resetCount() {
count = 0;
}

Expand Down
16 changes: 15 additions & 1 deletion java/src/main/java/com/elsevier/education/Exercise5.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,27 @@
public class Exercise5 {

public static class Singleton {

private static Singleton instance = null;

private Singleton() {

}

public static getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}

public void doSomething() {
System.out.println("Doing something....");
}
}

public static void main(String a[]){
Singleton st = new Singleton();
Singleton st = getInstance();
st.doSomeThing();
}
}