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.
Here is the Person class, which is given and should not be changed:
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;
}
}
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;
// 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
}