74
1 ESYS410 Lab 3 Instruction A. ESYS410 Lab # 3 - Simple Java application using RDi B. Lab 3 of 7 C. Lab Overview--Scenario / Summary: Part A. The objective of this exercise is to provide you with an understanding of different Java perspectives inside Rational Developer for IBM I (RDi) and Java coding using the scrapbook features in RDi. You will first learn to use the RDi to create a simple Java application; you will also experiment with Java expressions in the RDi scrapbook. You will work with different variable types, such as int, float, and String, creating and looping through arrays, concatenating strings, as well as using the System.out.println()method to output to the console. What you should be able to do At the end of this exercise, you should be able to create a Java project inside IBM Rational Developer I (RDi); demonstrate familiarity with the three predefined perspective in RDi; import and run a simple Java application; use the scrapbook; evaluate expressions; create variables; create and loop through arrays; concatenate strings; and write to the console. Part B. Debugging is an activity that is performed numerous times in developing Java application. ESYS410 Lab 3

lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

1

ESYS410 Lab 3 Instruction

A. ESYS410 Lab # 3 - Simple Java application using RDi

B. Lab 3 of 7

C. Lab Overview--Scenario / Summary:

Part A.

The objective of this exercise is to provide you with an understanding of different Java perspectives inside Rational Developer for IBM I (RDi) and Java coding using the scrapbook features in RDi. You will first learn to use the RDi to create a simple Java application; you will also experiment with Java expressions in the RDi scrapbook. You will work with different variable types, such as int, float, and String, creating and looping through arrays, concatenating strings, as well as using the System.out.println()method to output to the console.

What you should be able to do

At the end of this exercise, you should be able to

create a Java project inside IBM Rational Developer I (RDi);

demonstrate familiarity with the three predefined perspective in RDi;

import and run a simple Java application;

use the scrapbook;

evaluate expressions;

create variables;

create and loop through arrays;

concatenate strings; and

write to the console.

Part B.

Debugging is an activity that is performed numerous times in developing Java application. RDi provides a debugger that helps you rapidly detect and diagnose errors in your program. Based on a client/server design, it can be used to debug programs running on a remote machine or locally on your workstation. In this exercise, you perform local debugging. All of the functions available for local debugging also work for remote debugging.

You have created a program that echoes to the console. To exit, the user is expected to type the word Quit. However, the program doesn't work properly; once started, the user is caught in an infinite loop. The debugger is invoked to help find the cause of the problem and correct the code.

ESYS410 Lab 3

Page 2: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

2

The program uses StringBuffers to hold the user input and to set up the condition for exiting. StringBuffer comparison is the problem highlighted in this exercise. Unlike the String class, which has its own equals() method that compares characters within the strings, StringBuffer uses the equals() method inherited from Object. This method compares object references and will always return false if two different instances of StringBuffer are compared. Any exit condition based on this comparison will therefore always return false.

After completing this exercise, you will understand how to use the basic features of the Java debugger in RDi. You will learn how to set breakpoints, view and modify variables, and step through code.

What you should be able to do

At the end of this exercise, you should be able to

run an application in Debug mode in RDi; set breakpoints, view, and modify state variables and expressions; and step through your code using the Debugger to quickly track down problems and bugs

Part C.

The objective of this exercise is to provide you with a basic understanding of how JDBC works in Java application.

D. Deliverables:

1. For Exercises 1 and 2, submit the source code file. Each Java source file (*.java) must include a corresponding class file (*.class) program as evidence of success.

2. In addition to the program source code files and byte code files, put all your program source code files and screenshots of your program output files into a Word document.

3. You must use a zipped folder to send your Weekly Assignment to the Dropbox. Do not send subfolders within your zipped folder. Place ALL of the .java and .class files for the week into the one zipped folder. 

4. For Exercise 3, put your completed source code into a Word document.

Part A: Exercise 1 20 pointsPart B: Exercise 2 30 pointsPart C: Exercise 3 10 points

E. Lab Steps:

Part A:

Exercise 1 instructions

Section 1 - Navigating and customizing RDiIn this section, you will learn how to navigate and customize the different perspectives within RDi.

__ 1. Access Toolwire computing environment.__ a. Open IBM Rational Developer for System i.

ESYS410 Lab 3

Page 3: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

3

__ b. The Workspace Launcher dialog appears. Type the name of your workspace. Click OK.

__ c. The workspace loads and displays the Welcome frame.

__ 2. Go to File->New->Project to open a new Java project.

ESYS410 Lab 3

Page 4: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

4

The new Project wizard is now opened. Select Java Project and click Next.

Type in the project name: ESYS410Project3. Take the default JRE and select “Create separate source and output folders” as the project layout.

ESYS410 Lab 3

Page 5: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

5

Click on Finish. When the Open Associated Perspective window pops up, click Yes, and the Java perspective will now open.

ESYS410 Lab 3

Page 6: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

6

__ 3. Open Java Project.__ a. Right-click ESYS410Project3 and select New->Class.

