Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.

Similar presentations


Presentation on theme: "Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring."— Presentation transcript:

1 Chapter 8

2 About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring break.

3 Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output data. Object-oriented programming (OOP) is a programming language model organized around "objects" rather than "actions" data rather than logic.

4 What is an object? An entity in the real world: a student, a desk, a circle, a button, a window… How do we describe an object? (a student, for example) State of an object (properties or attributes) name, ID, year, address… Behavior of an object (actions) drop a class, add a class…

5 Some more examples of objects Thinking of a circle as an object: Properties: radius (or diameter) Actions: calculate the area of the circle, calculate the circumference of the circle …

6 Classes A class is a template that defines objects of the same type.

7 Why OOP? The definition of a class is reusable. It is possible to define subclasses to inherit some or all of the main class characteristics. Person  Student, Teacher The concept of data classes allows a programmer to create any new data type that is not already defined in the language itself.

8

9 OOP Languages Simula (1967) was the first object-oriented programming language. Java, Python, C++, Visual Basic.NET and Ruby are the most popular OOP languages today.

10 How to describe an object? A circle is an object. Properties: radius (or diameter)  data field (variable) double radius = 5; Actions: get the area of the circle  method double getArea(){ double area = 3.14 * radius * radius; return area; }

11 How to describe an object? An object is a bundle of variables and related methods.

12 Let’s try to define this class in Java!

13 How to create an object? an object = an instance You can create many objects (instances) of a class in the main class.

14 Declaring an object Syntax: ClassName objectName; Example: Circle circle1; Circle circle2; Circle circle3; int[] scores;

15 Creating an object circle1 = new Circle(); scores = new int[20]; You can also combine the declaration and creation of an object. int[] scores = new int[20]; Circle circle1 = new Circle(); Circle circle2 = new Circle(); Circle circle3 = new Circle(); Scanner input = new Scanner(System.in);

16 Accessing an Object’s Data and Methods dot operator (.) objectName.dataField objectName.method() For example: circle2.radius circle3.radius circle1.getArea(); circle2.getArea();

17 What does “static” mean? “Static” means that variable or method marked as such is available at class level. You don’t need to create an instance (object) to access it. “Static” means there is only one! E.g., public static void printMessage(){ System.out.println(“This is a circle object.”); } radius and getArea() are at instance level. We will talk about static more..

18 Let’s try to create a few Circle objects! Two ways: In another file (same package!) In the same file

19 You can also give a default value to the data fields. You can always overwrite them in the main class.

20 Exercise 1: The Rectangle Class Follow the example of the Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height, The default values are 1 for both. A method named getArea(). A method named getPerimeter().

21 Constructors Circle circle1 = new Circle(); What is Circle()?? Constructor is a special type of method, that is invoked to create a new object. By default: (if no constructor is provided) Circle() { }

22 Constructors Circle() { } A constructor must have the same name as the class. Constructors do not have a return type (not even void). Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects.

23 Constructors Circle(){ radius = 1; }

24 Constructors You can overload constructors. Multiple constructors can have the same name but different signatures. Circle(){ radius = 1; } Circle(double r){ radius = r; }

25 Add to Exercise 1.. A no-arg constructor that creates a rectangle with width of 1 and height of 1. A constructor that creates a rectangle with the specified width and height. In your TestRectangle.java, create two Rectangle objects by invoking the two different constructors.

26 Primitive Data Types vs. Object Types

27 When Copying Variables..

28 Static variables Instance variable rectangle2.width = 5; rectangle2.height = 10; class variable (static variable) static int numberOfRectangles;

29 Static methods Instance method rectangle2.getArea() static method static int getNumberOfRectangles()

30 Static constants final static double PI = 3.14;

31 Visibility Modifiers By default, the class, variable, or method can be accessed by any class in the same package.  public The class, data, or method is visible to any class in any package.  private The data or methods can be accessed only from within its own class.

32

33 Data fields should be private. This is not good: double radius; //In Circle.java circle2.radius = 5; //In TestCircle.java Why? Data is not protected. class becomes difficult to maintain. Data fields should be private. private double radius;

34 Then how can we give a value to radius of circle1? circle1.radius = 5 does NOT work any more! The get and set methods are used to read and modify private properties. getRadius(): to read setRadius(double r): to write

35 Add to exercise 1.. In your Rectangle.java, make width and height private. Add get and set methods for the two private variables. In TestRectangle.java, create another two rectangles. Set one rectangle to have width of 10, height of 5. Set another rectangle to have width of 25, height of 10; Print their width, height, area and perimeter.

36 Exercise 2: The Student class Design a class named Student that contains: A private String data field named name; A private int data field named id; A private String data field named major; A constructor that creates a student with the specified id, name and major. The get and set methods for name, id and major.

37 Array of Objects int[] scores = new int[10]; Circle[] circles = new Circle[10];

38 Passing Objects to Methods public static void printRadius(Circle c)

39 Use classes from the Java library The Date Class import java.util.Date toString() returns the date and time as a string

40 Use classes from the Java library GUI Components The JFrame, JButton, JTextfield, JPanel class import java.swing.*;


Download ppt "Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring."

Similar presentations


Ads by Google