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

submitting exercises #283

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
2 changes: 2 additions & 0 deletions java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ repositories {

dependencies {
testCompile 'junit:junit:4.12'
//added for Exercise2
compile 'javax.inject:javax.inject:1'
}

task wrapper(type: Wrapper) {
Expand Down
43 changes: 26 additions & 17 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,43 @@
*/
public class Exercise1 {

public static class Person {

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

public Person() {
/**
* the class must be set to final so it can not be extended.
*/
public static final class Person {

/**
* all members must be set to final.
*/
private final Set<String> phoneNumbers;
private final String firstName;
private final String lastName;

/**
* constructor must take all args so that they can be set.
* @param phoneNumbers
* @param firstName
* @param lastName
*/
public Person(Set<String> phoneNumbers,String firstName, String lastName) {
this.phoneNumbers = phoneNumbers;
this.firstName = firstName;
this.lastName = lastName;
}

/**
* all setters removed.
*/
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;
}
}
}
33 changes: 27 additions & 6 deletions java/src/main/java/com/elsevier/education/Exercise2.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,50 @@
package com.elsevier.education;


import javax.inject.Inject;

/**

TODO refactor the Car to use dependency injection of the engine
I used javax.injection and added it as a dependency.
TODO allow use of either a gas engine or electric engine (create an appropriate abstraction)
//made an abstract Engine class that both electric and gas extend
TODO make sure we have no-op implementations of the gas engine and electric engine
//by declaring the method abstract , the extending classes will have to implement.

*/
public class Exercise2 {

public static class Car {

private GasEngine engine = new GasEngine();

public Car() {


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

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

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

@Override
public void spinWheels() {
// no-op for now

}
}

public static class ElectricEngine extends Engine {
@Override
public void spinWheels() {

}
}

}
4 changes: 2 additions & 2 deletions java/src/main/java/com/elsevier/education/Exercise3.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public 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();
//removed the random number as it would cause uniqe hash values which is why it was getting added twice.
return id ;
}

public boolean equals(Object other) {
Expand Down
15 changes: 11 additions & 4 deletions java/src/main/java/com/elsevier/education/Exercise4.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
package com.elsevier.education;

import java.util.concurrent.atomic.AtomicInteger;

/**

TODO Is Counter thread-safe? If so, why, and if not, how can we fix it?
no, objects live on the heap while methods live on the stack,
the counter is static so one instance will be shared among threads.

we can fix this by using the java concurrent packate and swtich it to an AtomicInteger
we also could have used synchronized blocks.

*/
public class Exercise4 {

public static class Counter {

private int count = 0;
private AtomicInteger count = new AtomicInteger(0);

public int increment() {
return ++count;
return count.incrementAndGet();
}

public int getCount() {
return count;
return count.get();
}

public void resetCount() {
count = 0;
count.set(0);
}

}
Expand Down
19 changes: 16 additions & 3 deletions java/src/main/java/com/elsevier/education/Exercise5.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,31 @@
/**

TODO: Turn the "Singleton" class into an actual singleton. The main() method should still call .doSomething().
//There is a really cool library out there called lombok https://projectlombok.org/ which uses an annotation for this
//by actually adding the byte code to the class file but not muddying up the java.
//I did assume that you wanted to see the implemtation tho.

*/
public class Exercise5 {



public static class Singleton {
public void doSomething() {

private static Singleton singleton;
private Singleton(){}
public static Singleton getInstance(){
if(singleton == null){
singleton = new Singleton();
}
return singleton;
}
public void doSomeThing() {
System.out.println("Doing something....");
}
}

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