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

Coding Assinment #128

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
39 changes: 24 additions & 15 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 {
//Declare class as final so it cant be extended.
public static final class Person {

private Set<String> phoneNumbers;
private String firstName;
private String lastName;
//Make mutable fields final so its value can be assigned only once.
private final Set<String> phoneNumbers;
private final String firstName;
private final String lastName;

public Person() {
}
//Initialize the field via constructor
public Person(Set<String> phone, String fName, String lName) {
this.firstName = fName;
this.lastName = lName;
set<String> tempSet = new TreeSet<>(phone)
phoneNumbers = tempSet;
}

public Set<String> getPhoneNumbers() {
return phoneNumbers;
}
public void setPhoneNumbers(Set<String> newPhoneNumbers) {
public Set<String> getPhoneNumbers() {
return (Set<String>) ((TreeSet<String>)phoneNumbers).clone();
}

//no 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) {
lastName = newName;
}
}*/
}
}
15 changes: 11 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,7 +11,8 @@ public class Exercise2 {

public static class Car {

private GasEngine engine = new GasEngine();
@Autowired
private GasEngine engine

public Car() {
}
Expand All @@ -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 {

}
}
5 changes: 4 additions & 1 deletion java/src/main/java/com/elsevier/education/Exercise3.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 11 additions & 6 deletions java/src/main/java/com/elsevier/education/Exercise4.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Expand Down
22 changes: 21 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,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();
}
}