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.
megaphone

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.
java
Copy code
public 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.
java
Copy code
public 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.
java
Copy code
public 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.
java
Copy code
public 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;
}

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.