ESYS410 Lab 3

Page 7: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

7

__ b. Enter your project name, HelloWorld, and click Finish. Your Java perspective is now open and ready for coding.

ESYS410 Lab 3

Page 8: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

8

__ c. Enter the code to display the message “HelloWorld.”

ESYS410 Lab 3

Page 9: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

9

__ 4. Build and run Java Project.

Select Run->Run As->Java Application.

__d. You will now see the project output display in the Console view.

ESYS410 Lab 3

Page 10: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

10

Section 2-- Open the scrapbook__ 1.Using the new project HelloWorld, create a Java scrapbook page to experiment with code snippets before placing them in your Java class.

__ a. Right-click ESYS410Project3 to bring up its pop-up menu and select New -> Other. A New dialog appears.

__ b. In the list of wizards, expand the folder Java and then Java Run/Debug to choose the Scrapbook Page option.

ESYS410 Lab 3

Page 11: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

11

__ c. Click Next.__ d. In the New Scrapbook Page dialog, make sure the parent folder is ESYS410Project3

and enter file name MyScrap.

ESYS410 Lab 3

Page 12: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

12

__ e. Click Finish. The scrapbook page opens in the editor. The .jpage suffix is automatically added.

Section3: Use Display to evaluate expressions

From a Java scrapbook editor, you can select a code snippet, evaluate it, and display the result as a string.

__ 1.Open your newly created scrapbook page, MyScrap.jpage, in the editor (if it is not already open).

__ 2.Enter the following into the your page: 1 + 1.

__ 3.Highlight the line you have just entered, right-click, and then choose Display from the pop-up menu.

ESYS410 Lab 3

Page 13: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

13

__ 4.The text is the evaluated value of the expression entered. In this case, the expression is evaluated as (int)2.

__ 5. Repeat this process with the following lines. (Type the expression, highlight the line, right-click, and select Display one line at a time.)

1.2f + 2.1f1.2d + 2.1d1.2 + 2.1

The second line is evaluated as (float)3.3 because of the "f" trailing each number. The third line is evaluated as (double)3.3 because of the trailing "d."

Why is the fourth line evaluated as double rather than float?

Section 4: Use Inspect to evaluate expressions

From a Java scrapbook editor, you can also inspecting expressions, which shows the result of evaluating an expression in the Expressions view.

__ 1.Remove the expressions' evaluated values after each of the lines you have entered, leaving the expressions as you originally typed them.

ESYS410 Lab 3

Page 14: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

14

__ 2. Highlight each of the four lines one at a time, and for each, right-click and select Inspect from the pop-up menu.

__ 3. The results of each expression are displayed in a hover window.

__ 4. Press Ctrl-Shift-I to move the result to the Expressions view in the right part of your screen.

__ 5. Now, create an int variable and increment its value in three different ways.

__ a.Start by clearing the scrapbook (delete all of the lines you typed).

__ b.Still in the scrapbook, declare the variable named x, of type int, as follows:

int x = 0;

__ c.Next, enter the following lines, each one incrementing x by 1.

x = x + 1;

x++;

x += 1;

ESYS410 Lab 3

Page 15: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

15

__ d.Select all four lines, right-click, and select Inspect from the pop-up menu.

__ e.The hover window should now display 3 as the result, since each of the lines after the variable declaration increments the value of x by 1.

Section 5: Strings and concatenation

From a Java scrapbook editor, you can also show the object that results from evaluating a code snippet in the Expressions view. Here, you inspect the object that results from concatenating two strings.

__ 1. Clear the scrapbook page.

__ 2. Create a variable named myString of type String and initialize it to "Hello" as follows:

String myString = "Hello";

__ 3. To concatenate strings, you can use the "+" operator. On the next line, concatenate myString with the String "world" in the following way:

myString = myString + " world";

__ 4. Highlight the two lines, right-click and select Inspect to view the contents of the myString variable, which should now be the String "Hello world".

ESYS410 Lab 3

Page 16: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

16

__ 5. Press Ctrl-Shift-I to move the result to Expressions view.

__ 6. In the Expressions view, you see a "+" sign next to the words

String myString ...

__ 7. This is because myString is an object of type String, which means that it is composed of many fields. Click the "+" sign, and the Inspect window reveals the values of all of these fields.

__ 8. Note that the character array value contains the individual characters of the String "Hello world".

Section 6: System.out

ESYS410 Lab 3

Page 17: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

17

The java.lang package contains a System class, which has a PrintStream field named out. The out field represents the standard output stream, and can be used to output data, typically to the display.

__ 1. In the scrapbook, following the two lines from the previous step, enter the following line:

System.out.println(myString);

__ 2. Highlight the three lines together, right-click, and select Execute from the pop-up menu. This causes these three lines of code to be run as if they were a part of a compiled Java program.

__ 3. The results of running your code will show up in the Console view in the lower part of your screen.

Section 7: Place code into the Java class

Add the code you tested in the scrapbook to your HelloWorld Java class.

__ 1. Copy (Ctrl-C) the three lines of code in the scrapbook that initializes, concatenates, and prints myString.

__ 2. Click the HelloWorld.java editor tab. If you have closed the HelloWorld Java class, double-click it

ESYS410 Lab 3

Page 18: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

18

in the Package Explorer view to open it for editing.

__ 3. Paste the code that you copied from your scrapbook page into the HelloWorld.java class after the line

System.out.println("Hello World!");

__ 4. Your class should now look like the following diagram:

__ 5. Save your changes.

__ 6. Run the HelloWorld.java class by selecting Run -> Run As -> Java Application from the main menu.

__ 7. The output is displayed in the Console view.

Section 8: Create an array variable

Now you create an array of int values and output them to the console.

__ 1. In the Console view toolbar, click the Remove All Terminated Launches button to remove the output from HelloWorld.java.

__ 2. Return to the scrapbook page in the editor by clicking on the MyScrap.jpage tab.

__ 3. Clear the old contents in the scrapbook.

__ 4. Declare an array named numbers as follows:

int[] numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

This creates an array with 10 elements, filled with the numbers 0 through 9.

ESYS410 Lab 3

Page 19: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

19

__ 5. Next, loop through the array and output the entries one by one using the following code:

for (int i = 0; i < 10; i++) { System.out.println(numbers[i]); }

__ 6. Select all four lines together, right-click, and select Execute from the pop-up menu.

Section 9: Place code into the Java Class

Add the code you tested in the scrapbook to your HelloWorld Java class.

__ 1. Copy the four lines of code in your scrapbook that create an array of type int and loops through it for console output.

__ 2. Click the HelloWorld.java editor tab. If you have closed the HelloWorld Java class, double-click it in the Package Explorer view to open it for editing.

__ 3. Paste the code that you copied from your scrapbook page into the HelloWorld.java class after the line

System.out.println(myString);

__ 4. Your code should now look like follows.

__ 5. Run the HelloWorld.java class by clicking the Run button in the toolbar.

__ 6. The Console view should display the following output.

ESYS410 Lab 3

Page 20: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

20

Section 10: Java perspectives, views, and editor summary

At this point, you should know that within Application Developer, there are three predefined perspectives containing the views and editor that are most commonly used while developing Java SE applications:_ Java perspective_ Java Browsing perspective_ Java Type Hierarchy perspectiveThe following figure indicates all perspectives and views.

ESYS410 Lab 3

Page 21: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

21

Java perspectiveWe use the Java perspective to develop Java SE applications or utility Java projects (utility JAR files) containing code that is shared across multiple modules within an enterprise application. Views can be added by selecting Window →Show View.Package Explorer viewThe Package Explorer view displays all projects, packages, interfaces, classes, member variables, and member methods contained in the workspace. It allows us to easily navigate through the workspace.Hierarchy viewWe use the Hierarchy view to display the type hierarchy of a selected type. To view the hierarchy of a class type, select the class in the Package Explorer, press F4 or right-click the class, and select Open Type Hierarchy. Outline viewThe Outline view is very useful and is the recommended way to navigate through a type that is currently opened in the Java editor. It lists all elements, including package, import declarations, type, fields, and methods. The developer can sort and filter the elements that are displayed.Problems viewWhile editing resource files, various builders can automatically log problems, errors, or warnings in the Problems view. For example, when you save a Java source file that contains syntax errors, those will be logged as errors. When you double-click the icon for a problem, error, or warning, the editor for the associated resource

ESYS410 Lab 3

Page 22: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

22

automatically opens to therelevant line of code. Application Developer provides a quick fix for some problems. Declaration viewThis view displays the declaration and definition of the currently selected type or element. It is most useful to see the source code of a referenced type within your code. For example, if you reference a customer within your code and you want to see the implementation of the Customer class, just select the referenced type Customer, and the source code of the Customer class is displayed in this view.Console viewThe console is the view in which the Application Developer writes all outputs of a process and allows you to provide keyboard inputs to the Java application while running it. Uncaught exceptions are also displayed in the console.Call Hierarchy viewThe Call Hierarchy view displays all callers and callees of a selected method. To view the call hierarchy of a method, select it in the Package Explorer or in the source code, press Ctrl+Alt+H or right-click, and select Open Call Hierarchy.

End of exercise

Part B:

Exercise 2 InstructionThe overall purpose of this exercise is to have you learn how to use the debugging features in RDi to debug Java applications. In the first part of the exercise, you create a new Java project called Debug. You also create a new Java class called ExitCondition. Next, in Part 3, you implement the main() method of the ExitCondition class and run the class. While testing this class, you discover that it does not work as expected. In Parts 4, 5, and 6, you run the application in debug mode and step through the code to determine the problem; you also make use of the Variables view. In Part 7, you use breakpoints and step through code once again to further isolate the problem; once the problem is discovered, you then fix it.

Part 1: Opening a new workspace

