-
Notifications
You must be signed in to change notification settings - Fork 0
/
TuitionCalculatorFors.java
52 lines (43 loc) · 2.09 KB
/
TuitionCalculatorFors.java
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
/*
Name: Andrew Frongello
Class section: COP2800-2245-2501
Instructor: Jose Aparicio Yanes
Date: 2024-07-03
Task challenge: "Suppose that the tuition for a university is $10,000 this year
and tuition increases 7% every year. Write a program that calculates in how many
years the tuition will be doubled. Use For loops."
Program description: The program first initializes two variables representing the
beginning tuition of 10,000 and the year, AND two constants representing the
annual tuition increase of 7% and the target value of 20,000. DecimalFormat is
used to provide standard financial formatting for the tuition variable for when it
needs to be displayed to the user. A for loop is then used to find the tuition of
each subsequent year until the tuition doubles (reaches the target value of 20,000).
I set the termination expression as when the value of tuition is greater than the
target value. This stops the loop one decrement before crossing the target value.
So, to find the final year, one last increment is performed after the loop. The
result is then shown to the user.
*/
import java.math.BigDecimal;
import java.text.DecimalFormat;
class TuitionCalculatorFors {
public static void main(String[] args) {
BigDecimal tuition = new BigDecimal(10000);
final BigDecimal INCREASE = new BigDecimal(0.07);
final BigDecimal TARGET = new BigDecimal(20000);
int year = 0;
DecimalFormat df = new DecimalFormat("$#,###.00");
String tuitionFormatted = df.format(tuition);
for (year = 1; tuition.compareTo(TARGET) <= 0; year++) {
System.out.println("\nYear: " + year);
System.out.println("Tuition: " + tuitionFormatted);
tuition = tuition.add(tuition.multiply(INCREASE));
tuitionFormatted = df.format(tuition);
}
year = year++;
tuition = tuition.add(tuition.multiply(INCREASE));
tuitionFormatted = df.format(tuition);
System.out.println("\nYear: " + year);
System.out.println("Tuition: " + tuitionFormatted);
System.out.println("\nYour tuition will double after " + year + " years.\n");
}
}