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 recHeight; 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() { recHeight = 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 ht, int width) { recHeight = ht; 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 h, int w) { recHeight = h; 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 setHeight(int h) { recHeight = h; } 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 getHeight() { return recHeight; } 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 height of “ + getHeight() + " 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 automatically sub classes of the Object class. This adds greater flexibility when writing programs in Java. Object StringMovieMonster

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"; if (one.equals(two)) System.out.print(“Strings are equal“);

21 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 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”); OUTPUT heights not equal

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; 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); OUTPUT Sam is 10 ft tall and weighs 200.0 lbs

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

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

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, String n) { code } //more methods }

40 Monster m = new Monster(); MONSTER Properties: height = 0, weight = 0, name = “no name” 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, name = “no name” 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, name = “no name” methods m 0x239

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

44


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

Similar presentations


Ads by Google