- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Computer Programming Tutorials
ECLIPSE IDE JAVA Tutorial – Creating Relationships Between Classes
Unfinished Business from The Last Tutorial
Although it is unrelated to the outcome of the last tutorial there is one item which should be mentioned. We have seen several occasions where ECLIPSE has generated code. In the code the comment:
// TODO Auto-generated constructor stub
being included. As a comment it does not seem very important but it does have a distinct use. The word TODO triggers ECLIPSE to create a reminder that more work needs to be done at the point in the code following it. ECLIPSE used the TODO to generate an uncompleted task. To see the tasks go to Window > Show View > Tasks.
Developers can create reminders of tasks for themselves. In a later tutorial task administration details will be covered.
The Purpose of the TODO Comment
The Task View
Prequel to the Tutorial
This tutorial relies on the work completed in the tutorials
- "ECLIPSE IDE - "Test First Methods"- An Introduction to Test Driven Design (TDD)"
-
"ECLIPSE IDE - Tutorial Adding a Class to the Package After Creating the Test".
Outline of Application Requirements
Chipmunk's Car Rental Agency has a number of cars which may be rented out to any of a number of client companies. To find out which company has which car we rely on the relationship between the BillingAddress class company name and the Car class model.
We continue to used the the CarRentalTest. Starting at the project we open up the project, go to the test folder and open up the CarRentalTest.
Starting out with a test, testGetCompanyName, which is intended to get the name of the company renting one of Chipmunk's cars, there are some methods to create.
The “test first” methodology requires the coding of two methods:
-
a method setBillingAddress, and
-
a method getBillingAddress
The setBillingAddress method and the getBillingAddress methods. ECLIPSE “quick fix” can accomplish creating these methods in the Car class.
Access Modifiers: What do they Mean?
Access modifiers:
public – any class can use the method
private – only this class has access
none – access is limited to methods in the package in which it is contained
A relationship has been created between the Car class and the BillingAddress class.
The Car class is said to depend upon the BillingAddress class, since the billingAddress is named as a type within the the Car class (as the return value of the getBillingAddress, billingAddress and the value ba2 passed to the setBillingAddress.
The relationship is said to be one-to-one since one car can be rented by only one company at a time.
Starting the Test Method - testGetCompanyName()
The test method beings with code we have seen before. We create two objects a Car object and a BillingAddress object as seen in the following code snippet.
Car and BillingAddress Object Creation
What Actions the Test Performs
The illustration shows the following actions we next performed:
- we need to create a method to set the name of the company which has rented the car and add that information to the car class
- get the information (company name) based on the name of the car rented
- finally, we have the assertEquals which tests the actual company name value against the expected company name.
We used "quick fix" to create these two method stubs in the Car class. Now we will open the Car class .
Essentially, the coding of the CarRentalTest is complete with the exception of a code enhancement which we will complete at the end of this tutorial.
The Completed Code for the Test
Finishing the Coding of the Methods in the Car Class
Opening the Car class file we begin coding the two methods, both are quite simple but illustrate several important points.
The setting of the company name in the care field performed by the setBillingAddress method is a s follows:
public void setBillingAddress(BillingAddress ba2) {
this.billingAddress = ba2;
Note that billingAddress is not defined as a field in the Car class. ECLIPSE "quick fix is used to create this field.
It is created as a private field, since it it only used in this class. Notice in the ECLIPSE generated field the class name begins with an uppercase letter while the field name begins with a lowercase letter:
private BillingAddress billingAddress;
This is way it is a good practice to always start classes with an uppercase while fields and methods should always start with a lower case letter.
Another point about the access modifers, since we want all of the defined fields to be accessible in this class and since they have been defined as private or public the aceess modifiers are not needed. They can be left off.
The getting of the Company name is just returning the value of this field, billing address:
public BillingAddress getBillingAddress() {
return billingAddress;
The Code for the Two Methods
Success!
After saving the file, we go to CarRentalTest and select Run> Run As > JUnit test. The test runs successfully.
JUnit Test Output
A Relationship Exists Between the Classes
In this example we have created a relationship between the Car class and the BillingAddress class.
Formally, one would say that the Car class is dependent upon the BillingAddress class as the BillingAddress is named as a type in the Car class, the companyName field.
The relationship is further said to be one-to-one since a car can only have one renting company at a time.
A Code Enhancement
Let's look at the testGetCompanyName Method.
We use two lines of code to arrive at the company name. This is unnecessary.
// get the name of the Company which has rented the car
BillingAddress testBillingAddress = c2.getBillingAddress();
String testCompanyName = testBillingAddress.getCompanyName();
The two preceding statements can be combined into one. But, one might say, this is a working program! I'm affreaid that if I change it somehow I wouldn't remember how to change it back if something goes wrong. I'd like to be able to compare it with this code if I need to debug my change.
Well, one could always copy and paste the code to be replaced to a temporary file, but ECLIPSE has a better method.
Simply highlight the code to be change ant use the control key (CNTL) with the slash character (/) and ECLIPSE will change the statements into comments.
This key combination can be toggled. Another entry of CNTL-/ would set the comments back to statements.
Next, le's look at the code change.
The Proposed Code Change
The field testBillingAddress is really unnecessary.
If you begin typing as one statement, once you type the dot at the end of the term getBillingAddress() method. ECLIPSE presents a suggestion panel which includes the method getCompanyName(). The resulting statement is:
String testCompanyName = c2.getBillingAddress().getCompanyName();
which can be interpreted as follows:
use the c2.getBillingAddress() method to get a BillinAddress object and then use the getCompanyName method to retrieve the name field.
ECLIPSE Suggestions for Statement Completion
Wrap Up and What's Next
In this tutorial we created a relationship between two classes and utilized "test first" methodology in our test creation.
New concepts were introduced: access method, class dependence.
The notion that the // TODO comment has implications for creating identifiable tasks was hinted on. There will be more about tasks in future tutorials.
An ECLIPSE trick, "CNTL-/ was introduced as a way to toggle code in an out of source.
What's UP Next. .We will learn how to run all of out tests in one test invocation.
The major part of the next tutorial will focus on lists, lists of objects where the number of objects is unknown in advance. In our car rental business we really can't determine what the maximum number of cars or customers might be. ECLIPSE provides a JAVA class to create lists and manipulate them.