Presentation is loading. Please wait.

Presentation is loading. Please wait.

MIT AITI 2004 Lecture 7 Classes and Objects - Part I.

Similar presentations


Presentation on theme: "MIT AITI 2004 Lecture 7 Classes and Objects - Part I."— Presentation transcript:

1 MIT AITI 2004 Lecture 7 Classes and Objects - Part I

2 The GradeBook Example How we coded the GradeBook... Constructed an array of grades Wrote methods to perform operations on the array Passed the array as an argument to these methods

3 Procedural Programming The GradeBook we coded is an example of procedural programming The program is written as one long sequence of operations Sequences of operations can be placed into methods/functions/procedures.

4 A GradeBook Object A new idea for our grade book program... Construct a GradeBook object that stores an array of grades inside it GradeBook has operations like printGrades and meanGrade to others can use. Our code constructs a GradeBook and asks it to perform its operations.

5 Object-Oriented Programming Creating a GradeBook object is an example of object-oriented programming In OOP, we create objects that encapsulate both data and operations Instead of creating one long sequence of operations, we create objects and define how they interact.

6 What is an Object? An Object has two primary components: state – properties of the object behavior – operations the object can perform Examples objectstatebehavior dogbreed, isHungryeat, bark grade bookgradesmean, median lighton/offswitch

7 Objects in Java A class defines a new type of Object To create a Object type to represent a light switch... class LightSwitch { // state and behavior here }

8 Fields An Object's state is stored in variables called fields Fields are declared (and optionally initialized) inside the braces of the class Light switch example with a field... class LightSwitch { boolean on = true; }

9 Methods An Object's behavior is defined by its methods Methods, like fields, are written inside the braces of the class Methods can access the fields (the state) of their object and can change them

10 Light Switch Example class LightSwitch { boolean on = true; // field boolean isOn() { // returns return on; // the state } void switch() { // changes on = !on; // the state }

11 Constructing Objects We use the new keyword to construct a new instance of an Object We can assign this instance to a variable with the same type of the Object Note: classes define new datatypes ! LightSwitch ls = new LightSwitch();

12 ls.on; ls.isOn(); ls.switch(); Using Fields and Methods To access the field of an instance of an Object use instance.field To access the method of an instance use instance.method(arguments)

13 Example Using Light Switch What does this main method print out? LightSwitch ls = new LightSwitch(); System.out.println(ls.on); ls.switch(); System.out.println(ls.isOn()); true false

14 Person Example class Person { String name = "Greg"; int age = 23; String getName() { return name; } void setName(String n) { name = n; } int getAge() { return age; } void setAge(int a) { age = a; } }

15 Constructing Person Objects To create an instance of the Person class with a name of "George" and an age of 22 Can we create a Person that has the name George and the age 22 from the moment it is created? Answer: Yes! Person george = new Person(); george.setName("George"); george.setAge(22);

16 Constructors Constructors are special methods used to construct an instance of a class They have no return type They have the same name as the class of the Object they are constructing They initialize the state of the Object Call the constructor by preceding it with the new keyword

17 Person Constructor Now we can construct George as follows: class Person { String name; int age; Person(String n, int a) { name = n; age = a; } //... } Person george = new Person("George", 22);

18 Default Constructor When you do not write a constructor in a class, it implicitly has a constructor with no arguments and an empty body Result: every class has a constructor class LightSwitch { // Leaving out the constructor // is the same as... LightSwitch() {} }

19 Multiple Constructors A class can have multiple constructors class LightSwitch { boolean on; LightSwitch() { on = true; } LightSwitch(boolean o) { on = o; }

20 This Keyword Instance can refer to itself with the keyword this class LightSwitch { boolean on; LightSwitch() { on = true; // this.on = true } LightSwitch(boolean on) { this.on = on; }

21 Cascading Constructors A constructor can call another constructor with this(arguments) class LightSwitch { boolean on; LightSwitch() { this(true); } LightSwitch(boolean on) { this.on = on; }

22 Classes Recap Classes have fields to store the state of the objects in the class and methods to provide the operations the objects can perform. We construct instances of a class with the keyword new followed by a call a constructor method of the class. We can assign an instance to a variable with a datatype named the same as the class. If you do not provide a constructor, the class will have one with no arguments and no statements by default.

23 Apple Example class Apple { String color; double price; Apple(String color, double price) { this.color = color; this.price = price; } Apple(double price) { this("green", price); } String getColor() { return color; } double getPrice() { return price; } void setPrice(double p) { price = p; } }

24 Apple Quiz What will these lines print out? Apple a = new Apple("red", 100.0); System.out.println(a.getColor()); System.out.println(a.getPrice()); a.setPrice(50.5); System.out.println(a.getPrice()); Apple b = new Apple(74.6); System.out.println(b.getColor()); System.out.println(b.getPrice()); b.setPrice(a.getPrice()); System.out.println(b.getPrice()); red 100.0 50.5 green 74.6 50.5


Download ppt "MIT AITI 2004 Lecture 7 Classes and Objects - Part I."

Similar presentations


Ads by Google