Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
The following question is a pass exam question that I got incorrect, Could someone help me understand what I did wrong and what I needed to do?
The following was given:
Java
public int goodTimes(int n) 
{if (n==1)return 0;else return 1 + goodTimes(n/2); }

From this I needed to determine the value of goodTimes(22)

What I have tried:

I tried creating a class, then I pasted the code above into it, then I created a main method that would call the function.

Java
class GoodTimes{
    public int goodTimes(int n) {
        if (n==1)
        return 0;
        else
        return 1 + goodTimes(n/2); 
        
    } 
}
class Main {
    public static void main(String[] args) {
        System.out.println(goodTimes(22));
    }
}
Posted
Updated 9-Jan-23 5:32am
v2
Comments
Richard MacCutchan 9-Jan-23 10:42am    
And what was the result when you ran the code?
Aisha Yosuf 9-Jan-23 10:44am    
An error occured : error: cannot find symbol

Try
public class GoodTimes
{ 
    public static int goodTimes(int n)
    {
      if (n==1)
        return 0;
      else
        return 1 + goodTimes(n/2);

    }
    public static void main( String args[])
    {
      System.out.println(goodTimes(22));
    }
}

Note your stop condition (n==1) only works with positive values of n.
 
Share this answer
 
Comments
Aisha Yosuf 9-Jan-23 10:49am    
when I ran the code I got an error that says class GoodTimes is public, should be declared in a file named GoodTimes.java
CPallini 9-Jan-23 10:53am    
Do you need to put it in a different file?
Aisha Yosuf 9-Jan-23 10:55am    
The above error says to do so, but I can't remember how I would declare GoodTimes in a file named GoodTimes.java
CPallini 9-Jan-23 10:59am    
Just rename your source file GoodTimes.java
Aisha Yosuf 9-Jan-23 11:05am    
Thank you so much I aprreciate your help! :)
To add to what CPallini says: You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!

I'm not saying we don't want to help you fix them - sometimes I can't see my own errors because I read what I meant to write - but fixing syntax errors is part of the job, and if you can't do it for yourself people are going to look at you as a bit weird should you get a job in the industry!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900