🎯 Unit 3 Activities

Table of Contents


πŸ’» ACTIVITY PROGRAM SETUP INSTRUCTIONS
  1. Go to the public template repository for our class: BWL-CS Java Template
  2. Click the button above the list of files then select Create a new repository
  3. Specify the repository name: CS2-Unit-3-Activity
  4. Click

    Now you have your own personal copy of this starter code that you can always access under the Your repositories section of GitHub! πŸ“‚

  5. Now on your repository, click and select the Codespaces tab
  6. Click Create Codespace on main and wait for the environment to load, then you’re ready to code!

ACTIVITY #1: Class Pet 🐢🐱🐹🐰🐸

You’ve been hired to create a software system for the Awesome Animal Clinic! They would like to keep track of their animal patients. Your task is to write a complete class that defines a Pet object to keep track of pet records at the animal clinic.

Here are some attributes (data/fields/instance variables) of the pets that the clinic wants to track:

  • name
  • age
  • weight
  • type (dog, cat, lizard, etc.)
  • sterile (tracks if the pet is neutered/spayed or not)

Your class must include all the key object-defining class components covered so far: instance variables, constructors, accessor/getter methods, a toString method, and mutator/setter methods.

PART A: Set up Files

  1. Add some outline comments to Main.java:
     public class Main {
         public static void main(String[] args) {
             // CREATE 2 Pet objects with different initial values
        
             // TEST all Pet methods (getters, toString, setters)
        
         }
     }
    

    Because we don’t have a Pet class defined, we can’t actually work with any Pet objects in this file yet… we will come back to this file in Part C.

  2. Navigate to your Codespace’s πŸ“ File Explorer tab then βž• Create a new file named Pet.java.
  3. In Pet.java, write the class header and include a set of curly brackets to set up the file structure.
     public class Pet {
         // 1. Declare INSTANCE VARIABLES
        
         // 2. Define CONSTRUCTOR
        
         // 3. Define METHODS (getters, toString, setters)
        
     }
    

PART B: Define the Class

  1. Inside the Pet class, start by declaring the 5 instance variables with appropriate data types.

    Remember to make all the instance variables private!

  2. Write a constructor with a parameter list. This constructor should accept an argument for each of the instance variables.

    Link to notes section: πŸ““ 3.4 Writing Constructors - Parameterized

  3. Write accessor/getter methods, one for each of the instance variables.

    Link to notes section: πŸ““ 3.5 Writing Methods - Getters

  4. Write a toString method that returns a text string of the data in a Pet record.

    Link to notes section: πŸ““ 3.5 Writing Methods - toString

  5. Write mutator/setter methods, one for each of the instance variables.

    Link to notes section: πŸ““ 3.5 Writing Methods - Setters

PART C: Test the Class

  1. Navigate back to Main.java, this β€œclient” file is where you can run/test your Pet class code as a user.
  2. Inside the main() method, create 2 new Pet objects with different values by calling the constructor and passing in arguments.

    Link to notes section: πŸ““ 3.4 Writing Constructors - Creating Objects

  3. Call each of your accessor methods, mutator methods, and toString methods to demonstrate how they work.

REVIEW: CALLING METHODS

Remember that if you want to use an object’s instance method in another class, you must first create an object instance of that class, and then call its methods using the syntax pattern objectName.methodName().

Here are some examples, assuming turt is an object of the Turtle class:

// Calling a VOID method (no return)
turt.forward();

// Calling a NON-VOID method (store the return value)
int xPosition = turt.getXPos();
// OR just use it immediately (if you don't need it later)
System.out.println( turt.getXPos() );