Java Reference
In-Depth Information
(Note that in the preceding expression we added 0.5 to round the number to the nearest
integer.)
The revised program is:
import java.util.*;
//Line 1
public class LogicErrorCorrection
//Line 2
3
{
//Line 3
static Scanner console = new Scanner(System.in);
//Line 4
public static void main(String[] args)
//Line 5
{
//Line 4
int fahrenheit;
//Line 6
int celsius;
//Line 7
System.out.print("Enter temperature in "
+ "Fahrenheit: ");
//Line 8
fahrenheit = console.nextInt();
//Line 9
System.out.println();
//Line 10
celsius = ( int ) (5.0 / 9 * (fahrenheit - 32)
+ 0.5);
//Line 11
System.out.println(fahrenheit + " degree F = "
+ celsius + " degree C.");
//Line 12
}
//Line 13
}
//Line 14
Sample Run: In this sample run, the user input is shaded.
Enter temperature in Fahrenheit: 110
110 degree F = 43 degree C.
As we can see, using temporary println statements, we were able to find the problem.
After correcting the problem, the temporary println statements are removed.
The temperature conversion program contained logic errors not syntax errors. Using
println statements to print the values of expressions and/or variables to see the results of
calculation is an effective way to find and correct logic errors.
QUICK REVIEW
1 . A reference variable is a variable that stores the address of a memory space.
2 . In Java, all variables declared using a class are reference variables.
3 . A reference variable does not directly store data in its memory space. It
stores the address of the memory space where the actual data is stored.
4 . Class objects are instances of that class .
5 . Using the operator new to create a class object is called instantiating an
object of that class .
 
 
Search WWH ::




Custom Search