Java programming 1. (75 points) Successfully implement a Tamagotchi farm a. (10
ID: 3830822 • Letter: J
Question
Java programming 1. (75 points) Successfully implement a Tamagotchi farm a. (10 points) Create the requested number of Tamagotchi
b. (30 points) Allow the user to feed, play with, and cleanup after each individual Tamagotchi.
c. (10 points) Display name and age of each Tamagotchi
d. (10 points) Display warnings to the user for hunger, boredom and messiness.
e. (10 points) Display “Deceased” after a Tamagotchi dies
f. (5 points) End the game when all of the Tamagotchi die and provide final score.
2. (25 points) Follow the best practices we have learned in this class
a. (15 points) Correctly use the provided Tamagotchi class and an ArrayList
b. (5 points) Correct data types, and useful variable names.
c. (5 points) Format and document code using best practices learned in class. Use at least 5 helpful comments to explain your code to me (in addition to all of the comments I left in the skeleton code).
Details Your code will ask the user how many Tamagotchi they want to play with, then create that many objects using the provided Tamagotchi class. The code will then repeatedly display the current information for each Tamagotchi and allow the user to perform one action at a time to take care of them. This continues until all of the Tamagotchi die. Below are a few rounds of play to illustrate the interaction:
How many Tamagotchi would you like? 5
0) Age: 0
1) Age: 0
2) Age: 0
3) Age: 0
4) Age: 0
Which Tamagotchi do you want to play with? 0
Do you want to (1) feed, (2) play, or (3) cleanup? 1
0) Age: 1
1) Age: 1
2) Age: 1
3) Age: 1
4) Age: 1
Which Tamagotchi do you want to play with? 0
Do you want to (1) feed, (2) play, or (3) cleanup? 2
0) Age: 2
1) Age: 2
2) Age: 2
3) Age: 2
4) Age: 2
Your code will continue to ask the user which Tamagotchi they want to play with and what action they want to take. Each time they specify an action, your code will call the appropriate method on that Tamagotchi (feed(), play(), or clean()), and then call the age() method on every Tamagotchi to ensure that they all age together. After all the Tamagotchi are updated, you will output the status of each object again. Over time, your Tamagotchi will complain about one or more problems until they die. The examples below illustrate the four special messages you need to handle. I recommend you manage these inside your toString method.
Example 1: Hunger When a Tamagotchi’s food rating is below 5, it should display a “Hungry!” warning. In the example below, the first Tamagotchi (0th index) is showing this hungry warning 0) Age: 10 Hungry! 1) Age: 10 2) Age: 10 3) Age: 10 4) Age: 10
Example 2: Messy Similarly, when a Tamagotchi’s mess rating is above 5, it should display a “Messy!” warning. In the example below, the Tamagotchi’s at the 2 and 4 index are getting messy and the Tamagotchi at index 0 is still hungry.
0) Age: 11 Hungry!
1) Age: 11
2) Age: 11 Messy!
3) Age: 11
4) Age: 11 Messy!
Example 3: Boredom Similarly, when a Tamagotchi’s play rating is below 5, it should display a “Bored!” warning. In the example below, the Tamagotchi’s at the 2, 3 and 4 index are getting messy, the Tamagotchi at index 0 is still hungry and now it is bored as well.
0) Age: 13 Hungry! Bored!
1) Age: 13
2) Age: 13 Messy!
3) Age: 13 Messy!
4) Age: 13 Messy!
Example 4: Death Eventually, the Tamagotchi will get old enough that they will die. I have written the aging algorithm to set the isAlive variable for you, but your toString method will need to detect the death and output a “Deceased” message instead of any of the other warnings. In the example below there are lots of different warnings for the aging Tamagotchi, but only “Deceased” is displayed for the one at index 3 because it has now died. Note: After this round, the deceased object’s age will stay the same because the age() method only affects objects with isAlive set to true.
0) Age: 20 Hungry! Bored! Messy!
1) Age: 20 Bored! Messy!
2) Age: 20 Messy!
3) Age: 20 Deceased
4) Age: 20 Bored! Messy!
Final Example: End of Game The game ends when all of the Tamagotchi die. You will alert the user and then output the final information for all of the Tamagotchi like this. Note that the final score is a sum of the ages for each of the Tamagotchi
Game over!
0) Age: 23 Deceased
1) Age: 26 Deceased
2) Age: 22 Deceased
3) Age: 20 Deceased
4) Age: 25 Deceased Total Score: 116
Programming Strategy This project requires a good design as well as a solid knowledge of Objects and ArrayLists. I recommend you spend 20 minutes mapping out the logic for your main class first. Use this high-level logic to map out what you want to write in comments in the code. Finally, start writing the code. I recommend you first write a toString method in the Tamagotchi class that outputs all of the information from the class so you can see how the aging process works and make sure your method calls are working. Once that is ironed out, finish your project by writing the logic to output what I specified above. This is the skeleton for the code I do not think I need to write another class, just the main method and using array lists instead of arrays
import java.util.Random;
import java.util.ArrayList;
import java.util.Scanner;
public class Project6_MichaelMcKenna {
/* * The Tamagotchi Class contains five data elements that track the * current state of a single Tamagotchi toy. The class has a toString * method which must be written by the student for the project to work. * * */
public class Tamagotchi {
private Random rand = new Random();
private int food;
private int play;
private int mess;
private int age;
private boolean isAlive;
/*----------------------------------------------------------------------- * Constructor * There is only one constructor for a Tamagotchi. It uses random numbers * to set up all of the necessary information for this instance. */
public Tamagotchi()
{
food = 10 + rand.nextInt(5);
play = 10 + rand.nextInt(5);
mess = 0;
age = 0;
isAlive = true;
} // ----------------------------------------------------------------------- /* * ====================================================================== * Mutators These methods alter the state of this Tamagotchi */ /* *
feed() This method is called each time the user feeds this Tamagotchi It * adjusts the state of the Tamagotchi as needed for a single feeding. */
public void feed() {
food++;
mess++;
} /* * play() This method is called each time the user plays with this * Tamagotchi It adjusts the state of the Tamagotchi as needed for a single * play time. */
public void play() {
play++;
} /* * clean() This method is called each time the user cleans up this * Tamagotchi It adjusts the state of the Tamagotchi as needed for a single * cleanup. */
public void clean() {
mess--;
} /* * age() This method is called once each round for each Tamagotchi. It * adjusts the state of the Tamagotchi as needed to show that it has * survived one more round. Note that this method has no affect on dead * Tamagotchis as they do not age. */
public void age() {
if (isAlive) {
age++;
if (rand.nextInt(2) == 0) {
food--; }
if (rand.nextInt(2) == 0) {
play--;
}
if (rand.nextInt(2) == 0) {
mess++;
}
if ((food <= 0 || play <= 0 || mess > 10) && rand.nextInt(2) == 0)
{
isAlive = false;
} } }
// ====================================================================== /* * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Accessors These methods do not alter the underlying data for this * Tamagotchi. They only provide helpful information to the user. */ /* * getAge() This method returns the current age of this Tamagotchi */
public int getAge() {
return age;
}
/* * isAlive() This method returns true if this Tamagotchi is still alive. */
public boolean isAlive() {
return isAlive;
}
/* * toString() This method returns a String that is nicely formatted to * summarize the state of this Tamagotchi. * * TODO: Write this method and use it in your main method to output the * state of each Tamagotchi in the format specified in the handout. Here are * some examples: * * For a deceased Tamagotchi that was 12 rounds old: * Age: 12 Deceased * *
For a living Tamagotchi that is 8 rounds old, with no complaints * Age: 8 * *
For a living Tamagotchi that is 13 rounds old, that is hungry (AKA: Their * food rating is under 5) * Age: 13 Hungry! * *
For a living Tamagotchi that is 20 rounds old, that is bored (AKA: Their * play rating is under 5) * Age: 20 Bored! * *
For a living Tamagotchi that is 10 rounds old, that is Messy (AKA: Their * mess rating is over 5) * Age: 10 Messy! * *
For a living Tamagotchi that is 5 rounds old, that is Hungry, Bored, and * Messy * Age: 5 Hungry! Bored! Messy! */
public String toString() {
return "";
} } }
Explanation / Answer
package chegg;
import java.util.Random;
import java.util.ArrayList;
import java.util.Scanner;
public class TamagotchiGame{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("How many Tamatogachi you want to play with?");
int num=sc.nextInt();
ArrayList<Tamagotchi> tlist=new ArrayList<Tamagotchi>();
for(int i=0;i<num;i++){
Tamagotchi t=new Tamagotchi();
tlist.add(t);
System.out.println(t);
}
int gameover=0;
while(true){
System.out.println("Which Tamagotchi you want to play with?");
int tnum=sc.nextInt();
System.out.println("Do you want to (1) feed, (2) play, or (3) cleanup?");
int op=sc.nextInt();
if(tlist.get(tnum).isAlive()){
if(op==1)
tlist.get(tnum).feed();
else if(op==2)
tlist.get(tnum).play();
else if(op==3)
tlist.get(tnum).clean();
for(int i=0;i<num;i++){
tlist.get(i).age();
System.out.println(tlist.get(i));
}
}
for(int i=0;i<num;i++){
gameover=1;
if(tlist.get(i).isAlive()){
gameover=0;
break;
}
}
if(gameover==1)
return;
}
}
}
/* * The Tamagotchi Class contains five data elements that track the * current state of a single Tamagotchi toy. The class has a toString * method which must be written by the student for the project to work. * * */
class Tamagotchi {
private Random rand = new Random();
private int food;
private int play;
private int mess;
private int age;
private boolean isAlive;
/*----------------------------------------------------------------------- * Constructor * There is only one constructor for a Tamagotchi. It uses random numbers * to set up all of the necessary information for this instance. */
public Tamagotchi()
{
food = 10 + rand.nextInt(5);
play = 10 + rand.nextInt(5);
mess = 0;
age = 0;
isAlive = true;
} // ----------------------------------------------------------------------- /* * ====================================================================== * Mutators These methods alter the state of this Tamagotchi */ /* *
//feed() This method is called each time the user feeds this Tamagotchi It * adjusts the state of the Tamagotchi as needed for a single feeding. */
public void feed() {
food++;
mess++;
}
/* * play() This method is called each time the user plays with this * Tamagotchi It adjusts the state of the Tamagotchi as needed for a single * play time. */
public void play() {
play++;
} /* * clean() This method is called each time the user cleans up this * Tamagotchi It adjusts the state of the Tamagotchi as needed for a single * cleanup. */
public void clean() {
mess--;
} /* * age() This method is called once each round for each Tamagotchi. It * adjusts the state of the Tamagotchi as needed to show that it has * survived one more round. Note that this method has no affect on dead * Tamagotchis as they do not age. */
public void age() {
if (isAlive) {
age++;
if (rand.nextInt(2) == 0) {
food--; }
if (rand.nextInt(2) == 0) {
play--;
}
if (rand.nextInt(2) == 0) {
mess++;
}
if ((food <= 0 || play <= 0 || mess > 10) && rand.nextInt(2) == 0)
{
isAlive = false;
} } }
// ====================================================================== /* * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Accessors These methods do not alter the underlying data for this * Tamagotchi. They only provide helpful information to the user. */ /* * getAge() This method returns the current age of this Tamagotchi */
public int getAge() {
return age;
}
/* * isAlive() This method returns true if this Tamagotchi is still alive. */
public boolean isAlive() {
return isAlive;
}
/* * toString() This method returns a String that is nicely formatted to * summarize the state of this Tamagotchi. * * TODO: Write this method and use it in your main method to output the * state of each Tamagotchi in the format specified in the handout. Here are * some examples: * * For a deceased Tamagotchi that was 12 rounds old: * Age: 12 Deceased * *
For a living Tamagotchi that is 8 rounds old, with no complaints * Age: 8 * *
For a living Tamagotchi that is 13 rounds old, that is hungry (AKA: Their * food rating is under 5) * Age: 13 Hungry! * *
For a living Tamagotchi that is 20 rounds old, that is bored (AKA: Their * play rating is under 5) * Age: 20 Bored! * *
For a living Tamagotchi that is 10 rounds old, that is Messy (AKA: Their * mess rating is over 5) * Age: 10 Messy! * *
For a living Tamagotchi that is 5 rounds old, that is Hungry, Bored, and * Messy * Age: 5 Hungry! Bored! Messy! */
public String toString() {
String f="";
String m="";
String b="";
String d="";
if(food<5)
f="Hungry! ";
if(mess>5)
m="Messy! ";
if(play<5)
b="Bored! ";
if(!this.isAlive()){
d="Deceased! ";
return "Age :"+this.getAge()+" "+d;
}
//System.out.println(food+" "+mess+" "+play);
return "Age :"+this.getAge()+" "+f+m+b+d;
} }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.