Results 1 to 13 of 13
Thread: Instantiating
- 03-14-2010, 12:41 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
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; } }
I appreciate any help I can get on this one. Thanks in advance!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()); } }
Moderator Edit: Code tags addedLast edited by Fubarable; 03-14-2010 at 03:57 AM. Reason: Moderator Edit: Code tags added
- 03-14-2010, 01:16 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
- 03-14-2010, 03:51 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
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.
- 03-14-2010, 04:00 AM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
- 03-14-2010, 04:06 AM #6
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
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.
- 03-14-2010, 04:12 AM #7
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
OK, you understand that you need to compile both Household.java and testHousehold.java, and you should be running just testHousehold.class?
-Gary-
- 03-14-2010, 04:19 AM #8
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
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.
- 03-14-2010, 04:21 AM #9
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
It worked just fine? Did you find the other two errors?
-Gary-
- 03-14-2010, 04:29 AM #10
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
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.
- 03-14-2010, 04:32 AM #11
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Great! Please consider posting your final code, and marking the thread SOLVED. It may be a help to other beginners browsing the forum.
-Gary-
- 03-14-2010, 04:49 AM #12
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
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());
}
}
- 03-14-2010, 04:58 AM #13
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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
-
javax.comm:Error instantiating class com.sun.comm.Win32Driver
By bbq in forum Advanced JavaReplies: 7Last Post: 07-12-2010, 04:24 PM -
[SOLVED] instantiating a class from other classes of different types...
By olbion in forum New To JavaReplies: 2Last Post: 05-05-2008, 10:55 AM -
How to make Beans Lazily-instantiating beans
By Java Tip in forum Java TipReplies: 0Last Post: 03-30-2008, 10:10 AM -
How to make Beans Lazily-instantiating beans
By JavaBean in forum Java TipReplies: 0Last Post: 09-26-2007, 08:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks