Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP Powerpoint slides from A+ Computer Science

Similar presentations


Presentation on theme: "OOP Powerpoint slides from A+ Computer Science"— Presentation transcript:

1 OOP Powerpoint slides from A+ Computer Science
Modified by Mr. Smith for his course

2 Pieces of the OOP Puzzle

3 instance variables public class Rectangle() {
private int recHeight; private int recWidth; ………………. } Constructors are typically used to initialize all of the Object’s data/properties. The default constructor usually sets all data/properties to a zero value. The exact zero value depends on the specified type of each instance variable. Instance variables are used to store information for a given object. Each instance of an object has its own copy of each instance variable.

4 constructors Default Constructor
public Rectangle() { recHeight = 0; recWidth = 0; } Default Constructor Constructors are typically used to initialize all of the Object’s data/properties. The default constructor usually sets all data/properties to a zero value. The exact zero value depends on the specified type of each instance variable. Constructors are similar to methods. Constructors set the properties (instance variables) of an object to an initial state.

5 constructors Initialization Constructor
public Rectangle(int ht, int width) { recHeight = ht; recWidth = width; } Initialization Constructor Constructors are typically used to initialize all of the Object’s data/properties. The initialization constructor usually sets the data/properties to a provided parameter value. Constructors are similar to methods. Constructors set the properties (instance variables) of an object to an initial state.

6 modifier methods public void setDimensions(int h, int w) {
recHeight = h; recWidth = w; } Modifier methods are used to modify the Object’s data/properties. Set methods are modifier methods. Modifier methods are methods that change the properties of an object. Modifier methods are sometimes referred to as Mutator methods.

7 modifier methods public void setHeight(int h) { recHeight = h; } public void setWidth(int w) recWidth = w; Set methods are modifier methods. Set methods are used to change the data/properties of an Object. Modifier methods are methods that change the properties of an object. Modifier methods are sometimes referred to as Mutator methods.

8 accessor methods public int getHeight() { return recHeight; } public int getWidth() return recWidth; Accessor methods are used to retrieve the data/properties from the Object. Get methods are accessor methods. Accessor methods do not make changes to the data/properties. Accessor methods are methods that retrieve or grant access to the properties of an object, but do not make any changes.

9 accessor methods public String toString() { return “Rectangle with height of “ + getHeight() + " and width of " + getWidth(); } The toString() method is an accessor method. The toString() method should return all data/properties. The toString() should not change the data/properties of the Object. The toString method is a special accessor method. It returns the string representation of the object. It does not modify the values of the instance variables.

10 encapsulation All data members (instance variables) should have private access. The public constructors, accessor methods, and modifier methods should be used to manipulate and retrieve the data. All data is tucked away nicely inside the class.

11 The public methods give you access to an object’s private
encapsulation The public methods give you access to an object’s private data / properties. getIt( ) private data / instance variables / properties Class/ Object setIt( ) toString( )

12 RectangleTester classes
Show Rectangle and RectangleTester classes

13 Lab/Homework: Movie class
Create a Movie class to store information about a movie and a MovieBox class that represents a kiosk to buy movies from.

14 Object

15 Object class In Java, all classes are automatically sub classes of the Object class. This adds greater flexibility when writing programs in Java. Object String Movie Monster

16 All classes extend Object!
public class Monster { ………… } The class automatically extends Object. You don’t have to explicitly extend it. extends Object Java automatically applies the extends Object to all new classes created. All classes extend Object.

17 Object class Because all classes are sub
classes of Object, all classes start with the same methods. .equals( ) checks to see if the object points to the same object in memory as another object. .toString( ) returns the string representation of the object. and more Object contains quite a few methods, but equals() and toString() are the most commonly used.

18 Open MonsterOne.java

19 equals

20 The equals() method The equals() method is used to see
if two objects have the same contents. String one = "comp"; String two = "sci"; if (one.equals(two)) System.out.print(“Strings are equal“); To determine if two Objects are the same, the data/properties of both Objects must be compared.

