Download Create a Java project and package

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Course: Information Systems
Exercise 1: Create a Java project and package
To get started with this tutorial, you need to do some initial set up. First, create a Java
project to hold all of the different elements you will be creating (i.e. packages, Java
classes, diagrams, etc.).
Create a Java project
Creating a Java project is very easy:
1.
2.
3.
4.
5.
6.
7.
Open Rational Software Architect.
To open the Java perspective, from the Window menu, select Open Perspective > Java.
Make sure all of your open projects are closed.
From the File menu, select New > Project.
Make sure the Show All Wizards box is checked.
Expand the Java folder.
Select Java Project.
Figure 1. The New Project window
1
Course: Information Systems
8. Fill in the Create a Java project wizard as follows:

Project Name: IBMPayroll

Location: Create new project in workspace.

Project layout: Use project folder as root for sources and class files.

The wizard should look like this:
Figure 2. The New Java Project window
9. Click Finish.
2
Course: Information Systems
Now, create a package to house your Java classes.
Create a package
1. In the Package Explorer view, right-click the newly created IBMPayroll project.
2. Select New > Package.
Figure 3. New Package
3. In the New Java Package window, name the package com.ibm.common.
4. Click Finish.
5. The Package Explorer should now look like this:
Figure 4. Package Explorer view
3
Course: Information Systems
Exercise 2: Create and edit a UML class diagram
Now on to the more exciting part of the tutorial. Let's move on to UML class diagrams.
This section of the tutorial covers the creation and population of a UML class diagram.
Create a Java class diagram
1. In the Package Explorer view, right-click the IBMPayroll project.
2. Select New > Class Diagram.
3. Fill in the Create Class Diagram window as follows:
 Enter or select IBMPayroll/com/ibm/common as the parent folder.
 Name the file IBMPayrollDiagram.
The window should look like this:
Figure 5. New Class Diagram window
4. Click Finish.
5. From the Palette
(right side of editor), click the Class button, then click
anywhere in the class diagram editor.
6. Fill in the New Java Class window as follows:
 Source Folder: IBMPayroll.
 Package: com.ibm.common.
 Name: Zipcode.
4
Course: Information Systems

Make sure the bottom three check boxes are checked.
The New Java Class window should look like this:
Figure 6. New Java Class window
7. Click Finish. The diagram editor should now show a UML visualization of a Java class. Note
that this is NOT a UML 2.0 model. It is a representation of the Java code using UML 2.0
notation.
Figure 7. Zipcode UML representation
5
Course: Information Systems
8. Create two additional classes in the com.ibm.common package: PhoneNumber and
Employee (repeat steps from 5.).
9. From the Palette view, click the Association button
10. Drag-and-drop from the Employee class to the Zipcode class.
11. Fill in the Create Association window as follows:

Name the association zipcode.

Leave the remaining options as default.

Click Finish.
Figure 8. Create Association window
12. Repeat the same steps for an association between Employee and PhoneNumber.
13. Name the association phoneNumber.
6
Course: Information Systems
14. Arrange the classes in the diagram editor to look like this:
Figure 9. Class diagram after adding three classes
15. Double-click any of the newly created Java classes in the diagram editor to look at its source
file.
16. Examine the automatically generated source files.
Edit a Java class diagram
In this section, the Zipcode and PhoneNumber classes are edited using Rational
Software Architect visual tools. Add a field to the PhoneNumber class and a field and
method to the Zipcode class.
1. In the Package Explorer view, double-click IBMPayrollDiagram.dnx in the IBMPayroll
project.
2. In the editor, right-click the PhoneNumber class.
3. Select Add Java > Field....
Figure 10. Add Java Field
4. Fill in the Create Java Field window as follows:
 Name: theNumber
7
Course: Information Systems


Type: java.lang.String
Visibility: private
The window should look like this:
Figure 11. Create Java Field window
5. Click Finish.
6. Repeat the same steps to add a field to the Zipcode class.
7. Fill in the Create Java Field window as follows:
 Name: theCode
 Type: java.lang.String
 Visibility: private
 Click Finish.
8. In the editor, right-click the Zipcode class.
8
Course: Information Systems
9. Select Add Java > Method....
Figure 12. Add Java method
10. Fill in the Create a Java Method window as follows:
 Name: isZipcode
 Type: boolean
 Visibility: public
 In the Parameters section, click Add.
 Name the Parameter code and the type java.lang.String.
Figure 13. Create parameter
11. Click OK.
12. The Create Java Method window should look like this:
Figure 14. Create Java Method window
9
Course: Information Systems
13. Click Finish. In the Package Explorer view you should see an error in the Zipcode class
now, that's OK.
14. Double-click the Zipcode class in the diagram editor to open the source file.
15. Replace the isZipcode() method with the following code:
10
Course: Information Systems
Listing 1. isZipcode()
public boolean isZipcode(String code) {
boolean isValid = true;
try {
int length = code.length();
if ((length != 5) && (length != 10)) {
isValid = false;
}
for (int i = 0; i < length; i++) {
char c = code.charAt(i);
if ((length == 10) && (i == 5) && (c != '-')) {
isValid = false;
} else if ((c < '0') || (c > '9')) {
isValid = false;
}
}
} catch (Exception e) {
System.out.println("Error in isZipcode()" + e);
} finally {
if (isValid == false)
return false;
}
return isValid;
}
}
16. Be sure to save all of your projects now (CTRL + SHIFT + S) or File > Save All. Notice the
error is now gone.
17. In the Package Explorer view, right-click the Employee class.
18. Select Source > Generate Getters and Setters...
Figure 15. Generate Getters and Setters
19. In the Generate Getters and Setters window, check phoneNumber and zipcode to
generate both getters and setters.
11
Course: Information Systems
Figure 16. Generate Getters and Setters window
20. Repeat the same steps for the Zipcode class and the PhoneNumber class.
21. Again, save all the new changes (CTRL + SHIFT + S) or File > Save All.
22. The diagram editor should now look like this:
Figure 17. Final class diagram
12
Course: Information Systems
Try by yourself
1. Add into Employee class the field firstName, type: java.lang.String, and with private visibility.
2. Generate getters and setters for firstName.
3. Add a following missing code into the source code:
package com.ibm.common;
import
import
import
import
com.ibm.common.Zipcode;
com.ibm.common.PhoneNumber;
java.util.Scanner;
java.lang.String;
public class Employee {
private Zipcode zipcode;
private PhoneNumber phoneNumber;
private String firstName;
public Employee() {
// TODO Auto-generated constructor stub
}
// Construct a second constructor
public Employee (String newFirstName) {
firstName = newFirstName;
}
public String getfirstName() {
return firstName;
}
//This method allows the contents of firstName
public void setfirstName (String newFirstName) {
firstName = newFirstName;
}
public Zipcode getZipcode() {
return zipcode;
}
public void setZipcode(Zipcode zipcode) {
this.zipcode = zipcode;
}
public PhoneNumber getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(PhoneNumber phoneNumber) {
this.phoneNumber = phoneNumber;
}
public static void main(String[] args) {
13
Course: Information Systems
// TODO Auto-generated method stub
String firstName = "";
Scanner input = new Scanner(System.in);
//create a new employee
Employee myEmployee = new Employee(firstName);
System.out.println ("Enter employee name: ");
firstName = input.next();
//invoke setName method
myEmployee.setfirstName(firstName);
System.out.println("The employee's name is " +
myEmployee.getfirstName());
}
}
4.
5.
6.
7.
8.
File > Save all
Window > Show View > Console
In the Package Explorer view, right-click the Employee.java
Run > Java Application.
In Console view, enter first name of the employee...
14