Reputation: 35
Obviously, I'm not looking for all the answers but some direction would be much appreciated.
So I have this program that prompts the user to enter their annual interest rate and savings amount. The program calculates the compound value for 6 months.
I'm having issues calling the new method into the main method to execute the calculations. The main method asks the users for their annual interest rate and the savings amount. It calls the new method, and if all are positive numbers it executes the program. If any are negative it comes up with an error.
I thought I called the method in the last line with calling it but obviously I am wrong. I have tried googling and am having trouble understanding this calling aspect. Any information would be appreciated. I'm new so I'm still working on this programming thing! Not sure why that last curly bracket isn't in the code. Here's the code:
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class FifthAssignment {
static double savingAmount = 0.0; // the 2 given static doubles
static double annualInterestRate = 0.0;
int sixthMonth;
public static double compoundValueMethod(double savingAmount, double annualInterestRate, int sixthMonth) {
return sixthMonth; // sixMonth is the compound value
}
{
DecimalFormat formatter = new DecimalFormat(".00");
double monthlyInterestRate = annualInterestRate / 12;
if (savingAmount < 0)
if (annualInterestRate < 0) {
JOptionPane.showMessageDialog(null, "Error. Both of your values are negative!");
System.exit(0);
}
if (savingAmount < 0) {
JOptionPane.showMessageDialog(null, "Error. Your savings amount is negative!");
}
else if (annualInterestRate < 0) {
JOptionPane.showMessageDialog(null, "Error. Your annual interest rate is negative!");
}
else {
for (int i = 1; i <= 6; i++) {
sixthMonth = (int) ((savingAmount + sixthMonth) * (1 + monthlyInterestRate));
}
}
}
public static void main(String[] args) {
DecimalFormat formatter = new DecimalFormat(".00"); // bring in the
// decimal
// formatting for
// program
String SA = JOptionPane.showInputDialog("What is your savings amount? "); // Window
// pops
// up
// asking
// for
// your
// savings
// amount.
// As
// a
// string?
savingAmount = Double.parseDouble(SA); // Returns a double given to
// value in string above
String AIR = JOptionPane.showInputDialog("What is the annual interest rate? "); // Window
// pops
// up
// asking
// for
// annual
// interest
// rate.
// String
// as
// above.
annualInterestRate = Double.parseDouble(AIR); // Returns the same as
// savings amount but
// for annual interest
// rate
{
JOptionPane.showMessageDialog(null, "Your total compounded value after 6 months is "
+ compoundValueMethod(savingAmount, annualInterestRate, 6));
}
}
}
Upvotes: 0
Views: 105
Reputation: 2419
Function Call should have parenthesis
Try This:
JOptionPane.showMessageDialog(null, "Your total compounded value after 6 months is " + compoundValueMethod(savingAmount,annualInterestRate,6));
Your function should return the computed value, but in your case you are returning 'void', so it should be like this
public static int compoundValueMethod(double savingAmount, double annualInterestRate, int sixthMonth) {
int res = 0;
DecimalFormat formatter = new DecimalFormat(".00");
sixthMonth = 6;
double monthlyInterestRate = annualInterestRate / 12;
if (savingAmount < 0)
if (annualInterestRate < 0) {
JOptionPane.showMessageDialog(null, "Error. Both of your values are negative!");
System.exit(0);
}
if (savingAmount < 0) {
JOptionPane.showMessageDialog(null, "Error. Your savings amount is negative!");
}
else if (annualInterestRate < 0) {
JOptionPane.showMessageDialog(null, "Error. Your annual interest rate is negative!");
}
else {
for (int i = 1; i <= 6; i++) {
res = (int) ((savingAmount + res ) * (1 + monthlyInterestRate));
}
}
return res;
}
Upvotes: 1
Reputation: 70
Based on the current code you have written, the function is not called properly and should look like :
JOptionPane.showMessageDialog(null, "Your total compounded value after 6 months is " + compoundValueMethod(savingAmount,annualInterestRate,6));
If you always want the value of 6 to be passed to your function, then instead of having it as a parameter to the function, you should be better off declaring a static final class variable like this:
private final static int SIXTH_MONTH = 6;
If you want to read the number of months, then yes your function should have that parameter, you should read that parameter just like the other 2 params you are reading and pass it along. And you should remove the line that is assigning the value of 6 to your method parameter.
sixthMonth = 6;
and instead use the value passed in to the function from the main() function
Upvotes: 0