Java PROGRAMING Create a My Currency class as specifies by the UML diagram below
ID: 3799347 • Letter: J
Question
Java PROGRAMING Create a My Currency class as specifies by the UML diagram below, and write a driver program to test it Your class should behave as follows the two String should formal! with two decimals and add a S x should a Ways be a double with at most two decimals no more. Not just rounded on output, but rounded on input as well So in the non-default constructor and you will need to round the value sent to nearest two decimals for example double num.= 3 14499999; My Currency. X in should be 3.14. NOT 3.14499999 Math, round/double) will return the nearest integer value to the double value sent Why? Using a double value for currency sometimes produces more man two decimals especially when figuring percentages for interest etc Even if rounded to two decimals the value used in calculations will be the actual value not the rounded value This may look Ike calculations with currencies are off a bit So. to make sure all adds up. we use values actually net just rounded for output the constructor and matador that take a Siring parameter should allow a leading S. if it is there, strip it off before your try-catch. Now. a rile a program that uses your new My Currency class as follows Track an investment input from the user an, initial investment amount which should ce a minimum of $1,000.00, and an annual interest rate (You may use your method if you wish). Output (to a file) a report that shrews for each month from 0 to 45 {4 years) the interest earned that month and the current value of the if, vestment, interest should be compounded each month That means tie interest earned in any month is added to the value, and the next month interest calculated in this new amount Sample output (only showing first 6 months, you will have 48 in you' file): Loan Value Table. Starting principle: APR 4.00%Explanation / Answer
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Sam
*/
public class MyCurrency {
public MyCurrency(double newX) {
this.x = Math.round(newX*100)/100.0;
}
public MyCurrency(String newX) {
if (newX.startsWith("$"))
newX = newX.substring(1);
try {
this.x = Math.round(Double.parseDouble(newX)*100)/100.0;
} catch (Exception e) {
this.x = 0;
}
}
public double getVal() {
return x;
}
public void setVal(double newX) {
this.x = Math.round(newX*100)/100.0;
}
public void setVal(String newX) {
if (newX.startsWith("$"))
newX = newX.substring(1);
try {
this.x = Math.round(Double.parseDouble(newX)*100)/100.0;
} catch (Exception e) {
this.x = 0;
}
}
@Override
public String toString() {
return "$" + x;
}
private double x;
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
MyCurrency amount = null;
while (true) {
System.out.print("Enter investbent amount: ");
try {
amount = new MyCurrency(br.readLine());
} catch (IOException ex) {
Logger.getLogger(MyCurrency.class.getName()).log(Level.SEVERE, null, ex);
}
if (amount.getVal() > 1000)
break;
System.out.println("Insufficient amount");
}
double rate = 0;
int period = 0;
do{
System.out.print("Enter rate of interest: ");
try {
rate = Double.parseDouble(br.readLine());
} catch (IOException ex) {
Logger.getLogger(MyCurrency.class.getName()).log(Level.SEVERE, null, ex);
} catch (NumberFormatException e){
rate = 0;
}
}while (rate==0);
do{
System.out.print("Enter term of investment: ");
try {
period = Integer.parseInt(br.readLine());
} catch (IOException ex) {
Logger.getLogger(MyCurrency.class.getName()).log(Level.SEVERE, null, ex);
} catch (NumberFormatException e){
period = 0;
}
}while (period<=0 || period>48);
try(PrintWriter pw = new PrintWriter("FILE NAME HERE")) {
pw.println("Month Starting Interest Balance");
for (int i =1; i<=period; i++) {
pw.print(i+" "+amount.toString()+" ");
double interest = amount.getVal()*rate/1200;
pw.print(interest+" ");
amount.setVal(amount.getVal()+interest);
pw.println(amount.toString());
}
} catch (FileNotFoundException ex) {
Logger.getLogger(MyCurrency.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Here you go champ!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.