CS101 - Lab 2

Introduction to Java programming: loops

The goal for this lab is to get you confortable using loops. Start by reviewing the problems we did in class and read carefully the lecture notes. Then go ahead and start on the problems below.

You will most likely encounter many problems in this lab. Before you raise your hand for help, try to understand what happens with your program. If it is a compile problem, read the messages on the screen; the compiler tries to give you a hint of what went wrong; can you figure out what it's telling you?

If your program compiles, but when run it does not give you quite the desired output, then we say that it has bugs. Try to debug it. Try to understand what happens, what are the instructions that are being executed, in what order. To debug, people use lots of "print" instructions. Print out the values of the variables that are relevant; this will help you understand how values change and when the errors occurs.

Programming style: Pay attention to issues of programming style:

I will take points off if you do not follow the programming style guidelines above. Read carefully the handout on programming style handed out in class.

Work individually, and call me if you need help. You are encouraged to discuss ideas and techniques broadly with other class members, but not specifics. Discussions should be limited to questions that can be asked and answered without using any written medium (e.g. pencil and paper or email). You should at no point look at the screen of your colleagues.


  1. Blood cell histogram

    Write a program that takes as input an arbitrary number of red blood cell counts in the range 0 to 50 and does two things:

    1. computes and prints the average red blood cell count
    2. prints out a simple histogram of the red blood cell counts using the following categories:
      • healthy red blood cell: 35 to 50 (inclusive)
      • anemic red blood cell: 0 to less than 35

    You can assume that the user enters valid blood cell counts.

    Example: The program should look like this (or similar):

    Number of patients: 7
    Enter the blood cell counts: 
    24.5 
    36.8 
    25.66
    44.12 
    48.0 
    45.22 
    33.9 
    
    The average blood cell count was 36.8857
    
    The distribution of the red blood cell counts was: 
    
    healthy: ****
     anemic: ***
    
Use the following skeleton for your program: BloodCellCount.java

What to turn in:

Send me only the .java file (not the entire folder), as attachment.