__ 1.Start the Java Developer in RDi with a new workspace called Debug to hold the contents of this lab.

__ 2.When the new workspace of RDi opens, click the X at the top left corner of the Welcome page to close it.

Part 2: Create a new Java project, package, and class

You first create a Java project, package, and class for the application that you will debug.

ESYS410 Lab 3

Page 23: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

23

__ 1.Create a new Java project.

__ a. Switch to a Java perspective by selecting Window > Open Perspective > Other > Java Project from the main menu.

__ b. From the main menu, select File > New > Project.__ c. In the New Project dialog, select Java Project.

__ d. Click Next.__ e. In the New Java Project dialog that appears, enter Debug for the Project name.

ESYS410 Lab 3

Page 24: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

24

__ f. Click Finish.

__ 2.Create a new package inside the project.

__ a. In the Package Explorer view of the Java perspective, select the newly created Debug Java project.

__ b. Bring up the pop-up menu by right-clicking the selection. Select New > Package.

ESYS410 Lab 3

Page 25: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

25

__ c. The New Java Package dialog appear. Ensure the Source Folder is set to Debug.

__ d. Type edu.devry.debug in the Name field.

__ e. Click Finish. A new Java package and dependent JAR files appear in the Package Explorer view.

__ 3.Create a new class in the package.

__ a. In the Package Explorer view, expand the Debug project.

__ b. Right-click the edu.devry.debug package to bring up the pop-up menu. Select New > Class.

ESYS410 Lab 3

Page 26: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

26

__ c. In the New Java Class dialog, ensure that the Source Folder is Debug and the Package is edu.devry.debug.

__ d. Enter ExitCondition in the Name field.

__ e. Select the public static void main (String[] args) check box to generate a main method stub.

__ 4.Click Finish. You will get the following screen.

ESYS410 Lab 3

Page 27: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

27

Part 3: Implementing and testing the ExitCondition class

The ExitCondition class with a main method stub now appears in a Java editor. You implement this class to respond to user input from the system. You also test the application to ensure that it performs as expected.

__ 1. Implement the main method.

__ a. Add the following import statement to the class:

import java.io.*;

ESYS410 Lab 3

Page 28: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

28

__ b.Update the main method according to the code below. Remember to update the method signature to throw an IOException exception.

__ c. Save ExitCondition.java (Ctrl-S). If any errors appear in the Problems view, fix them before continuing.

__ 2.Test the ExitCondition class.

__ a. In the Package Explorer view, expand Debug > edu.devry.debug.

__ b. Select ExitCondition.java by left-clicking on it.

ESYS410 Lab 3

Page 29: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

29

__ c. From the main toolbar, click the down arrow to the right of the Run Icon and select Run…. The launch configuration wizard will now appear.

__ d. On the left pane, select Java Application. Click the New launch configuration button on the toolbar at the top of the selection pane to create the new configuration.

__ e. A new entry under Java Application opens.

ESYS410 Lab 3

Page 30: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

30

Note: The wizard automatically creates a new configuration called ExitCondition to run the Java class of the same name. This dialog allows you to specify input parameters and customize runtime class path settings.

__ f. Click Run to execute the application.

__ g. If the Console view is not open, you can open it by selecting Window > Show View > Console from the main menu.

__ h. The Console view should display a prompt from the ExitCondition application.

ESYS410 Lab 3

Page 31: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

31

Make sure the Console view is selected by clicking on its title bar.Type in line 1 and press Enter. Notice that this application repeats your input in the console.

Type in Quit and press Enter.

__ i. Typing Quit into the console did not terminate the application as expected. In the next part, you use the debugging tools to determine why the program did not respond to the word Quit.

__ 3. You need to force the program to terminate, as the Quit command does not work. Click the Terminate icon on the Console view toolbar.

Part 4: Working with the Debugger

To discover the cause of the problem, you can read over the code inside the workbench editor. A more efficient way is to step through the logic of the code, examining the application as it is being executed. The Debug perspective provides a variety of tools to check an application in real time.

__ 1. Start the application in Debug mode.

__ a. In the Package Explorer view, expand Debug > edu.devry.debug.

ESYS410 Lab 3

Page 32: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

32

__ b. Select ExitCondition.java by left-clicking on it.

__ c. Click the Debug icon from the main toolbar. Note that you want to click the icon itself, not the down arrow to the right.

__ d. Switch to the Debug perspective by selecting Window > Open Perspective > Debug.

Note: Before you leave the Java perspective, ensure that ExitCondition.java is open in the Java editor view.

The Debug perspective appears, displaying information on the running application. Notice three of the views in the figures below:

The Debug view displays all threads executing in the runtime environment. At this point, only the ExitCondition application is running.

The Java Editor displays the code of the currently selected thread. You can set breakpoints and step through code using this editor.

ESYS410 Lab 3

Page 33: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

33

The Console view shows any output from the application itself. You can also type in input to the system using this view.

__ 2. Pause execution of the ExitCondition application.

