lecture 14
play

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


  1. Lecture 14 Objects and Classes

  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

  3. Object-oriented programming (OOP) • Identity = variable name, location in memory • State = data fields (variables) • Behavior = methods

  4. Object example: ball • Identity = redBall • State = ballColor, radius, position • Behavior = fall(), bounce()

  5. Creating and using objects • 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

  6. Creating and using objects • 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

  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; } }

  8. Circle Example

  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

  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; }

  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();

  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

  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()); } }

  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

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

  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); } }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend