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.
Related
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;
}
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;
}
}
I know that this method checks if the integer is even, but how exactly? I understood on examples like fibonacci or factorial how recursion works but not on this on. I think I don't get it because of the syntax.
// Assume n >= 0
public static boolean even(int n) {
return (n<=1) ? (n==0) : !even(n-1);
}
What my problem is: It's a boolean method, why is there no "true" or "false"?
How exactly does it check if it's even? If I would do it in if-statements without recursion I would check it like this:
if((n%2) == 0)
return true;
it's a JAVA short if else:
condition ? trueCase: elseCase;
it equals to below syntax:
if(condition){
trueCase;
}
else{
elseCase;
}
In your code:
return (n<=1) ? (n==0) : !even(n-1);
equals to:
if(n<=1)){
if(n==0){
return true;
}
else{
return false;
}
}
else{
if(even(n-1)){
return false;
}
else{
return true;
}
}
The statements
if((n%2) == 0)
return true;
else
return false;
is equivalent to
return (n%2) == 0;
any expression that evaluates to a boolean value can be used instead of directly using one of the two boolean values.
As for the function named "even", it basically says an integer is even if the previous integer is not even, unless it is smaller than one, in which case it is even if it is zero. Since it evaluates to a boolean value, so you can return it as the return value.
You should note that your function does not work for negative values.
public boolean percentDepreciatedOutOfRange() {
if (percentDepreciated < DEPRECIATION_MIN || percentDepreciated > DEPRECIATION_MAX) {
return true;
}
else {
return false;
}
}
Can the code above be written without the else statement and just have return false; after the if statement and still have the same result? If so, why? This is what I mean
public boolean percentDepreciatedOutOfRange() {
if (percentDepreciated < DEPRECIATION_MIN || percentDepreciated > DEPRECIATION_MAX) {
return true;
}
return false;
}
You should just write
public boolean percentDepreciatedOutOfRange() {
return percentDepreciated < DEPRECIATION_MIN || percentDepreciated > DEPRECIATION_MAX);
}
which is more readable. Avoid statements like
If (xxx) { return true; }
else { return false; }
because the if just adds noise around the expression.
The reason why this is legal is that the boolean type is the same in evaluating the if expression and in the return type of the method.
Because return actually exits the function (method).
In the case the if is not entered the rest of the code will execute normally.
Since you have boolean returns, having return false at the end of the function, means everything up to the end failed.
You could even rewrite it to:
public boolean percentDepreciatedOutOfRange() {
if (percentDepreciated < DEPRECIATION_MIN) {
return true;
}
if (percentDepreciated > DEPRECIATION_MAX) {
return true;
}
return false;
}
To clearly state the tests in the method.
Another way I like:
Defining return variable as 1st statement with default value.
Change the variable in method in various biz logic
Return the variable value (Single return statement)
public boolean percentDepreciatedOutOfRange() {
boolean status = false;
status = percentDepreciated < DEPRECIATION_MIN || percentDepreciated > DEPRECIATION_MAX;
return status;
}
I am a beginner and really confused with this code as what happens to boolean value, "done". I have spent two hours understanding this code and I am very frustrated.
public boolean traverse(int row, int column){
boolean done = false;
if(row == grid.length-1 && column == grid[0].length-1)
done = true;
else{
done = traverse(row + 1. column); //down
if(!done) done = traverse (row, column+1)//right
}
if (done) grid[row][column] = PATH;
}
return done;
}
First, "done" is declared FALSE. Then it moves to an if and else statement and becomes local to it.Now, outside the IF and ELSE statement, when it says if(done) do something, it is when done is FALSE or TRUE? Also while in the ELSE statement does (!done) mean that it is FALSE as it is declared TRUE in IF ELSE statement?
In simple words, when you use an IF, IF and ELSE statement with a boolean in it, does it mean it has to be TRUE?
For instance:
boolean love = false;
if(love) do this;
Now, does it mean "do this" when love is false?
The ! operator negotiates the boolean value. That means a statement becomes true if the boolean variable is false. So, for your example this won't be executed:
boolean love = false;
if(love) do this; //not executed
But with a ! it would be executed:
boolean love = false;
if(!love) do this; //executed
In the other case this wouldn't be executed:
boolean love = true;
if(!love) do this; //not executed
EDIT:
For further explanations on if-Statements maybe you will have a look on the official docs by oracle:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
EDIT 2:
Maybe if you write your code this way it is more understandable for you:
public boolean traverse(int row, int column){
boolean done = false;
if(row == grid.length-1 && column == grid[0].length-1) {
done = true;
}else{
done = traverse(row + 1. column); //down
if(!done) {
done = traverse (row, column+1)//right
}
}
if (done) {
grid[row][column] = PATH;
}
return done;
}