Mode Histogram Download

Dave Wittry

classes, strings Student approved


Introduction

A mode is a value in an array that is larger than both the value immediately before it in the array and the value immediately after it.

In other words, a mode occurs at index k in the array A if A[k] > A[k-1] and A[k] > A[k+1]

The array is unimodal if the value increase until they reach a mode, then decrease, so that there is only one mode.

For example, the array A shown below is unimodal with its mode occurring at index 4:

Index k A[k]
0       3 
1       5 
2       9 
3       10 
4       12 
5       11 
6       9 
7       4

Assume that the mode does not occur at the first or last entry in the array.


Task

Write method isMode which returns true if data[k] is larger than data[k-1] and larger than data[k+1]; otherwise, it returns false.

In the example above, the call isMode(A, 4) returns true and the call isMode(A, 5) returns false.

Write method modeIndex which returns the index of the mode of data. You may assume that data is unimodal and the mode occurs at an index k, where 0 < k < data.length() – 1. In the example above, the call modeIndex(A) returns 4.

Write method printHistogram which returns a String containing a character histogram of a unimodal array of nonnegative values, data, such that the longest bar of the histogram (the mode) has longestBar characters barChar, and all other bars have a number of barChar characters proportional to the corresponding value in the array data (rounding down)


Starter code

/**
 * Author:
 * period:
 *
 */
import java.util.ArrayList;

// NOTE: One thing we've noticed is that you are NOT "writing one, testing one"
//       method as you go along - we are now 1/2 way through the year. We hope it
//       doesn't take you the whole year to believe us when we say that writing one
//       method and completely testing it out before moving on will get you to your
//       goal (perfection!) much faster. You will eventually say, "ahhh, you were
//       right" - we're just hoping that is sooner than later! :-)

public class ModeHistogram {
	
	// constructor is complete - it's private to prevent user from instantiating
	// an instance of this class - access to all methods is static
	private ModeHistogram() {}
	
	// precondition:	0 < k < data.length - 1
	public static boolean  isMode(int data[], int k) {
		...
	}	
	
	// precondition:	0 < k < data.size() - 1
	//                ArrayList holds Integer objects
	public static boolean isMode(ArrayList data, int k) {
		...
	}
	
	// precondition:	data is unimodal and data.length >= 3
	public static int modeIndex(int data[]) {
		...
	}
	
	// precondition:	data is unimodal and data.size() >= 3
	//                data holds Integer objects
	public static int modeIndex(ArrayList data) {
		...
	}

	// precondition:	data is unimodal and data.length >= 3;
	//                data[k] >= 0  for  0 <= k < data.length	
	public static String printHistogram(int data[], int longestBar, char barChar) {
		...
	}

	// precondition:	data is unimodal and data.size() >= 3;
	//                the integer value at data.get(k) >= 0   for   0 <= k < data.size()	
	public static String printHistogram(ArrayList data, int longestBar, char barChar) {
		...
	}
}







Expected output

For example, assume that vector data contains the values shown below.
Indexk data[k]Length of bar
035
158
2915
31015
41220
51118
6915
746
String returned as a result of the call printHistogram(data, 20, ‘x’):

xxxxx 
xxxxxxxx 
xxxxxxxxxxxxxxx 
xxxxxxxxxxxxxxxx 
xxxxxxxxxxxxxxxxxxxx 
xxxxxxxxxxxxxxxxxx 
xxxxxxxxxxxxxxx 
xxxxxx

Notes about the String returned


Useful tools to solve the problem