• No results found

– Creating the Bank Class

In document Java Programming Language, Java SE 6 (Page 95-102)

Expressions and Flow Control

Task 1 – Creating the Bank Class

In this task, you create the Bank class that must satisfy the UML diagram in Figure 5-1 on page L5-8. The class has the following characteristics:

Class Name: Bank Project: BankPrj

Location: Source Packages Package: com.mybank.domain

Task 2 – Deleting the Current TestBanking Class

In this task, you delete the current TestBanking class in the com.mybank.test source package of the BankPrj project.

Oracle University and GUIDANCE VIEW SDN BHD use only

Exercise 2 – Using Arrays to Represent One-to-Many Associations (Level 1)

Task 3 – Copying the TestBanking Class

In this task, you copy the TestBanking.java file from the

resources/05_arraysdirectory to the com.mybank.test source package of the BankPrj project.

Task 4 – Compiling the TestBanking Class

In this task, you compile the TestBanking class.

Task 5 – Running the TestBanking Program

In this task, you run the TestBanking program. The output should be similar to the following:

Customer [1] is Simms, Jane Customer [2] is Bryant, Owen Customer [3] is Soley, Tim Customer [4] is Soley, Maria

Oracle University and GUIDANCE VIEW SDN BHD use only

Exercise 2 – Using Arrays to Represent One-to-Many Associations (Level 2)

Exercise 2 – Using Arrays to Represent One-to-Many Associations (Level 2)

In this exercise, you use arrays to implement the association between a bank and its multiple customers.

This exercise contains the following sections:

“Task 1 – Creating the Bank Class”

“Task 2 – Deleting the Current TestBanking Class”

“Task 3 – Copying the TestBanking Class”

“Task 4 – Compiling the TestBanking Class”

“Task 5 – Running the TestBanking Program”

Preparation

There is no preparation for this exercise.

Tool Reference – Tool references used in this exercise:

Java Development: Java Classes: Creating Java Classes

Java Development: Other Files: Deleting Files

Java Development: Java Classes: Copying Existing Resources

Java Development: Java Classes: Modifying Java Classes: Compiling Java Classes

Java Development: Java Classes: Modifying Java Classes: Executing Java Programs

For this exercise, you work in the BankPrj project in the projects directory.

1 2 3

Demonstration – The demonstration for this exercise can be found in the demos/05_arrays/exercise2 directory.

Oracle University and GUIDANCE VIEW SDN BHD use only

Exercise 2 – Using Arrays to Represent One-to-Many Associations (Level 2)

Task 1 – Creating the Bank Class

Complete the following steps:

1. Create the Bank class with the following characteristics:

Class Name: Bank Project: BankPrj

Location: Source Packages Package: com.mybank.domain

2. Add two instance variables to the Bank class: customers (an array of Customerobjects) and numberOfCustomers (an integer that keeps track of the next customers array index).

3. Add a public constructor that initializes the customers array with some appropriate maximum size (at least bigger than five).

4. Add the addCustomer method. This method must construct a new Customerobject from the parameters (first name and last name) and place it on the customers array. It must also increment the

numberOfCustomersinstance variable.

5. Add the getNumOfCustomers accessor method, which returns the numberOfCustomersinstance variable.

6. Add the getCustomer method. This method returns the customer associated with the given index parameter.

Task 2 – Deleting the Current TestBanking Class

In this task, you delete the current TestBanking class in the com.mybank.testsource package of the BankPrj project.

Task 3 – Copying the TestBanking Class

In this task, you copy the TestBanking.java file from the

resources/05_arraysdirectory to the com.mybank.test source package of the BankPrj project.

Task 4 – Compiling the TestBanking Class

In this task, you compile the TestBanking class.

Oracle University and GUIDANCE VIEW SDN BHD use only

Exercise 2 – Using Arrays to Represent One-to-Many Associations (Level 2)

Task 5 – Running the TestBanking Program

In this task, you run the TestBanking program. The output should be similar to the following:

Customer [1] is Simms, Jane Customer [2] is Bryant, Owen Customer [3] is Soley, Tim Customer [4] is Soley, Maria

Oracle University and GUIDANCE VIEW SDN BHD use only

Exercise 2 – Using Arrays to Represent One-to-Many Associations (Level 3)

Exercise 2 – Using Arrays to Represent One-to-Many Associations (Level 3)

In this exercise, you use arrays to implement the association between a bank and its multiple customers.

This exercise contains the following sections:

“Task 1 – Creating the Bank Class”

“Task 2 – Deleting the Current TestBanking Class”

“Task 3 – Copying the TestBanking Class”

“Task 4 – Compiling the TestBanking Class”

“Task 5 – Running the TestBanking Program”

Preparation

There is no preparation for this exercise.

Tool Reference –Tool References used in this exercise:

Java Development: Java Classes: Creating Java Classes

Java Development: Other Files: Deleting Files

Java Development: Java Classes: Copying Existing Resources

Java Development: Java Classes: Modifying Java Classes: Compiling Java Classes

Java Development: Java Classes: Modifying Java Classes: Executing Java Programs

For this exercise, you work in the BankPrj project in the projects directory.

1 2 3

Demonstration – The demonstration for this exercise can be found in the demos/05_arrays/exercise2directory.

Oracle University and GUIDANCE VIEW SDN BHD use only

Exercise 2 – Using Arrays to Represent One-to-Many Associations (Level 3)

Task 1 – Creating the Bank Class

Complete the following steps:

1. Create the Bank class with the following characteristics:

Class Name: Bank Project: BankPrj

Location: Source Packages Package: com.mybank.domain package com.mybank.domain;

public class Bank { // insert code here }

2. Add two instance variables to the Bank class: customers (an array of Customerobjects) and numberOfCustomers (an integer that keeps track of the next customers array index).

public class Bank {

private Customer[] customers;

private int numberOfCustomers;

// insert methods here }

3. Add a public constructor that initializes the customers array with some appropriate maximum size (at least bigger than five).

public Bank() {

customers = new Customer[10];

numberOfCustomers = 0;

}

4. Add the addCustomer method. This method must construct a new Customerobject from the parameters (first name, last name) and place it on the customers array. It must also increment the numberOfCustomersinstance variable.

public void addCustomer(String f, String l) { int i = numberOfCustomers++;

customers[i] = new Customer(f, l);

}

5. Add the getNumOfCustomers accessor method, which returns the numberOfCustomersinstance variable.

public int getNumOfCustomers() { return numberOfCustomers;

}

Oracle University and GUIDANCE VIEW SDN BHD use only

Exercise 2 – Using Arrays to Represent One-to-Many Associations (Level 3)

6. Add the getCustomer method. This method returns the customer associated with the given index parameter.

public Customer getCustomer(int customer_index) { return customers[customer_index];

}

Task 2 – Deleting the Current TestBanking Class

In this task, you delete the current TestBanking class in the com.mybank.testsource package of the BankPrj project.

Task 3 – Copying the TestBanking Class

In this task, you copy the TestBanking.java file from the

resources/05_arraysdirectory to the com.mybank.test source package of the BankPrj project.

Task 4 – Compiling the TestBanking Class

In this task, you compile the TestBanking class.

Task 5 – Running the TestBanking Program

In this task, you run the TestBanking program. The output should be similar to the following:

Customer [1] is Simms, Jane Customer [2] is Bryant, Owen Customer [3] is Soley, Tim

In document Java Programming Language, Java SE 6 (Page 95-102)

Related documents