Rewrite the following program using the DecimalFormat class
Rewrite the following program using the DecimalFormat class so that your output looks like that below. Once again, the example is not calculated as 3/10ths of a percent.
Welcome to NIU Investments!
Please enter the amount you would like to invest today: 34543.25
Total Investment: $34,543.25
Service Charge: $22.33
-------------
Total Amount Due: $34,565.58
Thank you for your investment!
Program:
package org.students;
import java.util.Scanner;
public class NIUInvestments {
//Declaring constant
public static final double SCHARGE=0.0006464;
public static void main(String[] args) {
//Declaring variables
double total_investment,service_charge,total_amount_due;
//Scanner class object is used read the inputs entered by the user
Scanner sc=new Scanner(System.in);
System.out.println(\"Welcome to NIU Investments!\");
//This loop continues to execute until the suer enters a valid number
while(true)
{
//getting the total investment entered by the user.
System.out.print(\"Please enter the amount you would like to invest today:\");
total_investment=sc.nextDouble();
/*Checking the total investment is with the range
* If not,Display the error message end prompt to enter again
*/
if(total_investment<0 || total_investment>99999999.99)
{
System.out.println(\"::Invalid Number.Total investment must be greater than 0 and less than 99999999.99 ::\");
continue;
}
else
break;
}
//Calculating the service charge
service_charge=total_investment*SCHARGE;
//calculating the total amount due
total_amount_due=total_investment+service_charge;
//Displaying the total investment
System.out.printf(\"\ Total Investment (in dollars):\\t%.2f\ \",total_investment);
//Displaying the service charge
System.out.printf(\" Service Charge (in dollars):\\t%.2f\ \",service_charge);
System.out.println(\"\\t\\t-----------\");
//Displaying the total amount due
System.out.printf(\"Total Amount Due (in dollars):%.2f\ \",total_amount_due);
System.out.println(\"\ Thank you for your investment!\");
}
}
____________________________________________
Output:
Welcome to NIU Investments!
Please enter the amount you would like to invest today:34543.25
Total Investment (in dollars): 34543.25
Service Charge (in dollars): 22.33
-----------
Total Amount Due (in dollars):34565.58
Thank you for your investment!
________________________Thank You
Solution
Here 3% percent means 3/100=0.03. where as 3/10th percent means 3/10X100 %=30%..But according to the problem the service charge 22.33 is not 3/10th% of 34543.25 Should I have to Modify.Where as 3/10th% of 34543.25 is 10362.975.
Could you please clarify .So that the output will be accurate.
_______________________
NIUInvestments.java
import java.text.DecimalFormat;
import java.util.Scanner;
public class NIUInvestments {
//Declaring constant
public static final double SCHARGE=0.0006464;
public static void main(String[] args) {
//Declaring variables
double total_investment,service_charge,total_amount_due;
DecimalFormat df=new DecimalFormat(\"#.##\");
//Scanner class object is used read the inputs entered by the user
Scanner sc=new Scanner(System.in);
System.out.println(\"Welcome to NIU Investments!\");
//This loop continues to execute until the suer enters a valid number
while(true)
{
//getting the total investment entered by the user.
System.out.print(\"Please enter the amount you would like to invest today:\");
total_investment=sc.nextDouble();
/*Checking the total investment is with the range
* If not,Display the error message end prompt to enter again
*/
if(total_investment<0 || total_investment>99999999.99)
{
System.out.println(\"::Invalid Number.Total investment must be greater than 0 and less than 99999999.99 ::\");
continue;
}
else
break;
}
//Calculating the service charge
service_charge=total_investment*SCHARGE;
//calculating the total amount due
total_amount_due=total_investment+service_charge;
//Displaying the total investment
System.out.println(\"Total Investment :$\"+df.format(total_investment));
//Displaying the service charge
System.out.println(\" Service Charge :$ \"+df.format(service_charge));
System.out.println(\"\\t\\t-----------\");
//Displaying the total amount due
System.out.println(\"Total Amount Due:$\"+df.format(total_amount_due));
System.out.println(\"\ Thank you for your investment!\");
}
}
______________________________
Output:
Welcome to NIU Investments!
Please enter the amount you would like to invest today:34543.25
Total Investment :$34543.25
Service Charge :$ 22.33
-----------
Total Amount Due:$34565.58
Thank you for your investment!
__________________________________


