Department of Mathematics - Lehman College



Chapter 6: More about Objects and Methods

Additional Reference

Object-oriented programming (OOP) involves programming using objects.

• An object represents an entity in the real world that can be distinctly identified. For example, a student, a desk, a circle, a button, and even a loan can all be viewed as objects.

• An object has a unique identity, state, and behaviors. The state of an object consists of a set of data fields (also known as properties or instance variables) with their current values. The behavior of an object is defined by a set of methods.

Objects

• An object has both a state and behavior. The state defines the object, and the behavior defines what the object does.

Classes

• Classes are constructs that define objects of the same type. A Java class uses variables to define data fields and methods to define behaviors. Additionally, a class provides a special type of methods, known as constructors, which are invoked to construct objects from the class.

UML Class Diagram

Constructors

Constructors are a special kind of methods that are invoked to construct objects.

Circle() {

}

Circle(double newRadius) {

radius = newRadius;

}

• A constructor with no parameters is referred to as a no-arg constructor.

·       Constructors must have the same name as the class itself.

·       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 instantiating and initializing objects.

new ClassName();

Example:

new Circle();

new Circle(5.0);

• A class may be declared without constructors. In this case, a no-arg constructor with an empty body is implicitly declared in the class. This constructor, called a default constructor, is provided automatically only if no constructors are explicitly declared in the class.

• To declare a reference variable, use the syntax:

ClassName objectRefVar;

Example:

Circle myCircle;

• Declaring/Creating Objects in a Single Step

ClassName objectRefVar = new ClassName();

Example:

Circle myCircle = new Circle();

• Referencing the object’s data:

objectRefVar.data

Example: myCircle.radius

• Invoking the object’s method:

objectRefVar.methodName(arguments)

Example: myCircle.getArea()

public class Circle {

/** The radius of the circle */

double radius;

/** Construct a circle with radius 1 */

Circle() {

radius = 1.0;

}

/** Construct a circle with a specified radius */

Circle(double newRadius) {

radius = newRadius;

}

/** Return radius */

public double getRadius() {

return radius;

}

public void setRadius(double radius) {

this.radius = radius;

}

/** Return the area of this circle */

double getArea() {

return radius * radius * Math.PI;

}

}

//TestCircle.java:

public class TestCircle {

/** Main method */

public static void main(String[] args) {

// Create a Circle with radius 5.0

Circle myCircle = new Circle(5.0);

System.out.println("The area of the circle of radius "

+ myCircle.getRadius() + " is " + myCircle.getArea());

// Increase myCircle's radius by 10%

myCircle.setRadius(myCircle.getRadius() * 1.1);

System.out.println("The area of the circle of radius "

+ myCircle.getRadius() + " is " + myCircle.getArea());

}

}

• Recall that you use

Math.methodName(arguments) (e.g., Math.pow(3, 2.5))

to invoke a method in the Math class.

Can you invoke getArea() using Circle.getArea()? The answer is no. All the methods used before this chapter are static methods, which are defined using the static keyword. However, getArea() is non-static. It must be invoked from an object using

objectRefVar.methodName(arguments) (e.g., myCircle.getArea()).

• The data fields can be of object reference types. For example, the following Student class contains a data field name of the String type.

public class Student {

String name; // name has default value null

int age; // age has default value 0

boolean isScienceMajor; // isScienceMajor has default value false

char gender; // c has default value '\u0000'

}

• If a data field of an object reference type does not reference any object, the data field holds a special literal value, null.

• The default value of a data field is null for a reference type, 0 for a numeric type, false for a boolean type, and '\u0000' for a char type.

public class Student {

String name;

int age;

boolean isScienceMajor;

char gender;

}

public class Test {

public static void main(String[] args) {

Student student = new Student();

System.out.println("name? " + student.name);

System.out.println("age? " + student.age);

System.out.println("isScienceMajor? " + student.isScienceMajor);

System.out.println("gender? " + student.gender);

}

}

• Java assigns no default value to a local variable inside a method.

public class Test {

public static void main(String[] args) {

int x; // x has no default value

String y; // y has no default value

System.out.println("x is " + x); //compiliation error

System.out.println("y is " + y); //compilation error

}

}

• Differences between Variables of Primitive Data Types and Object Types

• Copying Variables of Primitive Data Types and Object Types

• Garbage Collection: As shown in the previous figure, after the assignment statement c1 = c2, c1 points to the same object referenced by c2. The object previously referenced by c1 is no longer referenced. This object is known as garbage. Garbage is automatically collected by JVM.

Using Classes from the Java Library

