Results 1 to 6 of 6
  1. #1
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default 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:

    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;		
      }	
    }
    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.

    "Syntax error on toke "void", volatile expected."

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,508
    Blog Entries
    7
    Rep Power
    20

    Default Re: Confusion about methods

    Quote Originally Posted by jim01 View Post
    Java Code:
      //create a main method to withdraw money from account
      private void double withdraw(double amountToWithdraw){
        double withdraw = balance - amountToWithdraw;		
      }
    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.

    "Syntax error on toke "void", volatile expected."
    A method can have only one return type: either void (when it doesn't return anything), or double if it returns a value of type double; you tried to specify both.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default Re: Confusion about methods

    Quote Originally Posted by JosAH View Post
    A method can have only one return type: either void (when it doesn't return anything), or double if it returns a value of type double; you tried to specify both.

    kind regards,

    Jos
    I see. Thank you. I am still doing something wrong though because even if I change it to this:

    Java Code:
      //create a method to withdraw money from account
      public void double withdraw(){
      }
    or this:

    Java Code:
      //create a method to withdraw money from account
      public void double withdraw(double withdraw){
      }
    I get the same error message. It just doesn't like that void

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,508
    Blog Entries
    7
    Rep Power
    20

    Default Re: Confusion about methods

    Quote Originally Posted by jim01 View Post
    I see. Thank you. I am still doing something wrong though because even if I change it to this:

    Java Code:
      //create a method to withdraw money from account
      public void double withdraw(){
      }
    or this:

    Java Code:
      //create a method to withdraw money from account
      public void double withdraw(double withdraw){
      }
    I get the same error message. It just doesn't like that void
    Now you're doing it again "void double" is two return types, which is wrong; it is either "void" or "double", not both of them.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Aero is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default 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?

  6. #6
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default Re: Confusion about methods

    Quote Originally Posted by JosAH View Post
    Now you're doing it again "void double" is two return types, which is wrong; it is either "void" or "double", not both of them.

    kind regards,

    Jos
    Ahh, I get it! (finally). When you said return type I focused on the body of the method instead of the header. Don't ask me why because I do not have a good excuse.

    Thank you for your help and patience.

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Confusion here @@' Help!
    By pleasurelyours in forum New To Java
    Replies: 7
    Last Post: 06-09-2010, 03:42 PM
  3. Boolean confusion
    By KM88 in forum New To Java
    Replies: 13
    Last Post: 11-02-2009, 12:44 PM
  4. confusion with methods sleep() and join()
    By sandeepsai39 in forum Threads and Synchronization
    Replies: 4
    Last Post: 05-08-2009, 02:32 PM
  5. Tic Tac Toe confusion
    By jigglywiggly in forum New To Java
    Replies: 15
    Last Post: 04-12-2009, 01:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •