Question

My Java Programming Teacher Gave me this for practice before the exam, butI cant get it to work, and I need a working versio

piece IllegalArgumentException. Otherwise the method will proceed to create the corresponding Toy and return it. e input does

to level up, then the method does not do anything. An example of the method output could be the following: YAY time to level

Mimitchi Name: Level: Energy: XP: Meals: Toys: 10.00 137 [Trooper the yellow stuffed cat, Mary the blue ball] (C) FILEIO0 Fil

- Meals consumed The name of a file containing all its toy:s An example of such files is the mimitchi.txt which is provided w

If an InputMismatchException was raised (because the user entered something that was not a int when asked for an int, for ins

Welcome to our last game of the semester! Please enter the name of the Tamagotchi youd like to play with: Mimitchi Your Tama

Second output: Welcome to our last game of the semester! Please enter the name of the Tamagotchi youd like to play with: Mim

Choose one of the following options: 1) Mimitchi plays with their best toy 2) Mimitchi plays with a random toy Mímitch1 playe

The following are screen grabs of the provided files

import java. io. IOException; import java.util.Arraylist: import java.util.Random; public class Tamagotchi // static attribut

mport Java.l.Arcayiist: import java.util.Random; public class Toy / static attributes private static String(] namesBob, Pe

Thanks so much for your help, and have a nice day!

