In: Computer Science
/* my calculator is messed up. I need to display interest rate + initial investment. I keep getting huge numbers, Just need to fix my calculator thats all. import java.util.Scanner;//Import java scanner class public class InterestCalculator1 { //create fields / variables private double initialInvestment; private double annualInterestRate; private double numberOfYears; private double investmentReturn; private double finalAmount; Scanner input = new Scanner(System.in);//create new scanner object //constructor public InterestCalculator1(double initialInvestment, double annualInterestRate, double numberOfYears){ super(); this.initialInvestment = initialInvestment; this.annualInterestRate = annualInterestRate; this.numberOfYears = numberOfYears; // this.finalAmount = finalAmount; } //setters and getters public double getInitialInvestment() { return initialInvestment; } public void setInitialInvestment(double initialInvestment) { this.initialInvestment = initialInvestment; } public double getAnnualInterestRate() { return annualInterestRate; } public void setAnnualInterestRate(double annualInterestRate) { this.annualInterestRate = annualInterestRate; } public double getNumberOfYears() { return numberOfYears; } public void setNumberOfYears(double numberOfYears) { this.numberOfYears = numberOfYears; }//end setters and getters //begin userPrompt method public void promptUser() { System.out.println("Enter initial investment amount: ");//prompt user for initial investment amount setInitialInvestment(input.nextDouble());//sets initial investment System.out.println("Enter monthly interest rate: ");//prompt user for monthly interest rate setAnnualInterestRate(input.nextDouble() * 0.01);//converts input double into a decimal percentage rate and sets to annualInterestRate System.out.println("Enter number of years you are planing to leave initial investment in account: "); //prompt user for number of years planing to leave initial investment in account setNumberOfYears(input.nextDouble());//sets number of years planning on leaving investment in account }//ends prompt user method //begin investment calculator method public void investmentCalculator() { investmentReturn = getInitialInvestment() * Math.pow(1 + getAnnualInterestRate(), getNumberOfYears() * 12);//calculates investment return finalAmount = investmentReturn + getInitialInvestment();//final amount equal investment return + initial amount }//ends investment calculator method //begin toString @Override public String toString() { return "With an initial investment of: $" + getInitialInvestment() + "\n" + "at a monthly interest rate of: %" + getAnnualInterestRate() + "\n" + "Left in the account for: " + getNumberOfYears() + " years " + "\n" + "Will provide a return of: $" + String.format("%.02f", investmentReturn )+ "\n" + "and your final amount of: $" + getInitialInvestment() + " plus $" + String.format("%.02f",investmentReturn) + "\n" + "Equals: $" + String.format("%.02f", finalAmount); } }
NOTE:-- If you are stuck somehwere feel free to ask in the comments but do not give negative rating to the question as it affects my answering rights...i will try to help you
Note:->>Check The formula that you provided in the calculateInterest function..if it is fine than i guess the following output is correct...Also when you are using getters and setters to set the data using the prompt function then no need of parametrized constructor...it is just adding of unnecessary code to the program.....the other things are kept almost the same
import java.util.Scanner;//Import java scanner class
import java.io.*;
public class InterestCalculator1 {
//create fields / variables
private double initialInvestment;
private double annualInterestRate;
private double numberOfYears;
private double investmentReturn;
private double finalAmount;
Scanner input = new Scanner(System.in);//create new scanner object
//setters and getters
public double getInitialInvestment() {
return initialInvestment;
}
public void setInitialInvestment(double initialInvestment) {
this.initialInvestment = initialInvestment;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public double getNumberOfYears() {
return numberOfYears;
}
public void setNumberOfYears(double numberOfYears) {
this.numberOfYears = numberOfYears;
}//end setters and getters
//begin userPrompt method
public void promptUser() {
System.out.println("Enter initial investment amount: ");//prompt user for initial investment amount
this.setInitialInvestment(input.nextDouble());//sets initial investment
System.out.println("Enter monthly interest rate: ");//prompt user for monthly interest rate
this.setAnnualInterestRate(input.nextDouble() * 0.01);//converts input double into a decimal percentage rate and sets to annualInterestRate
System.out.println("Enter number of years you are planing to leave initial investment in account: "); //prompt user for number of years planing to leave initial investment in account
this.setNumberOfYears(input.nextDouble());//sets number of years planning on leaving investment in account
}//ends prompt user method
//begin investment calculator method
public void investmentCalculator() {
investmentReturn = getInitialInvestment() * Math.pow(1 + getAnnualInterestRate(), getNumberOfYears() * 12);//calculates investment return
finalAmount = investmentReturn + getInitialInvestment();//final amount equal investment return + initial amount
}//ends investment calculator method
//begin toString
@Override
public String toString() {
return
"With an initial investment of: $" + getInitialInvestment() + "\n" +
"at a monthly interest rate of: %" + getAnnualInterestRate() + "\n" +
"Left in the account for: " + getNumberOfYears() + " years " + "\n" +
"Will provide a return of: $" + String.format("%.02f", investmentReturn )+ "\n" +
"and your final amount of: $" + getInitialInvestment() + " plus $" + String.format("%.02f",investmentReturn) + "\n" +
"Equals: $" + String.format("%.02f", finalAmount);
}
public static void main(String []args)throws IOException{
InterestCalculator1 obj = new InterestCalculator1();
obj.promptUser();
obj.investmentCalculator();
System.out.println(obj);
}
}
Output-->
->