Lecture 14 Objects and Classes
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
Object-oriented programming (OOP) • Identity = variable name, location in memory • State = data fields (variables) • Behavior = methods
Object example: ball • Identity = redBall • State = ballColor, radius, position • Behavior = fall(), bounce()
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
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
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; } }
Circle Example
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
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; }
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();
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
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()); } }
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
Date import java.util.* ; public class DateTesting { public static void main(String[] args) { Date date = new Date(); System.out.println(date.toString()); } }
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); } }
Recommend
More recommend