Tino Intro To Java

Last modified: January 12, 2024

Lab A3.2 Cat App

In this lab, you will create a class representing a Cat and a driver class that uses the Cat class.

Cat Class

Define a class named in the format PX_LastName_FirstName_Cat as follows:

  1. Declare the following fields. Be sure to choose appropriate names and data types for each field.
    • an instance variable to store the name of the cat. This variable should only be directly accessible inside the Cat class.
    • an instance variable to store the age of the cat in years as a whole number. This variable should only be directly accessible inside the Cat class.
    • an instance variable to hold the weight of the cat as a decimal in pounds. This variable should only be directly accessible inside the Cat class.
    • an instance variable that holds a true/false value representing whether the cat is domesticated (true) or wild (false). This variable should only be directly accessible inside the Cat class.
    • an instance variable to hold the hunger of the cat as a decimal between 0 (full) and 1 (starving). This variable should only be directly accessible inside the Cat class.
    • a class variable representing the default weight of a cat. This variable should be freely accessible from any class. Initialize the default weight to 0.24.
    • a class variable representing the default hunger of a cat. This variable should only be accessible from inside the Cat class and should be unchangeable once it is initialized. Initialized the default hunger to 0.25.
  2. Declare a default constructor that initializes a Cat with the following attributes:
    • The name of the cat is Tom
    • The age of the cat is 0
    • The weight of the cat is the default weight
    • The cat is wild
    • The hunger of the cat is the default hunger
  3. Declare a constructor that takes parameters for name, age, weight, and domesticated.
    • Initialize the appropriate instance variables to the values passed in to the parameters.
    • initialize hunger to the default hunger.
  4. Write a getter and setter for name, but just define getters for age, weight, domesticated and hunger.
    • Write an instance method named domesticatedStatus that returns a String that says domesticated if the cat is domesticated or wild if the cat is wild.
    • Write an instance method named ageDescription that returns a String describing what the cat is called based on age:
      • A cat under 2 years old is called a kitten
      • A 2 year old cat is called a juvenile cat
      • A cat that is more than 2 years old is called an adult cat
  5. Write an instance method named printDescription method that prints a description of the cat in the format given by the examples below:
    • Garfield is a 4 year old domesticated adult cat weighing 21.0 pounds
    • Nermal is a 1 year old domesticated kitten weighing 3.3 pounds
    • Slasher is a 2 year old wild juvenile cat weighing 5.9 pounds
  6. Write an instance method named feed that takes a decimal parameter and reduces the cat's hunger level by the amount of the parameter, down to a minimum of 0. If a wild cat reaches a hunger level of 0, it becomes domesticated.
  7. Write an instance method named growOlder that increases the cat's age by one year. Each time a cat under the age of 5 ages by a year, it gains 2.5 pounds. So a 4 year old cat who grew older would become 5 years old and gain 2.5 pounds, but a 5 year old cat who grew older would become 6 years old and stay the same weight.
  8. Write an instance method named play that first prints out a message based on the status of the cat and then increases the cats hunger by 0.25 up to a maximum of 1.
    • Wild cats always print out a message that says The cat darts away!
    • If a domesticated cat's hunger was at least 0.75 when play was called, print out Feed me meow!
    • if a domesticated cat's hunger was less than 0.5 when play was called, print out a message like this: Tom pounces on the toy! (where Tom is the name of the cat)
    • In all other cases, domesticated cats print out a message like this: Tom swats lazily at the toy. (where Tom is the name of the cat)

Driver Class

