the below is the code I made for a codingbat problem. I think my code looks fine. the problem is below:
When squirrels get together for a party, they like to have cigars. A squirrel party is successful when the number of cigars is between 40 and 60, inclusive. Unless it is the weekend, in which case there is no upper bound on the number of cigars. Return true if the party with the given values is successful, or false otherwise.
cigarParty(30, false) → false
cigarParty(50, false) → true
cigarParty(70, true) → true
it keeps saying compile issue, "(" expected at line 6. Am i doing something wrong that I can't see?
public boolean cigarParty(int cigars, boolean isWeekend) {
if (cigars>=40 || cigars <= 60 && isWeekend){
return true;
} if else(cigars<40){
return false;
} else {
return false;
}
}
if else is wrong you should correct it to else if. This is the correct code :
public boolean cigarParty(int cigars, boolean isWeekend) {
if (cigars>=40 || cigars <= 60 && isWeekend){
return true;
}else if(cigars<40){
return false;
} else {
return false;
}
}
There is no if else, you have to use else if.
public boolean cigarParty(int cigars, boolean isWeekend) {
if (cigars>=40 || cigars <= 60 && isWeekend){
return true;
}else if(cigars<40){
return false;
} else {
return false;
}
}
Related
What I am trying to do here out of flower1 and flower2, one is even and one is odd then return true.
If both are even, return false.
If both are odd, return false.
When my code is:
public class OppositesAttract {
public static boolean isLove(final int flower1, final int flower2) {
if(flower1%2==0 && flower2%2==0){
return false;
}else
if(flower1%2!=0 && flower2%2!=0){
return false;
} else
if(flower1%2==0 || flower2%2==0){
return true;
}
}
}
I get a "missing return statement" error.
So I added:
public class OppositesAttract {
public static boolean isLove(final int flower1, final int flower2) {
if(flower1%2==0 && flower2%2==0){
return false;
}else
if(flower1%2!=0 && flower2%2!=0){
return false;
} else
if(flower1%2==0 || flower2%2==0){
return true;
}else{
return true;
}
}
}
Now, the code works but I do not understand why I have to add the additional return statement.
The compiler doesn't know the first 3 terms cover all situations.
if(flower1%2==0 && flower2%2==0){
return false;
} else if(flower1%2!=0 && flower2%2!=0){
return false;
} else if(flower1%2==0 || flower2%2==0){
return true;
}
to you this reads as: all options are covered. but the compiler just sees:
if (somethingThatMayBeTrue) {
} else if (somethingElseThatMayBeTrue) {
} else if (aThirdThingThatMayBeTrue) {
} .... and what if none of them are?
You may know that the last else if should always be true (since you know they are not both uneven) but the compiler doesn't generally try to understand your code.
in your case, the last clause (aThirdThingThatMayBeTrue, flower1%2==0 || flower2%2==0) is actually (somethingThatIsAlwaysTrueIfPreviousTermsAreFalse).
so you can treat it as such:
if(flower1%2==0 && flower2%2==0){
return false;
} else if(flower1%2!=0 && flower2%2!=0){
return false;
} else {
return true;
}
I have written a method that searches a tree to test if any integers are negative.
But I am struggling to get the right Boolean value returned. Any pointers as to where I am going wrong?
What I want to achieve is that as soon as the condition statement is met a false is returned but unfortunately my code is always returning a true
static boolean allE(Tree<Integer> x) {
if (x.isEmpty()) return true;
else {
if (x.getValue()%2 != 0) return false;
}
allE(x.getLeft());
allE(x.getRight());
return true;
}
When you recurse on allE you need to consider the result of that recursion. Easiest fix I see, change
allE(x.getLeft());
allE(x.getRight());
return true;
to
return allE(x.getLeft()) && allE(x.getRight());
Although, I think it makes more sense to write the algorithm such that you recurse on even explicitly. And allE saves three letters compared to allEven (please use meaningful names for methods and variables). Something like,
static boolean allEven(Tree<Integer> node) {
if (node.isEmpty()) {
return true;
} else if (node.getValue() % 2 == 0) {
return allEven(node.getLeft()) && allEven(node.getRight());
}
return false;
}
You're not using the results of your recursive tests properly.
It should be:
if (x.isEmpty()) {
return true;
}
boolean thisNodeEven = x.getValue() % 2 == 0;
return thisNodeEven && allE(x.getLeft()) && allE(x.getRight());
I suppose you need to evaluate the return-value of the recursive calls:
static boolean allE(Tree<Integer> x)
{
if (x.isEmpty())
return true;
else {
if (x.getValue()%2 != 0) return false;
}
if(!allE(x.getLeft()) || !allE(x.getRight()))
return false;
return true;
}
I keep getting the error: "missing return statement." Isn't my return statement listed 5 times? Does anyone know why I'm getting this and how to fix it? It refers to the second to last bracket. Any help/ideas as to why this is happening is appreciated. Thanks.
public class words
{
// instance variables - replace the example below with your own
private String w;
/**
* Default Constructor for objects of class words
*/
public words()
{
// initialise instance variables
w="";
}
/**
* Assignment constructor
*/
public words(String assignment)
{
w=assignment;
}
/**
* Copy constructor
*/
public words(words two)
{
w=two.w;
}
/**
* Pre: 0<=i<length( )
* returns true if the character at location i is a vowel (‘a’, ‘e’, ‘i', ‘o’, ‘u’ only), false if not
*/
private boolean isVowel(int i)
{
if (w.charAt(i)=='a')
return true;
else if (w.charAt(i)=='e')
return true;
else if (w.charAt(i)=='i')
return true;
else if (w.charAt(i)=='o')
return true;
else if (w.charAt(i)=='u')
return true;
}
}
Tell me what do you return if w.charAt(i) is 'b'.
You need to add a last line:
private boolean isVowel(int i)
{
if (w.charAt(i)=='a')
return true;
else if (w.charAt(i)=='e')
return true;
else if (w.charAt(i)=='i')
return true;
else if (w.charAt(i)=='o')
return true;
else if (w.charAt(i)=='u')
return true;
else return false;
}
private boolean isVowel(int i){
//...
return false;
}
You are missing the case when your i is not a vowel.
Problems
Use brackets. Code without them is annoying to read.
Use the && operator when checking multiple if statements with the same body (all return true)
Use a switch statement if you are comparing the same thing (w.charAt(i)) multiple times but they have different bodies
The actual problem you have here is that if w.charAt(i) is not a vowel then it returns nothing. Include a return statement after all your checks
Use a for loop with an array of vowels
(Note: I have intentionally not included code because it is not helpful to give you the answer. If you don't understand any of the terms used above, comment or google them to understand completely. That will allow you to get the most out of the answer.)
private boolean isVowel(int i)
{
if (w.charAt(i)=='a')
return true;
else if (w.charAt(i)=='e')
return true;
else if (w.charAt(i)=='i')
return true;
else if (w.charAt(i)=='o')
return true;
else if (w.charAt(i)=='u')
return true;
return false;//Default return statement if nothing has matched.
}
--You are missing default return statement. If no match found what your metohd will return ?
--It was the issue, i have updated your code here, if nothing found it will return false.
While other people are explaining the code does not compile because of a missing return statement, I would like to point out you can basically do this as an one liner as shown below.
private boolean isVowel(int i) {
return w.charAt(i) == 'a' || w.charAt(i) == e || w.charAt(i) == 'i' || w.charAt(i) == 'o' || w.charAt(i) == 'u';
}
You are missing the return in your code. Ideally you should not have that many returns.
private boolean isVowel(int i)
{
boolean found=false;
if (w.charAt(i)=='a')
found= true;
else if (w.charAt(i)=='e')
found= true;
else if (w.charAt(i)=='i')
found= true;
else if (w.charAt(i)=='o')
found= true;
else if (w.charAt(i)=='u')
found= true;
return found;
}
You have two options
1. to use a flag like above. you should use brackets, that makes the code easy to read.
2. at the end of your code, just add return false.
public class CodingBat {
public static void main(String[] args){
CodingBat object = new CodingBat();
System.out.print(object.parrotTrouble(true,20));
}
public boolean parrotTrouble(boolean talking, int hour) {
if(talking == false){
return false;
}
else if(hour > 7 || hour >20){
return true;
}
}
}
I am confused, why I am getting an error where the public method parrotTrouble is underlined saying it must return a boolean, which I currently have?
Compiler says you need to return some value since your method return type is boolean.
You have returned false in If condition, and return true in else if condition.
you need to return something outside of if/else if as well.
something as below, as per comments from #Andreas, code can be reduced to below
Original as per OP
public boolean parrotTrouble(boolean talking, int hour) {
if (talking == false) {
return false;
} else if (hour > 7 || hour > 20) {
return true;
}
return booleanValue; // can be true/false as per your logic
}
Edit
public boolean parrotTrouble(boolean talking, int hour) {
return (talking && (hour > 7 || hour > 20));
}
As pointed by #Codebender, wither use if else condition, then no
need to return boolean value at the last, but if you are using if -
else if you have to return boolean value at the last. As compiler is
also not sure that it will go in one of the conditions surely.
public boolean parrotTrouble(boolean talking, int hour) {
boolean answer = false;
if(talking == false){
answer = false;
}
else if(hour < 7 || hour >20){
answer = true;
}
return answer;
}
Thank you for your help, I changed my code to this and it worked correctly and answered the question <3
What do you think will be the best way to refactor this kind of boolean method?
if (someService.isTrue(importantArg)) {
return true;
} else if (someService.isSomeTrue(anotherArg)) {
return isAnotherCondition(entry);
} else {
return super.thisMethod();
}
This is a minor refactoring, but you can remove the elses as you can't reach that code if the previous condition was true (if it was true, it would have returned a value and exited the method)
if (someService.isTrue(importantArg)) {
return true;
}
if (someService.isSomeTrue(anotherArg)) {
return isAnotherCondition(entry);
}
return super.thisMethod();
return someService.isTrue(importantArg) || (someService.isSomeTrue(anotherArg)
&& isAnotherCondition(entry)) || super.thisMethod();