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-GirishRevankar #141

Open
wants to merge 5 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
31 changes: 18 additions & 13 deletions java/src/main/java/com/elsevier/education/Exercise1.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,39 @@
*/
public class Exercise1 {

public static class Person {
final public static class Person {

private Set<String> phoneNumbers;
private String firstName;
private String lastName;
//make data members final private so that it can not be changed outside constructor.
final private Set<String> phoneNumbers;
final private String firstName;
final private String lastName;

public Person() {
public Person(Set<String> phonenumbers, String firstname, String lastname) {
this.firstName=firstname;
this.lastName=lastName;
this.phoneNumber=phonenumbers.clone();
}

public Set<String> getPhoneNumbers() {
return phoneNumbers;
return phoneNumbers.clone();
}
public void setPhoneNumbers(Set<String> newPhoneNumbers) {
//remove all setter methods
/*public void setPhoneNumbers(Set<String> 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) {
/*public void setLastName(String newName) {
lastName = newName;
}
}*/
}
}
}
30 changes: 26 additions & 4 deletions java/src/main/java/com/elsevier/education/Exercise2.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,41 @@ 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 setEngine(Engine engine){
this.engine=engine;
}

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

public static class GasEngine {
public static class Engine {
public abstract void startEngine();
public abstract void changeGear();
public abstract void spinWheels();
}

public static class GasEngine extends Engine{
public void spinWheels() {
// no-op for now

startEngine();
changeGear();
}

}
public static class ElectricEngine extends Engine{

public void spinWheels(){
startEngine();
changeGear();
}
}
}
}
3 changes: 2 additions & 1 deletion java/src/main/java/com/elsevier/education/Exercise3.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public Person(int newId) {
id = newId;
}

//your are returning different hashcode for same object everytime.
public int hashCode() {
return id * generator.nextInt();
}
Expand All @@ -28,4 +29,4 @@ public boolean equals(Object other) {
return id.equals(((Person)other).id);
}
}
}
}
8 changes: 7 additions & 1 deletion java/src/main/java/com/elsevier/education/Exercise4.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public static class Counter {

private int count = 0;

//this method need to synchronize as two thread can simultaneously access the count. one can be updating and other might read
//giving inconsistence results.

public int increment() {
return ++count;
}
Expand All @@ -19,9 +22,12 @@ public int getCount() {
return count;
}

//this method need to synchronize as two thread can simultaneously access the count. one can be updating and other might read
//giving inconsistence results.

public void resetCount() {
count = 0;
}

}
}
}
14 changes: 13 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,25 @@
public class Exercise5 {

public static class Singleton {
private static Singleton instance=null;

protected Singleton(){
}

public static Singleton 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 = Singleton.getInstance();
st.doSomeThing();
}
}