Please help me on this BASIC Java programming question...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • codyland
    New Member
    • Jun 2008
    • 1

    Please help me on this BASIC Java programming question...

    My professor gave us the following assignment:

    Write a Java program to compute the number of days for the following scheme. The recipient receives a penny on day 1 and the amount is doubled on subsequent days. That is on day 2 the amount received is 2, on day three 4 pennies, day four 8 pennies and so on. The recipient accumulates the amount received. Write a program to compute how many days will it take for the recipient to accumulate more than 100 dollars.

    Here is the code i came up with but continue to receive errors when compiling...wha t is wrong? :-(

    import java.util.*;

    public class assignment4 {

    public assignment4() {
    }
    public static void main(String[] args);{
    }

    int dailyAmount, numberOfDays;
    double totalAmount;

    Scanner keyboard = new Scanner(System. in);

    totalAmount = 0;
    dailyAmount = 1;
    numberOfDays = 1;

    while (totalAmount <= 100)
    {
    totalAmount = dailyAmount*2;
    numberOfDays++;
    dailyAmount = dailyAmount*2;
    }
    if (totalAmount >= 100)
    System.out.prin tln("The total number of days it took for the balance to equal $100 was" + (numberOfDays)) ;


    }

    Thank you in advance for any help you can give...
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Do the Maths first. You have a geometric progression with a = 1 and r = 2.
    The sum of the first n terms is given by
    a(1- r(^n)) / (1 - r).
    You have been given the required sum as 100, so rearrange the above
    for n.
    You can then write a function based on your equation for n.
    Realize that while n in your function may be non-integral, you'd need to round it up to give the correct response to the question.

    P.S When posting compilation problems, include the error message reported by the compiler.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by codyland
      [code=java]public class assignment4 {

      public assignment4() {
      }
      public static void main(String[] args);{
      }

      int dailyAmount, numberOfDays;
      double totalAmount;

      // and more statements follow ...
      }
      [/code]
      The structure of that class doesn't make any sense: statements should be put
      in the body of methods and constructors, not somewhere else in a class.

      Like this:

      [code=java]
      public class X {
      public X() { // your constructor
      // statements for your ctor go here ...
      }
      public static void main(String args) { // the main entry point of your program
      // statements go here ..
      }
      }[/code]

      kind regards,

      Jos

      Comment

      Working...