Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Unable to resolve Java error

I am trying to complete a program in Java but I can not figure out how to remove 2 errors. The errors seem simple but I have not had any luck resolving them. Desperately trying to get the test program to run. Can anybody help? My code is below.

// SavingsAccount.java
// Represents a bank account
import java.util.Scanner;

public class SavingsAccount
{
private double savingsBalance; // funds available for withdrawal
private static double annualInterestRate = 0.04; //interest rate
private double calculateMonthlyInterest;
private double modifyInterestRate;

// Account constructor initializes attributes
public SavingsAccount( double balance ) //
{
savingsBalance = balance;
} // end SavingsAccount constructor

public void calculateMonthlyInterest ()
{
calculateMonthlyInterest = monthlyInterest;
savingsBalance += ((savingsBalance * annualInterestRate) / 12);
}

public void modifyInterestRate (double balance)
{
annualInterestRate = balance;
}

// returns savings balance
public double getSavingsBalance()
{
return savingsBalance;
} // end getSavingsBalance

} // end class Account

//Program4: SavingsAccountTest.java
// Create and manipulate an Account object.
import java.util.Scanner;

public class SavingsAccountTest
{
// main method begins execution of Java application
public static void main( String args[] )
{
//Test class SavingsAccount. Instantiate two savingsAccount objects, saver1 and saver2,
//with balances of $2000.00 and $3000.00, respectively. Set annualInterestRate to 4%,
//then calculate the monthly interest and print the new balances for both savers.
//Then set the annualInterestRate to 5%, calculate the next month's interest and print the new balances for both savers.

SavingsAccount saver1 = new Account(2000.00); // create Savings Account object
SavingsAccount saver2 = new Account(3000.00); // create Savings Account object

//
saver1.calculateMonthlyInterest(.04);
saver2.calculateMonthlyInterest();
System.out.printf( "Saver 1 Savings Balance: $%.2f\n",
saver1.getSavingsBalance() );
saver2.calculateMonthlyInterest();
System.out.printf( "Saver 2 Savings Balance: $%.2f\n",
saver2.getSavingsBalance() );

saver1.calculateMonthlyInterest(.05);
saver2.calculateMonthlyInterest();
System.out.printf( "Saver 1 Savings Balance: $%.2f\n",
saver2.calculateMonthlyInterest();
System.out.printf( "Saver 2 Savings Balance: $%.2f\n",
saver2.calculateMonthlyInterest();

} // end main

} // end class AccountTest
[2980 byte] By [needabreak] at [2007-11-11 9:58:45]
# 1 Re: Unable to resolve Java error
what were your errors?
masher at 2007-11-11 22:32:12 >
# 2 Re: Unable to resolve Java error
Like masher said, it's very hard to analyze the script if you do not give us the errors that the compiler noted...
Shsep at 2007-11-11 22:33:13 >
# 3 Re: Unable to resolve Java error
from what i can see

SavingsAccount saver1 = new Account(2000.00); // create Savings Account object
SavingsAccount saver2 = new Account(3000.00); // create Savings Account object

should be

SavingsAccount saver1 = new SavingsAccount(2000.00); // create Savings Account object
SavingsAccount saver2 = new SavingsAccount(3000.00); // create Savings Account object

well thats what i think it should be anyways
anubis at 2007-11-11 22:34:11 >
# 4 Re: Unable to resolve Java error
calculateMonthlyInterest = monthlyInterest;
What is montlyInterest? I don't see it declared anywhere in your class.
This code snippet doesn't do anything logical.
Not to mention that you never set a value for calculateMonthlyInterest, so your going to get whatever garbage is in that memory location and will most likely crash your program. Major logic problem in this method.

saver1.calculateMonthlyInterest(.05);
your calculateMonthlyInterest method doesn't take any arguments.

You don't need to use System.out.printf() because your not giving it a format, print() or println() would workd just as well.

You import scanner, but never use it, which is a good thing becuase scanner is buggy.
Red_Jester at 2007-11-11 22:35:11 >
# 5 Re: Unable to resolve Java error
haha, i think those are the correct errors^^^^^
anubis at 2007-11-11 22:36:15 >