Critter Caretaker The Critter Caretaker program takes care of a virtual pet. The
ID: 3631995 • Letter: C
Question
Critter Caretaker
The Critter Caretaker program takes care of a virtual pet. The player can feed and play with the critter. He/she can also listen to the critter to learn how the critter is feeling.
1. File Critter.java contains a partial definition for a class representing a critter. Save it to your directory and study it to see what methods it contains. Then complete the Critter class as described below.
a. Fill in the code for method geHunger, which should return the hunger level as an integer.
b. Fill in the code for method play, which should decrease the boredom level by 3 and set it to 0 if it is less than 0.
d. Fill in the code for method talk which prints the critter’s mood, which can be mad (if mood > 15), frustrated (if mood > 10 and mood <= 15), ok (if mood > 5 and mood <= 10), or happy (if mood <= 5).
2. File CritterCaretaker.java contains a driver program that uses the Critter class above. Save it to your directory and study it to see what it does. Then compile and run it to test the Critter class.
// ****************************************************************
// Critter.java
//
// Define a Critter class.
// ****************************************************************
public class Critter
{
//declare instance data
private int hunger = 0;
private int boredom = 0;
//-----------------------------------------------
//constructor
//-----------------------------------------------
public Critter()
{
System.out.println("A new critter has been born!");
}
//-----------------------------------------------
//greet: print a greeting message with the hunger level and
// the boredom level.
//-----------------------------------------------
public void greet()
{
System.out.println("Hi, I'm a critter. My hunger level is " + hunger + " and my boredom level is " + boredom + ".");
}
//-----------------------------------------------
//getBoredom: return the boredom level
//-----------------------------------------------
public int getBoredom()
{
return boredom;
}
//-----------------------------------------------
//getHunger: return the hunger level
//-----------------------------------------------
//add header for getHunger
{
//add body of getHunger
}
//-----------------------------------------------
//getMood: return the mood as the addition of hunger
// level and the boredom level
//-----------------------------------------------
public int getMood()
{
return hunger + boredom;
}
//-----------------------------------------------
//passTime: increase the hunger level and the boredom
// level by 1
//-----------------------------------------------
public void passTime()
{
hunger++;
boredom++;
}
//-----------------------------------------------
//eat: decrease the hunger level by 3 and set it to 0 if
// it is less than 0
//-----------------------------------------------
public void eat()
{
System.out.println("Brruppp");
hunger -= 3;
if(hunger < 0)
hunger = 0;
}
//-----------------------------------------------
//play: decrease the boredom level by 3 and set it to 0 if
// it is less than 0
//-----------------------------------------------
//add header for play
{
//add body of play
}
//-----------------------------------------------
// talk: print how the critter feel, which can be mad (if mood > 15), // frustrated (if mood > 10 and mood <= 15), ok (if mood > 5 and mood // <= 10), or happy (if mood <= 5).
//-----------------------------------------------
public void talk()
{
System.out.print("I feel ");
int mood = getMood();
// add your code here
}
}
// ****************************************************************
// CritterCaretaker.java
//
// Takes care of the Critter by giving the player options of listening,
// feeding, or playing with the critter.
// ****************************************************************
import java.util.Scanner;
public class CritterCaretaker{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
Critter crit = new Critter();
crit.greet();
int choice;
do
{
System.out.println("Critter Caretaker ");
System.out.println("0 - Quit");
System.out.println("1 - Listen to your critter");
System.out.println("2 - Feed your critter");
System.out.println("3 - Play with your critter");
System.out.println("Enter your choice");
choice = scan.nextInt();
switch(choice)
{
case 0:
System.out.println("Good bye.");
break;
case 1:
crit.talk();
System.out.println("My hunger level is " + crit.getHunger() + " and my boredom level is " + crit.getBoredom() + ".");
crit.passTime();
break;
case 2:
crit.eat();
System.out.println("My hunger level is " + crit.getHunger() + " and my boredom level is " + crit.getBoredom() + ".");
crit.passTime();
break;
case 3:
crit.play();
System.out.println("My hunger level is " + crit.getHunger() + " and my boredom level is " + crit.getBoredom() + ".");
crit.passTime();
break;
default:
System.out.println("Sorry, the choice you made " + choice + " is not a valid choice.");
}
}while(choice!=0);
}
}
Explanation / Answer
please rate - thanks
// ****************************************************************
// Critter.java
//
// Define a Critter class.
// ****************************************************************
public class Critter
{
//declare instance data
private int hunger = 0;
private int boredom = 0;
//-----------------------------------------------
//constructor
//-----------------------------------------------
public Critter()
{
System.out.println("A new critter has been born!");
}
//-----------------------------------------------
//greet: print a greeting message with the hunger level and
// the boredom level.
//-----------------------------------------------
public void greet()
{
System.out.println("Hi, I'm a critter. My hunger level is " + hunger + " and my boredom level is " + boredom + ".");
}
//-----------------------------------------------
//getBoredom: return the boredom level
//-----------------------------------------------
public int getBoredom()
{
return boredom;
}
//-----------------------------------------------
//getHunger: return the hunger level
//-----------------------------------------------
//add header for getHunger
public int getHunger()
{return hunger;
//add body of getHunger
}
//-----------------------------------------------
//getMood: return the mood as the addition of hunger
// level and the boredom level
//-----------------------------------------------
public int getMood()
{
return hunger + boredom;
}
//-----------------------------------------------
//passTime: increase the hunger level and the boredom
// level by 1
//-----------------------------------------------
public void passTime()
{
hunger++;
boredom++;
}
//-----------------------------------------------
//eat: decrease the hunger level by 3 and set it to 0 if
// it is less than 0
//-----------------------------------------------
public void eat()
{
System.out.println("Brruppp");
hunger -= 3;
if(hunger < 0)
hunger = 0;
}
//-----------------------------------------------
//play: decrease the boredom level by 3 and set it to 0 if
// it is less than 0
//-----------------------------------------------
//add header for play
public void play()
{
boredom-=3;
if(boredom<0)
boredom=0;
//add body of play
}
//-----------------------------------------------
// talk: print how the critter feel, which can be mad (if mood > 15), // frustrated (if mood > 10 and mood <= 15), ok (if mood > 5 and mood // <= 10), or happy (if mood <= 5).
//-----------------------------------------------
public void talk()
{
System.out.print("I feel ");
int mood = getMood();
if(mood>15)
System.out.println("mad");
else if(mood>10)
System.out.println("frustrated");
else if(mood>5)
System.out.println("ok");
else
System.out.println("happy");
// add your code here
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.