chapter 1.pdf
Document Details
Uploaded by PatientTabla
Full Transcript
Data Structure CCCS 224 Chapter 1 Review: Objects Object-oriented programming concepts Classes, objects, and inheritance O.O. Programming Concepts qObject-oriented programming (OOP) involves programming using objects. qAn object represents an en...
Data Structure CCCS 224 Chapter 1 Review: Objects Object-oriented programming concepts Classes, objects, and inheritance O.O. Programming Concepts qObject-oriented programming (OOP) involves programming using objects. qAn object represents an entity in the real world that can be distinctly identified. An object is an instance of a class. qFor example, a student, a desk, a circle, a button, and even a loan can all be viewed as objects. An object has a unique identity, state, and behaviors. qThe state of an object consists of a set of data fields (also known as properties, attributes) with their current values. qThe behavior of an object is defined by a set of methods. Objects Class Name: Circle A class templa te Data Fields: radius is _______ Methods: getArea Circle Object 1 Circle Object 2 Circle Object 3 Three objects of the Circle class Dat a Fields : Data Fields: Data Fields: radius is 10 radius is 25 radius is 125 An object has both a state and behavior. The state defines the object (current values of its attributes), and the behavior defines what the object does. 3 Classes qClasses are constructs that define objects of the same type. A class is used to group related objects together. A class is considered as a factory to create objects. qA Java class uses variables (attributes) to define data fields and methods to define behaviors. qAdditionally, a class provides a special type of methods, known as constructors, which are invoked to construct objects from the class. 4 An Example of Classes class Circle { double radius = 1.0; Data field Circle() { } Constructors Circle(double newRadius) { radius = newRadius; } double getArea() { Method return radius * radius * 3.14159; } }//end of a class 5 Constructors qConstructors are a special kind of methods that are invoked to initialise objects. //Zero-argument constructor Circle() { } //One-argument constructor Circle(double newRadius) { radius = newRadius; } 6 Constructors, cont. qA constructor with no parameters is referred to as a no-arg or zero-arg constructor. qConstructors must have the same name as the class itself. qConstructors do not have a return type—not even void. qConstructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects. 7 Default Constructor qA class may be defined without constructors. In this case, a no-arg constructor with an empty body is implicitly defined in the class. qThis constructor, called a default constructor, is provided automatically only if no constructors are explicitly defined in the class. 8 Declaring Object Reference Variables To declare an object (reference variable), use the syntax: ClassName objectRefVarName; Example: Circle myCircle; To create an object: objectName = ClassName; mycircle = new Circle(); 9 Trace Code, cont. Circle myCircle = new Circle(5.0); myCircle no value Circle yourCircle = new Circle(); : Circle yourCircle.radius = 100; radius: 5.0 Create a circle 10 Trace Code, cont. Circle myCircle = new Circle(5.0); myCircle reference value Circle yourCircle = new Circle(); yourCircle.radius = 100; : Circle radius: 5.0 Assign object reference to myCircle 11 Trace Code, cont. myCircle reference value Circle myCircle = new Circle(5.0); Circle yourCircle = new Circle(); : Circle yourCircle.radius = 100; radius: 5.0 yourCircle no value Declare yourCircle 12 Trace Code, cont. myCircle reference value Circle myCircle = new Circle(5.0); : Circle Circle yourCircle = new Circle(); radius: 5.0 yourCircle.radius = 100; yourCircle no value : Circle Create a new radius: 1.0 Circle object 13 Trace Code, cont. reference value Circle myCircle = new Circle(5.0); myCircle Circle yourCircle = new Circle(); : Circle yourCircle.radius = 100; radius: 5.0 yourCircle reference value Assign object reference to : Circle yourCircle radius: 1.0 14 Example: Defining Classes and Creating Objects TV channel: int The current channel (1 to 120) of this TV. volumeLevel: int The current volume level (1 to 7) of this TV. on: boolean Indicates whether this TV is on/off. The + sign indicates +TV() Constructs a default TV object. a public modifier. +turnOn(): void Turns on this TV. +turnOff(): void Turns off this TV. +setChannel(newChannel: int): void Sets a new channel for this TV. +setVolume(newVolumeLevel: int): void Sets a new volume level for this TV. +channelUp(): void Increases the channel number by 1. +channelDown(): void Decreases the channel number by 1. +volumeUp(): void Increases the volume level by 1. +volumeDown(): void Decreases the volume level by 1. 15 Class TV public class TV { public void setVolume(int newVolumeLevel) { if (on && newVolumeLevel >= 1 && //Attributes newVolumeLevel 1) // constructors channel--; } public TV() {} //Methods public void volumeUp() { if (on && volumeLevel < 7) public void turnOn() {on = true; } volumeLevel++; public void turnOff() { on = false;} } public void setChannel(int newChannel) { public void volumeDown() { if (on && volumeLevel > 1) if (on && newChannel >= 1 && newChannel