Back

Activity 4.1 Introduction to Object-Oriented Programming

divider

Introduction

Activity 4.1

Introduction to Object-Oriented Programming

Topics

  • What are Objects?
  • What are Classes?
  • Instance variables (Attributes) and Methods (Behavior)
  • Creating Objects with new
  • Using the Dot Operator to Access Members

Why Object-Oriented Programming?

So far, you've worked with variables, methods, conditionals, and loops. These tools let us solve many problems, but larger programs can become difficult to organize.

Object-Oriented Programming (OOP) helps us organize code around things in our program: students, bank accounts, dogs, game characters, messages, and more.

Objects

An object is a specific instance of something. Think nouns:

  • One specific dog named "Sparky"
  • Your personal bank account
  • A single student in this class

Objects have:

  • Attributes (data about the object)
  • Behavior (actions the object can perform)

Classes

A class is a blueprint for creating objects. It describes what all objects of that type know and can do.

  • A Dog class might store a dog's name, breed, and age.
  • It might define behaviors like bark() and eat().

A class is also a custom data type that you define.

Example: Dog Class and Instances

Identity: Dog Class
Attributes: name, breed, age, color
Behavior: Bark(), Eat()

Example: Dog Class and Instances

Dog Object Dog Object Dog Object Dog Object
Fido, Greyhound, Age 3, gray Dodger, Ger. Shep, Age 6, brown Pooch, Husky, Age 1, blue Star, Shiba Inu, Age 2, tan
Fido barks and eats Dodger barks and eats Pooch barks and eats Star barks and eats

Demo: Dog Class

class Dog {
String name;
String breed;
int age;
void bark() {
System.out.println("Woof!");
}
void eat() {
System.out.println(name + " is eating.");
}
}

This Dog class defines:

  • Instance Variables: name, breed, age
  • Methods: bark(), eat()

Creating an Object (Instantiating)

To use a class, create an object using the new keyword.

public class Program {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.name = "Sparky";
myDog.breed = "Boxer";
myDog.age = 1;
System.out.println("Meet " + myDog.name + " the " + myDog.breed + ".");
myDog.eat();
myDog.bark();
}
}

Creating an Object (Instantiating)

public class Program {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.name = "Sparky";
myDog.breed = "Boxer";
myDog.age = 1;
System.out.println("Meet " + myDog.name + " the " + myDog.breed + ".");
myDog.eat();
myDog.bark();
}
}
  1. Dog myDog declares a variable of type Dog.
  2. new Dog() creates a new object.
  3. myDog.name = "Sparky"; uses the dot operator to access an instance variable.
  4. myDog.bark(); calls a method on the object.

Key Terms

Class
A blueprint or template for objects. Defines the instance variables (data) and methods (behavior) shared by all objects of that type.
Object
A specific instance of a class, with its own values stored in the instance variables.

Key Terms

Instance Variable
A variable declared inside a class that stores data about an object.
Method
A block of code inside a class that defines an action an object can perform.
Dot Operator
The . used to access an object's instance variables and methods, such as myDog.bark().

'F' → Fullscreen

Objectives

  • icon Describe the difference between a class and an object.
  • icon Create simple classes with instance variables and methods in Java.
  • icon Instantiate objects using the new keyword.
  • icon Access instance variables and call methods using the dot operator.
divider

Activity Tasks

  • icon Create a new project named 5-1-Classes-and-Objects.
  • icon Complete each task individually.

Task 1: Basic Student Class

In this task, you will create a simple Student class with a few instance variables and one method. Then you will create two student objects and call their method.

Step 1: Create a file named Student.java and type the following code:

Student.java
public class Student
{
String name;
int gradeLevel;
void introduce()
{
System.out.println("Hi, I'm " + name + " and I'm in grade " + gradeLevel + ".");
}
}

Step 2: Create a file named Program.java and type the following code:

Program.java
public class Program
{
public static void main(String[] args)
{
Student s1 = new Student();
s1.name = "Alex";
s1.gradeLevel = 10;
Student s2 = new Student();
s2.name = "Jordan";
s2.gradeLevel = 11;
s1.introduce();
s2.introduce();
}
}

Step 3: Run the program. Make sure you understand how the objects are created and how the introduce() method uses the instance variables.


Task 2: Adding GPA and Behavior

Now you will extend the Student class to store a GPA and add new behavior based on that data.

Step 1: Update your Student.java to match the code below:

Student.java
public class Student
{
String name;
int gradeLevel;
double gpa;
void introduce()
{
System.out.println("Hi, I'm " + name + " and I'm in grade " + gradeLevel + ".");
}
void showGpa()
{
System.out.println(name + "'s GPA is " + gpa + ".");
}
void checkHonorRoll()
{
if (gpa >= 3.5)
{
System.out.println(name + " made the honor roll!");
}
else
{
System.out.println(name + " did not make the honor roll this time.");
}
}
}

Step 2: Replace the code in Program.java with the following:

Program.java
public class Program
{
public static void main(String[] args)
{
Student s1 = new Student();
s1.name = "Alex";
s1.gradeLevel = 10;
s1.gpa = 3.8;
Student s2 = new Student();
s2.name = "Jordan";
s2.gradeLevel = 11;
s2.gpa = 3.2;
s1.introduce();
s1.showGpa();
s1.checkHonorRoll();
System.out.println();
s2.introduce();
s2.showGpa();
s2.checkHonorRoll();
}
}

Step 3: Run the program. Observe how each student's instance variables are used to determine whether they made the honor roll.

Challenge: Add a third student of your choice with a different GPA and test your methods again.

divider

Sample Output

Your program output should look similar to the sample output below (exact wording or spacing may vary slightly).

Sample Output
Hi, I'm Alex and I'm in grade 10.
Alex's GPA is 3.8.
Alex made the honor roll!
Hi, I'm Jordan and I'm in grade 11.
Jordan's GPA is 3.2.
Jordan did not make the honor roll this time.
divider

Reflection Questions

You may write your reflection answers as comments at the bottom of your Program.java file.

  1. In your own words, what is the difference between a class and an object? Use the Student example in your answer.
  2. Explain how the dot operator is used in this activity. Include at least one example of accessing an instance variable and one example of calling a method.
  3. How would this activity be different if we tried to store all of the student information in separate variables instead of using a Student class?
  4. What is one advantage of organizing your code into classes and objects when programs get larger and more complex?
divider

Submission

Submit your completed Student.java, Program.java, and your reflection answers to the appropriate dropbox.

Activity Complete