chapter 8. about the midterm exam.. exam on march 12 monday (tentatively) review on march 7...

40
Objects and Classes Chapter 8

Upload: joseph-blankenship

Post on 01-Jan-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

Objects and Classes

Objects and ClassesChapter 8About the Midterm Exam..Exam on March 12 Monday (Tentatively)Review on March 7 WednesdayCover from Chapter 6Grades will be out before spring break.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.

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, addressBehavior of an object (actions)drop a class, add a classSome more examples of objectsThinking of a circle as an object:Properties: radius (or diameter)Actions: calculate the area of the circle, calculate the circumference of the circle

ClassesA class is a template that defines objects of the same type.

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, TeacherThe concept of data classes allows a programmer to create any new data type that is not already defined in the language itself.

OOP LanguagesSimula (1967) was the first object-oriented programming language. Java, Python, C++, Visual Basic .NET and Ruby are the most popular OOP languages today.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 methoddouble getArea(){ double area = 3.14 * radius * radius; return area;}How to describe an object?An object is a bundle of variables and related methods.

Lets try to define this class in Java!How to create an object?an object = an instanceYou can create many objects (instances) of a class in the main class.

Declaring an objectSyntax:ClassName objectName;Example:Circle circle1;Circle circle2;Circle circle3;int[] scores;Creating an objectcircle1 = 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);

Accessing an Objects Data and Methodsdot operator (.)objectName.dataFieldobjectName.method()

For example:circle2.radiuscircle3.radiuscircle1.getArea();circle2.getArea();

What does static mean?Static means that variable or method marked as such is available at class level.You dont 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..Lets try to create a few Circle objects!

Two ways:In another file (same package!)In the same fileYou can also give a default value to the data fields.You can always overwrite them in the main class.Exercise 1: The Rectangle ClassFollow 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().ConstructorsCircle 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() {}

ConstructorsCircle() {}

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.ConstructorsCircle(){ radius = 1;}

ConstructorsYou can overload constructors.Multiple constructors can have the same name but different signatures.

Circle(){ radius = 1;}

Circle(double r){ radius = r;}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.Primitive Data Types vs. Object Types

When Copying Variables..

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

Static methodsInstance method rectangle2.getArea()static methodstatic int getNumberOfRectangles()Static constantsfinal static double PI = 3.14;Visibility ModifiersBy default, the class, variable, or method can beaccessed by any class in the same package. publicThe 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.

Data fields should be private.This is not good:double radius; //In Circle.javacircle2.radius = 5; //In TestCircle.javaWhy?Data is not protected.class becomes difficult to maintain.Data fields should be private.private double radius;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 readsetRadius(double r): to write

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.Exercise 2: The Student classDesign 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.

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

Passing Objects to Methodspublic static void printRadius(Circle c)Use classes from the Java libraryThe Date Class

import java.util.DatetoString() returns the date and time as a stringUse classes from the Java libraryGUI Components

The JFrame, JButton, JTextfield, JPanel classimport java.swing.*;

Three objects of the Circle class

A class template

Class Name: Circle

Properties:

radius is _______

Actions:

getArea

Circle Object 3

Properties:

radius is 125

Circle Object 2

Properties:

radius is 25

Circle Object 1

Properties:

radius is 10

Class Name: Circle

Data Fields:

radius is _______

Methods:

getArea

Circle Object 3

Data Fields:

radius is 125

Circle Object 2

Data Fields:

radius is 25

Circle Object 1

Data Fields:

radius is 10

Three objects of the Circle class

A class template

Created using new Circle()

Object typeCircle c c

1

c: Circle

radius = 1

Primitive typeint i = 1 i

reference

Before:

Primitive type assignment i = j

i

1

2

j

2

j

2

After:

i

Before:

Object type assignment c1 = c2

c1

c2

C2: Circle

radius = 9

c2

c1: Circle

radius = 5

After:

c1

c1: Circle

radius = 5

C2: Circle

radius = 9

public class C3 {

void aMethod() {

C1 o = new C1();

o.x;

o.y;

o.z;

o.m1();

o.m2();

o.m3();

}

}

public class C2 {

void aMethod() {

C1 o = new C1();

o.x;

o.y;

o.z;

o.m1();

o.m2();

o.m3();

}

}

package p2;

public class C1 {

public int x;

int y;

private int z;

public void m1() {

}

void m2() {

}

private void m3() {

}

}

package p1;

public class C3 {

C1;

C2;

}

package p2;

public class C2 {

C1

}

class C1 {

...

}

package p1;