CS
99
Summer
2001
7.31
Lab
10
Reading 6.1, 6.2.
This lab is very short to give you more time to study for the exam
on Thursday.
Overview
The goals of this lab are to:
·
Complete the implementation of class Histogram which determines and prints a frequency distribution of values
entered by the user.
Part I:
Histogram, Arrays
Design and implement an application that creates a histogram that
allows you to visually inspect the frequency distribution of a set of values.
Call the class Histogram and save it in a
file called Histogram.java.
The program should read in an arbitrary number of integers in the range
1 to 100 inclusive. You should produce an error message if any input falls
outside of this range and provide a way for the user to indicate that they are
done inputting the data, which is to say, have a stopping value. I suggest making that value -1 or 0.
Hint for the
first part:
If you need a template for constructing the main method, look at
your solution to Lab 7: Finding the Mode or your solution to Lab 6: the Hi-Lo
Guessing Game. You can also look at our official solutions online.
You should construct an int array called freq that contains 10 elements.
Update the appropriate element when the user enters a number, using a
formula similar to the one shown in lecture.
Try not to use if statements to
decide which element should be updated (if you can).
Once the user has finished inputting the data, produce a chart
similar to the one below that indicates how many input values fell in the
ranges 1-10, 11-20, and so on. Print one asterisk for each value entered.
1 -10 | *****
11 - 20 | ***
21 - 30 |
31 - 40 |
********
41 - 50 | **
51 - 60 | ****
61 - 70 | ******
71 - 80 | ***
81 - 90 | **
91 - 100 |
********
Hint for the
second part:
To print the chart, you will need to use a while (or for loop) with array freq. The value in array element n is the number of asterisks that need to be printed for range 10*n+1 to 10*n + 10; i.e., freq[0] is the number of asterisks in the range 1-10.
Part II:
Bonus, more practice with arrays and methods
Fill in the methods for the class below and save the completed
class in a file called Arrays.java. Make sure your methods accurately implement
the method headers. Test your code in
method main.
class Arrays {
public
static void main( String[] args ) {
/*
Your testing code goes here*/
}
/**
* rightShift()
returns a new array the same size as the input array, but whose elements
* are the elements of a shifted 1 position to
the right. Elements falling off the right end
* are added at the left
* Example: 1 2 3 4 5 --> 5 1 2 3 4
*/
public
static int[] rightShift( int[] a ) {
int[]
b = new int[ a.length ];
/*
Your code for making b a right-shifted version of array a */
return
b;
}
/**
* delete
(array, element) takes an integer array 'array', and integer 'element', and
returns
* new array consisting of all elements of
'array' except for occurrences of 'element'. The
* new array's size should be the size of
'array' minus the number of elements removed.
*/
public
static int[] delete( int[] a, int k ) {
}
/* isPalindrome(char[] array) takes an array of
characters and returns true if the characters
* in the array constitute a palindrome, false otherwise. A palindrome
is the same
* forwards as backwards, e.g. 'a' 'b'
'c' 'b' 'a'.
*/
public static boolean isPalindrome(
char[] c ) {
}
}
Part III:
Submitting the Lab
Hand in printouts for
·
Histogram.java
·
Arrays.java (if you choose
to do it).
As
usual, don't forget to put your name, net ID, and date at the top of each file.
This lab is due Thursday,
02 August 2001. However, you may hand it in by Friday to Siddharth in his
office.