Home › Forums › Programming › Java program
- This topic has 7 replies, 5 voices, and was last updated 11 years, 11 months ago by
Anonymous.
-
AuthorPosts
-
-
September 29, 2011 at 3:00 pm #8219
Anonymous
InactiveHi guys,
I’m doing an account example for my java module and am stuck on a tiny bit of code. Basically when I set the annual interest rate to 4.5% in my getMonthlyInterestRate method it doesnt compute anything. Any help would be very much appreciated here is my code:
import java.util.Date ;
public class Exercise08_07
{
public static void main(String[] args)
{
Account account = new Account(1122, 20000) ;
account.setAnnualInterestRate(4.5) ;
account.withdraw(2500) ;
account.deposit(3000) ;
System.out.println("Balance is " + account.getBalance());
System.out.println("Monthly interest is " + account.getMonthlyInterestRate());
System.out.println("This account was created at " + account.getDateCreated());}
}public class Account
{
private int id ;
private double balance ;
private double annualInterestRate ;
private Date dateCreated ;
private double monthlyInterestRate ;public Account()
{
}public Account(int id, double balance)
{
this.id = id ;
this.balance = balance ;
}public int getID()
{
return id ;
}public void setID(int id)
{
this.id = id ;
}public double getBalance()
{
return balance ;
}public void setBalance(double balance)
{
this.balance = balance ;
}public double getAnnualInterestRate()
{
return annualInterestRate ;
}public void setAnnualInterestRate(double annualInterestRate)
{
annualInterestRate = annualInterestRate / 100 ;
}public Date getDateCreated()
{
return dateCreated = new Date() ;
}public double getMonthlyInterestRate()
{
return monthlyInterestRate = getAnnualInterestRate() / 12 ;
}public void withdraw(double amount)
{
balance = balance – amount ;
System.out.println("Money Withdrawn") ;
System.out.println("New Balance is " + balance) ;
}public void deposit(double amountDeposited)
{
balance = balance + amountDeposited ;
System.out.println("Money Deposited") ;
System.out.println("New Balance is " + balance) ;
}
} -
September 29, 2011 at 3:26 pm #47431
Anonymous
InactiveFirst of all this is not a programming support forum…… :wink:
Second of all if you ever get a job in a bank let me know which one…..
i.e setBalance in public (getBalance should total any account transactions ;-) )Setting a balance should never be public, it should be based on transactions, even when you add points to your "live"/psn account this is as a result of a redeem code tx or a credit card tx.
Now to the code issue, missing this in front of annualInterestRate
Using this refers to the class annualInterestRate as opposed to the method annualInterestRatepublic void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate / 100;
}Happy coding …. by the way get cracking on your C++ too :D
-
September 29, 2011 at 3:31 pm #47432
Anonymous
InactiveCool excellent stuff everything duly noted! :)
Your not a lecturer of mine by any chance with the whole c++ thing or are you an active member in the civil programming war between java and c++ :D
Thanks
-
September 29, 2011 at 3:51 pm #47433
Anonymous
InactiveNo civil war just prefer C++ 8)
-
September 29, 2011 at 3:56 pm #47434
Anonymous
InactiveBTW Java runs on top of something called a JVM …. written in C.
-
September 30, 2011 at 8:29 am #47438
Anonymous
InactiveRaw machine code is where it’s at really. Also, C is not C++ as you know ;)
-
October 1, 2011 at 12:46 pm #47440
Anonymous
InactiveThey’re all just tools. One tool suits one purpose, another tool suits a different one, and the world keeps spinning round.
-
October 3, 2011 at 8:59 am #47441
Anonymous
InactiveAlso, so save you confusing problems like this, don’t give your class and function variables the same name…that’s bad practice and will give you headaches.
-
-
AuthorPosts
- The forum ‘Programming’ is closed to new topics and replies.