__ a. In the Debug view, select the thread labeled Thread [main].__ b. Click the Suspend button from the Debug title bar.

ESYS410 Lab 3

Page 34: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

34

The Debugger suspends the main thread and attempts to open an editor on the class containing the last method executed. In your case, you were waiting on user input, and the last method invoked was readBytes() in the FileInputStream class. Confirm this fact by examining the method stack trace.

__ 3. Examine the topmost element in the method stack trace.

__ a. In the Debug view, if the suspended main thread is not already expanded, expand it by clicking the plus sign to the left of Thread [main].

__ b. Select the topmost element below Thread [main] in the method stack trace, FileInputStream.readBytes(byte[], int, int, FileDescriptor). This is the last method invoked before the thread was suspended.

ESYS410 Lab 3

Page 35: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

35

__ 4. Examine the last element in the method stack trace, where the ExitCondition class was executing before it was suspended.

__ a. In the Debug view, select the bottommost element below Thread [main] in the method stack trace, ExitCondition.main(String[]).

__ b. ExitCondition.java should be automatically loaded in the editor. The line with the BufferedReader object reading a line from Standard Input will be marked by an arrow in the left-edge ruler. This is expected, as the application is waiting for input in the Console view.

ESYS410 Lab 3

Page 36: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

36

__ 5. Use the Variables view to the right of the Debug view to check all variables in scope.

__ a. Make sure ExitCondition.main(String[]) is still selected in the Debug view.

__ b. Examine the first variable, args. Since no arguments were passed to the Java application, this String array has no elements to expand.

ESYS410 Lab 3

Page 37: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

37

__ c. Examine the remaining variables, br and exiting. For variables other than primitive types and String objects, object fields can be examined by clicking the + sign beside the variable.

__ d. Expand exiting > value to examine the contents of the value array. It is a field under the exiting variable. Fields comprised of objects can be further expanded by selecting the "+" sign. The exiting StringBuffer object contains the value of Quit.

__ 6. Another way to inspect an Object's string representation is to invoke its toString() method.

__ a. In the Variables view, select the value array.

__ b. Click the Menu button (downward triangle) on the Variables view toolbar, and make sure the Layout > Vertical View Orientation is selected.

ESYS410 Lab 3

Page 38: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

38

__ c. Locate the Detail pane that appears on the lower portion of the Variables view. It displays the result of calling the toString() method on the value array.

__ d. Confirm that this result is equivalent to the value field.

You can change the location of the Detail pane by selecting Layout > Vertical View Orientation or Horizontal View Orientation after clicking the Menu button. You can also toggle it off by selecting Variables View Only.

__ 7.While stepping through the lines of code in ExitCondition, you can examine the contents of the br BufferedReader object in real-time.

__ a. In the Variables view, expand the fields in the br BufferedReader object by clicking the + box.

__ b. Expand the cb field. This array of char data type holds the contents of the BufferedReader object.

ESYS410 Lab 3

Page 39: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

39

At the moment, there is no value to display. As you step through the code, you expect values to appear.

Part 5: Stepping through the code

Using the four stepping modes and other commands, you can execute Java applications one line of code at a time. This allows you to examine, adjust variable values, and monitor the program execution path. Stepping through code is an essential practice for checking logic errors in programs.

The Step Into button evaluates the current line of code one level deeper into the call stack. For example, with the code stopped in the ExitCondition class at the BufferedReader's readLine() method, selecting Step Into reveals the code being executed in the BufferedReader class, if available. A new editor appears with the source of the invoked class, and execution is suspended at the first statement. The F5 function key is equivalent to the Step Into button.

The Step Over button executes until the next line of code in the same level of the call stack. This is a useful function if you are not concerned with the methods invoked by the current line of code. The F6 function key is equivalent to the Step Over button.

The Step Return button continues execution until control is returned to the next level higher in the call stack. For example, while stepping over lines of code inside the readLine() method in BufferedReader, selecting Step Return continues execution until control is returned to the ExitCondition class. Issuing a Step Return command in the main method will run the program until completion. The F7 function key is equivalent to the Step Return button.

ESYS410 Lab 3

Page 40: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

40

The Use Step Filters/Step Debug button allows stepping through code that contains debug information (that is, the source code) while skipping code that does not. The currently selected line executes and execution continues until reaching the next line with debug information. Use the button in the Debug view toolbar or press Shift + F5.

The Run to Line command allows execution to continue until reaching the currently highlighted line in the editor. This command is a useful alternative to continuously issuing the Step Over command or inserting a breakpoint to the code. It can be accessed by selecting Run > Run to Line from the workbench menu or by using the button's keyboard equivalent, Ctrl + R.

__ 1. Resume program execution suspended at the line waiting for input from the user.

__ a. Confirm that the program is suspended at the following line in ExitCondition:

s = new StringBuffer(br.readLine());

__ b. Click the Step Over button once. This resumes operation with the application waiting for input in the console.

ESYS410 Lab 3

Page 41: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

41

__ c. Select the Console view. Click the line below the Enter any string... prompt.