In order to test you Cat class, define a driver class named PX_LastName_FirstName_App that has a main method that does the following:

  1. Print the default weight for cats like this: The default weight for cats is 0.24 (do NOT hardcode the 0.24)
  2. Change the default weight for cats to 0.16
  3. Print a message saying the default weight for cats changed like this: The default weight for cats has changed to 0.16 (do NOT hardcode the 0.16)
  4. Create a Cat using the default constructor and store it in a variable named tinyTom.
  5. Call printDescription on tinyTom.
  6. Change the default weight for cats back to 0.24
  7. Print a message saying the default weight for cats changed like this: The default weight for cats has changed to 0.24 (do NOT hardcode the 0.24)
  8. Declare a variable named garfield and initialize it to a Cat named Garfield who is 4 years old, weighs 21 pounds and is domesticated.
  9. Call printDescription on garfield.
  10. Create a Cat using the default constructor and store a reference to it in a local variable (hereafter referred to as the cat)
  11. Call printDescription on the cat.
  12. Set the cat's name to Tazfire.
  13. Print out a message that reports the change in name like this: Changed name to Tazfire (Do NOT hardcode the name of the cat)
  14. Print out a message reporting the result of calling ageDescription on the cat like this: The cat is a kitten
  15. Declare a local variable named hunger and initialize it to the hunger of the cat.
  16. Print the hunger of the cat like this: Hunger is 0.25
  17. Call play on the cat.
  18. Call feed on the cat, passing 0.2 for the amount.
  19. Call printDescription on the cat.
  20. Call feed on the cat, passing 0.35 for the amount.
  21. Print a message reporting the current hunger of the cat like this: Hunger is 0.0
  22. Call printDescription on the cat.
  23. Call growOlder on the cat twice.
  24. Print out a message reporting the result of calling ageDescription on the cat like this: The cat is now a juvenile cat
  25. Call printDescription on the cat.
  26. Call play on the cat 4 times using a loop.
  27. Call growOlder on the cat.
  28. Print out a message reporting the result of calling ageDescription on the cat like this: The cat is now an adult cat
  29. Call printDescription on the cat.

A note about String comparison. To find out if two Strings say the same thing, you have to use the equals method, NOT ==. Here is an example:

String a = getName();
String b = getJob();
if (a.equals(b)) {
    System.out.println("Your name is your job!");
}

Prompt the user for an action like this: What would you like to do with the cat (feed, play, grow)? * If the user enters feed:

  1. Ask the user how much they would like to feed the cat like this: How much will you feed the cat?
  2. Call feed on the cat, passing the amount the user entered as the parameter.
  3. Print the hunger of the cat like this: Hunger is 0.25
    • If the user enters play, call play on the cat
    • If the user enters grow call growOlder on the cat and then call printDescription on the cat.
    • If the user enters anything else, print out a message saying Invalid command.

Sample Output

The output should always start like this:

The default weight for cats is 0.24
The default weight for cats has changed to 0.16
Tom is a 0 year old wild kitten weighing 0.16 pounds
The default weight for cats has changed to 0.24
Garfield is a 4 year old domesticated adult cat weighing 21.0 pounds
Tom is a 0 year old wild kitten weighing 0.24 pounds
Changed name to Tazfire
The cat is a kitten
Hunger is 0.25
The cat darts away!
Tazfire is a 0 year old wild kitten weighing 0.24 pounds
Hunger is 0.0
Tazfire is a 0 year old domesticated kitten weighing 0.24 pounds
The cat is now a juvenile cat
Tazfire is a 2 year old domesticated juvenile cat weighing 5.24 pounds
Tazfire pounces on the toy!
Tazfire pounces on the toy!
Tazfire swats lazily at the toy.
Feed me meow!
The cat is now an adult cat
Tazfire is a 3 year old domesticated adult cat weighing 7.74 pounds

If the user enters feed for the prompt, followed by 0.35, the output would finish like this:

What would you like to do with the cat (feed, play, grow)? feed
How much will you feed the cat? 0.35
Hunger is 0.65

If the user enters play, the output would finish like this:

What would you like to do with the cat (feed, play, grow)? play
Feed me meow!

If the user enters grow, the output would finish like this:

What would you like to do with the cat (feed, play, grow)? grow
Tazfire is a 4 year old domesticated adult cat weighing 10.24 pounds

Submission

  • Make sure you have named your classes correctly using the format PX_LastName_FirstName_Cat and PX_LastName_FirstName_App where X is your period. For example, if your name is Michael Wang and you are in 2nd period, the classes should be named P2_Wang_Michael_Cat and P2_Wang_Michael_App.
  • Make sure you changed all references in your code accordingly so the code will still compile.
  • If you changed the class names in BlueJ, the file names should have been automatically changed to match.
  • Look in the file explorer/finder inside your project folder and find the corresponding java files named in the format PX_LastName_FirstName_Cat.java and PX_LastName_FirstName_App.java. You should submit those two files below.
  • You can drag and drop or click on the Browse button and select both files so they are both submitted in a single submission.

You must Sign In to submit to this assignment

Dark Mode

Outline