21 equals() OUTPUT heights not equal public class Monster {
private int height; private double weight; private String name; // constructor method and other methods not shown public boolean equals(Object obj) Monster other = (Monster) obj; if (getHeight() == other.getHeight() ) return true; return false; } //test code in the main method Monster one = new Monster(33, 200.0, “Stu”); Monster two = new Monster(12, 150.0, “Alice”); if (one.equals(two)) System.out.print(“heights are equal”); else System.out.print(“heights not equal”); equals() OUTPUT heights not equal To determine if two Objects are the same, the data/properties of both Objects must be compared. Monster contains a height property only. Monsters are compared by comparing the heights. If the heights of two Monsters are the same, the Monsters are considered equal.

22 Open Monster and MonsterEqualsTester.java

23 toString

24 The toString() method The toString() method is used to return a version of your object in String form. The most common use of this method is when you are printing the object. System.out.print(Monster); Since the print() method requires a String data type, Java automatically calls the toString() method before printing the object. To determine if two Objects are the same, the data/properties of both Objects must be compared.

25 toString() OUTPUT Sam is 10 ft tall and weighs 200.0 lbs
public class Monster { private int height; private double weight; private String name; // constructor method and other methods not shown public String toString() return name + “ is “ + height + “ ft tall and weighs “ + weight + “ lbs”; } // test code in the main Monster one = new Monster(10, 200.0, ”Sam”); System.out.println(one); toString() To determine if two Objects are the same, the data/properties of both Objects must be compared. Monster contains a height property only. Monsters are compared by comparing the heights. If the heights of two Monsters are the same, the Monsters are considered equal. OUTPUT Sam is 10 ft tall and weighs lbs

26 Private/Public

27 All members with public access can be accessed
What does public mean? All members with public access can be accessed inside and outside of the class where they are defined.

28 All members with private access can only be accessed
What does private mean? All members with private access can only be accessed inside of the class where they are defined.

29 Open Private.java

30 Constructors

31 Constructors If you do not provide any constructors, Java will provide a default constructor. This allows you to instantiate the object, but its properties (instance variables) will not be initialized.

32 Open MonsterTwo.java

33 Overloading Overloading occurs when you have more than one method or constructor with the same name in the same class. Each method or constructor must have a different parameter list. The number of parameters and the data types of the parameters matter

34 class Monster { private int height; private double weight; private String name; public Monster(){ height=0; weight=0.0; name=“no name”; } public Monster(int ht){ height=ht; public Monster(double wt){ weight=wt; public Monster(int ht, double wt, String nm){ name=nm; Overloading The Monster constructor has been overloaded as it appears 4 times. Each time Monster() is written, a different set of parameters is provided. Java can differentiate between the Monster() constructors by the parameter list.

35 Overloading class Monster { private int height; private double weight;
private String name; //……constructors not shown public void setProperty(int ht) height = ht; } public void setProperty(double wt) weight = wt; public void setProperty(String nm) name = nm; Overloading The Monster constructor has been overloaded as it appears 4 times. Each time Monster() is written, a different set of parameters is provided. Java can differentiate between the Monster() constructors by the parameter list.

36 Open MonsterOverload and MonsterOverloadTester

37 Monster Object Diagram Monster() - constructors
setX( ) - modifiers getX( ) - accessors toString( ) - accessor Monster

38 constructor modifier accessor accessor
public class Monster { //instance vars / data fields public Monster(){ //code } public void setX( params ){ public int getX(){ public String toString() { constructor modifier accessor accessor

39 Constructors class Monster{ // instance variables
public Monster(){ code } public Monster(int ht) { code } public Monster(int ht, int wt) { code } public Monster(int ht, int wt, String n) { code } //more methods }

40 Monster Instantiation 1
Monster m = new Monster(); m MONSTER Properties: height = 0, weight = 0, name = “no name” methods m is a reference variable that refers to a Monster object.

41 Monster Instantiation 2
Monster m = new Monster(23); 0x234 m MONSTER Properties height = 23, weight = 0, name = “no name” methods 0x234 m is a reference variable that refers to a Monster object.

42 Monster Instantiation 3
Monster m = new Monster(23, 45); 0x239 m MONSTER Properties height = 23, weight = 45, name = “no name” methods 0x239 m is a reference variable that refers to a Monster object.

43 Monster Instantiation 4
Monster m = new Monster(23, 45, “Gus”); 0x2B3 m MONSTER Properties height = 23, weight = 45, name = “Gus” methods 0x2B3 m is a reference variable that refers to a Monster object.

44 Start work on Animal Class


Download ppt "OOP Powerpoint slides from A+ Computer Science"

Similar presentations


Ads by Google