Results 1 to 6 of 6
Thread: Confusion about methods
- 10-02-2011, 07:55 PM #1
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
Confusion about methods
I am having some confusion about methods. I somewhat understand setter and getter methods. I am confused when it comes to void and how to handle methods that do not return or set a new value.
For example, I am working on the following problem:
The class contains:
1. An int data field named id for the account (default 0).
2. A double data field named balance for the account (default 0.0)
3. A double data field named annualInterestRate that stores the current interest rate (default 0.0)
4. A Date data field named dateCreated that stores the date when the account was created.
5. A no-arg constructor that creates a default account.
6. A constructor that creates an account with the specified id and initial balance.
7. The accessor and mutator methods for id, balance, and annualInterestRate.
8. The accessor method for dateCreated.
9. A method named getMonthlyInterestRate() that returns the monthly interest rate.
10. A method named withdraw() that withdraws a specified amount from the account.
11. A method named deposit() that deposits a specified amount to the account.
Then I have to write a test program but I am not worried about that yet.
So, I'm pretty sure I'm good to go up until I get to number ten, creating a method named withdraw() that withdraws a specified amount from the account. From the wording, it is not a get or set method. It appears to be used to subtract a withdraw amount from the account balance. So this is what I have come up with:
The problem is that I get the following error message in Eclipse. I have no idea what it meas and have not been able to find any information on it in Eclipse help.Java Code:public class Account { //declare variables private int id = 0; private double balance = 0.0; private double annualInterestRate = 0.0; private java.util.Date dateCreated; //Construct a default account public Account(){ } //Construct an account with a specified id and initial balance public Account(int newId, double initialBalance){ id = newId; balance = initialBalance; } //return id public int getId(){ return id; } //set a new id public void setID(int id){ this.id = id; } //return balance public double getBalance(double balance){ return balance; } //set new balance public void setBalance(double balance){ this.balance = balance; } //return annualInterestRate public double getannualInterestRate(double annualInterestRate){ return annualInterestRate; } //set a new annualInterestRate public void setAnnualInterestRate(double annualInterestRate){ this.annualInterestRate = annualInterestRate; } //return dateCreated public java.util.Date getDateCreated(){ return dateCreated; } //return monthly interest rate public double getMonthlyInterestRate(){ double monthlyInterestRate = annualInterestRate / 12; return monthlyInterestRate; } //create a main method to withdraw money from account private void double withdraw(double amountToWithdraw){ double withdraw = balance - amountToWithdraw; } }
"Syntax error on toke "void", volatile expected."
- 10-02-2011, 08:49 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Re: Confusion about methods
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-02-2011, 09:16 PM #3
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
Re: Confusion about methods
I see. Thank you. I am still doing something wrong though because even if I change it to this:
or this:Java Code://create a method to withdraw money from account public void double withdraw(){ }
I get the same error message. It just doesn't like that voidJava Code://create a method to withdraw money from account public void double withdraw(double withdraw){ }
- 10-02-2011, 09:21 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 10-02-2011, 09:39 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 14
- Rep Power
- 0
Re: Confusion about methods
Hmm can you declare variables in your class and outside your method?
Anyway that looks weird to me.
But the main thing is a void double, that can't happen. A void returns nothing and a double returns a double, you can't combine those. Also a main method, you probably want to use a main(String[] args) for that?
- 10-02-2011, 09:41 PM #6
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
Confusion here @@' Help!
By pleasurelyours in forum New To JavaReplies: 7Last Post: 06-09-2010, 03:42 PM -
Boolean confusion
By KM88 in forum New To JavaReplies: 13Last Post: 11-02-2009, 12:44 PM -
confusion with methods sleep() and join()
By sandeepsai39 in forum Threads and SynchronizationReplies: 4Last Post: 05-08-2009, 02:32 PM -
Tic Tac Toe confusion
By jigglywiggly in forum New To JavaReplies: 15Last Post: 04-12-2009, 01:47 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks