How can I simplify this Java code? - java

I am fairly new in Java and need to do some ugly if / else code.
if (st1 == 0 || st2 == 0 || st3 == 0) {
if (st1 == 0) {
return a;
} else if (st2 == 0) {
return b;
} else {
return c;
}
}
But to me it seems like there should be some simpler way to do this kind of code.
(I know i could leave the outer if away, but it is for the purpose of showing the problem)
I hope somebody has an idea on how to beautify this code :)

Remove the outer condition, and remove unnecessary 'else':
if (st1 == 0) {
return a;
}
if (st2 == 0) {
return b;
}
if (st3 == 0) {
return c;
}

Related

Comparison method violates its general contract in some circumstances

I am using this code to sort items by date:
Collections.sort(productList, (d1, d2) -> {
if (d1.getDateExpired() == null && d2.getDateExpired() == null) {
return 1;
} else if (d1.getDateExpired() == null && d2.getDateExpired() != null) {
return 1;
} else if (d1.getDateExpired() != null && d2.getDateExpired() == null) {
return -1;
} else {
return d1.getDateExpired().compareTo(d2.getDateExpired());
}
})
I know there are some answers, but i cannot understand the bug, especially that I am cannot reproduce the issue, it happen to 2 user out of ~300.

How to compare two 2D Arrays in Java?

I am a beginner trying to write a function in Java that returns true if two passed 2D arrays of int type are the same size in every dimension, and false otherwise. Requirements are that if both arrays are null you should return true. If one is null and the other is not you should return false.
Somehow getting an error for my code:
public static boolean arraySameSize(int[][] a, int[][] b) {
if (a == null && b == null) {
return true;
}
if (a == null || b == null) {
return false;
}
if (a.length == b.length) {
for (int i = 0; i < a.length; i++) {
if (a[i].length == b[i].length) {
return true;
}
}
}
return false;
}
Any help would be appreciated!
Edit: Problem is "Runtime Error: null"
Your logic looks almost spot-on already. The only issue I see is in the logic handling the case where both arrays are not null and have the same first dimension. You should be returning false if any index does not have matching lengths:
public static boolean arraySameSize(int[][] a, int[][] b) {
if (a == null && b == null) {
return true;
}
if (a == null || b == null) {
return false;
}
if (a.length != b.length) {
return false;
}
// if the code reaches this point, it means that both arrays are not
// null AND both have the same length in the first dimension
for (int i=0; i < a.length; i++) {
if (a[i] == null && b[i] == null) {
continue;
}
if (a[i] == null || b[i] == null) {
return false;
}
if (a[i].length != b[i].length) {
return false;
}
}
return true;
}
Follow the demo link below to see some of examples of this method working correctly.
Demo

Java nested ifs

Below is a small function which when given two numbers (a, b), returns true if one of the numbers is a teen-number. Returns false if both are teen. Returns false if both are not teen. I failed these test cases but I can't figure out why. Help?
(13, 99), (14, 20), and (16, 9)
public boolean loneTeen(int a, int b)
{
if(a<=19 && a>=13)
{
if(b<=19 && b>=13)
{
return false;
}
}
else if(a<=19 && a>=13)
{
return true;
}
else if(b<=19 && b>=13)
{
return true;
}
return false;
}
All three test cases will enter first if branch, they will not match inner condition and, since they already matched first branch, will not match any of else if's. So, they will all return false which is wrong.
Using a small auxiliary method can make your life much easier (and the code more readable!):
private boolean isTeen(int a) {
return a > 12 && a < 20;
}
public boolean loneTeen(int a, int b) {
if(isTeen(a) && isTeen(b) ||
!isTeen(a) && !isTeen(b)) {
return false;
}
return true;
}
First else won't be executed as you are putting the same condition on both if and else.try putting
if( a>=13 && a<=19 && b>=13 && b<=19){return false;}
else if(a>=13 && a<= 19){return true;}
else if(b<=19 && b>=13){return true;}
else return false;

this method must return a result of type boolean, java

public boolean Winner() {
for (int z = 0; z < 3; z++) {
if (board[z] != null && board[z] == board[z+3] && board[z] == board[z+6]
) {
return true;
}
}
for(int i=0; i<7;i+=3){
if (board[i] != null && board[i] == board[i+1] && board[i] == board[i+2]) {
return true;}
}
}
It returns me this error: this method must return a result of type boolean. What am I doing wrong?
Right now, the function isn't guaranteed to return a boolean, because it's possible that neither of the if statements will ever be entered.
You could fix it like this (but only do this if it's actually what your logic needs!):
public boolean Winner() {
for (int z = 0; z < 3; z++) {
if (board[z] != null && board[z] == board[z+3] && board[z] == board[z+6]
) {
return true;
}
}
for(int i=0; i<7;i+=3){
if (board[i] != null && board[i] == board[i+1] && board[i] == board[i+2]) {
return true;}
}
return false;
}
The Java compiler doesn't make assumptions that a for loop will have an iteration or that an if statement block will run.
There are execution paths where there is no return statement. What happens if the execution path doesn't execute any of the existing return statements and drops to the bottom? There isn't a return there.
Add a return at the bottom.
All possible ways that the method can exit need to return something. If your code makes it through both for loops without having an if condition evaluate to true, then you need a return at the end that specifies what gets returned.
The compiler is not aware that at least one of the loops will be executed. It takes into account all the possibilities of the execution and one of them is that neither of the loops will be executed and in that case there is no return statement. So insert a return statement out of the loops as well.
The answer to this question is easy. It happened to me too. The problem in your code is that you don't say to the computer what to do in case that the "if" statement is wrong, so you just have to add an "else {return false}" to every "if". Another tip is: please make your code cleaner and readable.
public boolean Winner() {
for (int z = 0; z < 3; z++) {
if (board[z] != null && board[z] == board[z+3] && board[z] == board[z+6]) {
return true;
} else {
return false;
}
}
for (int i=0; i<7; i+=3) {
if (board[i] != null && board[i] == board[i+1] && board[i] == board[i+2]) {
return true;
} else {
return false;
}
}
}

java - tree structure method

I've been asked to write a recursive method to investigate whether or not there are any single children. I have get the base cases but am a bit confused about how to go about the recursive section as I will need to investigate both the right and the left subtree and return false if one of them has a single child and true if one of them has 0 children or recur.
what I have so far is:
public static boolean noSingleChildren( BinaryTreeNode t ) {
if (rightC == null || leftC == null) {
return false;
} else if (rightC == null && leftC == null) {
return true;
} else {
return............
}
}
The logic is quite simple:
If the current node only has a single child, you're done.
Otherwise, recursively ask each non-null child the same question, and combine the answers using logical "or".
Since this looks like homework, I leave the implementation to you.
public static boolean noSingleChildren( BinaryTreeNode t ) {
if (rightC == null || leftC == null) {
return false;
} else if (rightC == null && leftC == null) {
return true;
} else {
return noSingleChildren(t.getLeftBranch()) || noSingleChildren(t.getRightBranch());
}
}
Ho, I love trees questions:
public static boolean hasSingleChildren( BinaryTreeNode t ) {
if (t == null) {
return false;
} else if (t.rightC == null && t.leftC != null) {
return true;
} else if (t.rightC != null && t.leftC == null) {
return true;
} else {
return hasSingleChildren(t.rightC) || hasSingleChildren(t.leftC);
}
}

Categories

Resources