Results 1 to 13 of 13

Thread: Instantiating

  1. #1
    wr66 is offline Member
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Default Instantiating

    Hello Everyone,
    I am doing an assignment, and I must admit, I am confused.
    The assignment is as follows:
    Complete the Java class, Household.java, that has the following instance variables:

    occupants
    annualIncome

    where occupants is type integer and annualIncome is type double.
    Complete the implementation of the class Household.java by completing the following methods:

    public Household()
    Constructor that takes no arguments and sets the instance variables occupants to 1 and annualIncome to 0.0.
    public int getOccupants()
    Accessor method that returns the current value of the instance variable occupants.
    public double getAnnualIncome()
    Accessor method that returns the current value of the instance variable annualIncome.
    public void setOccupants(int occ)
    Mutator method passed an integer value to change the value stored in the instance variable occupants.
    public void setAnnualIncome(double inc)
    Mutator method passed a double value to change the value stored in the instance variable annualIncome.
    Make no changes to these method headers.
    Documentation

    Be sure to complete the documentation at the top of the source file with the due date, your name, and section.

    Testing the Household Class

    To test your Household class, download the file TestHousehold.java, which you can download below. MAKE NO CHANGES TO THE FILE TestHousehold.java.
    here is what I came up with:

    Java Code:
    //Household.java
    
    public class Household  {
        private int occupants;
        private double annualIncome;
     public static void main (String[] args)  {
     	new Household();
     }
        public Household()  {
        occupants=1; 
        annualIncome=0.0;
      }	
      public int getOccupants(int occ)  {
        return occupants; 
      }
      public double getAnnualIncome(double inc)  {
        return annualIncome; 
     }
     public void setOccupants(int occ)  {
        occupants=occ;  
     }
     public void setAnnualIncome(double inc)  {
        annualIncome=inc;
      } 
     
    }

    Java Code:
    //testHousehold.java
    / Program to test the Household class
    // Assignment 7 CIT111
    // Make no changes to this file
    public class TestHousehold  {
    
      public static void main(String[] args) {
    
        Household house1;
    
        house1 = new Household();
    
        System.out.println("The first house has " + house1.getOccupants() +
        " occupant(s) with and annual income of $" + house1.getAnnualIncome());
    
        // Change state of object house1
        house1.setOccupants(3);
        house1.setAnnualIncome(55000.00);
    
        System.out.println("The first house has " + house1.getOccupants() +
        " occupant(s) with and annual income of $" + house1.getAnnualIncome());
    
      }
    }
    I appreciate any help I can get on this one. Thanks in advance!

    Moderator Edit: Code tags added
    Last edited by Fubarable; 03-14-2010 at 04:57 AM. Reason: Moderator Edit: Code tags added

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Your code is quite close to being correct. I see three problems right now, but I think it's better if you find them for yourself. Clean up your spacing and indentation, and add in the documentation (which is an important part of the assignment) and I think you'll find the errors. If not, come back and ask again.

    -Gary-

  3. #3
    wr66 is offline Member
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Default

    I have checked the code over and over, and I can't figure out why I don't get the desired outcome. All I get is :
    "Press any key to continue..."

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

    Default

    Quote Originally Posted by wr66 View Post
    I have checked the code over and over, and I can't figure out why I don't get the desired outcome. All I get is :
    "Press any key to continue..."
    The TestHousehold class shouldn't compile correctly with your current Household class as posted or if it does compile, then you've changed the code from what you've posted.

    What error messages are you seeing when you try to compile it? What lines generate these errors? These messages will help you solve your problem, and learning to interpret them will be necessary for you to move forward. Please come on back soon with this information so we can help you.

    Best of luck.

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    OK, let's start with this -- why do you have a main() in your class?

    The other two errors should reveal themselves after you fix that.

    -Gary-

  6. #6
    wr66 is offline Member
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Default

    I didn't think I should have the "main" method included, but when I take it out, I get the error message "Exception in thread "main" java.lang.NoSuchMethodError: main.

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    OK, you understand that you need to compile both Household.java and testHousehold.java, and you should be running just testHousehold.class?

    -Gary-

  8. #8
    wr66 is offline Member
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Default

    Ok, now I feel like an idiot! I took out the "main" method, and compiled both Household and TestHousehold and ran TestHousehold (thanks Gary). It worked just fine.
    So after looking at this thing all day, it finally worked.
    Thanks to everyone for all your help.

  9. #9
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    It worked just fine? Did you find the other two errors?

    -Gary-

  10. #10
    wr66 is offline Member
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Default

    Yes, as a matter of fact I think I did. In the following methods,

    public int getOccupants(int occ) {
    return occupants;
    }
    public double getAnnualIncome(double inc) {
    return annualIncome;
    I removed the int occ, and the int inc respectively.

  11. #11
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Great! Please consider posting your final code, and marking the thread SOLVED. It may be a help to other beginners browsing the forum.

    -Gary-

  12. #12
    wr66 is offline Member
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Default Instantiating solved

    Here is the final solution to my instantiating problem.
    Thanks again for everyone's help.

    //Household.java

    public class Household {

    private int occupants;
    private double annualIncome;

    public Household() {
    occupants=1;
    annualIncome=0.0;
    }

    public int getOccupants() {
    return occupants;
    }

    public double getAnnualIncome() {
    return annualIncome;
    }

    public void setOccupants(int occ) {
    occupants=occ;
    }

    public void setAnnualIncome(double inc) {
    annualIncome=inc;
    }
    }


    //testHousehold.java
    / Program to test the Household class
    // Assignment 7 CIT111
    // Make no changes to this file

    public class TestHousehold {

    public static void main(String[] args) {

    Household house1;

    house1 = new Household();

    System.out.println("The first house has " + house1.getOccupants() +
    " occupant(s) with and annual income of $" + house1.getAnnualIncome());

    // Change state of object house1
    house1.setOccupants(3);
    house1.setAnnualIncome(55000.00);

    System.out.println("The first house has " + house1.getOccupants() +
    " occupant(s) with and annual income of $" + house1.getAnnualIncome());

    }
    }

  13. #13
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by ruchira_sandanayaka View Post
    Q.Write a java program which reads a five digit number from the command mode and print the digits of
    the number in reverse order.
    e.g. If the program name is reverse.java then java reverse 12345 should print original number equal
    12345 in the next line the number in the reverse order 54321.

    Please do u can give a answer this quossion..?????
    1. This is a new question, so please start a new thread.
    2. Please tell us what you have tried so far and specifically where you are getting stuck. We're not likely to want to do your work for you, but plenty of people will steer you in the right direction if we can see where you're going wrong.

    -Gary-

Similar Threads

  1. Replies: 7
    Last Post: 07-12-2010, 05:24 PM
  2. Replies: 2
    Last Post: 05-05-2008, 11:55 AM
  3. How to make Beans Lazily-instantiating beans
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-30-2008, 11:10 AM
  4. How to make Beans Lazily-instantiating beans
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-26-2007, 09:41 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
  •