ISF DP Computer Science

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 lab

Be 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 the attribute/method is private and can be used only inside the class.

  • The + symbol means the attribute/method is public 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 settters. 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 #

โœ๏ธ It can be found here

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