π― Unit 3 Activities
Table of Contents
π» ACTIVITY PROGRAM SETUP INSTRUCTIONS
- Go to the public template repository for our class: BWL-CS Java Template
- Click the button above the list of files then select
Create a new repository - Specify the repository name:
CS2-Unit-3-Activity - Click
Now you have your own personal copy of this starter code that you can always access under the
Your repositoriessection of GitHub! π - Now on your repository, click and select the
Codespacestab - Click
Create Codespace on mainand 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:
nameageweighttype(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
- 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
Petclass defined, we canβt actually work with any Pet objects in this file yetβ¦ we will come back to this file in Part C. - Navigate to your Codespaceβs π File Explorer tab then β Create a new file named
Pet.java. - 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
- Inside the
Petclass, start by declaring the 5 instance variables with appropriate data types.Remember to make all the instance variables
private! - 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
- Write accessor/getter methods, one for each of the instance variables.
Link to notes section: π 3.5 Writing Methods - Getters
- Write a
toStringmethod that returns a text string of the data in aPetrecord.Link to notes section: π 3.5 Writing Methods - toString
- 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
- Navigate back to
Main.java, this βclientβ file is where you can run/test yourPetclass code as a user. - Inside the
main()method, create 2 newPetobjects with different values by calling the constructor and passing in arguments.Link to notes section: π 3.4 Writing Constructors - Creating Objects
- Call each of your accessor methods, mutator methods, and
toStringmethods 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() );