Binary Converter (scope)
Look at the following program:
import java.io.*;
public class Binary
{
public static void main (String args [])
{
int anint = 0;
String continu = "y";
System.out.println ("Welcome!");
while (continu.compareTo ("y") == 0)
{
anint = IBIO.inputInt ("Enter an integer between 0 and 32: ");
System.out.println (anint + " in binary is: " + convert (anint));
continu = IBIO.inputString ("Would you like to do it again? (y/n) ");
}
System.out.println ("Good bye!");
} //main
public static int convert (int num)
{ //Pre: num < 0 && num > 32
//Post: converts the decimal number to binary
int temp = 0;
if (num - 16 >= 0)
{
num = num - 16;
temp = temp + 10000;
}
if (num - 8 >= 0)
{
num = num - 8;
temp = temp + 1000;
}
if (num - 4 >= 0)
{
num = num - 4;
temp = temp + 100;
}
if (num - 2 >= 0)
{
num = num - 2;
temp = temp + 10;
}
if (num - 1 >= 0)
{
num = num - 1;
temp = temp + 1;
}
return temp;
} //convert
} //Binary class
(a)
(b)
Are there any local variables? How can
you tell?
(c)
Are there any parameters? How can you
tell?
(d)
Draw a structure chart for the above
program.
(e)
What does this program do?