Suppose we add $100 to a checking account in year 1, $200 in year 2, $300 in yea
ID: 3836618 • Letter: S
Question
Suppose we add $100 to a checking account in year 1, $200 in year 2, $300 in year 3, and so on. The account earns no interest. After how many years will the balance reach a given target? Modify the program below to produce the answer. Complete the following file:
//what I added to the given file
import java.util.Scanner;
/**
This program computes the time required to double an investment.
*/
public class Investment
{
public static void main(String[] args)
{
double balance = 0;
int year = 0;
Scanner in = new Scanner(System.in);
System.out.print("Target: ");
double target = in.nextDouble();
// Add $100 in year 1, $200 in year 2, ..., until the
// target has been reached
while (balance != target)
{
year++;
double amount = 100*year;
balance = balance + amount;
}
System.out.println("Year: " + year);
System.out.println("Balance: " + balance);
}
}
from tester:
Testing Investment java Test 1 Expected output Actual output Target 5000 Target 5000 Timed out after 15 seconds Year 10 Balance 5500.0 fail Test 2 Target 1000 Year 4 Balance 1000.0 passExplanation / Answer
Hi
I have modified the condition and highlighted the code changes below
Investment.java
import java.util.Scanner;
/**
This program computes the time required to double an investment.
*/
public class Investment
{
public static void main(String[] args)
{
double balance = 0;
int year = 0;
Scanner in = new Scanner(System.in);
System.out.print("Target: ");
double target = in.nextDouble();
// Add $100 in year 1, $200 in year 2, ..., until the
// target has been reached
while (balance < target)
{
year++;
double amount = 100*year;
balance = balance + amount;
}
System.out.println("Year: " + year);
System.out.println("Balance: " + balance);
}
}
Output:
Target: 5000
Year: 10
Balance: 5500.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.