Ask Java Expert


Home >> Java

Testing and Debugging Java Code

Introduction

Errors can be categorized into:

- Syntax errors
- Logical errors
- Runtime errors

A syntax error occurs when your program does not comply with the syntactical rules of the Java language. Usually, these errors are caused by misspelled words and missing punctuation, among others. Syntax errors are relatively easy to find and correct as the compiler points out the line where the syntax error has occurred. Runtime errors occur during the execution of a program when the program attempts to perform an invalid operation, like dividing by zero, causing the immediate termination of program's execution.

Logical errors are errors in your program due to the design of a faulty algorithm to solve the problem at hand or in the implementation of such an algorithm. When your application has a logical error, it might compile and run without the computer generating any error messages, but it will not work as expected. Logical errors are generally more difficult to locate and fix than syntax errors. The process of finding a logical or runtime error is called debugging. An easy way to find a logical error is by tracing the value of the variables in a program while running using System.out.println(...); statements or by tracing the execution of the program step by step using a debugger.

In this lab you will learn how to monitor the value of the variables in a Java program and how to use the Eclipse debugger to locate its logical errors.

Lab Objectives

By the end of the lab, you should be able to:

- use a debugger to identify and repair runtime and logical errors;
- set breakpoints and watch-points on statements and expressions;
- trace the execution of the program (using incremental execution).

The program should do the following:

I. Prompt the user for a valid temperature in degrees Fahrenheit.
II. If the user enters a valid temperature in degrees Fahrenheit (i.e. temperature greater than or equal to -459.67), then the program asks the user the temperature scale to perform the conversion and displays such a conversion.

The program TemperatureConverter.java has several logical errors that you need to identify and correct using System.out.println and the Eclipse debugger.

1. After you have downloaded the program TemperatureConverter.java to your computer, create a new Java Project called TemperatureConverter in Eclipse (using your default location) as shown below:

Create a new class called TemperatureConverter in the TemperatureConverter Java Project as shown below:

Using Notepad, open the file TemperatureConverter.java you have just downloaded on the desktop and copy all of its content into the class TemperatureConverter in your Eclipse project.

2. Add System.out.println statements as needed to help you understand and debug the program. After debugging, you should remove or comment out these println statements so they don't show up in the correct version of your program.

3. Compile and run the program.

4. Execute the program entering the value 56 at the prompt and enter 3 when prompted for the temperature scale.

o [Worksheet Item 1] What do you observe when you run the program?

5. One easy way to debug your program is to print to the console the values of the variables and other messages that will help you trace the execution of the program in order to find out the logical errors. However, this technique can be very time consuming if you are dealing with larger programs. Instead, a more convenient way to locate logical errors in a program is by tracing the execution of the program and monitoring the values of the variables using a debugger. For the rest of this lab exercise, you will learn the basics of the Eclipse debugger that will help you debug and fix logical errors in a Java program.

6. Click on the Open Perspective , and then the Debug Perspective in Eclipse:

Afterwards, Eclipse will open several windows to help you to trace the execution of this project.

7. Scroll through the Java source code and set break points where it is indicated by a comment (look for "Set a breakpoint here"). To set a breakpoint, click on the left margin (in blue-grey) on the line where you want to place the breakpoint. You will see a small dot in the margin to the left of the line.

8. After you have set the indicated breakpoints in the program, click on the tab Breakpoints.

o [Worksheet Item 2] Where (what line numbers) have you set the breakpoints?

9. Run the program in Debug mode by clicking the little bug in the toolbar , or Run/Debug in the menu bar. Click OK on the window Save and Launch.

10. Click on the Variables window tab, , it should be located in the top right window.

o [Worksheet Item 3] What is the value of the named constant MIN_FAHRENHEIT and the variable tempScaleStr displayed in the Variables window?

11. The debugger will stop on the first breakpoint you set. Observe that the debugger highlights the line of code that will execute next.


Now, click on the Step Over icon, , in the toolbar to execute the statement

fahrenheit = keyboard.nextDouble();

Click on the Console window, then enter 56 and hit enter. Afterwards, do the following:

a) Click on the Step Over icon, , several times to see what happens and answer the following questions in the Debugging worksheet:

o [Worksheet Item 4a] Does the first if statement display the error message correctly?
o [Worksheet Item 4b] What is the problem?
o [Worksheet Item 4c] How would you fix the program so it terminates when the user enters a Fahrenheit temperature less than the possible minimum temperature?

b) Terminate the current execution of the program by clicking on the

Terminate icon, , in the toolbar or to the right of the Console Window.

12. Modify the first if statement so it works correctly.

13. Run the program in Debug mode again by clicking the little bug in the toolbar, or Run/Debug in the menu bar. When you reach the first and second breakpoints, use

Step Over icon, , to be sure that the first if statement works properly. If you have modified this if statement correctly you will notice that the next statement to be executed is the prompt that asks the user to enter a temperature scale:

