Lecture 14 Objects and Classes Object-oriented programming (OOP) - - PowerPoint PPT Presentation

lecture 14
SMART_READER_LITE
LIVE PREVIEW

Lecture 14 Objects and Classes Object-oriented programming (OOP) - - PowerPoint PPT Presentation

Lecture 14 Objects and Classes Object-oriented programming (OOP) Were always looking for ways to standardize, organize and simplify our code. Objects can help We use objects to represent things in the real world - like a student, a


slide-1
SLIDE 1

Lecture 14

Objects and Classes

slide-2
SLIDE 2

Object-oriented programming (OOP)

  • We’re always looking for ways to standardize, organize

and simplify our code. Objects can help

  • We use objects to represent things in the real world -

like a student, a receipt, a hand, etc

  • Objects have a unique identity, state, and behavior.
  • Especially useful when you have multiple copies of the

same type

slide-3
SLIDE 3

Object-oriented programming (OOP)

  • Identity = variable name, location in memory
  • State = data fields (variables)
  • Behavior = methods
slide-4
SLIDE 4

Object example: ball

  • Identity =
  • State =
  • Behavior =

redBall ballColor, radius, position fall(), bounce()

slide-5
SLIDE 5

Creating and using

  • bjects
  • Objects are created using a class
  • A class is like a template for the object
  • A class stores the data fields and methods for the object
  • An object is often referred to as an instance of a class.
  • So the variable redBall would be of type Ball, for example
  • Class is to Object/Instance, as Lasagne Recipe is to an actual

lasagne

slide-6
SLIDE 6

Creating and using

  • bjects
  • Classes also have special methods, constructors
  • Constructors are used when making a new object.
  • These are useful when you want to set default values, for

example

slide-7
SLIDE 7

Circle Example

public class Circle { /** The radius of this circle */ double radius = 1; /** Construct a circle object */ Circle() { } /** Construct a circle object */ Circle(double newRadius) { radius = newRadius; } /** Return the area of this circle */ double getArea() { return radius * radius * Math.PI; } /** Return the perimeter of this circle */ double getPerimeter() { return 2 * radius * Math.PI; } /** Set new radius for this circle */ void setRadius(double newRadius) { radius = newRadius; } }

slide-8
SLIDE 8

Circle Example

slide-9
SLIDE 9

Constructors - they’re weird

  • Must have the same name as the class
  • NO return type! Not even void
  • Invoked using new when the object is created
  • Can be overloaded
slide-10
SLIDE 10

Constructors - they’re weird

/** Construct a circle object */ Circle() { } /** Construct a circle object */ Circle(double newRadius) { radius = newRadius; } /** Construct a circle object */ Circle(double newRadius, int xIn, int yIn){ radius = newRadius; xPos = xIn; yPos = yIn; }

slide-11
SLIDE 11

Reference variables

  • Objects are created and accessed using a reference

variable

  • This is a variable that is used to refer to a specific

instance of a class

ClassName objectRefVar = new ClassName(); Circle myCircle = new Circle();

slide-12
SLIDE 12

Instance variables

  • Variables in the class
  • can be accessed using the dot operator (e.g.

redBall.radius)

  • If it is a reference value (not a primitive type) and doesn’t

have a default value, it will be null. Good example are strings

  • null is a literal value, similar to true or false
slide-13
SLIDE 13

Test class for circle

public class TestCircle { public static void main(String[] args) { Circle circle1 = new Circle(); System.out.println("The area of the circle of radius " + circle1.radius + " is " + circle1.getArea()); Circle circle2 = new Circle(25); System.out.println("The area of the circle of radius " + circle2.radius + " is " + circle2.getArea()); circle2.setRadius(100); System.out.println("The area of the circle of radius " + circle2.radius + " is " + circle2.getArea()); } }

slide-14
SLIDE 14

Tons of classes available in java already!

  • For example, Date, Random, Scanner, etc.
  • Objects of these types are created and used in the exact

same way

slide-15
SLIDE 15

Date

import java.util.*; public class DateTesting { public static void main(String[] args) { Date date = new Date(); System.out.println(date.toString()); } }

slide-16
SLIDE 16

Random

import java.util.*; public class RandomTesting { public static void main(String[] args) { Random rand1 = new Random(3); Random rand2 = new Random(3); Random rand3 = new Random(); for (int i = 0; i < 100; i++){ System.out.println("rand1: " + rand1.nextInt(10)); System.out.println("rand2: " + rand2.nextInt(10)); } // For a new random integer within a range int max = 10; int min = 5; int randomNum = rand3.nextInt((max - min) + 1) + min; System.out.println(randomNum); } }