• Java provides a system-independent encapsulation of date and time in the java.util.Date class. You can use the Date class to create an instance for the current date and time and use its toString method to return the date and time as a string.

java.util.Date date = new java.util.Date();

System.out.println(date.toString());

displays a string like Sun Mar 09 13:50:19 EST 2003.

Instance Variables, and Methods

• Instance variables belong to a specific instance.

• Instance methods are invoked by an instance of the class.

Static Variables, Constants, and Methods

• Static variables are shared by all the instances of the class.

• Static methods are not tied to a specific object.

• Static constants are final variables shared by all the instances of the class.

• To declare static variables, constants, and methods, use the static modifier.

Example:

public class Circle2 {

/** The radius of the circle */

double radius;

/** The number of the objects created */

static int numberOfObjects = 0;

/** Construct a circle with radius 1 */

Circle2() {

radius = 1.0;

numberOfObjects++;

}

/** Construct a circle with a specified radius */

Circle2(double newRadius) {

radius = newRadius;

numberOfObjects++;

}

/** Return numberOfObjects */

static int getNumberOfObjects() {

return numberOfObjects;

}

/** Return the area of this circle */

double getArea() {

return radius * radius * Math.PI;

}

}

public class TestCircle2 {

/** Main method */

public static void main(String[] args) {

// Create c1

Circle2 c1 = new Circle2();

// Display c1 BEFORE c2 is created

System.out.println("Before creating c2");

System.out.println("c1 is : radius (" + c1.radius +

") and number of Circle objects (" +

Circle2.numberOfObjects + ")"); //you can access this by c1.numberofObjects but with warning

// Create c2

Circle2 c2 = new Circle2(5);

// Change the radius in c1

c1.radius = 9;

// Display c1 and c2 AFTER c2 was created

System.out.println("\nAfter creating c2 and modifying " +

"c1's radius to 9");

System.out.println("c1 is : radius (" + c1.radius +

") and number of Circle objects (" +

c1.numberOfObjects + ")");

System.out.println("c2 is : radius (" + c2.radius +

") and number of Circle objects (" +

c2.numberOfObjects + ")");

}

}

Visibility Modifiers and Accessor/Mutator Methods

• 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 by the declaring class.

• The get and set methods are used to read and modify private properties.

The private modifier restricts access to within a class, the default modifier restricts access to within a package, and the public modifier enables unrestricted access.

• An object cannot access its private members, as shown in (b). It is OK, however, if the object is declared in its own class, as shown in (a).

• Why Data Fields Should Be private?

To protect data.

To make class easy to maintain.

Example of Data Field Encapsulation

public class Circle3 {

private double radius = 1;

/** The number of the objects created */

private static int numberOfObjects = 0;

/** Construct a circle with radius 1 */

public Circle3() {

numberOfObjects++;

}

/** Construct a circle with a specified radius */

public Circle3(double newRadius) {

radius = newRadius;

numberOfObjects++;

}

/** Return radius */

public double getRadius() {

return radius;

}

/** Set a new radius */

public void setRadius(double newRadius) {

radius = (newRadius >= 0) ? newRadius : 0;

}

/** Return numberOfObjects */

public static int getNumberOfObjects() {

return numberOfObjects;

}

/** Return the area of this circle */

public double getArea() {

return radius * radius * Math.PI;

}

}

public class TestCircle3 {

public static void main(String[] args) {

Circle myCircle = new Circle(5.0);

System.out.println("The area of the circle of radius "

+ myCircle.getRadius() + " is " + myCircle.getArea());

// Increase myCircle's radius by 10%

myCircle.setRadius(myCircle.getRadius() * 1.1);

System.out.println("The area of the circle of radius "

+ myCircle.getRadius() + " is " + myCircle.getArea());

}

}

Immutable Objects and Classes

• If the contents of an object cannot be changed once the object is created, the object is called an immutable object and its class is called an immutable class. If you delete the set method in the Circle class in the preceding example, the class would be immutable because radius is private and cannot be changed without a set method.

• A class with all private data fields and without mutators is not necessarily immutable. For example, the following class Student has all private data fields and no mutators, but it is mutable.

Example

public class Student {

private int id;

private BirthDate birthDate;

public Student(int ssn,

int year, int month, int day) {

id = ssn;

birthDate = new BirthDate(year, month, day);

}

public int getId() {

return id;

}

public BirthDate getBirthDate() {

return birthDate;

}

}

public class BirthDate {

private int year;

private int month;

private int day;

public BirthDate(int newYear,

int newMonth, int newDay) {

year = newYear;

month = newMonth;

day = newDay;

}

public void setYear(int newYear) {

year = newYear;

}

}

public class Test {

public static void main(String[] args) {

Student student = new Student(111223333, 1970, 5, 3);

BirthDate date = student.getBirthDate();

date.setYear(2010); // Now the student birth year is changed!

}

}

What Class is Immutable?

• For a class to be immutable, it must mark all data fields private and provide no mutator methods and no accessor methods that would return a reference to a mutable data field object.

• In reality, such an immutable class may not be useful.

Passing Objects to Methods

• Passing by value for primitive type value (the value is passed to the parameter)

• Passing by value for reference type value (the value is the reference to the object)

public class TestPassObject {

public static void main(String[] args) {

// Create a Circle object with radius 1

Circle3 myCircle = new Circle3(1);

// Print areas for radius 1, 2, 3, 4, and 5.

int n = 5;

printAreas(myCircle, n);

// See myCircle.radius and times

System.out.println("\n" + "Radius is " + myCircle.getRadius());

System.out.println("n is " + n);

}

/** Print a table of areas for radius */

public static void printAreas(Circle3 c, int times) {

System.out.println("Radius \t\tArea");

while (times >= 1) {

System.out.println(c.getRadius() + "\t\t" + c.getArea());

c.setRadius(c.getRadius() + 1);

times--;

}

}

}

Scope of Variables

• The scope of instance and static variables is the entire class. They can be declared anywhere inside a class.

• The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be initialized explicitly before it can be used.

The this Keyword

• Use this to refer to the object that invokes the instance method.

• Use this to refer to an instance data field.

• Use this to invoke an overloaded constructor of the same class.

Example: Serving as Proxy to the Calling Object

Calling overloaded constructor

[pic]

Class Abstraction and Encapsulation

Class abstraction means to separate class implementation from the use of the class. The creator of the class provides a description of the class and let the user know how the class can be used. The user of the class does not need to know how the class is implemented. The detail of implementation is encapsulated and hidden from the user.

Example: Loan class

public class Loan {

private double annualInterestRate;

private int numberOfYears;

private double loanAmount;

private java.util.Date loanDate;

/** Default constructor */

public Loan() {

this(7.5, 30, 100000);

}

public Loan(double annualInterestRate, int numberOfYears,

double loanAmount) {

this.annualInterestRate = annualInterestRate;

this.numberOfYears = numberOfYears;

this.loanAmount = loanAmount;

loanDate = new java.util.Date();

}

/** Return annualInterestRate */

public double getAnnualInterestRate() {

return annualInterestRate;

}

/** Set a new annualInterestRate */

public void setAnnualInterestRate(double annualInterestRate) {

this.annualInterestRate = annualInterestRate;

}

/** Return numberOfYears */

public int getNumberOfYears() {

return numberOfYears;

}

/** Set a new numberOfYears */

public void setNumberOfYears(int numberOfYears) {

this.numberOfYears = numberOfYears;

}

/** Return loanAmount */

public double getLoanAmount() {

return loanAmount;

}

/** Set a newloanAmount */

public void setLoanAmount(double loanAmount) {

this.loanAmount = loanAmount;

}

/** Find monthly payment */

public double getMonthlyPayment() {

double monthlyInterestRate = annualInterestRate / 1200;

return loanAmount * monthlyInterestRate / (1 -

(Math.pow(1 / (1 + monthlyInterestRate), numberOfYears * 12)));

}

/** Find total payment */

public double getTotalPayment() {

return getMonthlyPayment() * numberOfYears * 12;

}

/** Return loan date */

public java.util.Date getLoanDate() {

return loanDate;

}

}

import javax.swing.JOptionPane;

public class TestLoan {

/** Main method */

public static void main(String[] args) {

// Enter yearly interest rate

String annualInterestRateString = JOptionPane.showInputDialog(

"Enter yearly interest rate, for example 8.25:");

// Convert string to double

double annualInterestRate =

Double.parseDouble(annualInterestRateString);

// Enter number of years

String numberOfYearsString = JOptionPane.showInputDialog(

"Enter number of years as an integer, \nfor example 5:");

// Convert string to int

int numberOfYears = Integer.parseInt(numberOfYearsString);

// Enter loan amount

String loanString = JOptionPane.showInputDialog(

"Enter loan amount, for example 120000.95:");

// Convert string to double

double loanAmount = Double.parseDouble(loanString);

// Create Loan object

Loan loan =

new Loan(annualInterestRate, numberOfYears, loanAmount);

// Format to keep two digits after the decimal point

double monthlyPayment =

(int)(loan.getMonthlyPayment() * 100) / 100.0;

double totalPayment =

(int)(loan.getTotalPayment() * 100) / 100.0;

// Display results

String output = "The loan was created on " +

loan.getLoanDate().toString() + "\nThe monthly payment is " +

monthlyPayment + "\nThe total payment is " + totalPayment;

JOptionPane.showMessageDialog(null, output);

}

}

Example: Creating Windows Using the JFrame Class

import javax.swing.JFrame;

public class TestFrame {

public static void main(String[] args) {

JFrame frame1 = new JFrame();

frame1.setTitle("Window 1");

frame1.setSize(200, 150);

frame1.setLocation(200, 100);

frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame1.setVisible(true);

JFrame frame2 = new JFrame();

frame2.setTitle("Window 2");

frame2.setSize(200, 150);

frame2.setLocation(410, 100);

frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame2.setVisible(true);

}

}

More on Math class

Method Abstraction

You can think of the method body as a black box that contains the detailed implementation for the method.

Benefits of Methods

• Write a method once and reuse it anywhere.

• Information hiding. Hide the implementation from the user.

• Reduce complexity.

The Math Class

Class constants:

– PI

– E

Class Methods

– Trigonometric Methods

– Exponent Methods

– Rounding Methods

– min, max, abs, and random Methods

• Trigonometric Methods

sin(double a)

cos(double a)

tan(double a)

acos(double a)

asin(double a)

atan(double a)

Examples:

Math.sin(0) returns 0.0

Math.sin(Math.PI / 6) returns 0.5

Math.sin(Math.PI / 2) returns 1.0

Math.cos(0) returns 1.0

Math.cos(Math.PI / 6) returns 0.866

Math.cos(Math.PI / 2) returns 0

• Exponent Methods

exp(double a) //Returns e raised to the power of a.

log(double a) //Returns the natural logarithm of a.

log10(double a) //Returns the 10-based logarithm of a.

pow(double a, double b) //Returns a raised to the power of b.

sqrt(double a) //Returns the square root of a.

Examples:

Math.exp(1) returns 2.71

Math.log(2.71) returns 1.0

Math.pow(2, 3) returns 8.0

Math.pow(3, 2) returns 9.0

Math.pow(3.5, 2.5) returns 22.91765

Math.sqrt(4) returns 2.0

Math.sqrt(10.5) returns 3.24

• Rounding Methods

double ceil(double x) //x rounded up to its nearest integer. This integer is returned as a double value.

double floor(double x) // x is rounded down to its nearest integer. This integer is returned as a double value.

double rint(double x) //x is rounded to its nearest integer. If x is equally close to two integers, the even one is returned as a double.

int round(float x) // Return (int)Math.floor(x+0.5).

long round(double x) // Return (long)Math.floor(x+0.5).

Example

Math.ceil(2.1) returns 3.0

Math.ceil(2.0) returns 2.0

Math.ceil(-2.0) returns –2.0

Math.ceil(-2.1) returns -2.0

Math.floor(2.1) returns 2.0

Math.floor(2.0) returns 2.0

Math.floor(-2.0) returns –2.0

Math.floor(-2.1) returns -3.0

Math.rint(2.1) returns 2.0

Math.rint(2.0) returns 2.0

Math.rint(-2.0) returns –2.0

Math.rint(-2.1) returns -2.0

Math.rint(2.5) returns 2.0

Math.rint(-2.5) returns -2.0

Math.round(2.6f) returns 3

Math.round(2.0) returns 2

Math.round(-2.0f) returns -2

Math.round(-2.6) returns -3

• min, max, and abs

max(a, b)and min(a, b) // Returns the maximum or minimum of two parameters.

abs(a) //Returns the absolute value of the parameter.

random() //Returns a random double value in the range [0.0, 1.0).

Examples:

Math.max(2, 3) returns 3

Math.max(2.5, 3) returns 3.0

Math.min(2.5, 3.6) returns 2.5

Math.abs(-2) returns 2

Math.abs(-2.1) returns 2.1

More on Problem Decomposition for Structured programming (Top-Down, Bottom-Up)

Stepwise Refinement

The concept of method abstraction can be applied to the process of developing programs. When writing a large program, you can use the “divide and conquer” strategy, also known as stepwise refinement, to decompose it into subproblems. The subproblems can be further decomposed into smaller, more manageable problems.

Example: printCalendar application

