Lab #6 CMSC 131
Object-Oriented Programming I
Summer 2011

Giant Binary Numbers

 

 

Objective

This exercise will help you practice loops, conditional statments (if and if/else), logic and even a little math.

 

Instructions

  1. 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.

  2. 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:

    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.


  3. 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)


  4. 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:




  5. Modify your main method. Now try making a few calls to the drawOne method to make sure it works.


  6. 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.)


  7. 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

Web Accessibility