1 of 20

Intro to Object-Oriented Programming

COMP 210 / 2024 Summer Session I / Lecture 03

Guest Lecturers: Ajay Gandecha and Liana Ma

2 of 20

Announcements

  • The schedule for the week is posted on the website.
    • EX01 due tonight! EX02 released, due Wed at 11:59pm. ​
  • Quiz tomorrow!​
    • Review session this afternoon at 4pm in SN 156 (CSXL Fishbowl)

3 of 20

In-Class Example

Problem Statement:

  • Design a Java program that finds the perimeter of a triangle.

Live Demonstration in IntelliJ

  • Create a new class file for Lesson 3 and follow along!

4 of 20

Motivations for OOP

  • Our code works great for one triangle, but becomes inefficient at scale.
  • We need a better, more readable way to organize our code.
  • This course is about designing and analyzing data structures.
  • Data Structure: a particular way to represent data so it can be used effectively.
    • e.g. ArrayList, String

5 of 20

Object Oriented Programming

  • (OOP): Programming software to structure data using objects.

Fields: Describes the attributes of the object.

Ex) color, weight, width, height

Methods: Describes what objects can do (the actions they can perform).

Ex) addWeight(), changeColor()

(??)�OBJECT

6 of 20

Example 1

  • What is the mystery object?

Fields: breed, name, age

Methods: fetchBone(), digHole()

(??)�OBJECT

7 of 20

Classes (Review from COMP 110)

  • Classes are blueprints for objects - they define objects’ fields and methods.

Fields: Describes the attributes of the object.

Methods: Describes what objects can do (the actions they can perform).

Constructor: Defines how objects are created.

  • Constructors are like factories that create the object (sets its initial attributes).

8 of 20

Classes and Objects

  • An object is defined by its state… an object is really just a collection of named fields that represent some information about the object​.

  • Object design reflects purpose… what fields are included will reflect how that object is to be used​.

9 of 20

Python

Java

10 of 20

Going Back to our Example

  • Let’s think about our triangle perimeter problem with an OOP mindset.

(x,y)

(x,y)

(x,y)

Our program created variables for every x and y coordinate for every point in our triangle (6 variables in total)

Let’s make a Point object.� (Represents a triangle in 3 variables!)

11 of 20

Creating the Point class

  • What are some example fields and methods for this object?

Fields:

Methods:

x, y

distanceTo(point)

Point

12 of 20

Demo in IntelliJ

13 of 20

Going Back to our Example

Let’s make a Triangle object that stores our three points!

  • Let’s think about our triangle perimeter problem with an OOP mindset.

Point

Our new program created variables for every Point in our triangle (3 variables in total)

Point

Point

14 of 20

Creating the Triangle class

  • What are some example fields and methods for this object?

Fields:

Methods:

pointA, pointB, pointC

findPerimeter()

Triangle

15 of 20

Demo in IntelliJ

16 of 20

Step 1: Name the Object

Creating a Java Class

  • In Java this means create a class for that kind of object.

17 of 20

Step 2: Declare its Fields

  • The fields of your object are pieces of information that collectively define it.
    • Declared like variables
    • Must specify type and adhere to variable naming rules.
    • Declared in class definition, NOT within a method, but floating off by themselves.
    • Good idea to keep them together at the top of the class.

  • Here you start to make design decisions
    • In our example, triangles defined by six numbers interpreted as coordinates.
    • How else could a triangle be defined?
  • Note that part of this is deciding on types as well.
  • What would be impact of choosing something other than double?

​​

18 of 20

Step 3: Declare a Constructor

  • Constructor is a special type of method
  • Purpose is to create and initialize a new instance.
  • Declaration differs from a normal method
  • Name must match class name.
  • Does not have a any sort of return value in its signature.
  • Within the constructor, the keyword this refers to the new object (i.e., the instance) to be initialized.
  • Any information needed should be passed in as parameters.
  • Code in the constructor is responsible for making sure that the fields of this are appropriately set.
  • To call a constructor, use the new keyword
  • Result will be a reference to the new instance (i.e., object) of the class

19 of 20

Step 4: Define Instance Methods

  • Functions/procedures that depend on the specific instance

  • Declare instance methods without “static” keyword
  • That is what makes it an instance method.
  • Instance methods only make sense in the context of a specific instance.
  • Must be called with the “.” operator using a reference to an object:
  • reference.method()
  • Within an instance method, the keyword this provides a reference to the instance for which the method was called.
  • To get value of a particular instance field: this.field
  • Or just field if unambiguous (i.e., no local name in call frame with same name)

20 of 20

Anatomy of a Class