COSC 122 Programming II
In this assignment you will write a Java program that displays a set of numeric data in a graphical manner.
- Teams
You are encouraged to work with a partner for this lab. You and your partner should begin thinking about the problems and begin writing the code before lab time.
- What to do
Write a Java program that reads in an arbitrary number of nonnegative integer values. The input is terminated by an integer value less than zero. Use a Scanner object to read in the values. After all the values have been entered, print a histogram of the number of values in the following ranges: 0-99, 100-199, 200-299, 300-399, 400-499, and 500+.
If the user enters the following values:
115 556 130 605 714 808 415 494 769 278 451 114 816 398 669 481 166 81 335 459 388 504 548 402 646 299 458 733 187 615 932 983 342 302 483 543 358 373 469 881 458 114 214 253 265 869 55 198 337 889 533 694 343 307 620 712 710 650 827 62 115 951 257 116 801 31 192 820 8 686 208 444 300 671 200 325 562 761 185 197 126 380 771 384 131 397 773 570 523 262 510 304 360 793 481 391 313 534 145 645 -1the resulting histogram would be
0- 99 5|****** 100-199 15|******************* 200-299 9|*********** 300-399 19|************************ 400-499 12|*************** 500+ 40|**************************************************Each line lists the data range, the actual number of values in that range, a vertical bar, and then the bar of asterisks representing that quantity.
The longest bar always should contain 50 asterisks, and the other bars should be scaled proportionally. The sample input
450 401 3 78 444 200 -1would display
0- 99 2|********************************* 100-199 0| 200-299 1|***************** 300-399 0| 400-499 3|************************************************** 500+ 0|and input consisting merely of
-1would produce
0- 99 0| 100-199 0| 200-299 0| 300-399 0| 400-499 0| 500+ 0|Be sure ties are handled properly:
1 1 100 100 -1would produce
0- 99 2|************************************************** 100-199 2|************************************************** 200-299 0| 300-399 0| 400-499 0| 500+ 0|The formatting of your output should be identical to example outputs above.
You are welcome to use methods in this assignment if you wish, but you are not required to do so.
- How to do it
The problem is not so difficult to solve if you break it down into smaller, simpler problems. Follow the steps below as you develop your histogram program.
- Read in the values and tally the ranges.
Write a Java program that reads in an arbitrary number of
nonnegative integer values.
The input is terminated by an integer value less than zero.
Use a Scanner object to read in the values.
After all the values have been entered,
print the number of values entered in the following
ranges: 0-99, 100-199, 200-299, 300-399, 400-499,
and 500+.
If the user enters the following values:
115 556 130 605 714 808 415 494 769 278 451 114 816 398 669 481 166 81 335 459 388 504 548 402 646 299 458 733 187 615 932 983 342 302 483 543 358 373 469 881 458 114 214 253 265 869 55 198 337 889 533 694 343 307 620 712 710 650 827 62 115 951 257 116 801 31 192 820 8 686 208 444 300 671 200 325 562 761 185 197 126 380 771 384 131 397 773 570 523 262 510 304 360 793 481 391 313 534 145 645 -1your program would print
0- 99 5 100-199 15 200-299 9 300-399 19 400-499 12 500+ 40You will need six counter variables that keep track of the quantity of values entered in the respective ranges; for example, you should have an integer variable named count200_299 or something similar used to count numbers in the range 200–299. Its value initially should be zero.
In a loop like the one you used in the first assignment to compute the average you will use if/else statements to increment the appropriate counter variable depending on the number entered. You must not print anything until the loop has finished executing. This is because you do not know what the final value will be for any of the ranges until all the numbers are entered.
Once the user has finished entering numbers (that is, typed a negative number), the loop is over. Print the values of all of your counter varaibles. One of your printing statement would be
System.out.printf("200-299 %4d", count200_299);This uses the formatted printing method printf to right justify the counter values. The control code %4d means format the integer that follows the format string (count200_299) so that it is right justified within four spaces.
- Determine the maximum range.
After you are satisfied that the first part is
working correctly, you must add the code to compute
the number of asterisks to print. The longest bar
should contain exactly 50 asterisks, so you must determine
which of the ranges is the largest. Add the logic to
determine which of your six counter variables holds
the largest value. This consists of a series of
if statements. Assign this largest
value to another
variable, perhaps named maxCount.
At this point you should print the value of
maxCount to see if you computed it correctly.
In the example above you would get:
0- 99 5 100-199 15 200-299 9 300-399 19 400-499 12 500+ 40 Maximum range value is 40You later will remove this printing statement when you are sure your work to this point is correct. This printing statement should not be present in your final version of this program.
Note that your program does not need to know which range holds the most values; it simply needs to know how many values are in the range with the largest number of values.
- Compute the number of asterisks to print.
Now that you know the most values that can appear in
any range,
you may determine how many asterisks to print for each
range. The arithmetic is straightforward. Suppose
you need to compute the number of asterisks in the
range 200–299. Divide your
count200_299 variable by maxCount.
This gives you the fraction necessary to compute the
length of the bar. The number of asterisks for the
200–299 range is this
fraction times 50, the maximum length. So, suppose
count200_299 is 13, and maxCount is
65. Mathematically, 13/65 = 0.2, and
0.2 * 50 = 10,
so you would print
a row of 10 asterisks for the range 200–299.
For the variable holding the largest count (65),
you would get 65/65 = 1, and
1 * 50 = 50, so the range with the
largest count will result in 50 asterisks.
You may assign the number of asterisks to print for a
given range to
another variable; for example, for the range
200–299 you may name the variable
stars200_299.
Be aware that in Java an integer divided by an integer yields an integer result. It makes sense to count the numbers entered by the user with integer variables (the user enters 12 numbers or 13, not 12.3, for example). However, for the mathematics described above to work correctly, you must use floating-point arithmetic. Integer 13 divided by integer 65 is integer 0, not 0.2. One way to do this is to use a type cast:
double fract200_299 = ((double) count200_299)/maxCount;Another way involves a floating-point literal to force floating-point arithmetic:
double fract200_299 = (1.0*count200_299)/maxCount;Either way will work.
Similarly, once you have computed the number of asterisks to print, be aware that you must print a whole number of asterisks: you can print 12 asterisks or 13, but you cannot print 12.7 asterisks. This means that once you have computed the number of asterisks using floating-point arithmetic you must round the result to the nearest whole number. In Java, for example, if fract200_299 is a floating-point variable, you may round it to the integer variable stars200_299 with the statement
int stars200_299 = (int)(fract200_299 + 0.5);Adding the 0.5 value and then type casting properly rounds fract200_299. Simply type casting fract200_299 to an integer without first adding 0.5 will merely truncate it, not properly round it. Truncation by itself always rounds down, which is not what we want to do here.
So, the number of asterisks we eventually wish to print for the range 200–299 is stored in the stars200_299 variable.
You will need to compute the number of asterisks for each of the six ranges.
- Print the asterisks.
Once you know how many asterisks to print for a given
range you may use one
for loop or while loop for each
range to print the asterisks all on one line.
Your will interleave these bar-printing loops with the
range printing statements you completed above.
Within your loop you will print one asterisk at
a time, so use System.out.print
instead of
System.out.println so the cursor does not
move to the next line after printing an asterisk.
After you finish printing a bar, you will use
System.out.println to move the cursor down to
the next line so you can generate the output for the
next range.
It also is possible to print the asterisks immediately as you compute the number of asterisks in the previous step.
0- 99 5|****** 100-199 15|******************* 200-299 9|*********** 300-399 19|************************ 400-499 12|*************** 500+ 40|**************************************************
At this point your histogram program will be complete.
- Read in the values and tally the ranges.
Write a Java program that reads in an arbitrary number of
nonnegative integer values.
The input is terminated by an integer value less than zero.
Use a Scanner object to read in the values.
After all the values have been entered,
print the number of values entered in the following
ranges: 0-99, 100-199, 200-299, 300-399, 400-499,
and 500+.
- Check out
Your finished program will be evaluated for correctness and compliance.