0% found this document useful (0 votes)
7 views7 pages

Java OOP

This document serves as an introduction to Object-Oriented Programming (OOP) using Java, explaining key concepts such as classes, objects, attributes, and methods. It highlights the advantages of OOP over procedural programming and provides examples to illustrate the relationship between attributes and methods. Additionally, it includes exercises for students to practice their understanding of OOP concepts.

Uploaded by

juan.torres123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Java OOP

This document serves as an introduction to Object-Oriented Programming (OOP) using Java, explaining key concepts such as classes, objects, attributes, and methods. It highlights the advantages of OOP over procedural programming and provides examples to illustrate the relationship between attributes and methods. Additionally, it includes exercises for students to practice their understanding of OOP concepts.

Uploaded by

juan.torres123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

GUIDE No 1

Intro to OOP Page < # > de


<#>
Second marking period

AREA: Computer Science DATE:

Intro to OOP GRAD 11 A B C D


TOPIC:
E:
TEACHER: Mr. Diego Tovar
STUDENT
NAME:

Java
What is OOP?

OOP stands for Object-Oriented Programming.

Object-oriented programming (OOP) focuses on creating objects that encapsulate


both data and methods. Compared to procedural programming, OOP offers several
advantages:
• OOP is faster and easier to execute
• OOP provides a clear structure for the programs
• OOP helps to keep the Java code DRY "Don't Repeat Yourself", and makes
the code easier to maintain, modify and debug

To understand how Object-Oriented Programming (OOP) works, it's essential to


discuss classes and objects. But what exactly are classes and objects?
Classes and objects are the two fundamental components of object-oriented
programming (OOP).
GUIDE No 1
Intro to OOP Page < # > de
<#>
Second marking period

The following illustration highlights the difference between a class and its
objects:

class objects
Fruit Apple
Banana
Mango

Another example:

class objects
Car Volvo
Audi
Toyota

Everything in Java is associated with classes and objects, along with their attributes
and methods. For example: in real life, a car is an object. The car has attributes,
such as weight and color, and methods, such as drive and brake.
A Class is like an object constructor, or a "blueprint" for creating objects.

Attributes and methods

In Object-Oriented Programming (OOP), attributes and methods are key elements


that define the behavior and characteristics of an object.
• Attributes: These are the variables or properties of a class that store
information about the object.
• Methods: These are the functions defined within a class that describe the
behaviors or actions the object can perform.

Attributes
GUIDE No 1
Intro to OOP Page < # > de
<#>
Second marking period

Attributes represent the "state" or characteristics of an object. They are typically


defined within a class and are associated with each instance (object) of the
class.
Example:

class Car {

String brand; // Attribute: brand of the car

int year; // Attribute: year of manufacture

Think of attributes as the details about an object. In this example, every Car object
will have its own brand and year.

Methods
Methods define the actions or behaviors an object can perform. They are like verbs
or actions linked to the object.

class Car {

String brand;

int year;

void startEngine() { // Method: an action the car can perform

System.out.println("The engine is now running!");

void displayInfo() { // Method: another action

System.out.println("Brand: " + brand + ", Year: " + year);

}
GUIDE No 1
Intro to OOP Page < # > de
<#>
Second marking period

Methods are like instructions for what an object can do. In this example, the
startEngine method makes the car "do something," and the displayInfo method
shows the car's details.

Combining Attributes and Methods


Attributes and methods work together to define an object's full functionality.
The relationship between attributes and methods:
• Attributes: Think of them as "nouns" (what the object is).
• Methods: Think of them as "verbs" (what the object does).

Example:

class Car {

String brand;

int year;

void startEngine() { // Method: an action the car can perform

System.out.println("The engine is now running!");

void displayInfo() { // Method: another action

System.out.println("Brand: " + brand + ", Year: " + year);

}
GUIDE No 1
Intro to OOP Page < # > de
<#>
Second marking period

public class Main {

public static void main(String[] args) {

Car myCar = new Car();

myCar.brand = "Toyota";

myCar.year = 2022;

myCar.displayInfo(); // Output: Brand: Toyota, Year: 2022

myCar.startEngine(); // Output: Toyota engine started!

Activity
Exercise 1: Fill in the Blanks
Instructions: The code below is incomplete, fill in the blanks.

class ________ { // Fill in the class name

String ________; // Attribute 1

String ________; // Attribute 2

void displayInfo() { // Method

System.out.println("________: " + brand + ", Color: " + color); // Complete the print
statement

}
GUIDE No 1
Intro to OOP Page < # > de
<#>
Second marking period

Exercise 2 Debugging Challenge

Instructions: The code below has several syntax mistakes identify them and fix
them.

class Dog {

String breed;

int age;

void bark() {

System.out.print("Woof! Woof!");

void displayDetails() {

System.out.println("Breed: " + breed + ", Age: " + year);

}
GUIDE No 1
Intro to OOP Page < # > de
<#>
Second marking period

public class Main {

public static void main(String[] args) {

Dog myDog = new Dog();

myDog.breed = "Labrador";

myDog.age = 3;

myDog.bark();

myDog.displayinfo();

Exercise 3
Create a class of your choice (e.g., Animal, Book, Phone) with at least:
• 4 attributes (e.g., name, type)
• 3 methods (e.g., makeSound(), displayDetails())
Then create an object of that class, assign values to the attributes, and call the
methods.

Exercise 4
Create a class representing something you use daily (e.g., Smartphone, Backpack,
or Laptop). The class should include:
• At least 4 attributes and at least 4 methods.

You might also like