How would you fix the program

Assignment Help JAVA Programming
Reference no: EM131554113

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

Reference no: EM131554113

Questions Cloud

Create a class called employee : Create a class called Employee that includes three pieces if information as data members---a first name (type string), a last name
Determine the probability that the person is infected : A certain virus infects one in every 200 people. A test used to detect the virus in a person is positive 80% of the time when the person has the virus and 5%.
Three-letter acronyms are common in the business world : Three-letter acronyms are common in the business world. For example, in Java you use the IDE (Integrated Development Environment) in the JDK
Deducting amount of capital required to fund annuity due : What is the estimated profit your insurance company can expect to have at end of 35 years after deducting amount of capital required to fund your annuity due.
How would you fix the program : How would you fix the program so it terminates when the user enters a Fahrenheit temperature less than the possible minimum temperature?
Discuss four specific information systems : Identify and briefly discuss four specific information systems that are used to fight crime.
Which project has a higher IRR : A company is evaluating two different projects, both of which cost $25 million and last 6 years. Which project has a higher IRR?
Write a business plan for one of the two given ventures : Write a business plan for one of the two following ventures: Expanding a one-store operation to a two-store operation.
Multiple precision arithmetic : A singly-linked list can be used to store large integers one digit at a time. For example, the integer 1234 could be stored in a list by storing 1 in the first.

Reviews

Write a Review

JAVA Programming Questions & Answers

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd