The document describes modifying the TruncatedDollarFormat class to truncate amounts to two decimal places when output. It provides the code for the TruncatedDollarFormat and TruncatedBankAccount classes, with the latter using the former to output balances compounded annually, monthly, and daily over 10 years. The output is shown as a sample run at the end.
1. This project calls for the modification of the "DollarFormat" class that is in listing 6.14 (p. 424-
425) of the textbook. Access the file named "DollarFormat.java" and modify as instructed in
the problem statement. Access the test application to test your modifications when you produce
"TruncatedDollarFormat". Start by going into JGRASP and open "DollarFormat.java",
rename the class to "TruncatedDollarFormat" and save the file as
"TruncatedDollarFormat.java", then make the necessary modifications as defined in the
problem statement, save and compile the file, then open the "TruncatedBankAccount"
application. Review the TruncatedBankAccount source code to see how each of the methods are
called. DO NOT make any changes to the TruncatedBankAccount application but instead modify
your TruncatedDollarFormat class to accommodate it.
this is the file that needs to be modified
public class TruncatedDollarFormat
{
/**
Displays amount in dollars and cents notation.
Rounds after two decimal places.
Does not advance to the next line after output.
*/
public static void write(double amount)
{
if (amount >= 0)
{
System.out.print('$');
writePositive(amount);
}
else
{
double positiveAmount = -amount;
System.out.print('$');
System.out.print('-');
writePositive(positiveAmount);
}
}
2. //Precondition: amount >= 0;
//Displays amount in dollars and cents notation. Rounds
//after two decimal places. Omits the dollar sign.
private static void writePositive(double amount)
{
int allCents = (int)(Math.round(amount * 100));
int dollars = allCents / 100;
int cents = allCents % 100;
System.out.print(dollars);
System.out.print('.');
if (cents < 10)
System.out.print('0');
System.out.print(cents);
}
/**
Displays amount in dollars and cents notation.
Rounds after two decimal points.
Advances to the next line after output.
*/
public static void writeln(double amount)
{
write(amount);
System.out.println( );
}
}
this is the test file
import java.util.*;
public class TruncatedBankAccount
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
3. double initialBalance;
double interestRate;
double newBalance; // after 10 years
char answer; // sentinel to repeat or end program
int iteration; // loop counter
do // repeat entire program if user says yes
{
System.out.println("Please enter an initial balance "
+ "(dollars.cents):");
initialBalance = keyboard.nextDouble();
System.out.println
("Please enter an interest rate in percent "
+ "(e.g. 5.25):");
interestRate = keyboard.nextDouble();
System.out.print("In ten years an initial balance of ");
TruncatedDollarFormat.writeln(initialBalance);
System.out.println(" at an interest rate of "
+ interestRate + "% will be worth:");
// compound annually
newBalance = initialBalance;
for(iteration =1; iteration <= 10; ++iteration)
newBalance = newBalance
+ (interestRate/100) * newBalance;
TruncatedDollarFormat.write(newBalance);
System.out.println(" compounded annually");
// compound monthly
newBalance = initialBalance;
for(iteration =1; iteration <= 120; ++iteration)
newBalance = newBalance
+ (interestRate/12) * (newBalance/100);
TruncatedDollarFormat.write(newBalance);
4. System.out.println(" compounded monthly");
// compound daily
newBalance = initialBalance;
for(iteration =1; iteration <= 3650; ++iteration)
newBalance = newBalance
+ (interestRate/365) * (newBalance/100);
TruncatedDollarFormat.write(newBalance);
System.out.println(" compounded daily");
System.out.println();
System.out.println("Do you want to do it again?");
System.out.println("Enter y for yes or n for no.");
answer = keyboard.next().charAt(0);
}while ((answer == 'y') || (answer == 'Y'));
}
}
the output should look like this
Solution
Hi friend, I did not get your question/requirement.
The code that you have posted is working according to your requirement.
It produces required result.
Please let me know in case of any modification. Please specify your requirement clearly
################ TruncatedDollarFormat.java #############
public class TruncatedDollarFormat
{
/**
Displays amount in dollars and cents notation.
Rounds after two decimal places.
Does not advance to the next line after output.
*/
public static void write(double amount)
{
if (amount >= 0)
5. {
System.out.print('$');
writePositive(amount);
}
else
{
double positiveAmount = -amount;
System.out.print('$');
System.out.print('-');
writePositive(positiveAmount);
}
}
//Precondition: amount >= 0;
//Displays amount in dollars and cents notation. Rounds
//after two decimal places. Omits the dollar sign.
private static void writePositive(double amount)
{
int allCents = (int)(Math.round(amount * 100));
int dollars = allCents / 100;
int cents = allCents % 100;
System.out.print(dollars);
System.out.print('.');
if (cents < 10)
System.out.print('0');
System.out.print(cents);
}
/**
Displays amount in dollars and cents notation.
Rounds after two decimal points.
Advances to the next line after output.
*/
public static void writeln(double amount)
{
write(amount);
System.out.println( );
}
6. }
################# TruncatedBankAccount.java #################
import java.util.*;
public class TruncatedBankAccount
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
double initialBalance;
double interestRate;
double newBalance; // after 10 years
char answer; // sentinel to repeat or end program
int iteration; // loop counter
do // repeat entire program if user says yes
{
System.out.println("Please enter an initial balance "
+ "(dollars.cents):");
initialBalance = keyboard.nextDouble();
System.out.println
("Please enter an interest rate in percent "
+ "(e.g. 5.25):");
interestRate = keyboard.nextDouble();
System.out.print("In ten years an initial balance of ");
TruncatedDollarFormat.writeln(initialBalance);
System.out.println(" at an interest rate of "
+ interestRate + "% will be worth:");
// compound annually
newBalance = initialBalance;
for(iteration =1; iteration <= 10; ++iteration)
newBalance = newBalance
+ (interestRate/100) * newBalance;
TruncatedDollarFormat.write(newBalance);
System.out.println(" compounded annually");
// compound monthly
newBalance = initialBalance;
for(iteration =1; iteration <= 120; ++iteration)
7. newBalance = newBalance
+ (interestRate/12) * (newBalance/100);
TruncatedDollarFormat.write(newBalance);
System.out.println(" compounded monthly");
// compound daily
newBalance = initialBalance;
for(iteration =1; iteration <= 3650; ++iteration)
newBalance = newBalance
+ (interestRate/365) * (newBalance/100);
TruncatedDollarFormat.write(newBalance);
System.out.println(" compounded daily");
System.out.println();
System.out.println("Do you want to do it again?");
System.out.println("Enter y for yes or n for no.");
answer = keyboard.next().charAt(0);
}while ((answer == 'y') || (answer == 'Y'));
}
}
/*
Sample Run:
Please enter an initial balance (dollars.cents):
1265.54
Please enter an interest rate in percent (e.g. 5.25):
5.43
In ten years an initial balance of $1265.54
at an interest rate of 5.43% will be worth:
$2147.42 compounded annually
$2175.53 compounded monthly
$2178.11 compounded daily
Do you want to do it again?
Enter y for yes or n for no.
n
*/