[20 Marks] You just won the lottery – but the prize is paid in a very peculiar w
ID: 672911 • Letter: #
Question
[20 Marks] You just won the lottery – but the prize is paid in a very peculiar way. For the first week, you will receive one cent. For week 2, you will receive 2 cents; 4 cents for week 3; 8 cents for week 4 – and the amount received keeps doubling every week until the end of the year. So at the end of week one, you will have been paid 1 cent; at the end of week two, 3cents(2+1cents);attheendofweekthree,thetotalwillbe7cents(4+3+1)andsoon. Write a program that implements two recursive methods: one that given the week number, it will report the amount to be received for that week (i.e. week 10 pays $5.12); and second method that reports the total amount received to date given the week number (i.e. by the end of week 8, $2.55 has received in total). The program should repeatedly ask the user to indicate the number of weeks and report the results of the two recursive methods. If the user enters 0 weeks, the program will terminate.
Explanation / Answer
/**The java program that prompts user to enter number
* of weeks and display the amount paid for each week
* and display total amount in dollars at end of weeks*/
//Lottery.java
import java.util.Scanner;
public class Lottery
{
public static void main(String[] args)
{
int sentinal=0;
double amount=0;
//Scanner object to read input
Scanner keyboard=new Scanner(System.in);
System.out.println("Enter number of weeks : ");
//read number of weeks
int weeks=keyboard.nextInt();
//continue loop until user enter 0 to stop
while(weeks!=sentinal)
{
System.out.println("Lotter payment system ");
System.out.printf("%-10s%-10s ","Week","Payment(in cents)");
for (int index = 0; index < weeks; index++)
{
//call method1 to find the amount in cents for weeks
amount=method1(index);
System.out.printf("%-10d%-10.2f ",(index+1),amount);
}
//call method2 find total amount paid in total dollars
amount=method2(weeks);
System.out.println("By the end of week "+weeks+", $"+amount/100 +" has received in total.");
//reset amount to zero
amount=0;
System.out.println("Enter number of weeks : ");
//prompt for next value
weeks=keyboard.nextInt();
}
}
/**The method method2 takes the number of weeks and
*calls the method1 to find the number of cents
*for the week paid and adds the total cents added to the
*pay*/
private static double method2(int weeks)
{
double pay=0;
for (int week = 0; week < weeks; week++)
{
pay+=method1(week);
}
return pay;
}
/**The method1 that takes the weeks and returns the number of cents
* paid in that week*/
private static double method1(int weeks)
{
if(weeks==0)
return 1;
else
return 2*method1(weeks-1);
}
}
--------------------------------------------------------------------------------------------------------------------------------
Sample Output:
Enter number of weeks :
8
Lotter payment system
Week Payment(in cents)
1 1.00
2 2.00
3 4.00
4 8.00
5 16.00
6 32.00
7 64.00
8 128.00
By the end of week 8, $2.55 has received in total.
Enter number of weeks :
10
Lotter payment system
Week Payment(in cents)
1 1.00
2 2.00
3 4.00
4 8.00
5 16.00
6 32.00
7 64.00
8 128.00
9 256.00
10 512.00
By the end of week 10, $10.23 has received in total.
Enter number of weeks :
0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.