-
Notifications
You must be signed in to change notification settings - Fork 9
ExtendedInteger done. #6
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?
Changes from all commits
e9ae4f5
88c9676
38c7209
0525d8b
09124dc
f90ff68
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 |
---|---|---|
|
@@ -4,116 +4,178 @@ | |
* 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 & 1) == 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 & 1) == 1) { | ||
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. same as above 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. Fixed. |
||
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 | ||
return false; | ||
if (value <= 1) { | ||
return false; | ||
} else { | ||
for (int i = 2; i < value; i++) { | ||
if (value % i == 0) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* 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 | ||
return null; | ||
|
||
int result = 0; | ||
int resultSum = 0; | ||
int j = 0; | ||
int k = 0; | ||
int first = 1; | ||
int lengthChar = value.length; | ||
if (lengthChar == 0) { | ||
return null; | ||
} | ||
if (value[0] == '-') { | ||
first = -1; | ||
} else if (value[0] == '+') { | ||
k = 0; | ||
} else { | ||
k = -1; | ||
} | ||
for (int i = lengthChar - 1; i != k; i--) { | ||
if (Character.isDigit(value[i])) { | ||
result = Character.getNumericValue(value[i]); | ||
result *= Math.pow(10, j++); | ||
} else { | ||
return null; | ||
} | ||
resultSum += result; | ||
} | ||
resultSum *= first; | ||
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. You algorithm is OK, but implementation details sucks. But, anyway, you code do pass all the tests. |
||
ExtendedInteger resultInt = new ExtendedInteger(resultSum); | ||
return resultInt; | ||
} | ||
|
||
/** | ||
* 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; | ||
|
||
char[] newChar = value.toCharArray(); | ||
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. What if 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. Fixed. |
||
return parseInt(newChar); | ||
} | ||
|
||
/** | ||
* 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(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 | ||
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 == this) | ||
return true; | ||
|
||
if (obj == null) | ||
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. null check should always be the first one, as there is nothing to do if 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. Fixed. |
||
return false; | ||
|
||
if (!(getClass() == obj.getClass())) | ||
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. it's always better to avoid negation. 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. Fixed. |
||
return false; | ||
else { | ||
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. there is no need for else statement here, cause you always use |
||
ExtendedInteger tmp = (ExtendedInteger) obj; | ||
if (tmp.value == this.value) | ||
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. you can just return result of this expression: |
||
return true; | ||
else | ||
return false; | ||
} | ||
} | ||
|
||
} |
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.
just return your expression, there is no need for
if-else
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.
Fixed.
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.
@IgorAfanasenko I don't see any changes checked in, have you already perform commit and pushed your changes ?