This exercise will help you practice loops, conditional statments (if and if/else), logic and
even a little math.
- Create a new project in Eclipse called "Lab6". Inside the project,
create a new class called "GiantBinary" -- include a stub for the main method, which
you will write later. Below are descriptions of methods you should put into this class.
- Write a method with the following prototype:
public static void drawZero(int size)
The method should print out
text that looks like a big zero. You should also print a blank line
at the end. The parameter specifies the size
of the zero. See the examples below:
- If the parameter is 9, it should print:
000000000
000000000
000000000
000 000
000 000
000 000
000000000
000000000
000000000
- If the parameter is 15, it should print:
000000000000000
000000000000000
000000000000000
000 000
000 000
000 000
000 000
000 000
000 000
000 000
000 000
000 000
000 000
000000000000000
000000000000000
000000000000000
Notice that the "brush stroke" (the thickness of the sides) is always 3, regardless of the size requested.
Don't forget to print a blank line at the end.
- Write a main method. For now, have your main method make a few calls to the
drawZero method passing various sizes to see if it works. If it doesn't work, fix it. In case you have
forgotten, the prototype for the main method looks like this:
public static void main(String[] args)
- Write a method with the following prototype:
public static void drawOne(int size)
The method should print out
text that looks like a big one. You should also print a blank line
at the end. The parameter specifies the size
of the one. Each line in the output should begin with enough spaces that
the one is centered half-way across for the size -- see the examples below:
- If the parameter is 9, it should print:
111
111
111
111
111
111
111
111
111
Above, each line begins with 3 spaces so that the one is centered in a 9 by 9 region.
- If the parameter is 15, it should print:
111
111
111
111
111
111
111
111
111
111
111
111
111
111
111
Above, each line begins with 6 spaces so that the one is centered in a 15 by 15 region. If the size
requested were 49, then each line should begin with 23 spaces.
Don't forget to print a blank line at the end.
- Modify your main method. Now try making a few calls to the drawOne method to make
sure it works.
- Write a method with the following prototype:
public static void drawBinary(int n, int size)
The first parameter, n, is expected to be an integer from 0 to 255.
This method will display n in binary
representation by making calls to the previous methods, as needed. The size of the digits is determined by
the second parameter, size. Note that you should not display leading
zeros. (The only exception to this is the number zero itself. If n is equal to 0, then you should display
a zero.)
- Suppose n is 5 and size is 9. Since the binary representation of 5 is "101", the output should be:
111
111
111
111
111
111
111
111
111
000000000
000000000
000000000
000 000
000 000
000 000
000000000
000000000
000000000
111
111
111
111
111
111
111
111
111
- Suppose n is 12 and size is 9. Since the binary representation of 12 is "1100", the output should be:
111
111
111
111
111
111
111
111
111
111
111
111
111
111
111
111
111
111
000000000
000000000
000000000
000 000
000 000
000 000
000000000
000000000
000000000
000000000
000000000
000000000
000 000
000 000
000 000
000000000
000000000
000000000
- Modify your main method. Try making some calls to the drawBinary method to see
if it works. Perhaps try:
drawBinary(5, 9); // should look like 101
drawBinary(5, 19); // also 101 but with much bigger digits
drawBinary(12, 9); // should look like 1100
drawBinary(255, 9); // should look like 11111111
drawBinary(0, 9); // should look like 0