Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public interface Container {


int INITIAL_ARRAY_SIZE = 10;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,74 @@
package school.lemon.changerequest.java.container;

public class ContainerImpl implements Container{
private int size;
private int[] array;

public ContainerImpl(){
this.size = size();
this.array = new int[INITIAL_ARRAY_SIZE];
}

@Override
public int size() {
// TODO: please implement me
return 0;

return size;
}

@Override
public void clear() {
// TODO: please implement me
size = 0;
}

@Override
public Integer get(int index) {
// TODO: please implement me
return null;
if (index < 0 || index >= size) {
return null;
}
return array[index];
}


@Override
public void add(int element) {
// TODO: please implement me

if (array.length == size) {
int[] newArray = new int[array.length*3];
System.arraycopy(array, 0, newArray, 0, array.length);
array = newArray;
}
array[size++] = element;
}

@Override
public boolean add(int element, int index) {
// TODO: please implement me
return false;

if (index < 0 || index > size) {
return false;
}
checkSize();
System.arraycopy(array, index, array,index+1, size-index);
return true;
}

private void checkSize() {

if (size == array.length) {
int[] newArray = new int[array.length*3];
System.arraycopy(array, 0, newArray, 0, array.length);
array = newArray;
}
}

@Override
public boolean remove(int index) {
// TODO: please implement me
return false;
}

if (index < 0 || index >= size) {
return false;
}
System.arraycopy(array, index+1, array, index, size-index-1);
--size;
return true;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
* Integer representation with some additional features
*/
public class ExtendedInteger {
private int value;

public ExtendedInteger(int value) {
//TODO: implement me

this.value = value;
}

/**
Expand All @@ -15,8 +17,7 @@ public ExtendedInteger(int value) {
* @return true if value is even, false - otherwise
*/
public static boolean isEven(int value) {
//TODO: implement me
return false;
return value%2 == 0;
}

/**
Expand All @@ -25,8 +26,8 @@ public static boolean isEven(int value) {
* @return true if value is odd, false - otherwise
*/
public static boolean isOdd(int value) {
//TODO: implement me
return false;

return value%2 != 0;
}

/**
Expand All @@ -35,8 +36,16 @@ public static boolean isOdd(int value) {
* @return true if value is prime, false - otherwise
*/
public static boolean isPrime(int value) {
//TODO: implement me
return false;

if (value == 2) return true;
if (value < 2 || value % 2 == 0) {
return false;
}
for (int d = 2; d*d<value; d++){
if (value%d == 0)
return false;
}
return true;
}

/**
Expand All @@ -46,8 +55,12 @@ public static boolean isPrime(int value) {
* null in case specified value is null or the value does not contain a parsable integer
*/
public static ExtendedInteger parseInt(char[] value) {
//TODO: implement me
return null;

if (value == null || value.length == 0) {
return null;
}
String newValue = new String(value);
return ExtendedInteger.parseInt(newValue);
}

/**
Expand All @@ -57,63 +70,89 @@ public static ExtendedInteger parseInt(char[] value) {
* null in case specified value is null or the value does not contain a parsable integer
*/
public static ExtendedInteger parseInt(String value) {
//TODO: implement me
return null;

if ((("").equals(value)) || value == null) {
return null;
}
int val = 0;
int j = 0;
for (int i = value.length() - 1; i > 0; i--) {
if (Character.isDigit(value.charAt(i))) {
val = val + Character.getNumericValue(value.charAt(i)) * (int) Math.pow(10.0, j);
j++;
} else return null;
}
if (Character.isDigit(value.charAt(0))) {
val = val + Character.getNumericValue(value.charAt(0)) * (int) Math.pow(10.0, j);

} else if (value.startsWith("-")) {
val = val * (-1);
} else if(value.startsWith("+")) {}
else return null;
ExtendedInteger newInt = new ExtendedInteger(val);
return newInt;
}



/**
* Get int representation of {@code ExtendedInteger}
* @return int representation
*/
public int getValue() {
//TODO: implement me
return 0;

return this.value;
}

/**
* Check whether current value is even
* @return true if value is even, false - otherwise
*/
public boolean isEven() {
//TODO: implement me
return false;

return isEven(this.value);
}

/**
* Check whether current value is odd
* @return true if value is odd, false - otherwise
*/
public boolean isOdd() {
//TODO: implement me
return false;

return isOdd(this.value);
}

/**
* Check whether current value is prime
* @return true if value is prime, false - otherwise
*/
public boolean isPrime() {
//TODO: implement me
return false;

return isPrime(this.value);
}


/**
* Check whether current {@code ExtendedInteger} is equal to specified int value
* @return true if values are equal, false - otherwise
*/
public boolean equals(int value) {
//TODO: implement me
return false;

return this.value == value;

}

/**
* Check whether current {@code ExtendedInteger} is equal to specified object
* @return true if values are equal, false - otherwise
*/
@Override

public boolean equals(Object obj) {
//TODO: implement me
return false;
if (obj == null) return false;

if (!(obj instanceof ExtendedInteger)) return false;
return ((ExtendedInteger) obj).getValue() == this.value;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package school.lemon.changerequest.java.vehicles;


public class Airplane extends Vehicle implements IAirplane {
private int maximumHeightFeet;

public Airplane(int maximumHeightFeet, int manufacturedYear, String model, String make) {
super(manufacturedYear, make, model);
this.maximumHeightFeet = maximumHeightFeet;
}

@Override
public int getMaximumHeightFeet() {
return maximumHeightFeet;
}

@Override
public void setMaximumHeightFeet(int maximumHeightFeet) {
this.maximumHeightFeet = maximumHeightFeet;
}

@Override
public String accelerate() {
return "fire engines on wings";
}

@Override
public String steerLeft() {
return "lift wing flaps to turn left";
}

@Override
public String steerRight() {
return "lift wing flaps to turn right";
}

@Override
public boolean equals(Object obj) {
if (obj == null) return false;

if (this == obj) return true;

if (!(obj instanceof Airplane)) {
return false;
}
Airplane equalAir = (Airplane) obj;
return Math.abs(equalAir.maximumHeightFeet - maximumHeightFeet) <=1000 ;
}

@Override
public String toString() {

return String.format("This airplane is a %1$s %2$s %3$s that can reach %4$s feet.", this.getManufacturedYear(), this.getMake(), this.getModel(), this.getMaximumHeightFeet());
}

}
55 changes: 55 additions & 0 deletions src/main/java/school/lemon/changerequest/java/vehicles/Boat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package school.lemon.changerequest.java.vehicles;


public class Boat extends Vehicle implements IBoat {
private boolean motorized;

Boat(int manufacturedYear, String make, String model, boolean motorized) {
super(manufacturedYear, make, model);
this.motorized = motorized;
}

@Override
public boolean isMotorized() {
return motorized;
}

@Override
public void setMotorized(boolean motorized) {
this.motorized = motorized;
}

@Override
public String accelerate() {
return "jet water";
}

@Override
public String steerLeft() {
return "turn tiller left";
}

@Override
public String steerRight() {
return "turn tiller right";
}

@Override
public boolean equals(Object obj) {
if (obj == null) return false;

if (this == obj) return true;

if (!(obj instanceof Boat)) {
return false;
}
Boat equalBoat = (Boat) obj;
return equalBoat.motorized == motorized ;
}

@Override
public String toString() {

return String.format("This boat is a %1$s %2$s %3$s with %4$s", this.getManufacturedYear(), this.getMake(), this.getModel(), isMotorized() ? "(with motor)." : ".");
}
}
Loading