Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: I thought that this would be right?

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I thought that this would be right?

    Here is the prompt I got: Suppose you have one cent ($0.01) in a sock. Each day you double the amount of money you have in the sock. Thus on day one you have one cent, on day two you have two cents ($0.02), on day three you have four cents ($0.04), and so forth.

    The doubleEachDay method takes a double value, jackpot, as its input argument, and calculates the number of days needed to reach or exceed the jackpot amount, starting at 0.01 and doubling each day. The method returns the number of days required for doubling; this value should be stored in the integer variable numDays.

    Finish the doubleEachDay method below.
    public int doubleEachDay(double jackpot) {
      double amount = 0.01;
      int numDays = 0;
     
    Finish the method here
     
    return numDays;
    }

    So what I thought would work was
    while(double amount < jackpot){
      amount=(amount*2);
      numDays++;

    but that gave me a weird error message
    "compilation error (line 1, column 14):
    '.class' expected"

    So i feel like my code should have worked but obviously it didn't


  2. #2
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: I thought that this would be right?

    We cant know what your error is without knowing whats on the line you get an error on is. Your code seems fine, but the error is somewhere else, prolly inside the main function where you run your stuff etc.

    Link the whole code.

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I thought that this would be right?

    This is the whole code. The error says that it should say '.class' right after 'double amount' which makes no sense
    This is my most recent code that I plugged in in the blank space.
    while(amount > jackpot){
      amount=(amount*2);
      numDays++;
    now in this I dont get an error but i get a regular feedback from the online program im using for school which just says.
    The system has detected compilation errors. This could be caused by:
    Missing semicolon ; at the end of a statement.
    Unclosed braces {}.
    Unclosed parentheses ().
    Unterminated string literals "".
    Invalid method signature.
    Missing return statement.
    Redeclared variable or data member.
    etc.
    So it could be any of those things
    Last edited by jwb4291; June 29th, 2010 at 02:43 PM.

  4. #4
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: I thought that this would be right?

    If thats all the code you have in your class, you havent declared the class. EVERY .java file you create is a CLASS. The compiled files' extension is even .CLASS. The absolute first thing you do when you make a new java program is to name the file, then write this piece of code:

    public class [B][I]FileName[/I][/B] {
     
    //Code here, remember a main method if you want it to be runnable.
     
    }

  5. #5
    Junior Member
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I thought that this would be right?

    It is a homework assignment, the only place that I can actually type any code in is after
    in numDays = 0;
    code goes here
    return numDays;
    }
    it is just completing the method but Im not sure what to put there because I thought what I was putting wouldve worked

  6. #6
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: I thought that this would be right?

    public int doubleEachDay(double jackpot) {
      double amount = 0.01;
      int numDays = 0;
     
      while(amount [B][I]>[/I][/B] jackpot){ [B][I]<-- amount < jackpot, "is less than"[/I][/B]
        amount=(amount*2);
        numDays++;
        }
     
    return numDays;
    }

    This method is now complete, if it doenst work; theres something else wrong with the class. The method declares everything it needs and it returns an int as promised. (Note the error you did in the while-loop)

  7. #7
    Junior Member
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I thought that this would be right?

    Quote Originally Posted by Charlie View Post
    public int doubleEachDay(double jackpot) {
      double amount = 0.01;
      int numDays = 0;
     
      while(amount [B][I]>[/I][/B] jackpot){ [B][I]<-- amount < jackpot, "is less than"[/I][/B]
        amount=(amount*2);
        numDays++;
        }
     
    return numDays;
    }

    This method is now complete, if it doenst work; theres something else wrong with the class. The method declares everything it needs and it returns an int as promised. (Note the error you did in the while-loop)
    Yeah that didnt work. This is the feedback I got
    The input value for jackpot was 910.7072635769393

    Your code produced:
    0

    The correct solution produced:
    17

  8. #8
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: I thought that this would be right?

    Did you actually swap the > for a <?