-
Notifications
You must be signed in to change notification settings - Fork 9
OOP hometask #9
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
base: master
Are you sure you want to change the base?
OOP hometask #9
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +1,91 @@ | ||
| package school.lemon.changerequest.java.container; | ||
|
|
||
| public class ContainerImpl implements Container{ | ||
| import java.util.Arrays; | ||
|
|
||
| public class ContainerImpl implements Container { | ||
| /** | ||
| * The maximum size of array to allocate. | ||
| * Some VMs reserve some header words in an array. | ||
| * Attempts to allocate larger arrays may result in | ||
| * OutOfMemoryError: Requested array size exceeds VM limit | ||
| */ | ||
| private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; | ||
|
|
||
| private int size; | ||
| private int[] elementData; | ||
|
|
||
| public ContainerImpl() { | ||
| this.elementData = 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 | ||
| for (int i = 0; i < size; i++) | ||
| elementData[i] = 0; | ||
|
|
||
| size = 0; | ||
| } | ||
|
|
||
| @Override | ||
| public Integer get(int index) { | ||
| // TODO: please implement me | ||
| return null; | ||
| if (index >= size) { | ||
| return null; | ||
| } | ||
| return elementData[index]; | ||
| } | ||
|
|
||
| @Override | ||
| public void add(int element) { | ||
| // TODO: please implement me | ||
| ensureCapacityInternal(size + 1); | ||
| elementData[size++] = element; | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public boolean add(int element, int index) { | ||
| // TODO: please implement me | ||
| if (index <= size && index >= 0) { | ||
| ensureCapacityInternal(size + 1); | ||
| System.arraycopy(elementData, index, elementData, index + 1, | ||
| size - index); | ||
| elementData[index] = element; | ||
| size++; | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean remove(int index) { | ||
| // TODO: please implement me | ||
| if (index < size && index >= 0) { | ||
| int numMoved = size - index - 1; | ||
| if (numMoved > 0) | ||
| System.arraycopy(elementData, index + 1, elementData, index, | ||
| numMoved); | ||
| elementData[--size] = 0; | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private void ensureCapacityInternal(int minCapacity) { | ||
| if (minCapacity >= INITIAL_ARRAY_SIZE) { | ||
| if (minCapacity - MAX_ARRAY_SIZE > 0) { | ||
| minCapacity = hugeCapacity(minCapacity); | ||
| } | ||
| elementData = Arrays.copyOf(elementData, minCapacity); | ||
| } | ||
| } | ||
|
|
||
| private static int hugeCapacity(int minCapacity) { | ||
| if (minCapacity < 0) | ||
| throw new OutOfMemoryError(); | ||
| return (minCapacity > MAX_ARRAY_SIZE) ? | ||
| Integer.MAX_VALUE : | ||
| MAX_ARRAY_SIZE; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,119 +1,163 @@ | ||
| package school.lemon.changerequest.java.extendedinteger; | ||
|
|
||
|
|
||
| /** | ||
| * Integer representation with some additional features | ||
| */ | ||
| public class ExtendedInteger { | ||
| private int value; | ||
|
|
||
| public ExtendedInteger(int value) { | ||
| //TODO: implement me | ||
| this.value = value; | ||
| } | ||
|
|
||
| /** | ||
| * Check whether specified value is even | ||
| * | ||
| * @param value to check | ||
| * @return true if value is even, false - otherwise | ||
| */ | ||
| public static boolean isEven(int value) { | ||
| //TODO: implement me | ||
| if (value % 2 == 0) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Check whether specified value is odd | ||
| * | ||
| * @param value to check | ||
| * @return true if value is odd, false - otherwise | ||
| */ | ||
| public static boolean isOdd(int value) { | ||
| //TODO: implement me | ||
| if (value % 2 != 0) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Check whether specified value is prime | ||
| * | ||
| * @param value to check | ||
| * @return true if value is prime, false - otherwise | ||
| */ | ||
| public static boolean isPrime(int value) { | ||
| //TODO: implement me | ||
| if (value > 1) { | ||
| for (int i = 2; i <= value/2; i++) { | ||
| if (value % i == 0) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Parse specified char array and create instance of {@code ExtendedInteger} | ||
| * | ||
| * @param value to parse | ||
| * @return instance of {@code ExtendedInteger} or | ||
| * 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 | ||
| if ( !(value.length == 0 || value == null) ) { | ||
| char firstChar = value[0]; | ||
| boolean isNegative = false; | ||
| int result = 0; | ||
|
|
||
| if (firstChar == '-') { | ||
| isNegative = true; | ||
| } else if (firstChar == '+') { | ||
| isNegative = false; | ||
| } else if (Character.isDigit(firstChar)) { | ||
| result = Character.getNumericValue(value[0]); | ||
| } else return null; | ||
|
|
||
| for (int i = 1; i < value.length; i++) { | ||
| if (Character.isDigit(value[i])) { | ||
| int prevInt = result; | ||
| result = prevInt * 10 + Character.getNumericValue(value[i]); | ||
| } else return null; | ||
| } | ||
| return isNegative ? new ExtendedInteger(new Integer((-1)*result)):new ExtendedInteger(new Integer(result)); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Parse specified string and create instance of {@code ExtendedInteger} | ||
| * | ||
| * @param value to parse | ||
| * @return instance of {@code ExtendedInteger} or | ||
| * 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; | ||
| return parseInt(value.toCharArray()); | ||
| } | ||
|
|
||
| /** | ||
| * Get int representation of {@code ExtendedInteger} | ||
| * | ||
| * @return int representation | ||
| */ | ||
| public int getValue() { | ||
| //TODO: implement me | ||
| return 0; | ||
| return 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(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(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(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 | ||
| if (this.value == value) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * 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; | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like auto-generated code. It's much more readable with
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, changed |
||
|
|
||
| ExtendedInteger that = (ExtendedInteger) o; | ||
|
|
||
| return value == that.value; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return value; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package school.lemon.changerequest.java.vehicles; | ||
|
|
||
| /** | ||
| * Created by akliuieva on 29.06.17. | ||
| */ | ||
| public class Airplane extends Vehicle implements IAirplane{ | ||
| private int DEFAULT_VALUE_FOR_HASHCODE = 12; //is used to satisfy equals+hashCode contract | ||
| private int maximumHeightFeet; | ||
|
|
||
| public Airplane(int manufacturedYear, String make, String model, int maximumHeightFeet) { | ||
| super(manufacturedYear, make, model); | ||
| this.maximumHeightFeet = maximumHeightFeet; | ||
| } | ||
|
|
||
| @Override | ||
| public int getMaximumHeightFeet() { | ||
| return maximumHeightFeet; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (!(o instanceof Airplane)) return false; | ||
|
|
||
| Airplane airplane = (Airplane) o; | ||
|
|
||
| return Math.abs(maximumHeightFeet - airplane.maximumHeightFeet) <= 1000; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "This airplane is a " + getManufacturedYear() + " " + getMake() + " " + getModel() + " that can reach "+getMaximumHeightFeet()+" feet."; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you remember equals+hashCode contract?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok |
||
| return DEFAULT_VALUE_FOR_HASHCODE; | ||
| } | ||
|
|
||
| @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"; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For sure it's not a good idea to increase array size by 1 each time new element is added.
Please use some multiplier.