__ d. Type Nana Liu into the Console view. Press Enter.

ESYS410 Lab 3

Page 42: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

42

__ 2. Examine the variables currently in scope.

__ a. In the Variables view, expand the br element.

__ b. Expand the cb field of the char array. Examine the first 100 elements of the array by expanding the [0..99] element.

__ c. Notice that the br BufferedReader object contains the text that you have typed into the Console view.

__ 3.Examine the code evaluation using the Step Into command.

__ a. Click the Step Into button or press F5.

__ b. Look at the Thread [main] thread in the Debug view. One more level of execution has been added to the method call stack. You are now examining the StringBuffer class.

ESYS410 Lab 3

Page 43: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

43

__ c. Notice that the Variables view now contains the this StringBuffer object. As you move one level lower in the call stack, the variable scope changes.

ESYS410 Lab 3

Page 44: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

44

__ d. Click the Step Over button to position the current line at the count = string.length() line.

__ 4. Examine the code after another Step Into command.

__ a. Click the Step Into button again to evaluate the string.length() method call at the current line.

__ b. Examine the Variables view. The this object now refers to the string object from the string.length() method call.

__ 5.Click the Step Return or press the F7 button twice to return back to the ExitCondition application level of scope. The Debug and Variables views have been updated to the corresponding scope.

ESYS410 Lab 3

Page 45: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

45

__ 6. Examine the variables for comparing the user input to the exiting StringBuffer object.