14. Click on the Resume button, , in the toolbar, enter 3 when asked about the temperature scale
(and hit Enter afterwards).

15. Now, click on Step Over, , in the toolbar.

o [Worksheet Item 5a] Is the condition of the second if statement correct?

o [Worksheet Item 5b] How will you change it?

16. Terminate the current execution of the program (click on the Terminate icon, , in the toolbar or to the right of the Console window). Then, do the following:

c) Remove the first breakpoint you set by clicking on the dot in the margin on the left. d) Remove the breakpoint you set on the first if statement.
e) Modify the condition of the second if statement properly.

f) Run the program again in Debug mode (click on the little bug in the toolbar), or click

Run/Debug in the menu bar. Then click on Step Over, , or the Resume button, , in the toolbar to trace the execution of your program until it reaches the statement:

convertedDegrees = fahrenheit - 32* 4/9 ;

17. We are going to monitor the value of the following two expressions:

1) fahrenheit - 32
2) fahrenheit - 32* 4/9

To do so, select the expression, then right click and click on Watch. A window called
Expressions will open up.

o [Worksheet Item 6] What is the value of the expression just selected in the Watch window?

18. Click on the Resume button, , in the toolbar.

o [Worksheet Item 7a] Does the program compute the correct temperature in Reaumur degrees? (Check if multiplying the 1st expression by 4/9 gives you the value of the 2nd expression.
o [Worksheet Item 7b] Does the program assign the correct value fortempScaleStr?
o [Worksheet Item 7c] How can you modify the program to compute the value properly?

19. Fix the body of the if statement that computes the Reaumur temperature properly.

20. There are other logical errors, use the debugger to fix them. Then, compile, run, and test the program until you are sure the program is error free and works correctly to convert any valid temperature in Fahrenheit to an equivalent temperature when the scale chosen by the user is valid.
Submission and Grading

After you have completed and thoroughly tested TemperatureConverter.java, upload and submit it to eLC. Always double check that your submission was successful on eLC!

The lab will be graded according to the following guidelines.

- A score between 0 and 100 will be assigned.
- If the source file(s) are not submitted before the specified deadline's late period ends (48 hours after the deadline) or if they do not compile, then a grade of 0 will be assigned.
If the required comment for all labs describing the program and the academic honesty statement is
not included at the top of the file, then 10 points will be deducted. .

- The program will be evaluated against several test cases to determine whether each error has been corrected. Case(s) taken from the examples below as well as other test cases will be used.

Examples

Enter the temperature in Fahrenheit: 56
Enter the temperature scales you want to convert to:
1. Kelvin
2. Rankine
3. Reaumur
4. Celsius
Enter a temperature scale: 1
56.0 degrees Fahrenheit is 286.48333333333335 degrees Kelvin.

***

Enter the temperature in Fahrenheit: 56
Enter the temperature scales you want to convert to:
1. Kelvin
2. Rankine
3. Reaumur
4. Celsius
Enter a temperature scale: 2
56.0 degrees Fahrenheit is 515.6700000000001 degrees Rankine.

***
Enter the temperature in Fahrenheit: 113
Enter the temperature scales you want to convert to:
1. Kelvin
2. Rankine
3. Reaumur
4. Celsius
Enter a temperature scale: 3
113.0 degrees Fahrenheit is 36.0 degrees Reaumur.

***
Enter the temperature in Fahrenheit: 56
Enter the temperature scales you want to convert to:
1. Kelvin
2. Rankine
3. Reaumur
4. Celsius
Enter a temperature scale: 4
56.0 degrees Fahrenheit is 13.333333333333334 degrees Celsius.
CSC 201 - Lab 04
Debugging Worksheet

Student Name:

1. What do you observe when you run the program?

2. Where (what line numbers) have you set the breakpoints?

3. What is the value of the named constant MIN_FAHRENHEIT and the variable tempScaleStr displayed in the Variables window?

MIN_FAHRENHEIT:

tempScaleStr:

4. Answer the following questions:

a) Does the first if statement display the error message correctly?
b) What is the problem?
c) How would you fix the program so it terminates when the user enters a Fahrenheit temperature less than the possible minimum temperature?

5. Answer the following questions:

a) Is the condition of the second if statement correct?
b) How will you change it?

6. What is the value of the expression you just selected in the Watch window?

7. Answer the following questions:

a) Does the program compute the correct temperature in Rankine degrees? In Reamur degrees?
b) Does the program assign the correct value for the variable tempScaleStr?
c) How can you modify the program to compute this equivalence properly?

Prompts the user to enter a temperature in Fahrenheit and a temperature scale the user wants to convert it to. If both the temperature in Fahrenheit and the temperature scale are valid, the program converts the temperature in Fahrenheit to the specified temperature scale and displays the converted value.

Attachment:- Question.rar

Java, Programming

  • Category:- Java
  • Reference No.:- M92362832
  • Price:- $50

Priced at Now at $50, Verified Solution

Have any Question?


Related Questions in Java

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As