Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course."— Presentation transcript:

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

2

3 public class Rectangle() { private int recLength; private int recWidth; ………………. } 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 public Rectangle() { recLength = 0; recWidth = 0; } Constructors are similar to methods. Constructors set the properties (instance variables) of an object to an initial state.

5 public Rectangle(int len, int width) { recLength = len; recWidth = width; } Constructors are similar to methods. Constructors set the properties (instance variables) of an object to an initial state.

6 public void setDimensions(int l, int w) { recLength = l; recWidth = w; } Modifier methods are methods that change the properties of an object. Modifier methods are sometimes referred to as Mutator methods.

7 public void setLength(int l) { recLength = l; } public void setWidth(int w) { recWidth = w; } Modifier methods are methods that change the properties of an object. Modifier methods are sometimes referred to as Mutator methods.

8 public int getLength() { return recLength; } public int getWidth() { return recWidth; } Accessor methods are methods that retrieve or grant access to the properties of an object, but do not make any changes.

9 public String toString() { return “Rectangle with length of “ + getLength() + " and width of " + getWidth(); } 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 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 data / properties. getIt( ) setIt( ) toString( ) Class/ Object private data / instance variables / properties

12

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

14

15 In Java, all classes are sub classes of class Object. This adds greater flexibility when writing programs in Java. Object StringDateMonster

16 public class Monster { ………… } The class automatically extends Object. You don’t have to explicitly extend it. extends Object

17 Because all classes are sub classes of Object, all classes start with the same methods..equals( ).toString( )..... and more

18

19

20 The equals() method is used to see if two objects have the same contents. String one = "comp"; String two = "sci"; System.out.println(one.equals(two));

21 public class Monster { private int height; //methods public boolean equals(Object obj) { Monster other = (Monster) obj; if (getHeight() == other.getHeight() ) return true; return false; } //methods } //test code in the main Monster one = new Monster(33); Monster two = new Monster(12); System.out.println(one.equals(two)); OUTPUT false

22

23

24 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.

25 public class Monster { private int height; private double weight; //methods public String toString() { return height + “, “ + weight; } //methods } //test code in the main Monster one = new Monster(10,200.0); System.out.println(one); OUTPUT 10, 200.0

26

27 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 inside of the class where they are defined.

29

30

31 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

33 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 String name; private int height; private double weight; public Monster(){ height=0; weight=0.0; } public Monster(int ht){ height=ht; weight=0.0; } public Monster(double wt){ height=0; weight=wt; } public Monster(int ht, double wt){ height=ht; weight=wt; }

35 class Monster { private String name; private int height; private double weight; //……constructors not shown public void setProperty(String nm) { name = nm; } public void setProperty(int ht) { height = ht; } public void setProperty(double wt) { weight = wt; }

36

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

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

39 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, int age) { code } //more methods }

40 Monster m = new Monster(); MONSTER Properties: height = 0, weight = 0, age = 0 methods m m is a reference variable that refers to a Monster object.

41 Monster m = new Monster(23); m is a reference variable that refers to a Monster object. MONSTER Properties height = 23, weight = 0, age = 0 methods m 0x234

42 Monster m = new Monster(23, 45); m is a reference variable that refers to a Monster object. MONSTER Properties height = 23, weight = 45, age = 0 methods m 0x239

43 Monster m = new Monster(23, 45, 11); m is a reference variable that refers to a Monster object. MONSTER Properties height = 23, weight = 45, age = 11 methods m 0x2B3

44


Download ppt "Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course."

Similar presentations


Ads by Google