2D Arrays、辅导java编程设计、讲解java语言、辅导data science

- 首页 >> Java编程


Program 2: 2D Arrays

Introduction

Continuing with our data science theme, lets do another program to visualize a distribution.

A histogram bins the data and displays bars to represent how many data values are in each

bin. A stem-and-leaf diagram bins the data and displays the data values in each bin, as well as

the number of values in each bin. Our stem-and-leaf diagrams will display only nonnegative

integer data, so we will call them unsigned stem-and-leaf diagrams. In a stem-andleaf

diagram, the bins are displayed horizontally. Each bin is labelled by the data value div 10

(that is, the value without its last digit). Tese labels are collectively the stem of the diagram.

Te bins display the digits of the values in the bin, in order; these are the leaves of the

diagram. Finally, to make the diagrams more readable, only bin widths of 1, 2, 5, or 10 are

allowed.

A stem and leave diagram for the data values 28, 31, 31, 37, 39, 46, 47, 48, 51, 51, 55, 60, 60,

61, 65, 67, 69, 70, 71, 78, 81, 86, 88, 90 is shown in the box below (these are the average

monthly high and low temperatures in Harrisonburg). Te bin width is 5.

Te box below shows the stem-and-leaf diagram for the same data, but with a bin width of

10. You ought to be able to decode these diagrams; if not, ask.

In the Histogram PA, you needed to count how many values were in each bin (bar) in the

diagram. In this PA, you will need to record the digits of the values in each bin (leaf ), in

order. Because the leaves are diferent lengths, you will need to do this with a 2D array.

As before, you will be provided with several classes that will help you read and process data

for making stem-and-leaf diagrams. Tese classes are the following.

9 | 0

Program 2: 2D Array 2

DataFileUtilities

Te DataFileUtilities class has a several static public method that read text fles with

data values in them. You can use this class to read data for testing. Te method of most

interest for this assignment is the following.

static int[] readUnsignedData(String pathname)—Tis method attempts to

read the text fle whose path is given by the pathname argument. Te path is relative to

the working directory of the program. If the fle cannot be found or opened, then no

data is read. If the fle can be opened and read, then all the strings consisting of strings

of digits are isolated and read as Java non-negative int values. All other characters in the

fle are considered data separators. As many values are read as possible. If an input error

occurs, the method ignores any remaining data. Tis method always returns an int array

that is exactly the size of the number of int values read (which may be 0).

DataBinner

Te DataBinner class is the same as in PA1. Specifcally, a DataBinner instance does

calculations to place values into bins, where a bin is a range of values. Te DataBinner

constructor takes the minimum and maximum values in the range of possible values, along

with the width of a bin, and thereafter does computations to put values into bins and to

indicate properties of the bins. For example, a DataBinner with a minimum value of 5, a

maximum value of 9.75, and a bin width of 0.5 will divide this range into 10 bins of width

0.5 each, and place the number 7.25 into the ffth bin (with index 4). A bin starts at its start

value and extends to just less than its end value. Hence 5 goes into the frst bin but 5.5 goes

into the second bin. Te following are the public constructor and methods of the

DataBinner class.

DataBinner(double dataMin, double dataMax, double binWidth)—Create a

binner with the specifed parameters as explained above.

int getNumBins()—Return the number of bins that data can be placed into.

int binIndex(double x)—Return the bin number for value x. Tis number will

always be in the range 0 to getNumBins()-1. If x is less than the minumum or greater

than the maximum provided in the constructor, it throws an

IllegalArgumentException.

double binMinValue(int binIndex)—Return the lowest value in the bin with the

specifed index. If binIndex is less than 0 or greater than or equal to getNumBins()

then this method throws an IndexOutOfBoundsException.

UnsignedStemAndLeafWindow

UnsignedStemAndLeafWindow uses the UnsignedStemAndLeaf class to display stem-andleaf

diagrams in a graphical user interface. Its only public method is main(). You can use

this class to do an overall test of your class when it is done. It will NOT work well to test

the UnsignedStemAndLeaf class as it is being developed.

Program 2: 2D Array 3

UnsignedStemAndLeaf Class Requirements

