Presentation is loading. Please wait.

Presentation is loading. Please wait.

Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)

Similar presentations


Presentation on theme: "Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)"— Presentation transcript:

1 Writing a Class (defining a data-type)

2 Create a new project : Project (uncheck the “Create Main Class”)

3 Name the class For example: Add a new file - > Java Class

4 Add 4 private instance variables These instance variables are the object’s properties (attributes) For example: private String color; private double size; private String type; private boolean feather;

5 Add a constructor The constructor is a special method of the class that always has the same name as the class and has NO return type. The constructor is used to set the values of the object at construction time. The only way an object can be constructed is the way it is defined by the constructor.

6 Constructor example: public OrthHat ( String c, double s) { color = c; size = s; type = “Fedora”; //default type for all hats feather = false; //default for all hats }

7 To test the class, we must create a test class. Add a new file to your project – Java Main Class For example: OrthTestHat

8 Add code to create 2 objects of the class that you just created For example: OrthHat hat = new OrthHat (“BLUE”, 6.5); OrthHat hat2 = new OrthHat (“RED”, 4.8); SOPL (hat); SOPL (hat2);

9 Run your main class What does the ouput look like? An object variable points to the memory location of where the object can be found To print the values of the private instance variables (properties) we must write a method to do so.

10 Add a toString method to your class definition For example: public String toString ( ) { return “\nColor: “ + color + “\nSize: “ + size + “\nType: “ + type + “\nFeather: “ + feather; }

11 Now run your test class again The output should show the values of the instance variables (the state of the object). For example: Color: BLUE Size: 6.5 Type: Fedora Feather: false

12 We can do better though… Change your toString method so that it doesn’t print true or false, but rather prints a statement based on the value of the feather variable (see next slide). public String toString( ) {

13 Modified toString( ) public String toString ( ) { String str = “”; str += “\nColor: “ + color + “\nSize: “ + size + “\nType: “ + type; if (feather == true) str += “ with feather “; return str;

14 Add a method to you class definition that lets you change an instance variable For example: public void changeType (String t) { type = t; } Or public void changeFeather ( ) { feather = ! feather; }

15 Use this method in you test class For example: hat.changeType (“Baseball cap”); hat2.changeFeather( ); (NOTE: the values (arguments) passed into a method must match the class method definition)

16 Check your output Add another SOPL to your main code that displays the object property values again. SOPL (hat); SOPL (hat2); Did the output show the new information?


Download ppt "Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)"

Similar presentations


Ads by Google