__ a. Click the Step Over button to reach the if (s.equals(exiting)) { line.

__ b. Examine the new variable s in the Variables view, created in the previous statement.

__ c. Confirm the value of the variable s is Nana Liu. You can use the Detail Pane to quickly check the String value assigned to each object.

ESYS410 Lab 3

Page 46: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

46

__ d. Check the value for exiting. It is Quit, as assigned by your code.

ESYS410 Lab 3

Page 47: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

47

__ 8. Based on these values, you expect the equals() condition to return false. Verify this claim.

__ a. Highlight the expression s.equals(exiting) in the editor.

__ b. Bring up the pop-up menu by right-clicking the selection. Select Inspect.

ESYS410 Lab 3

Page 48: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

48

__ c. A hover window opens and displays the result as false.

ESYS410 Lab 3

Page 49: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

49

__ d. Press Ctrl-Shift-I to move the result to the Expression view.

__ e. Highlight the same expression in the editor again.

__ f. Right-click the expression and select Display from the pop-up menu.

__ g. This time, the same result is displayed as (boolean) false in a hover window.

ESYS410 Lab 3

Page 50: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

50

__ h. Press Ctrl-Shift-D to move the result to the Display view.

__ 9.You can now complete one iteration of the while loop, expecting to see the input echoed on the Console view.

__ a. In the editor, highlight the line s = new StringBuffer(br.readLine());.__ b. Right-click to bring up the pop-up menu and select Run to Line.

__ c. The Console view should now display an echo of your input.

ESYS410 Lab 3

Page 51: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

51

__ d. Click Step Over once so that the program awaits additional input.

Part 6: Debugging the exit condition

You now focus on the s.equals(exiting) statement in the ExitCondition class using the debugger. Specifically, you will use the debugger to determine why the expression never evaluates to true.

__ 1. Determine why the Quit command does not terminate the application.

__ a. Click the Console view below the Enter any string prompt.

__ b. Type Quit and press Enter.

__ c. In the Variables view, check the values for the variables s and exiting. They should both have the value of Quit.

ESYS410 Lab 3

Page 52: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

52

ESYS410 Lab 3

Page 53: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

53

__ 2. Since both variables have the same string value, you expect s.equals(exiting) to evaluate to true. Use the debugging tools to test this claim.

__ a. Select the expression s.equals(exiting) in the editor.

__ b. Bring up the pop-up menu by right-clicking the selection. Select either Display or Inspect to evaluate the expression.

__ c. Note that the expression evaluates to false.

__ d. Click the Step Over button and notice that the application now executes the else clause. This confirms that the s.equals(exiting) expression evaluates to false.

__ 3. Even though s and exiting appear to be equal, they are not being evaluated as equal. Why does the equals() method not find the string contents of the two objects to be the same? Use Hover Help to verify the class that implements this method.

__ a. Return to and highlight the s.equals(exiting) four lines prior.

__ a. In the editor, position the cursor over the equals() method.

From the Hover Help message, you can see that the class implementing the equals() method is Object. Remember that the implementation of equals() in the Object class compares object references, not values. Since s and exiting represent two distinct StringBuffer objects, their object references will not have the same

ESYS410 Lab 3

Page 54: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

54

value.

Part 7: Fixing the code and setting a breakpoint

At this point, you realize that the StringBuffer class does not override the equals() method inherited from Object. You cannot use it to compare string values. Instead, you use the String class because it overrides the equals() method, which compares the values stored in two strings.

__ 1. Terminate the currently running instance of ExitCondition.

__ a. Terminate the application by selecting ExitCondition [Java Application] in the Debug view and clicking the Terminate button on the Debug title bar.

__ b. Click the Remove All Terminated Launches button to clear the Debug view.

ESYS410 Lab 3

Page 55: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

55

__ c. Close the Debug perspective by Click the X.

__ 2. Change the StringBuffer objects to String objects in the ExitCondition class.

__ a. Switch to the Java perspective (Window > Open Perspective > Java), if necessary.

__ b. Open the ExitCondition.java file in a Java Editor view, if not already opened. This can be accomplished by double-clicking the file in the Package Explorer view.

__ c. Position the cursor at the beginning of the file.

__ d. From the main menu, select Edit > Find/Replace….

__ e. In the Find/Replace dialog Type StringBuffer in the Find field.

__ f. Type String in the Replace With field.

__ g. Check the Case Sensitive option.

__ h. Click Replace All. Confirm that four instances have been replaced.

ESYS410 Lab 3

Page 56: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

56

__ i. Click Close.

__ 3. Save your changes (Ctrl-S).

__ 4. Set a breakpoint on the if (s.equals(exiting)) line.

__ a. In the Java Editor view containing ExitCondition.java, place the cursor on the line with if (s.equals(exiting)) {.

__ b. Select Run > Toggle Line Breakpoint from the main menu. The breakpoint is indicated by a small blue dot in the gray margin to the left of the line.

Note: You can also double-click in the margin to the left of the line where you want the breakpoint set.

ESYS410 Lab 3

Page 57: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

57

__ 5. You now test your updated ExitCondition application.

__ a. Click the Debug button on the main toolbar. Your program executes and pauses for user input.

__ b. Open the Debug perspective by selecting Window > Open Perspective > Debug from the main menu.

Note: Notice in the editor view that the breakpoint symbol in the left margin now has a check mark. This indicates that the breakpoint is enabled and has been verified by the Java virtual machine.

ESYS410 Lab 3

Page 58: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

58

__ c. In the Console view, type Nana Liu and press Enter. Execution is suspended at the line where the breakpoint has been set.

ESYS410 Lab 3

Page 59: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

59

__ d. Notice the value of variables s and exiting in the Variables view. They are now String objects and have different values.

ESYS410 Lab 3

Page 60: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

60

__ 6. To highlight the variable modification feature of the debugger, change the value of the s variable.

__ a. In the Variables view, select the variable s.

__ b. Bring up the pop-up menu by right-clicking the selection. Select Change Value.

__ c. In the Change Object Value dialogue, type James Bond in the text field and click OK. The value of s has now been changed.

__ d. Click the Resume button in the Debug view toolbar to continue execution.

ESYS410 Lab 3

Page 61: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

61

__ e. Observe the text in the Console view. Notice that the application reflects the new value for the s variable.

__ 7. Test the Quit command in the ExitCondition application.

__ a. Click the area below the input prompt in the Console view.

__ b. Type Quit and press Enter.__ c. The execution suspends at the breakpoint on the line with the expression

s.equals(exiting). Notice in the Variables view that both s and exiting now have the same value of Quit.

ESYS410 Lab 3

Page 62: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

62

__ d. In the Java Editor, select the s.equals(exiting) expression. Bring up the pop-up menu and select Inspect.

__ e. Examine the hover window. Check the statement evaluates to true.

__ f. Click the Step Over button.

__ g. Notice that the next statement highlighted in the editor view is in the then clause of the if statement. This confirms that the equality check worked.

ESYS410 Lab 3

Page 63: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

63

__ h. Click the Resume button in the Debug view to complete the execution of the program. The program terminates, and the Console view displays the expected Goodbye message.

__ 8. Close the Debug perspective by right-clicking the Debug icon in the perspective switch toolbar and selecting Close.

End of exercise

What you did in this exercise

In this exercise, you debugged an application running on your local machine. You learned how to launch your application in debug mode in order to diagnose and fix errors. You used the tools provided by the debugger to step through code, view and modify variables, and set breakpoints. You also experimented with the various views available on the Debug perspective.

Part C:

Exercise 3 Instruction

Write a Java application using an IBM DB2 database that provides a JComboBox and a JTextArea to allow the user to perform a query that is either selected from the JComboBox or defined in the JTextArea. Sample predefined queries are:a) Select all employees working in Department SALES.b) Select hourly employees working over 30 hours.c) Select all commission employees in descending order of the commission rate.

// Solution: DisplayQueryResults.javaimport java.sql.*;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;