You must write an UnsignedStemAndLeaf class. Tis class is constructed from an array of

int data, and a bin width (the range of values in a bin). Bins always start at the bin that

contains the smallest data value (remember that in the Histogram class you could set where

the frst bin started, but not in this class). Also, note that the minimum value in a bin (the

bin start value) is always a multiple of the bin width. Clients can query the stem-and-leaf

diagram about its characteristics. Also, a client can ask for a string representation of the

diagram. (Tis is how the UnsignedStemAndLeafWindow class gets the data it needs to

display the diagram.) Te following are the public constructors and methods of the

UnsignedStemAndLeaf class.

UnsignedStemAndLeaf(int[] data, int binWidth)—Te data parameter has the

data displayed in the stem-and-leaf diagram. If it is null or empty then the stem-andleaf

diagram must have minimum and maximum values of 0 and one bin starting at 0,

extending for the bin width, with a leaf of size 0. If the binWidth is less than 1 it must

be set to 1; if greater than 2 but less than 5, it must be set to 5, if greater than 5 it must

be set to 10. Te constructor should do whatever processing is necessary to construct the

stem-and-leaf diagram according to the given (and perhaps modifed) parameters.

UnsignedStemAndLeaf(int[] data)—Tis constructor works just like the other one,

except that the bin width is 10.

void setBinWidth(int binWidth)—Tis method resets the bin width, possibly

modifying the binWidth argument the same way that the main constructor does. Ten it

re-bins the data.

int getBinWidth()—Return the current bin width.

int getNumBins()—Return the number of bins in the stem-and-leaf diagram.

int[] getBin(int binIndex)—Return the digits in the designated bin as the values

in an int array. Te array must be the bin size (which means it may be 0). Te bin values

must be in order from least to greatest.

String getDiagram(boolean direction)—Return a String representing the stemand-leaf

diagram. If the data is null or empty, then the string must be the single line

“Tere is no data to display.” Otherwise, the result must consist of one line per bin. Bins

are displayed in order from the bin containing the smallest values to the bin containing

the largest values, or from the bin containing the largest values to the bin containing the

smallest values. Tis is governed by the direction parameter. If direction is

UnsignedStemAndLeaf.MIN_TO_MAX, then bins are displayed smallest to largest; if

direction is UnsignedStemAndLeaf.MAX_TO_MIN, then the bins are displayed in the

opposite order. Tese constants must be public static final boolean values. Each

line in a non-empty diagram must begin with the int stem value of the bin displayed in

a feld of three characters. Tis value must be followed by a single space, the bar

character, and another space (that is, the string " | "). Tis must be followed by the leaf

digits with no spaces between them. Examples of the output if this method are shown in

the boxes above.

Program 2: 2D Array 4

Assignment Specifications

Your code must conform to the CS 159 coding standards, which are posted on Canvas. I

recommend that you use Checkstyle in Eclipse to ensure that your code conforms to style

requirements. AutoLab will use the class Checkstyle confguration fle to check for style, so

if you get no Checkstyle warnings in Eclipse, then your code will pass the style check in

AutoLab.

Your code will be checked for correctness in AutoLab. You may submit as many times as you

like in AutoLab.

Your code will be checked for for overall quality, called Elegance in AutoLab, by me. I will

look for things like good variable names, appropriate control structures, decent comments,

and so forth.

Hints and Recommendations

You should begin by understanding the requirements of this program. Read over the

specifcations and think about how this PA difers from PA1—there is much in common but

also a few important diferences. You may fnd it useful to start with a PA1 solution (yours or

mine) and modify it.

Write and debug the constructors and getNumBins() method frst. Ten work on the

setBinWidth(), and getBinWidth() methods. Finally, implement the getDiagram()

methods.

Due Dates and Deliverables

Tis program is due by midnight, Wednesday January 30th. You must submit your code in

AutoLab (autolab.cs.jmu.edu), and also you must submit a hard copy of the code to me (in

person, in my mail box in ISAT/CS 220, or under my door) by the due date. Te program is

late if both deliverables are not submitted on time. Te submitted fle must be called

UnsignedStemAndLeaf.java, and of course, the class must be called

UnsignedStemAndLeaf.



站长地图