My Java Programming Teacher Gave me this for practice before the exam, butI can't get it to work, and I need a working version to discuss with my teacher ASAP, and I would like to sleep at some point before the exam. Please Help TEST QUESTION 5: Tamagotchi For this question, you will write a number of classes that you can use to implement a game that simulates a Tamagotchi. Your code for this question will go in multiple .java files. Note that in addition to the required methods below, you are free to add as many other private methods as you see fit. It is up to you to figure out if the methods from the Toy class and the Tamagotchi class should be static or not. Playground and FilelO are utilities classes, therefore all their methods should be static. The general idea of the assignment is to implement a new data type Tamagotchi. An object of type Tamagotchi should be able to perform some basic tasks such as play, eat, and sleep. As a consequence to these actions the Tamagotchi's energy and experience will change. In addition to this, you will implement two utility classes that will allow you to use a Tamagotchi and to save the progress done each time you close the program. (A) TOY CLASS Toy.java represents a toy that a Tamagotchi will be able to play with. You are provided with some starter code for this class. Note that this code already contains four private static attributes as well as three private static methods. You should use them when you implement the rest of the class. The Toy class should contain the following private attributes: . A String name . A String color . A String type .A int representing the experience gained by playing with this Toy The class also contains the following public methods. . Two constructorS: One constructor that takes as input three Strings, and an int. These parameters represent the name, color, type, and experience awarded by the Toy, in that order. The constructor should use its inputs to initialize the attributes accordingly - - A second constructor that takes no inputs and initializes the attributes with values generated at random. Use getRandomName(), getRandomColor(), and getRandomType() to initialize the corresponding attributes. Generate a random integer between 10 (included) and 25 (excluded) to initialize the attribute representing the experience awarded by the toy. .A getName(), getColor), getType(), and getXp() to retrieve the corresponding attributes' values. The toString() method This method returns a String consisting of the toy's name, color, and type. For example, the method might return the following: "Trooper the yellow stuffed cat", where "Trooper" is the name of the toy, "yellow" is its color, and "stuffed cat" is its type. This method will be very handy for debugging your code, and you can use it whenever you need to display information about a toy . createToy) method This method takes a String as input containing all the information needed to represent a Toy. The method returns a Toy created using such information. A valid input String will contain the name the color, the type, and the experience to be awarded. Each piece of information will be separated
piece IllegalArgumentException. Otherwise the method will proceed to create the corresponding Toy and return it. e input does not contain 4 pleces of information your method shou row an findBestToy) method This method takes an ArrayList of Toys as input and returns the Toy that awards the greatest experience for playing with it. If the list is empty the method should return null. If there's more than one Toy awarding the highest experience, then the first one in the list should be returned. TAMAGOTCHI CLASS e that you are provided with some starter code for this class too. The code already contains two ate static attributes. Tamagotchi class should contain the following private attributes: A String representing its name An int indicating its level A double indicating its energy An int representing its experience points An int indicating how many meals the tamagotchi has consumed. An ArrayList of toys containing all its toys. class also contains the following public methods. Two constructors: - One constructor that takes as input a String, an int, a double, two ints, and an ArrayList of Toys. These parameters represent the name, level, energy, experience, meals consumed, and toys of a tamagotchi, in that order. The constructor should use its inputs to initialize the attributes accordingly. Remember how you should initialize attributes that are mutable reference types. - A second constructor that takes as input only one String representing the name of the tamagotchi. The constructor uses the input to initialize the attribute name, and then it initializes the remaining attributes as follow: * level with 1 * energy with MAX ENERGY (10.0) experience with 0 * meals consumed with 0 * the list of toys with a list with one element containing a Toy generated at random. A getName(), getLevel(), getEnergy(), getXp(), getNumOfMeals), and getToys() to retrieve the corresponding attributes' values. Remember how you should write a get method when dealing with attributes that are mutable reference types. A private method called levelUp) This method does not receive any input. It checks whether the tamagotchi has enough experience to level up. Ifits experience is greater than or equal to 50 level* (level + 1)/2, then the tamagotchi should level up. This is obtained by increasing its level by one, resetting the meals consumed to 0 and creating a brand new toy (randomly generated) to be added to its list of toys. The method should also print a message letting the user know that the tamagotchi has levelled up, its new level, and which new toy was added to the list. If the experience of the tamagotchi is not enough
to level up, then the method does not do anything. An example of the method output could be the following: YAY time to level up!! Mimitchi is now eel 2 Your new toy is Mary the blue ball The play) method This method takes as input an integer indicating a playing mode. You can assume that this integer is going to be either equal to 1 or to 2. The method should do the following: - If the Tamagotchi has no toys or it has energy less than 2, then the method throws an IllegalStateException with a message indicating that the tamagotchi is not able to play - Otherwise, if the playing mode is 1, the method selects the best toy out of this tamagotchi's toys, if the playing mode is equal to 2, it selects a random toy The method updates the experience and the energy of the tamagotchi. Let the experience gained be equal to x, then the energy used by the tamagotchi to play is a random double between x/2 0.0 (included) and x/20.0 + 0.5 (excluded) The method displays messages indicating which toy the tamagotchi played with and what are its energy and experience as a consequence of this. - Finally, the method should call levelUp() in case the experience gained was enough for the tamagotchi to change level. For example, when called, the method might display the following: Mimitchi played with Trooper the yellow stuffed cat and earned 17 xp. Mimitchi has now 135 xp, and 8.78 energy The feed) method This method takes no inputs. If the energy of the tamagotchi is less than 1 or if the tamagotchi has already consumed a number of meals greater than or equal to twice its current level, then the method throws an lllegalStateException indicating that the tamagotchi cannot eat. Otherwise, the method generates the following a random double between 0 (included) and 0.5 (excluded) representing the energy gained by - ea - a random integer between 1 (included) and 4 (excluded) representing the experience gained from eating. The method uses the two random numbers to update the corresponding attributes. It then increases the number of meals consumed by 1, and it displays a message indicating the updated energy and experience values of the tamagotchi. For example, when called, the method might display the following Nom nom nom Mimitchi has now 137 xp, and 8.91 energy The sleep() method This method takes no inputs. It simply displavs that the tamagotchi is now going to sleep and it resets its energy back to MAX-ENERGY The toString() method This method returns a String consisting of all the information regarding a Tamagotchi. For instance, if you display what this method return, you might see the following
Mimitchi Name: Level: Energy: XP: Meals: Toys: 10.00 137 [Trooper the yellow stuffed cat, Mary the blue ball] (C) FILEIO0 FilelO.java must contain the following private static methods: loadToys() method. This method takes as input a filename as a String parameter, and returns an ArrayList of oys. The loadToys method must use a FileReader and a BufferedReader in order to open the file specified by the filename. Declare the IOException in the header of the method using the throws keyword You can assume that the files that loadToys receives as input have all the same format: each line consists of a toy name, color, type, and experience points, each separated by a tab character (t) An example of a file containing toys is found in the provided files as mimitchiToys.txt. Using the information on each line and the method createToy() from the Toy class, create all the necessary Toys, and store them all in the ArrayList that will then be returned by the method. In this method you should catch the IllegalArgumentException that might be raised by the method createToy. In the catch block simply display a message stating that the format of the file is incorrect. In such a case, the method should return an empty ArrayList. (NOTE that empty does not mean null!) saveToys) method. This method takes as input an ArrayList of Toys as well as a String representing a filename The saveToys() method must use a FileWriter and a BufferedWriter in order write to the file specified by the filename. Make sure to handle the IOException using a try-catch block and print a meaningful message if necessary The format of the file created by saveToys() should match the format that is expected by loadToys(). (See the description given above) This method should return true if the file was successfully written, false otherwise FilelO.java must also contain the following public static methods: loadTamagotchi() method. This method takes as input a filename as a String parameter, and returns a Tamagotchi. The method must use a FileReader and a BufferedReader in order to open the file specified by the filename. Declare the IOException in the header of the method using the throws keyword. You can assume that the files that loadTamagotchi receives as input have all the same format: the contain 6 lines, with the following information about a tamagotchi - Name Level - Energv - Experience
- Meals consumed The name of a file containing all its toy:s An example of such files is the mimitchi.txt which is provided with the assignment. Use the readLine() method of the BufferedReader to retrieve the content of the file. Use the information retrieved to create and return the appropriate object of type Tamagotchi. saveTamagotchi() method. This method takes as input a Tamagotchi, a String representing the filename in which the tamagotchi should saved, and a String representing the filename in which the tamagotchi's toys should be saved. The saveTamagotchi() method must use a FileWriter and a BufferedWriter in order write to the file specified by the filename. Make sure to handle the IOException using a try-catch block and print a meaningful message if necessary The format of the file created by saveTamagotchi() should match the format that is expected by loadTamagotchi(). (See the description given above) Note that method should use its third input to write the last line of the file. More over, the method should also make sure that all the toys belonging to the specified Tamagotchi will be saved to the appropriate file This method should return true if the files was successfully written, false otherwise (D) PLAYGROUND (OPTIONAL) The Playground class has only one public static method called play(). You are highly encouraged to ad
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi, The entire assignment is too big to be completed in the given time, I am sharing the Toy .java and Tamogotchi.java classes both of which are fully implemented. Please share another question so that the second part can be addressed.

