ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

ECLIPSE IDE JAVA Tutorial – Creating Relationships Between Classes

Updated on August 9, 2014

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

This comment is part of ECLIPSE's way of reminding the developer that more work has to be done hers by triging a task.
This comment is part of ECLIPSE's way of reminding the developer that more work has to be done hers by triging a task.

The Task View

This is the task list. To see this at any time go to Window   Show View   Tasks
This is the task list. To see this at any time go to Window Show View Tasks

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

Two object are created: a Car object and a BillingAddress object.
Two object are created: a Car object and a BillingAddress object.

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

The two created method and the assertEquals completes the coding.
The two created method and the assertEquals completes the coding.

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

Code as entered in ECLIPSE.
Code as entered in ECLIPSE.

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

ECLIPSE "Knows"  the options for statement completion includes the BillingAddress methods.
ECLIPSE "Knows" the options for statement completion includes the BillingAddress methods.

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.

Rate this Tutorial for Clarity and Completeness, Thanks!

Cast your vote for This tutorial
working

This website uses cookies

As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://corp.maven.io/privacy-policy

Show Details
Necessary
HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
LoginThis is necessary to sign in to the HubPages Service.
Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
AkismetThis is used to detect comment spam. (Privacy Policy)
HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
Features
Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
MavenThis supports the Maven widget and search functionality. (Privacy Policy)
Marketing
Google AdSenseThis is an ad network. (Privacy Policy)
Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
Index ExchangeThis is an ad network. (Privacy Policy)
SovrnThis is an ad network. (Privacy Policy)
Facebook AdsThis is an ad network. (Privacy Policy)
Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
AppNexusThis is an ad network. (Privacy Policy)
OpenxThis is an ad network. (Privacy Policy)
Rubicon ProjectThis is an ad network. (Privacy Policy)
TripleLiftThis is an ad network. (Privacy Policy)
Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
Statistics
Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
ClickscoThis is a data management platform studying reader behavior (Privacy Policy)