Results 1 to 20 of 21
- 06-27-2011, 06:25 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Anyone feel like helping me out some more?
Hi everybody, I got this new assignment in, it's proving to be difficult. Basically it builds off the last program I made which was what this topic was all about. Yet it really has nothing to do with the subject other then the code. I have to create 3 classes a Student class which keeps track of the student's major, a Tutor class which keeps an hourly rate, also a smallRoom class which is some stupid room deal that only has room for one person.
My first and foremost problem is how do I get it to extend my last program? is says right in the class header extends Person, yet when I try to compile it says can not find symbol.
Here's another tidbit it say's that " the Tutor and Student classes are just made up of a constructor and a toString method." So am I suppose to indicate from that this is going to be relatively small program? and what exactly do I code new, and what am I suppose to inherit from the Person class is kind of my issue.Last edited by CGHMN; 06-27-2011 at 06:36 AM.
- 06-27-2011, 06:40 AM #2
Provide us with the full error so we can help you more. You have given us nothing worthwhile. Also if you don't include code when you give us the error, we can only tell you what the error means. Which is ultimately a waste of time because you could read the error code yourself.
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 06-27-2011, 06:47 AM #3
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Okay here's the code and the error, the only reason i left it out is because last time people got all bugged at all the code I was posting.
I'll give the Person class first this is what I made for the last assignment and I am suppose to build off it with two new classes Tutor ans Student.
Now here is the Tutor class:Java Code:public class Person extends Object { private String lastName; private int Money; private boolean mentalState; public Person(String last, int wealth, boolean happy) { lastName = last; setWealth( wealth ); mentalState = happy; } public void setLastName( String last ) { lastName = last; } public String getLastName() { return lastName; } public void setWealth( int wealth ) { Money = ( wealth < 0.0 ) ? 50 : wealth; } public void spend(int value) { if(value<0) Money = 500; else Money = Money - value; while(Money<0) { Money = 0; } } @Override public String toString() { return String.format(lastName+" has "+Money+" dollars."); } public void says() { if(mentalState == true) System.out.println("My name is "+lastName+" and I am doing great!"); else System.out.println("My name is "+lastName+" and I could be better.");; } public boolean canAfford(int wealth) { if(wealth >= Money) return false; else return true; } public void earned(int value) { if(value<0) Money = 500; else Money = Money + value; } public boolean isHappy() { if(Money>100) return true; else return false; } public int getDollars() { return Money; } public void setHappy(boolean happy) { mentalState = happy; } public String Person(String nameDollars) { return nameDollars; } }
Now when I try to compile it for kicks I get this error :Java Code:public class Tutor extends Person { private double hryRate; // the hourly rate that the tutor charges private String lstNm; private double money; private boolean smthng; public Tutor(String lastName,double wealth,boolean something,double hourlyRate) { lstNm = lastName; money = wealth; smthng = something; hryRate = hourlyRate; } }
Tutor.java:9: cannot find symbol
symbol : constructor Person()
location: class Person
{
^
1 error
and they are saved in the same directory and all that.
Also here is the code the assignment gave us this is what we have to construct our program.
I can post the instructions if that helps.Java Code:public class TestSmallRoom { public static void main(String [] args) { Tutor alan = new Tutor("Jackson", 100, true, 26.00); Tutor carol = new Tutor("Bennett", 100, true, -32.00); // bad rates default to $10.00 Student happy = new Student("Smiles", 500, true, "Software Engineering" ); Student ming = new Student("Li", 75, false, "Accounting"); Student prakash = new Student("Ramanan", 110, false, "Nursing" ); Student bill = new Student("Clinton", 5000, true, "Poli Sci"); alan.earned(52); bill.spend(3300); System.out.println(alan); System.out.println(carol); System.out.println(happy); System.out.println(ming); SmallRoom room1 = new SmallRoom("F105"); SmallRoom room2 = new SmallRoom("G110"); SmallRoom room3 = new SmallRoom("H300"); if (room1.isEmpty()) room1.enter(alan); if (room2.isEmpty()) room2.enter(prakash); System.out.println(room1); System.out.println(room2); System.out.println(room3); room1.enter(bill); // rejected since already occupied Person p = null; // The "leave" method set the room to empty and returns a // reference to whoever was in the room. Below, the reference // p will point to student prakash and room2 becomes empty. // A null reference is returned if the room was already empty // when the leave method was called. if (room2.isOccupied()) p = room2.leave(); System.out.println("Just left room 2: " + p); System.out.println("Room2 is now: " + room2); SmallRoom room4 = new SmallRoom("L002"); room3.enter(bill); room4.enter(room3.leave()); // Bill moves from room3 to room4 System.out.println("AT THE END:"); System.out.println(room1); System.out.println(room2); System.out.println(room3); System.out.println(room4); } }
I guess my main problem is that I do not really understand how to utilize super classes and inheritanceLast edited by CGHMN; 06-27-2011 at 07:02 AM.
- 06-27-2011, 07:25 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
A constructor has an important job to do: it must set things up so that an instance is "ready to use". (A person must have a name etc)
When one class extends another (like a Tutor is-a Person) then the *first* thing a constructor must do is call the superclass constructor. The thinking is that to gdt a fully functional tutor we must first have a fully functional person. If you don't call this constructor yourself the compiler will assume there is a no-argument Person() constructor to be used. In your case there is no such constructor, so the compiler grumbles.
The solution is to call the Person() constructor as the very first line of the Tutor() constructor. This will ensure that you have a person and the rest of the Tutor() constructor will mwke sure the resulting object is a ready-to-go tutor. When you call the Person() constructor you can -and should- pass any arguments that are needed to construct a Person.
- 06-27-2011, 07:29 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Cool, now I have a Tutor and Student class that compile.
So that works now, I just need to figure out how to set everything. I will be coming back to this topic shortly with another issue I appreciate the help.Java Code:public class Tutor extends Person { private double hryRate; // the hourly rate that the tutor charges public Tutor(String last, int wealth, boolean happy) { super(last,wealth,happy); } }
it seems TestSmallRoom won't compile it gives the following error.
symbol : constructor Tutor(java.lang.String,int,boolean,double)
location: class Tutor
Tutor alan = new Tutor("Jackson", 100, true, 26.00);
among many other errors, why can't this seem to see the Tutor class?Last edited by CGHMN; 06-27-2011 at 07:31 AM.
- 06-27-2011, 07:31 AM #6
1. There are two ways a class can have a no-argument constructor: either you provide one explicitly, or you don't provide any constructor at all in which case the compiler creates a no-argument constructor for you.
2. There are two ways a subclass constructor can invoke its direct superclass constructor: either you explicitly invoke a specific superclass constructor, or the compiler inserts a call to the no-argument superclass constructor.
Since you didn't provide a constructor for Tutor, the compiler has created a no-argument constructor which attempts to invoke a no-argument constructor of Person -- which doesn't exist, because you have created a constructor for Person -- one that takes 3 arguments.
In slightly less technical terms, a Tutor is-a Person, but as things stand, what values will be given to String last, int wealth and boolean happy when you construct a new Tutor()?
db
edit: I need to improve my typing speed
Last edited by DarrylBurke; 06-27-2011 at 07:34 AM.
- 06-27-2011, 07:34 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Glad you've got that sorted.
- 06-27-2011, 07:44 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
The declaration of the Tutor() constructor was fine - it needs that something argument. First it calls the person constructor, then it does the right thing with the something.
Btw you don't need to repeat the surname fields etc. A tutor will have those because it is-a person.
@darryl And I'm struggling with an Android! hence the random spellig...
- 06-27-2011, 08:04 AM #9
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Okay I believe I have the Tutor and Student class finished. However I am confused on how to make smallRoom because it doesn't inherit so i am not sure how to set all these room names and this occupied thing
- 06-27-2011, 08:04 AM #10
- 06-27-2011, 08:09 AM #11
- 06-27-2011, 08:15 AM #12
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Okay I have a problem here, in my smallRoom class I have the following code
Now I made a dumby method room1 for the smallRoomTest file to accept then and move on to the next error but it didn't it actually caught it and said this:Java Code:public class SmallRoom { private Person occupie; // make up a name for the person who is occupying the room private String roomName; // this field represents the name of the room public SmallRoom(Person occu, String roomNm) { occupie = occu; roomName = roomNm; room1(roomNm); } public void room1(String roomNm) { roomName = roomNm; } }
symbol : constructor SmallRoom(java.lang.String)
location: class SmallRoom
SmallRoom room1 = new SmallRoom("F105");
^
- 06-27-2011, 08:16 AM #13
- 06-27-2011, 08:20 AM #14
I guess I should have been more specific. There is a potential for problems if the Tutor class attempts to use those variables directly.
- 06-27-2011, 08:30 AM #15
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
For some reason the TestRoom class is catching a symbol not found error at a method that does exist heres the error
symbol : constructor SmallRoom(java.lang.String)
location: class SmallRoom
SmallRoom room1 = new SmallRoom("F105");
^
- 06-27-2011, 08:33 AM #16
- 06-27-2011, 08:33 AM #17
It is complaining about a constructor not a method. We don't read minds. Post your SmallRoom class.
- 06-27-2011, 08:36 AM #18
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Here's my smallRoom class
Then when I try to compile TestSmallRoom I get this errorJava Code:public class SmallRoom { private Person occupie; // make up a name for the person who is occupying the room private String roomName; // this field represents the name of the room public SmallRoom(Person occu, String roomNm) { occupie = occu; roomName = roomNm; } public void SmallRoom(String roomNm) { roomName = roomNm; } }
symbol : constructor SmallRoom(java.lang.String)
location: class SmallRoom
SmallRoom room1 = new SmallRoom("F105");
- 06-27-2011, 08:39 AM #19
Check the parameters.Java Code:public SmallRoom(Person occu, String roomNm) SmallRoom room1 = new SmallRoom("F105");
- 06-27-2011, 08:46 AM #20
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Similar Threads
-
Helping on Calculating Pie
By bubbamcgoo in forum New To JavaReplies: 10Last Post: 02-22-2011, 02:23 AM -
No one is helping
By anithajerome in forum Suggestions & FeedbackReplies: 3Last Post: 12-06-2010, 12:52 PM -
Helping about SWIFT MT Message
By 82rathi4u in forum Advanced JavaReplies: 1Last Post: 08-07-2009, 01:06 AM -
Can someone give me a helping hand...
By kwesiaryee in forum Advanced JavaReplies: 1Last Post: 09-17-2008, 07:11 PM -
Calculator Problem. Thanks for helping! ^^
By clark_sandy in forum New To JavaReplies: 3Last Post: 07-06-2008, 04:01 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks