Results 1 to 5 of 5
  1. #1
    ryamz is offline Member
    Join Date
    Jul 2010
    Posts
    16
    Rep Power
    0

    Post java program check

    Im beginning a java book and am having trouble on one of hte exercises.
    The exercise is

    1. Create a class named Household that includes data fields for the number of occupants and the annual income, as well as methods named setOccupants ( ), setIncome ( ), getOccupants ( ), and getIncome ( ) that set and return those values, respectively. Also, create a constructor that requires no arguments and automatically set the occupants field to 1 and the income field to 0. Name the program as Household.java.
    and here's my code:
    public class household
    {


    private int occupants;
    private double income;

    household house = new household();

    public void setOccupants( int newOccupants)
    {
    occupants = newOccupants;
    house.getOccupants();

    }
    public void setIncome( double income)
    {
    income = 0;
    house.getIncome();


    }
    public int getOccupants()
    {
    return occupants;
    house.setOccupants(16);

    }
    public double getIncome(double income)
    {
    return income;
    house.setIncome(157.73);
    }
    public household()
    {
    occupants = 1;
    income = 0;
    }

    }

    and the problem is this:
    buiLd output
    C:\Documents and Settings\bLah\My Documents\household.java:20: getIncome(double) in household cannot be applied to ()
    house.getIncome();
    ^
    1 error

    Process completed.
    general output
    java.lang.NoSuchMethodError: main
    Exception in thread "main"
    Process completed.
    anyone can heLp me about this? :(

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    Im a beginner but as I see your code to getIncome method is
    Java Code:
        public double [b]getIncome(double income)[/b]
        {
            return income;
            house.setIncome(157.73);
        }
    then you call your method like this
    Java Code:
        public void setIncome( double income)
        {
            income = 0;
            [b]house.getIncome();[/b]
            ...
    Good Luck,
    geje

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    In this simple exercise, your getter method such as getIncome() should not take a parameter. Rather it should simply return the value held by the income field, nothing more.

    setIncome on the other hand should take a value, the new income value, and you should use this parameter to set the income field, and do nothing more in that method. For instance:

    Java Code:
    public class SimpleEg {
       private int myValue;
    
       // all this method does is return the value held by the myValue field
       public int getMyValue() {
          return myValue;
       }
    
       // all this method does is set the value held by the myValue field
       public void setMyValue(int myValue) {
          this.myValue = myValue;
       }
       
       
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by mine0926 View Post
    Im a beginner but as I see your code to getIncome method is
    Java Code:
        public double [b]getIncome(double income)[/b]
        {
            return income;
            house.setIncome(157.73);
        }
    then you call your method like this
    Java Code:
        public void setIncome( double income)
        {
            income = 0;
            [b]house.getIncome();[/b]
            ...
    Good Luck,
    geje
    ???

    To the original poster: mine means well, but please ignore his post.

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    Actually mine0926 did raise a correct point; though he didn't state it well, he pointed out the fact that getIncome() was called with no parameters, but the function declaration was set up to accept one parameter.

    In any case, Fubarable's post has the right information for you.

Similar Threads

  1. Replies: 10
    Last Post: 05-01-2010, 09:51 PM
  2. how to check java
    By funkygarzon in forum New To Java
    Replies: 10
    Last Post: 03-08-2009, 08:39 AM
  3. Please check my java applet
    By mark.7100 in forum Java Applets
    Replies: 3
    Last Post: 07-22-2008, 04:33 PM
  4. CRC check program 1 error
    By javakid9000 in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 05:04 AM
  5. how to check available resources in java program
    By lealea in forum New To Java
    Replies: 3
    Last Post: 08-13-2007, 08:35 PM

Posting Permissions

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