Reputation: 1
My interest category for the line is "6.5" when it should be "65". The numbers stay same for each year, when they should be updating every year. Also, I can't seem to figure out how to get my new initial balance in the second year to update. By update I mean the new initial balance for the second year should be the ending balance of the first year, and so on.
My second problem is with my return statement calcInterest();
. I am having trouble carrying my result to a different method. Thanks for any and all help!
import java.util.*;
import java.text.*;
public class Interest {
public static void main(String[] args) {
// TODO Auto-generated method stub
printIntro(); // Inform user what program will compute
Scanner console = new Scanner(System.in);
System.out.print("What is the deposit balance ?: ");
double balance = console.nextDouble();
System.out.println(balance);
System.out.print("What is the interest rate ?: ");
double rate = console.nextDouble();
System.out.println(rate);
System.out.print("How many years?: ");
int years = console.nextInt();
System.out.println(years);
printTable(years, balance, rate);
}
public static void printIntro() {
System.out.println("This program will calculate interest on a deposit of your choosing over a specified period.");
}
public static void printTable(int numRows, double balance, double rate) {
System.out.println("Year" + "\t" + "Balance" + "\t" + "\t" + "Interest" + "\t" + "New Balance");
System.out.println("----" + "\t" + "-------" + "\t" + "\t" + "--------" + "\t" + "-----------");
for (int i = 1; i <= numRows; i++) {
printRow(i, balance, rate);
}
}
public static void printRow(int rowNum, double balance, double interest) {
System.out.println(rowNum + "\t" + balance + "\t" + "\t" + interest + "\t" + "\t" + (balance + interest));
balance = (balance + interest);
}
public static double calcInterest(double balance, double rate) {
double interest = balance * (rate / 100);
return interest;
}
}
Upvotes: 0
Views: 508
Reputation: 106440
I don't think you have a small problem; I think you have a large problem instead:
There is no implicit state in your application, so your calculations are broken.
(Also, you're not even using your last method.)
This is more of an architectural problem more than anything else, so instead of posting a full solution, I'll highlight just this method.
public static void printRow(int rowNum, double balance, double interest) {
System.out.println(rowNum + "\t" + balance + "\t" + "\t" + interest + "\t" + "\t" + (balance + interest));
balance = (balance + interest);
}
Java is, and always will be, pass-by-value. What this means for your application is that an operation such as balance = (balance + interest)
will never take any effect, because the change is only ever done in the scope of your printRow
method. This means that your values will stay the same, even though that's not what you intended to do.
The likely fixes here are:
balance
balance
I want to leave this as an exercise for the reader, since it's important for you to work through these issues on your own and explore what is appropriate for your application. I and other professionals may have approaches on how to solve it, but it's critical that you reach your own conclusions, too.
Upvotes: 1