public class DisplayQueryResults extends JFrame {private Connection connection;private Statement statement;private ResultSet resultSet;private ResultSetMetaData rsMetaData;private JTable table;private JComboBox inputQuery;private JButton submitQuery;private JTextField input;

ESYS410 Lab 3

Page 64: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

64

public DisplayQueryResults(){super( "Select Query. Click Submit to See Results." );

// The URL specifying the employees database to which this program // connects to using JDBCString url = "jdbc:db2j:employees";

// Fill out the code for loading the driver to allow connection to the database using a try-catch //statement________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

String queries[] = { "Select all employees working in Department " +"SALES.", "Select hourly employees working over 30 hours.","Select all comission employees in descending order of the " +"comission rate.", "Specify particular query" };

// If connected to database, set up GUIinputQuery = new JComboBox( queries );

submitQuery = new JButton( "Submit query" );submitQuery.addActionListener(

new ActionListener() {

public void actionPerformed( ActionEvent e ){getTable();}});

JPanel topPanel = new JPanel();input = new JTextField( 50 );input.addActionListener(

ESYS410 Lab 3

Page 65: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

65

new ActionListener() {

public void actionPerformed( ActionEvent e ){try {String query = input.getText();statement = connection.createStatement();resultSet = statement.executeQuery( query );displayResultSet( resultSet );}catch ( SQLException sqlex ) {sqlex.printStackTrace();}}});

JPanel centerPanel = new JPanel();centerPanel.setLayout( new FlowLayout() );centerPanel.add( new JLabel( "Enter query:" ) );centerPanel.add( input );topPanel.setLayout( new BorderLayout() );topPanel.add( inputQuery, BorderLayout.NORTH );topPanel.add( centerPanel, BorderLayout.CENTER );topPanel.add( submitQuery, BorderLayout.SOUTH );

table = new JTable( 4, 4 );

Container c = getContentPane();c.setLayout( new BorderLayout() );c.add( topPanel, BorderLayout.NORTH );c.add( table, BorderLayout.CENTER );

getTable();

setSize( 650, 200 );setVisible( true );

} // end constructor DisplayQueryResult

private void getTable(){try {int selection = inputQuery.getSelectedIndex();String query = null;

switch ( selection ) {case 0:query = "SELECT * FROM employees WHERE " +"departmentName = 'SALES'";break;case 1:query = "SELECT * FROM hourlyEmployees WHERE hours >= 30";break;case 2:

ESYS410 Lab 3

Page 66: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

66

query = "SELECT * FROM commissionEmployees ORDER BY " +"commissionRate DESC";break;case 3:query = input.getText();break;}

statement = connection.createStatement();

if ( query.substring( 0, 6 ).equals( "SELECT" ) ) {resultSet = statement.executeQuery( query );displayResultSet( resultSet );}

else statement.executeUpdate( query );

} // end try

catch ( SQLException sqlex ) {sqlex.printStackTrace();}

} // end method getTable

private void displayResultSet( ResultSet rs ) throws SQLException{// position to first recordboolean moreRecords = rs.next();

// If there are no records, display a messageif ( !moreRecords ) {JOptionPane.showMessageDialog( this,"ResultSet contained no records" );setTitle( "No records to display" );return;}

Vector columnHeads = new Vector();Vector rows = new Vector();

try {// get column headsResultSetMetaData rsmd = rs.getMetaData();

for ( int i = 1; i <= rsmd.getColumnCount(); ++i )columnHeads.addElement( rsmd.getColumnName( i ) );

// get row datado {rows.addElement( getNextRow( rs, rsmd ) );} while ( rs.next() );

// display table with ResultSet contentstable = new JTable( rows, columnHeads );JScrollPane scroller = new JScrollPane( table );

ESYS410 Lab 3

Page 67: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

67

Container c = getContentPane();c.remove( 1 );c.add( scroller, BorderLayout.CENTER );c.validate();

} // end try

catch ( SQLException sqlex ) {sqlex.printStackTrace();}

} // end method displayResultSet

private Vector getNextRow( ResultSet rs,ResultSetMetaData rsmd ) throws SQLException{Vector currentRow = new Vector();

for ( int i = 1; i <= rsmd.getColumnCount(); ++i )switch( rsmd.getColumnType( i ) ) {case Types.VARCHAR:case Types.LONGVARCHAR:currentRow.addElement( rs.getString( i ) );break;case Types.INTEGER:currentRow.addElement( new Long( rs.getLong( i ) ) );break;case Types.REAL:currentRow.addElement( new Float( rs.getDouble( i ) ) );break;case Types.DATE:currentRow.addElement( rs.getDate( i ) );break;default:System.out.println( "Type was: " +rsmd.getColumnTypeName( i ) );}

return currentRow;

} // end method getNextRow

public void shutDown(){try {connection.close();}catch ( SQLException sqlex ) {System.err.println( "Unable to disconnect" );sqlex.printStackTrace();}}

public static void main( String args[] ){final DisplayQueryResults app = new DisplayQueryResults();

ESYS410 Lab 3

Page 68: lms.courselearn.net  · Web view2017. 2. 17. · word . Quit. However, the program doesn't work properly; once started, the user is . caught in an infinite loop. The debugger is

68

app.addWindowListener(

new WindowAdapter() {

public void windowClosing( WindowEvent e ){app.shutDown();System.exit( 0 );}});}

} // end class DisplayQueryResults

END OF LAB

ESYS410 Lab 3