thank you for understanding.

=================================================================================

import java.util.ArrayList;
import java.util.Random;

public class Toy {

    private String name;
    private String color;
    private String type;
    private int experienceGained;

    private static String[] names = {"Bob", "Penny", "Fisher", "Snoopy", "Garfield", "Mary", "Chuchu", "Trooper", "Lovebug", "Bella"};
    private static String[] colors = {"red", "blue", "green", "yellow", "orange", "purple"};
    private static String[] types = {"car", "doll", "stuffed cat", "train", "ball", "kite", "teddy bear", "trike"};

    private static Random r = new Random();

    public Toy(String name, String color, String type, int experienceGained) {
        this.name = name;
        this.color = color;
        this.type = type;
        this.experienceGained = experienceGained;
    }

    public Toy() {

        this.name = getRandomName();
        this.color = getRandomColor();
        this.type = getRandomType();
        this.experienceGained = 10 + r.nextInt(15);
    }


    private static String getRandomName() {
        int i = r.nextInt(names.length);
        return names[i];
    }

    private static String getRandomColor() {
        int i = r.nextInt(colors.length);
        return colors[i];
    }

    private static String getRandomType() {
        int i = r.nextInt(types.length);
        return types[i];
    }

    public String getName() {
        return name;
    }

    public String getColor() {
        return color;
    }

    public String getType() {
        return type;
    }

    public int getXp() {
        return experienceGained;
    }

    @Override
    public String toString() {
        return getName() + " the " + getColor() + " " + getType();
    }

    public Toy createToy(String details) {

        String[] data = details.split("\\t");
        if (data.length != 4) {
            throw new IllegalArgumentException("Insufficient Data provided");
        }
        Toy aToy = new Toy(data[0], data[1], data[2], Integer.parseInt(data[3]));
        return aToy;

    }

    public Toy findBestToy(ArrayList<Toy> toys) {
        Toy theBestToy = null;
        int experience = 0;
        for (Toy aToy : toys) {
            if (experience < aToy.getXp()) {
                experience = aToy.getXp();
                theBestToy = aToy;

            }
        }
        return theBestToy;
    }
}

=========================================================================

import java.util.ArrayList;

public class Tamagotchi {

    private String name;
    private int level;
    private double energy;
    private int experiencePoint;
    private int mealsEaten;
    ArrayList<Toy> toys;

    public static final double MAX_ENERGY = 10.0;

    public Tamagotchi(String name, int level, double energy, int experiencePoint, int mealsEaten, ArrayList<Toy> toys) {
        this.name = name;
        this.level = level;
        this.energy = energy;
        this.experiencePoint = experiencePoint;
        this.mealsEaten = mealsEaten;
        this.toys = toys;
    }

    public Tamagotchi(String name) {
        this.name = name;
        level = 0;
        energy = MAX_ENERGY;
        mealsEaten = 0;
        Toy aToy = new Toy();
        toys = new ArrayList<>();
        toys.add(aToy);
    }

