Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default 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

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    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.

  3. #3
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    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/*
    {

    }*/


    }

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    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?

  5. #5
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    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.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Quote Originally Posted by globo View Post
    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?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by KevinWorkman View Post
    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

  9. #9
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    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. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Zack View Post
    Makes you wonder why we even try, doesn't it?
    Yup and I stay out of this thread from now on.

    kind regards,

    Jos

  11. #11
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    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.

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

    Default

    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.

  13. #13
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    12
    Rep Power
    0

    Default 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;
       }
    }
    Attached Thumbnails Attached Thumbnails Need Help with a class-problem2uml.jpg  
    Last edited by Sizzlewump; 10-11-2010 at 12:41 AM.

  14. #14
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    12
    Rep Power
    0

    Default 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-11-2010 at 12:42 AM.

  15. #15
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    12
    Rep Power
    0

    Default

    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

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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

    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.

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

  17. #17
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    12
    Rep Power
    0

    Default

    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();

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    incompatible types
    found : java.lang.String
    required: Person
    p = room2.leave();
    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.

  19. #19
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    12
    Rep Power
    0

    Default

    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.

  20. #20
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    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.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 8
    Last Post: 04-14-2010, 06:49 AM
  2. Replies: 7
    Last Post: 05-06-2009, 01:42 PM
  3. Replies: 8
    Last Post: 03-21-2009, 10:34 AM
  4. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 01:25 AM
  5. Replies: 0
    Last Post: 05-13-2008, 07:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •