• Nem Talált Eredményt

ARRAY OF OBJECTS 6.7

In document Programming Languages (Pldal 173-182)

     

Figure 6.4: The state of the person object. Notice that object birthDate has been included in the person object

Class Person uses class Date to demonstrate of how object of other class can be declared in another class. Besides that, class Person itself also has two another object of the class String to represent the name and the primitive data type of int to represent the id. Member birthDate are Date objects that contains attributes day, month and year.

Class Person in Program 6.12 creates an object of Date, initializes and display the values of its attributes. The constructor of Person takes five arguments (NAME, ID, DAY, MONTH and YEAR) in which arguments DAY, MONTH and YEAR are passed to the Date constructor. Take note that that member object does not need to be initialized immediately with constructor arguments.

ARRAY OF OBJECTS 6.7

name= Brown Smith id= 675 birthDate

day = 12 month=11 year = 1985

Array elements are not limited to primitive data types such integer or float. An object also can be the elements in an array.

Consider the class Date in Program 6.11. Let say you need to declare 10 objects of class Date to represent birth date of 10 students. Thus, the program can be written like this:

Program 6.14:

class obj{

public static void main (String[] args){

Date date1 = new Date(12, 11, 1985);

Date date2 = new Date(20, 10, 1980);

Date date3 = new Date(30, 12, 1974);

: : :

Date date9 = new Date(11, 2, 1970);

Date date10 = new Date(22, 12, 1979);

} }

The above program also can be written using array which was discussed in Topic 4. Here the array could be created using the Date object as shown in the following program:

Program 6.15:

class obj{

public static void main (String[] args){

Date[ ] date = new Date[10]; //declare the array //remember that the index of array should start with 0 date[0] = new Date(12, 11, 1985);

date[1] = new Date(20, 10, 1980);

date[2] = new Date(30, 12, 1974);

:

: :

date[8] = new Date(11, 2, 1970);

date[9] = new Date(22, 12, 1979);

} }

Objects can be handled easily using loops such as for or while loops if array of objects is used in the program. The following program shows of how array of objects of class Date (Program 6.11) is used inside a for loop:

Program 6.16:

class obj{

public static void main (String[] args){

Date[ ] date = new Date[10]; //declare an array date[0] = new Date(12, 11, 1985);

date[1] = new Date(20, 10, 1980);

date[2] = new Date(30, 12, 1974);

date[3] = new Date(11, 2, 1970);

date[4] = new Date(22, 12, 1979);

System.out.println("Students birthday:");

for (int counter=0; counter <=4; counter++) { date[counter].display();

} //for } //main

}//class

The output of Program 6.16 is shown below:

 

The diagram below shows the array of object date after the array is created in Program 6.16:

Figure 6.5: The array of object date after the array is created in Program 6.16

EXERCISE 6.2

1. (a) Consider the class definition for Complex as shown below:

public class Complex {

private double real;

private double imaginary;

Complex() {

real = 0.0;

imaginary = 0.0;

}

Complex(double re, double im) {

real = re;

imaginary = im;

}

public void setEqual(Complex number) {

real = number.real;

imaginary = number.imaginary;

}

public void showdata() {

double c;

char sign = '+';

c = imaginary;

if (c < 0) {

sign = '-';

c = -c;

}

System.out.println("The complex number is "

+ real + ' ' + sign + ' ' + c + "i\n");

}

public static void main(String[] args) {

Complex a = new Complex();

Complex b = new Complex(4.2, 3.6);

a.showdata();

b.showdata();

a.setEqual(b);

a.showdata();

} }

(i) List all the attributes owned by object Complex.

(ii) List all the attributes that could be invoked by object Complex.

(iii) Is the technique method overloading is used in the above class definition? Explain your answer.

(iv) What is the output produced by the above class when executed?

2. (i) Develop a complete Java class that will store information about a reference book. The particulars about this class are given below:

Class Name: Book

Attributes: bookTitle (String type and private) writer (String type and private) noISBN ( integer type and private)

Constructor: The purpose of this method is to assign the name of a book, itsÊ writer and the ISBN number to the appropriate attributes in this class. This method will have 3 parameters that contain information for bookTitle, writer and noISBN.

Member Methods:

public void setEqual (Book oldbook)

This method has only one parameter type of Book object. The purpose of this method is to assign the value of the attributes in parameter to the appropriate attributes of this class.

Member Methods:

public void showTitle()

Display the bookÊs title (that is value stored in the attribute bookTitle).

public void showWriter()

Display the name of the bookÊs writer (that is value stored in attribute writer).

public void showISBN()

Display the ISBN number of a book (that is value stored in attribute noISBN).

(ii) Develop a Java Application that will do the following tasks:

- Create two objects of the class Book above. The first object will store data of a book with the following particulars upon object creation:

BookÊs Name : Pengaturcaraan Berorientasikan Objek Writer : Dr. Sufian Idris

ISBN : 23457

The second object will store data of a book with the following particulars upon object creation:

BookÊs Name : Pengaturcaraan Komputer Writer : Dr. Alice Wong

ISBN : 78709

- Then, you need to display the titles of this books using the created objects.

- Then, use the second object to assign all their attributes to the attributes of first object using the method setEqual() that exists in class Book.

- Display all the values of the attributes in the first and the second object.

3. Consider the following class:

class Hoo { int a;

static String b;

void method1(){

}

static void method2(){

} }

Which of the statements are correct:

System.out.println(h.a);

System.out.println(h.b);

h.method1();

h.method2();

System.out.println(Hoo.a);

System.out.println(Hoo.b);

Hoo.method1();

Hoo.method2();

4. Describe the role of the this keyword. What is wrong with the following code:

class G { int k;

public void setK (int k){

k=k;

} }

 In the previous topic you have learned on how to declare a class and create objects from a class.

 In object oriented programming, class and objects play an important roles.

 Manipulating objects and their elements effectively is another important area that should be emphasised by programmer.

 This will enable them to write effective and efficient code.

 Knowing the following concepts will help you to write effective object oriented programs in Java:

Access modifiers Static class members Using this keyword

Sending objects to a method

Writing classes that has object as its member Array of objects

Inheritance (to be covered in the next topic)

access modifier method

pass-by-reference public

private

receiver object static

this

 

INTRODUCTION

Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. An example of where this could be useful is with an employee records system. You could create a generic employee class with states and actions that are common to all employees. Then more specific classes could be defined for salaried, commissioned and hourly employees. The generic class is known as the parent (or superclass or base class) and the specific classes as children (or subclasses or derived classes). The concept of inheritance greatly enhances the ability to reuse code as well as making design a much simpler and cleaner process.

T T o o p p i i c c

7

Object

7 Oriented Programming

In document Programming Languages (Pldal 173-182)