Ghost Sitter The Ghost Sitter program lets you have your very own ghost. The gho
ID: 3759941 • Letter: G
Question
Ghost Sitter
The Ghost Sitter program lets you have your very own ghost. The ghost only east candy and wants to be the spookiest Ghost around. The player drives the action by their choice. The play can inquire to how the ghost is feeling by speakWithTheSprits()
1. File Ghost.java contains a partial definition for a class representing a ghost. Save it to your directory and study it to see what methods it contains. Then complete the Ghost class as described below.
1. Fill in the code for method getHunger(), which should return the hunger level as an integer.
2. Fill in the code for method eatCandy(), which should decrease the hunger level by 3 and set it to 0 if it is greater than 0. The hunger level will start at zero and grow towards negative infinity. (The ghost doesn't actually eat the candy, the player does.)
3. Fill in the code for method scare, which should decrease the spookieness level by 3 and set it to 0 if it is greater than 0. 4. Fill in the code for method speakWithTheSprits which prints the ghost's mood, which can be vengeful (if mood < -15), whistful (if mood < -10 and mood >= -15), spooky (if mood < -5 and mood >= -10), or friendly (if mood > -5).
2. File GhostSitter.java contains a driver program that uses the Ghost class above. Save it to your directory and study it to see what it does. Then compile and run it to test the Ghost class. (Do not make any modifications to GhostSitter.java)
public class Ghost {
//These values are not positive. They MUST be negative!
private int hunger = 0;
private int spookiness = 0;
//Constructor
public Ghost(){
System.out.println("The world has given birth to a new Ghost!");
}
//scare: decrease the spookieness level by 3 and set it to 0 if it is greater than 0. .
public void scare(){
}
//getSpookiness: return the spookines level
public int getSpookiness(){
return spookiness;
}
//getHunger: return the hunger level;
//add header for getHunger
{
//add body of getHunger
}
//getMood: return the mood as the difference of the hunger
//level and the spookiness level
public int getMood(){
return hunger + spookiness;//This will be a negative number
}
//passTime: increase the hunger level and the spookiness
//level by 1
public void passTime()
{
hunger--; //won't go larger than 0.
spookiness--; //won't go larger than 0.
}
//eatCandy: increase the hunger level by 3 and set it to 0 if
//it is greater than 0.
public void eatCandy(){
System.out.println("MMMMMMmmmmmmmmmmm.........");
//add Code here
}
public void speakWithTheSpirits(){
System.out.print("I feel ");
int mood = getMood();
//add Code here.
}
}
import java.util.Scanner;
public class GhostSitter {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Ghost slimer = new Ghost();
int choice;
do {
System.out.println("Ghost Sitter ");
System.out.println("0 - Quit");
System.out.println("1 - Converse with your ghost");
System.out.println("2 - Offer a candy sacrifice to your ghost");
System.out.println("3 - Try and scare your ghost");
System.out.println("Enter your choice");
choice = scan.nextInt();
switch (choice) {
case 0:
System.out.println("Good bye.");
break;
case 1:
slimer.speakWithTheSpirits();
System.out.println("My hunger level is " + slimer.getHunger() + " and my spookieness level is "
+ slimer.getSpookiness() + ".");
slimer.passTime();
break;
case 2:
slimer.eatCandy();
System.out.println("My hunger level is " + slimer.getHunger() + " and my spookieness level is "
+ slimer.getSpookiness() + ".");
slimer.passTime();
break;
case 3:
slimer.scare();
System.out.println("My hunger level is " + slimer.getHunger() + " and my spookieness level is "
+ slimer.getSpookiness() + ".");
slimer.passTime();
break;
default:
System.out.println("Sorry, the choice you made " + choice + " is not a valid choice.");
}
} while (choice != 0);
}
}
Explanation / Answer
private int hunger = 0;
private int spookiness = 0;
//Constructor
public Ghost(){
System.out.println("The world has given birth to a new Ghost!");
}
//scare: decrease the spookieness level by 3 and set it to 0 if it is greater than 0. .
public void scare(){
}
//getSpookiness: return the spookines level
public int getSpookiness(){
return spookiness;
}
//getHunger: return the hunger level;
//add header for getHunger
{
//add body of getHunger
}
//getMood: return the mood as the difference of the hunger
//level and the spookiness level
public int getMood(){
return hunger + spookiness;//This will be a negative number
}
//passTime: increase the hunger level and the spookiness
//level by 1
public void passTime()
{
hunger--; //won't go larger than 0.
spookiness--; //won't go larger than 0.
}
//eatCandy: increase the hunger level by 3 and set it to 0 if
//it is greater than 0.
public void eatCandy(){
System.out.println("MMMMMMmmmmmmmmmmm.........");
//add Code here
}
public void speakWithTheSpirits(){
System.out.print("I feel ");
int mood = getMood();
//add Code here.
}
}
import java.util.Scanner;
public class GhostSitter {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Ghost slimer = new Ghost();
int choice;
do {
System.out.println("Ghost Sitter ");
System.out.println("0 - Quit");
System.out.println("1 - Converse with your ghost");
System.out.println("2 - Offer a candy sacrifice to your ghost");
System.out.println("3 - Try and scare your ghost");
System.out.println("Enter your choice");
choice = scan.nextInt();
switch (choice) {
case 0:
System.out.println("Good bye.");
break;
case 1:
slimer.speakWithTheSpirits();
System.out.println("My hunger level is " + slimer.getHunger() + " and my spookieness level is "
+ slimer.getSpookiness() + ".");
slimer.passTime();
break;
case 2:
slimer.eatCandy();
System.out.println("My hunger level is " + slimer.getHunger() + " and my spookieness level is "
+ slimer.getSpookiness() + ".");
slimer.passTime();
break;
case 3:
slimer.scare();
System.out.println("My hunger level is " + slimer.getHunger() + " and my spookieness level is "
+ slimer.getSpookiness() + ".");
slimer.passTime();
break;
default:
System.out.println("Sorry, the choice you made " + choice + " is not a valid choice.");
}
} while (choice != 0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.