CPS 4 Spring 2002 : Exam 1 Review Solutions


Binary Numbers

  1. Given that a single binary digit, or bit, can represent two distinct numbers, 1 or 0, and two bits can represent four distinct numbers, 00, 01, 10, or 11, how many different numbers can be represented by 10 bits?
    1. 256
    2. 512
    3. 1024
    4. 2048
       
  2. If one of those 10 bits is used to represent a number's sign, i.e., if the number is positive or negative, instead of another digit, then how many different numbers can be represented by 10 bits (9 bits and one sign bit)?
    1. 255
    2. 511
    3. 1023
    4. 2047
       
  3. Which of the following binary numbers correctly represents the decimal number 15?
    1. 11111
    2. 10001
    3. 1111
    4. 1101

Drawing

  1. Stoplight
    Write an applet that displays a picture of a stoplight. The stoplight should be centered within the applet and the size of each of its lights should be one-fourth that of the applet's standard size 400x400. The top circle should be red, the center circle should be yellow, and the bottom circle should be green. Finally, a white rectangle should surround the three circles exactly.
    public class Applet extends GP.Containers.Applet
    {
        public Applet ()
        {
            GP.Shapes.Rectangle box = new GP.Shapes.Rectangle();  // must be created first!
            GP.Shapes.Oval red = new GP.Shapes.Oval();
            GP.Shapes.Oval yellow = new GP.Shapes.Oval();
            GP.Shapes.Oval green = new GP.Shapes.Oval();
    
            GP.Attributes.Coordinate center = new GP.Attributes.Coordinate(200, 200);
            box.SetPosition(center);
            yellow.SetPosition(center);
            center = new GP.Attributes.Coordinate(200, 100);
            red.SetPosition(center);
            center = new GP.Attributes.Coordinate(200, 300);
            green.SetPosition(center);
    
            GP.Attributes.Dimension size = new GP.Attributes.Dimension(100, 300);
            box.SetSize(size);
            size = new GP.Attributes.Dimension(100, 100);
            red.SetSize(size);
            yellow.SetSize(size);
            green.SetSize(size);
    
            box.SetColor(new GP.Attributes.Colors.White());
            red.SetColor(new GP.Attributes.Colors.Red());
            yellow.SetColor(new GP.Attributes.Colors.Yellow());
            green.SetColor(new GP.Attributes.Colors.Green());
        }
    }
  2. Pie Chart
    Write an applet that displays a pie chart showing the percentages of students of each class taking this course. The pie chart should be centered within the applet and its size should be that of the applet's standard size 400x400. The pie should be divided according to the following percentages:
    • first years: 40 percent
    • sophomores: 10 percent
    • juniors: 10 percent
    • seniors: 40 percent

    The individual wedges should be colored accordingly:

    • first years: red
    • sophomores: blue
    • juniors: violet
    • seniors: green
public class Applet extends GP.Containers.Applet
{
    public Applet ()
    {
        GP.Shapes.Pie firstyears = new GP.Shapes.Pie();
        GP.Shapes.Pie sophomores = new GP.Shapes.Pie();
        GP.Shapes.Pie juniors = new GP.Shapes.Pie();
        GP.Shapes.Pie seniors = new GP.Shapes.Pie();

        GP.Attributes.Coordinate center = new GP.Attributes.Coordinate(200, 200);
        firstyears.SetPosition(center);
        sophomores.SetPosition(center);
        juniors.SetPosition(center);
        seniors.SetPosition(center);

        GP.Attributes.Dimension size = new GP.Attributes.Dimension(400, 400);
        firstyears.SetSize(size);
        sophomores.SetSize(size);
        juniors.SetSize(size);
        seniors.SetSize(size);

        firstyears.SetColor(new GP.Attributes.Colors.Red());
        sophomores.SetColor(new GP.Attributes.Colors.Blue());
        juniors.SetColor(new GP.Attributes.Colors.Violet());
        seniors.SetColor(new GP.Attributes.Colors.Green());
        // and now for the interesting part :)
        double start = 0;
        double end = 0.4 * 360;
        firstyears.SetStartAngle(new GP.Attributes.Angle(start));
        firstyears.SetEndAngle(new GP.Attributes.Angle(end));
        start = end;
        end = start + 0.1 * 360;
        sophomores.SetStartAngle(new GP.Attributes.Angle(start));
        sophomores.SetEndAngle(new GP.Attributes.Angle(end));
        start = end;
        end = start + 0.1 * 360;
        juniors.SetStartAngle(new GP.Attributes.Angle(start));
        juniors.SetEndAngle(new GP.Attributes.Angle(end));
        start = end;
        end = start + 0.4 * 359;
        seniors.SetStartAngle(new GP.Attributes.Angle(start));
        seniors.SetEndAngle(new GP.Attributes.Angle(end));
    }
}

Problem Solving

  1. Describe the procedure for saving your classwork and updating your course web page
     
    1. Save final version of classwork
    2. Transfer all necessary files from local machine to acpub using FTP program
    3. Transfer course web page from acpub to local machine for editing
    4. Edit course web page by inserting link onto bottom of page
    5. Save course web page
    6. Transfer course web page from local machine to acpub using FTP program
    7. Open course web page in browser and refresh
    8. Click on new link to verify it works!
       
  2. The United States Centers for Disease Control, CDC, determines obesity according to a body mass index, BMI, computed as follows:
    BMI = (weight in kilograms) / (height in meters)2

    An index of 27.8 or greater for men or 27.3 or greater for non-pregnant women is considered obese. Write java code that, given height and weight in metric units, determines the BMI.

    public Applet ()
    {
        double weightKG = 100.0;
        double heightM = 2.1;
        // enter Java code here to calculate BMI
        double BMI = weightKG / (heightM * heightM);
    }
     
  3. Now write your code such that the height and weight can be calculated given American units. Note that one meter is 39.37 inches, one inch is 2.54 centimeters, one kilogram is 2.2 pounds, and one pound is 454 grams.

public Applet ()
{
    double weightLB = 220.0;
    double heightF = 6.1;
    // enter Java code here to calculate BMI
    double weightKG = weightLB / 2.2;
    double heightM = (heightF * 12) / 39.37;
    double BMI = weightKG / (heightM * heightM);
}