Here are my answers: Do you agree with my answers ?
/* Point A */
The decimal variable is in scope, It is a local variable.
/* Point B */
Int decimal is a formal parameter
String hex is a local Variable
/* Point C */
Int hexValue is a local Variable
/* Point D */
Int hexValue is a local Variable
String hex is a local Variable
The decimal variable is in scope, It is a local variable.
[B]Now this is the question [/B]: Which variables are in the scope at each of the points marked by comments?. In other words if each of these comments were replaced by actual java statements, which variables could be use in the statement without a compilation error.
[B]Now Code : [/B]
import java.util.Scanner;
public class temp
{
public static void main(String[]args)
{
Scanner input= new Scanner(System.in);
System.out.println("Enter a decimal #");
int decimal = input.nextInt();
/*Point A */
System.out.println("The hex # for decimal" + decimal + " is " + decimalToHex(decimal));
}
public static String decimaltoH(int decimal)
{
String hex= "";
while (decimal !=0)
{
/*Point B */
int hexValue = decimal % 16;
hex = toHexChar(hexValue) + hex;
decimal = decimal /16;
/*Point C */
}
/*Point D */
return hex;
}
public static char toHChar(int hex value)
{
if (hexValue <= 9 && hexValue >=0)
{
return (char) (hexValue + '0');
}
else
{
return (char)(hexValue -10 + 'A')
}
}
}