Results 1 to 20 of 22
Thread: Need Help with a class
- 10-07-2010, 05:56 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
Need Help with a class
Hi to everybody,
I need to create a class based on this code to get this output. I already started but got stock:
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);
}
Output:
room F105 is occupied by tutor Jackson has 152 dollars and charges $26.00 per hour
room G110 is occupied by student Ramanan has 110 dollars, major is Nursing
room H300 is empty
- 10-07-2010, 05:59 PM #2
This isn't a homework service. Where are you stuck? What does the code do differently than what you expected? What did you try? Be specific.
- 10-07-2010, 06:01 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
This is what I did so far.
public class SmallRoom
{
private String Person; // make up a name for the person who is occupying the room
private String room;
// this field represents the name of the room
public SmallRoom(String room)
{
this.room = room;
this.Person = Person;
}
@Override public String toString()
{
return String.format ("room %s is occupired by %s", room, Person);
}
/*
public void isEmpty
{
}
public void isOccupired
{
}*/
public void enter(String Person)
{
return String.format ("room %s is occupired by %s", Person);
}
public void leave/*
{
}*/
}
- 10-07-2010, 06:15 PM #4
When posting code, make sure you use the code tags.
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 #5
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
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 #6
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 #7
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 #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,420
- Blog Entries
- 7
- Rep Power
- 17
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 #9
- 10-07-2010, 07:24 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,420
- Blog Entries
- 7
- Rep Power
- 17
- 10-07-2010, 07:27 PM #11
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
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.
-
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 #13
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.
Here is the Person class, which is given and should not be changed:
Java Code:public class Person { public static int DEFAULT_DOLLARS = 50; private String name; private int dollars; private boolean happy; public Person(String name, int dollars, boolean happy) { this.name = name; setDollars(dollars); this.happy = happy; } private void setDollars(int amount) { if (amount >= 0) dollars = amount; else dollars = DEFAULT_DOLLARS; } public void spend(int amount) { if (amount >= 0) { if (amount <= dollars) dollars -= amount; else dollars = 0; } } public void earned(int amount) { if (amount >= 0) dollars += amount; } public boolean canAfford(int amount) { return amount <= dollars; } public int getDollars() { return dollars; } public boolean isHappy() { return happy; } public String toString() { return String.format("%s has %d dollars", name, dollars); } 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; } }Last edited by Sizzlewump; 10-10-2010 at 11:41 PM.
- 10-10-2010, 11:20 PM #14
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.
Java 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; // constructor public SmallRoom( String roomNumber, Person p ) { roomNumber = null; // I was trying to initialize some variables here.. p = null; } // determine if the room is occupied public boolean isEmpty() { full = ( p == null ) ? true : false; return full; } public boolean isOccupied() { full = ( p != null ) ? true : false; return full; } public void enter ( Person p ) { full = true; } public String leave ( Person p ) { if( full == false ) { p = null; } return p.toString(); full = false; } public String toString() { return roomNumber; } // end method toString }Last edited by Sizzlewump; 10-10-2010 at 11:42 PM.
- 10-10-2010, 11:21 PM #15
Here are my compile errors:
TestSmallRoom.java:21: cannot find symbol
symbol : constructor SmallRoom(java.lang.String)
location: class SmallRoom
SmallRoom room1 = new SmallRoom("F105");
^
TestSmallRoom.java:22: cannot find symbol
symbol : constructor SmallRoom(java.lang.String)
location: class SmallRoom
SmallRoom room2 = new SmallRoom("G110");
^
TestSmallRoom.java:23: cannot find symbol
symbol : constructor SmallRoom(java.lang.String)
location: class SmallRoom
SmallRoom room3 = new SmallRoom("H300");
^
TestSmallRoom.java:46: leave(Person) in SmallRoom cannot be applied to ()
p = room2.leave();
^
TestSmallRoom.java:51: cannot find symbol
symbol : constructor SmallRoom(java.lang.String)
location: class SmallRoom
SmallRoom room4 = new SmallRoom("L002");
^
TestSmallRoom.java:54: leave(Person) in SmallRoom cannot be applied to ()
room4.enter(room3.leave()); // Bill moves from room3 to room4
^
6 errors
- 10-10-2010, 11:36 PM #16
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
Have you defined a constructor for the SmallRoom class that takes a String as argument?cannot find symbol
symbol : constructor SmallRoom(java.lang.String)
The compiler can't find it. You need to add one to that class.
You have defined the leave method to take a Person as argument(blue) but are trying to call it without one(red).leave(Person) in SmallRoom cannot be applied to ()
p = room2.leave();
- 10-10-2010, 11:52 PM #17
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.
By changing "leave(Person)" to "leave()" I then receive an error:
TestSmallRoom.java:46: incompatible types
found : java.lang.String
required: Person
p = room2.leave();
- 10-11-2010, 12:07 AM #18
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.incompatible types
found : java.lang.String
required: Person
p = room2.leave();
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 #19
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 #20
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
Similar Threads
-
Dynamic loading of a class (passing class definition over the network)
By eddie-w in forum Advanced JavaReplies: 8Last Post: 04-14-2010, 05:49 AM -
[SOLVED] How to pass information from child class to parent class
By pellebye in forum New To JavaReplies: 7Last Post: 05-06-2009, 12:42 PM -
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 09:34 AM -
Calling a method on original class from created class
By kpedersen in forum Advanced JavaReplies: 4Last Post: 08-20-2008, 12:25 AM -
Able to find class file in WEB-INF/classes but not after add sub folders in class dir
By vitalstrike82 in forum Web FrameworksReplies: 0Last Post: 05-13-2008, 06:16 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks