-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathPenniesForPay.java
More file actions
59 lines (48 loc) · 1.8 KB
/
Copy pathPenniesForPay.java
File metadata and controls
59 lines (48 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.Scanner;
import java.text.*;
/**@author Lemuel M. Uhuru
* @Date June 24, 2013
* Book Title: From Control Structures through Objects by Tony Gaddis
* Page: 267 Challenge 4: Pennies for Pay
*
* @Description Write a program that calculates the amount a person would
* earn over a period of time if his or her salary is one penny the first day,
* two pennies the second day, and continues to double each day. The program should
* display a table showing the salary for each day, and then show the total pay at the
* end of the period. The output should be displayed in a dollar amount, not the number
* of pennies. Do not accept a number less than 1 for the number of days worked.
*/
public class PenniesForPay {
public static void main(String[] args) {
// Declare var to store number of days worked
int numDays;
// User salary
double salary = 0.01;
double totalSalary = 0.01;
// Create scanner object
Scanner keyboard = new Scanner(System.in);
// Decimal formating for salary variables
NumberFormat df = DecimalFormat.getInstance();
df.setMaximumFractionDigits(2);
// Prompt user for input
System.out.print("Enter the number of days worked: ");
numDays = keyboard.nextInt();
// Input validation: Input must not be less than 1
while (numDays < 1){
System.out.print("Enter the number of days worked: ");
numDays = keyboard.nextInt();
}
// Display table header
System.out.println("Day " + " Salary " + "Total Salary");
// Display number of days
int day = 1;
// Construct while loop to display days, daily salary, and total salary.
while (numDays > 0){
System.out.println(day + " $" + salary + " $" + df.format(totalSalary));
salary *= 2;
totalSalary += salary;
day++;
numDays--;
}
}
}