Share
Explore

Student Lab Learning Workbook 1: Introduction to Java Object-Oriented Programming

Welcome to this student lab learning workbook!
This workbook aims to guide new Java learners through the process of learning Java Object-Oriented Programming (OOP) with simple exercises.
By completing these exercises, you will understand the core concepts of OOP and be able to construct simple Java programs with objects.
References:
Course PowerPoint Workbook:

Table of Contents

Introduction to Object-Oriented Programming

Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to represent real-world entities and their interactions.
The goal of OO Programming is to keep our code organized.
It aims to make code more organized, modular, and easier to maintain. There are four main principles of OOP:
Encapsulation: The bundling of data and methods that operate on that data within a single unit (class).
Inheritance: The ability for a class to inherit properties and methods from a parent class.
Polymorphism: The ability for a method to have different implementations based on the object it is acting upon.
Abstraction: The process of simplifying complex systems by breaking them down into smaller, more manageable parts.
There are 4 kinds of Data Members that can be in a Java Class:
Class Static Variables
Class Instance Variables
Method Local Variables
Loop Variables.
Note: The this keyword is a reference to an OBJECT. NOT a class.
image.png
There are 4 kinds of visibility modifiers in Java:
Public
Private
Protected
default

When we use the new keyword: We are making an OBJECT from a CLASS.
Student joe = new Student();
image.png
image.png
2 methods are overloaded if they have the same method name but they differ in the method signature: that is, the parameters we are passing in to the Method Declaration.

For your Test Questions: Keep track of the difference between defining a method and calling a method.

public class person{ static String name; static int age; static String program; public static void main(String[] args) { RunProgram(); }

public static void RunProgram(){ Introduce(); }
public static void Introduce() { System.out.print("HELLO My name is " + name + " My age is " + age + " My program is " + program ); } }

image.png
To make the Introduce() method non-static, remove the static keyword. Then, to call it from the RunProgram() method, you need to create an instance of the Person class.
public class Person { String name; int age; String program;
public static void main(String[] args) { RunProgram(); }
public static void RunProgram() { Person personInstance = new Person(); personInstance.Introduce(); }
public void Introduce() { System.out.print("HELLO My name is " + name + " My age is " + age + " My program is " + program ); } }
Note that I also changed the class name to Person with an uppercase 'P' to follow Java naming conventions for class names.

Exercise 1: Creating a Class

Task: Create a simple Java class named "Person" with no attributes or methods.

public class Person {
}
image.png

Exercise 2: Adding Attributes

Task: Add three attributes to the "Person" class: name, age, and gender.
javaCopy codepublic class Person { String name; int age; String gender;}

Exercise 3: Creating Methods

Task: Create a method called introduce() in the "Person" class that prints the person's name, age, and gender.
public class Person { String name; int age; String gender;
public void introduce() { System.out.println("Hi, my name is " + name + ", I am " + age + " years old, and I am a " + gender + "."); }}

Exercise 4: Creating an Object

Task: Create an object of the "Person" class and assign values to its attributes. Call the introduce() method for the object.
javaCopy codepublic class Main { public static void main(String[] args) { Person person1 = new Person(); person1.name = "Alice"; person1.age = 30; person1.gender = "female"; person1.introduce(); }}

Exercise 5: Using Getters and Setters

Task: Add getters and setters for the attributes of the "Person" class.
javaCopy codepublic class Person { private String name; private int age; private String gender;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public String getGender() { return gender; }
public void setGender(String gender) { this.gender = gender; }
public void introduce() { System.out.println("Hi, my name is " + name + ", I am " + age + " years old, and I am a " + gender + "."); }}

Exercise 6: Constructors

Task: Add a constructor to the "Person" class that accepts name, age, and gender as parameters.
javaCopy codepublic class Person { private String name; private int age; private String gender;
public Person(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; }
// Getters, setters, and introduce() method omitted for brevity}

Exercise 7: Inheritance

Task: Create a new class called "Student" that inherits from the "Person" class. Add a new attribute called major and provide getters and setters for it.
javaCopy codepublic class Student extends Person { private String major;
public Student(String name, int age, String gender, String major) { super(name, age, gender); this.major = major; }
public String getMajor() { return major; }
public void setMajor(String major) { this.major = major; }}

Exercise 8: Polymorphism

Task: Override the introduce() method in the "Student" class to include the major.
javaCopy codepublic class Student extends Person { private String major;
public Student(String name, int age, String gender, String major) { super(name, age, gender); this.major = major; }
public String getMajor() { return major; }
public void setMajor(String major) { this.major = major; }
@Override public void introduce() { System.out.println("Hi, my name is " + getName() + ", I am " + getAge() + " years old, and I am a " + getGender() + " studying " + major + "."); }}

Exercise 9: Encapsulation

Task: Make the attributes in the "Person" class private and use getters and setters to access them.
java
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.