// 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.

Okay, and what does that code do? What did you expect it to do? Why do you think that is? What have you tried? Are you seeing any Exceptions, or just weird behavior? What's weird about it? Where are you getting stuck?

10-07-2010, 06:20 PM

globo

Ok for now I need to create a method leave that set the room to empty and returns a refrence to whoever was in the room.
Give me an example of this code if you can.
Thanks.

10-07-2010, 06:32 PM

KevinWorkman

Quote:

Originally Posted by globo

Ok for now I need to create a method leave that set the room to empty and returns a refrence to whoever was in the room.
Give me an example of this code if you can.
Thanks.

Sorry, I can't just give out an example. But the description of the problem gives you hints about what you need to do. What does it mean to "set the room to empty"? Does that mean you need a simple boolean toggle? Or should you do something with the Person String (variables should start with lowercase letters, by the way)? Returning a reference to whoever was in the room sounds like you need a return statement, which means your method shouldn't be void, right?

10-07-2010, 06:32 PM

Norm

What do you need to do in the class to "set the room to empty"? What variable has a value that answers the question: the room is empty?

What is the definition for the method you want to write? What is the return type and what is the name and what is the argument to the method?

10-07-2010, 06:38 PM

JosAH

Quote:

Originally Posted by KevinWorkman

Sorry, I can't just give out an example. But the description of the problem gives you hints about what you need to do. What does it mean to "set the room to empty"? Does that mean you need a simple boolean toggle? Or should you do something with the Person String (variables should start with lowercase letters, by the way)? Returning a reference to whoever was in the room sounds like you need a return statement, which means your method shouldn't be void, right?

I wonder how the OP managed to finish his/her other assignment on his/her own: link. There are non-void methods in there and everything while this class doesn't even compile ...

kind regards,

Jos

10-07-2010, 07:17 PM

Zack

Quote:

Originally Posted by JosAH

I wonder how the OP managed to finish his/her other assignment on his/her own: link. There are non-void methods in there and everything while this class doesn't even compile ...

kind regards,

Jos

Makes you wonder why we even try, doesn't it?

10-07-2010, 07:24 PM

JosAH

Quote:

Originally Posted by Zack

Makes you wonder why we even try, doesn't it?

Yup and I stay out of this thread from now on.

kind regards,

Jos

10-07-2010, 07:27 PM

globo

You guys what's going on. I provided the solution that was given by the teacher. I could not do that program. A lot of people in my class did not get to run. It was too much for beginners.

10-07-2010, 08:02 PM

Fubarable

If you want help, then show your attempt to solve your current problem (with code tags) and explain what the problems you are having with your code. You have yet to do this even though it's been requested of you several times. Just saying that you're "stuck" doesn't help us help you.

10-10-2010, 11:12 PM

Sizzlewump

1 Attachment(s)

I have this assignment as well and would like some help

Hi everyone, please be patient as I am not familiar with posting threads, but..
I have attached the UML diagram of this problem. There are a few things missing from the assignment in the original post. The SmallRoom class is the code we need to design. Using composition we need to show that SmallRoom has a person. This is where I'm having trouble. Using inheritance we need to show that a Student is a person and a Tutor is a person via the extends keyword. TestSmallRoom is the driver class which creates the outputs and may not be altered.

There are some more stipulations which I will post shortly. Thanks a lot for any input.

public void says()
{
System.out.printf("My name is %s and I ", name);
if (happy)
System.out.println("am doing great!");
else
System.out.println("could be better.");
}

public void setHappy(boolean mood)
{
happy = mood;
}
}

10-10-2010, 11:20 PM

Sizzlewump

Here is my attempt at the SmallRoom class

Warning, this does not compile. I am a bit confused with declaring methods and variables properly ( public, private, void, etc.). I am able to get the compiler to build with only 6-8 errors, but my composition is lacking.

Any input is greatly appreciated.

Code:

public class SmallRoom
{
private Person p; // make up a name for the person who is occupying the room
private String roomNumber; // this field represents the name of the room
private boolean full;

Please start a new thread for a new problem.
Use code tags when posting code to preserve formatting. Info here: Java Forums - BB Code List

Quote:

cannot find symbol
symbol : constructor SmallRoom(java.lang.String)

Have you defined a constructor for the SmallRoom class that takes a String as argument?
The compiler can't find it. You need to add one to that class.

Quote:

leave(Person) in SmallRoom cannot be applied to ()
p = room2.leave();

You have defined the leave method to take a Person as argument(blue) but are trying to call it without one(red).

10-10-2010, 11:52 PM

Sizzlewump

I have provided my SmallRoom constructor that takes two arguments. One is a string, yes. But thank you for pointing that out. I have tried altering the second argument to a string which results in only 36 compile errors.

The error message says that the variable to the left of the = is a Person and the method to the right of the = returns a String.
What did you intend this statement to do?

You have to change one or the other to make the compiler happy. The two sides must use the same type: String or Person.

10-11-2010, 12:19 AM

Sizzlewump

The idea is I have many SmallRoom objects that can contain only 1 person. That being either a Student, Tutor, or even a Person type. I want to pass a Person into a SmallRoom using the enter method. When the driver pushes the Person out of the SmallRoom, it invokes the leave method. I need to find a way to get my compiler to recognize the correct data type to pass as arguments to those methods.

10-11-2010, 12:29 AM

globo

I got stock in the smallRoom class too.
I was able to do Tutor class and Student class and get to compile and run. If you like those I can give them to you.