• Top-down approach is to implement one method in the structure chart at a time from the top to the bottom. Stubs can be used for the methods waiting to be implemented. A stub is a simple but incomplete version of a method. The use of stubs enables you to test invoking the method from a caller. Implement the main method first and then use a stub for the printMonth method. For example, let printMonth display the year and the month in the stub. Thus, your program may begin like this:

// PrintCalendar.java: Print a calendar for a given month in a year

import javax.swing.*;

public class PrintCalendarSkeleton {

/** Main method */

public static void main(String[] args) {

// Prompt the user to enter year

String yearString = JOptionPane.showInputDialog(

"Enter full year (e.g., 2001):");

// Convert string into integer

int year = Integer.parseInt(yearString);

// Prompt the user to enter month

String monthString = JOptionPane.showInputDialog(

"Enter month in number between 1 and 12:");

// Convert string into integer

int month = Integer.parseInt(monthString);

// Print calendar for the month of the year

printMonth(year, month);

}

/** A stub for printMonth may look like this */

public static void printMonth(int year, int month) {

System.out.print(month + " " + year);

}

/** A stub for printMonthTitle may look like this */

public static void printMonthTitle(int year, int month) {

}

/** A stub for getMonthName may look like this */

public static String getMonthName(int month) {

return "January"; // a dummy value

}

/** A stub for getMonthNmae may look like this */

public static int getStartDay(int year, int month) {

return 1; // a dummy value

}

/** A stub for getNumberOfDaysInMonth may look like this */

public static int getNumberOfDaysInMonth(int year, int month) {

return 31; // a dummy value

}

/** A stub for getTotalNumberOfDays may look like this */

public static int getTotalNumberOfDays(int year, int month) {

return 10000; // a dummy value

}

/** A stub for getTotalNumberOfDays may look like this */

public static boolean isLeapYear(int year) {

return true; // a dummy value

}

}

• Bottom-up approach is to implement one method in the structure chart at a time from the bottom to the top. For each method implemented, write a test program to test it. Both top-down and bottom-up methods are fine. Both approaches implement the methods incrementally and help to isolate programming errors and makes debugging easy. Sometimes, they can be used together.

More on Packages

• There are three reasons for using packages:

1. To avoid naming conflicts. When you develop reusable classes to be shared by other programmers, naming conflicts often occur. To prevent this, put your classes into packages so that they can be referenced through package names.

2. To distribute software conveniently. Packages group related classes so that they can be easily distributed.

3. To protect classes. Packages provide protection so that the protected members of the classes are accessible to the classes in the same package, but not to the external classes.

• Packages are hierarchical, and you can have packages within packages. For example, java.lang.Math indicates that Math is a class in the package lang and that lang is a package in the package java. Levels of nesting can be used to ensure the uniqueness of package names.

• Choosing a unique name is important because your package may be used on the Internet by other programs. Java designers recommend that you use your Internet domain name in reverse order as a package prefix. Since Internet domain names are unique, this prevents naming conflicts. Suppose you want to create a package named mypackage on a host machine with the Internet domain name . To follow the naming convention, you would name the entire package com.prenhall.mypackage. By convention, package names are all in lowercase.

Package Directory

• Java expects one-to-one mapping of the package name and the file system directory structure. For the package named com.prenhall.mypackage, you must create a directory, as shown in the figure. In other words, a package is actually a directory that contains the bytecode of the classes.

com.prenhall.mypackage

• The com directory does not have to be the root directory. In order for Java to know where your package is in the file system, you must modify the environment variable classpath so that it points to the directory in which your package resides.

Setting classpath Environment

The com directory does not have to be the root directory. In order for Java to know where your package is in the file system, you must modify the environment variable classpath so that it points to the directory in which your package resides.

Suppose the com directory is under c:\book. The following line adds c:\book into the classpath:

classpath=.;c:\book;

The period (.) indicating the current directory is always in classpath. The directory c:\book is in classpath so that you can use the package com.prenhall.mypackage in the program.

package com.prenhall.mypackage;

class Format { // without public qualifier

public static double format(

double number, int numOfDecimalDigits) {

return Math.round(number * Math.pow(10, numOfDecimalDigits)) /

Math.pow(10, numOfDecimalDigits);

}

}

package programs;

import com.prenhall.mypackage.Format;

public class TestFormatClass {

/** Main method */

public static void main(String[] args) {

System.out.println(Format.format(10.3422345, 2));

System.out.println(Format.format(-0.343434, 3));

}

}

-----------------------

[pic]

[pic]

[pic]

[pic]

[pic]

[pic]

[pic]

[pic]

[pic]

[pic]

[pic]

[pic]

[pic]

[pic]

[pic]

[pic]

[pic]

[pic]

[pic]

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download