    public String getName() {
        return name;
    }

    public int getLevel() {
        return level;
    }

    public double getEnergy() {
        return energy;
    }

    public int getExperiencePoint() {
        return experiencePoint;
    }

    public int getMealsEaten() {
        return mealsEaten;
    }

    public ArrayList<Toy> getToys() {
        return toys;
    }
}

=======================================================================================

Add a comment
Know the answer?
Add Answer to:
The following are screen grabs of the provided files Thanks so much for your help, and have a n...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This assignment attempts to model the social phenomenon twitter. It involves two main classes: Tweet and...

    This assignment attempts to model the social phenomenon twitter. It involves two main classes: Tweet and TweetManager. You will load a set of tweets from a local file into a List collection. You will perform some simple queries on this collection. The Tweet and the TweetManager classes must be in separate files and must not be in the Program.cs file. The Tweet Class The Tweet class consist of nine members that include two static ones (the members decorated with the...

  • Purpose of it is to coordinate the interaction between the user and the lower-level functionality of...

    Purpose of it is to coordinate the interaction between the user and the lower-level functionality of the game. There are two overloaded constructors, one for constructing a brand new game and one for constructing a game from a file. w - Move Up    s - Move Down    a - Move Left    d - Move Right    q - Quit and Save Board If the user inputs any one of these characters, execute the corresponding move. This should result in a refreshed...

  • Your assignment is to write a grade book for a teacher. The teacher has a text file, which includ...

    Your assignment is to write a grade book for a teacher. The teacher has a text file, which includes student's names, and students test grades. There are four test scores for each student. Here is an example of such a file: Count: 5 Sally 78.0 84.0 79.0 86.0 Rachel 68.0 76.0 87.0 76.0 Melba 87.0 78.0 98.0 88.0 Grace 76.0 67.0 89.0 0.0 Lisa 68.0 76.0 65.0 87.0 The first line of the file will indicate the number of students...

  • 4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

    4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...

  • Java Software Originals, Inc., has been hired by Eaton Wright, the "pizza king", to help automate...

    Java Software Originals, Inc., has been hired by Eaton Wright, the "pizza king", to help automate a new chain of pizza delivery stores. SOI's system engineering staff have asked you to implement a prototype for the telephone operator's console. To start the project they have assigned you to implement the following two static methods (the first of which will be used to read files containing menus of pizza sizes and their prices, and pizza toppings and their prices; and the...

  • In java netbean8.1 or 8.2 please. The class name is Stock. It has 5 private attributes...

    In java netbean8.1 or 8.2 please. The class name is Stock. It has 5 private attributes (name, symbol, numberOfShares, currentPrice and boughtPrice)and one static private attribute (numberOfStocks). All attributes must be initialized (name and symbol to “No name/ symbol yet” and numberOfShares, currentPrice and boughtPrice to 0. Create two constructors, one with no arguments and the other with all the attributes. (However, remember to increase the numberOfStocks in both constructors. Write the necessary mutators and accessors (getters and setters) for...

  • create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines....

    create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...

  • C++ programming question, please help! Thank you so much in advance!!! In this exercise, you will...

    C++ programming question, please help! Thank you so much in advance!!! In this exercise, you will work with 2 classes to be used in a RPG videogame. The first class is the class Character. The Character class has two string type properties: name and race. The Character class also has the following methods: a constructor Character(string Name, string Race), that will set the values for the name and the race variables set/get functions for the two attributes a function print(),...

  • PLEASE DO IN JAVA 3) Add the following method to College: 1. sort(): moves all students...

    PLEASE DO IN JAVA 3) Add the following method to College: 1. sort(): moves all students to the first positions of the array, and all faculties after that. As an example, let fn indicate faculty n and sn indicate student n. If the array contains s1|f1|f2|s2|s3|f3|s4, after invoking sort the array will contain s1|s2|s3|s4|f1|f2|f3 (this does not have to be done “in place”). Students and faculty are sorted by last name. You can use any number of auxiliary (private) methods, if needed....

  • Problem 5: Monster Factory (10 points) (Game Dev) Create a Monster class that maintains a count...

    Problem 5: Monster Factory (10 points) (Game Dev) Create a Monster class that maintains a count of all monsters instantiated and includes a static method that generates a new random monster object. In software engineering, a method that generates new instances of classes based on configuration information is called the Factory pattern. UML Class Diagram: Monster - name: String - health: int - strength: int - xp: int + spawn(type:String): Monster + constructor (name: String, health: int, strength: int, xp:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT