Object Oriented Programming (OOP) #
This lab introduces the concept of OOP in Java.
[0] Setup #
Clone the Repository #
๐ป select [Projects
] > [Get from VCS
] > [Repository URL
]
#
๐ป Paste your URL to clone the labBe sure to change yourgithubusername
to your actual GitHub username.
https://github.com/isf-dp-cs/lab_oop_yourgithubusername
Switch to Java 21 #
๐ป Select [File
] > [Project Structure
]
๐ป From the SDK:
dropdown, select Java 21
[1] Killing the Cat #
First, take a look at the Cat
class:
public class Cat {
//attributes
private String name;
private int lives = 9;
//methods
public Cat(String name) {
this.name = name;
}
public void speak() {
System.out.println("meow!!");
}
// main method
public static void main(String[] args) {
Cat myCat = new Cat("Ollie"); // creates a new cat object
myCat.speak(); //calls the cat object's method
}
}
You can see that there are two attributes (name
and lives
) and three methods (the constructor
, speak
and main
).
The main
method already creates a Cat
object and gets it to seak
๐ป
Add a method
that lets the cat introduce itself. It should print a greeting out to the console, including the cat’s name.
๐ป
Test your new method by calling it in the main
method.
You can see that the cat starts out with 9 lives.
๐ป
Add a loop to the main
method that can kill the cat.
[2] Superhumans #
Here is an overview of the Superhuman class.
The
-
symbol means that theattribute
/method
isprivate
and can be used only inside the class.The
+
symbol means theattribute
/method
ispublic
and can be used outside of this class.Superhuman –Attributes– - name: String - age: int –Methods– + Superhuman(String name, int age) + getName(): returns a String + setName(String name): returns void + getAge(): returns an int + setAge(int age): returns void + printInfo(): returns void —this method should print out all the attributes
Test the Superhuman Class #
๐ป
In the main()
method that creates two superhuman objects and prints out their info using the printInfo
method.
๐ป
Add more steps to main()
to test out each of the settter
s. Call printInfo()
each time, to check that it’s working correctly
Add new features #
๐ป
Add in a new attribute, alias
. Make sure to add the correstponding setters/getters.
๐ป
Test your additions in the main
method.
[3] Deliverables #
โกโจFill in the Google Form #
Push to Github #
๐ป Select Commit from the menu on the left.
Select all your updated files. Be sure to include a descriptive commit message.
๐ป Click Commit and Push
๐ป Click Push