Skip to content
Open
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 @@ -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) {
Copy link
Member

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Copy link
Member

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 ?

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

Copy link
Author

Choose a reason for hiding this comment

The 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You algorithm is OK, but implementation details sucks.
Try to create more valuable variable names, for example:
What do you mean with your first variable ? In fact, you need to check the sign of int value, if it's plus or minus and multiply result by 1 or -1 respectively. So, probably variable name sign will suite here better.
Additionally you've got j, k, i indexes, but you really need only one.
Also, there is no need for 2 variables for result you can reuse one.

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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if value == null ?

Copy link
Author

Choose a reason for hiding this comment

The 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)
Copy link
Member

Choose a reason for hiding this comment

The 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 obj==null.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

return false;

if (!(getClass() == obj.getClass()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's always better to avoid negation.
this part could be rewritten as: getClass() != obj.getClass()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

return false;
else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no need for else statement here, cause you always use return

ExtendedInteger tmp = (ExtendedInteger) obj;
if (tmp.value == this.value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can just return result of this expression: return tmp.value == this.value

return true;
else
return false;
}
}

}