www.webdeveloper.com
Results 1 to 3 of 3

Thread: Beginner in Java, need help with homework

Hybrid View

  1. #1
    Join Date
    Feb 2012
    Posts
    7

    Beginner in Java, need help with homework

    Hi guys, I'm a newbie to this forum and to programming in general. My assignment is to instantiate three separate household objects and then use get and set methods to change the occupants and income, then have the program print out the number of occupants in each house and the income. So far I've got the Household class working but when I try to use it as a method in TestHousehold, I get the error messages "non-static method cannot be referenced from a static context." This is a beginning class and I'm supposed to keep the code as simple and straightforward as possible.

    Here's what I have so far, both classes:

    public class Household
    {
    private int Occupants;
    private double Income;

    Household()
    {
    Occupants = 1;
    Income = 0.0;
    }

    Household(int Occ)
    {
    Occupants = Occ;
    Income = 0.0;
    }

    Household(double Inc)
    {
    Occupants = 1;
    Income = Inc;
    }

    public int getOccupants()
    {
    return Occupants;
    }

    public void setOccupants(int Occ)
    {
    Occupants = Occ;
    }

    public double getIncome()
    {
    return Income;
    }

    public void setIncome(double Inc)
    {
    Income = Inc;
    }
    }

    ********************************************
    public class TestHousehold
    {
    public static void main(String[] args)
    {
    Household House1 = new Household();
    Household House2 = new Household();
    Household House3 = new Household();

    System.out.println("The house before using the set methods has " +

    Household.getOccupants() + " occupants and an income of " + Household.getIncome());


    House1.setOccupants(3);
    House2.setOccupants(2);
    House3.setOccupants(7);

    House1.setIncome(55000.00);
    House2.setIncome(0.00);
    House3.setIncome(80000.00);

    System.out.println("The first household has " + Household.getOccupants() + " occupants

    " + "and the second household has " + Household.getOccupants() + " occupants.");

    }
    }

    Can anyone tip me off as to what I'm doing wrong? Also, this may be a stupid question but can I format currency in java? Thanks in advance for the help.

  2. #2
    Join Date
    Oct 2005
    Posts
    843
    The problem is here:
    Quote Originally Posted by CaptainLaurie View Post
    Household.getOccupants() + " occupants and an income of " + Household.getIncome());
    If you look inside your Household class, you will see non of your methods are static, meaning that the class they are within needs to be instantiated before you can utilize them.

    The getOccupants method you created inside your Household class is merely a getter and will only return the value of the int Occupants that is stored within the instantiated Household class. In your code it would be House1, House2 or House3 that would called getOccupants() like:
    Code:
    Household House1 = new Household();
    
    // This would return 1 right away
    System.out.println(House1.getOccupants());
    
    House1.setOccupants(4);
    
    // Now this would return 4
    System.out.println(House1.getOccupants());
    So if you wanted to get the total off all occupants, you would need to create an int, and store the summation of all three of your houses.

    As to formatting currency, here is a wonderful StackOverflow post explaining multiple ways to do so: http://stackoverflow.com/questions/2...-number-format

    I hope all that answers your questions / solves your trouble. If not, let me know what Isn't clear and I can try and clarify more.

    EDIT: I just noticed you did the same thing for getIncome(), so everything would also apply to that. Its easy to do with loops and lists if you have gotten that far but if not just utilize what you've learnt thus far.
    Last edited by welsh; 02-13-2012 at 10:46 PM.
    Welsh

  3. #3
    Join Date
    Feb 2012
    Posts
    2
    "static" means a class function, not a object function.
    House1.getOccupants() is a object function call.---definiens need not keywords "static"
    Household.getOccupants() is a class function call.----definiens need keyword "static"

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles