227 Section: String 227 and DescriptiveStatistics
1. Add methods makeUpperCase(), equals(String227 other), and concat(String227 extra) to our String227 class.
publicclass String227 {
privateintnumChars;
privatechar[] theChars;
// Construct a mutable OurString object with an array of characters
public String227(char[] array) {
numChars = array.length;
theChars = newchar[numChars];
for (int i = 0; i < numChars; i++)
theChars[i] = array[i];
}
// Return the number of chars in this OurString object
publicint length() {
returnnumChars;
}
// Returns the char value in this sequence at the specified index.
// Precondition: index >= 0 & index < this.length()
publicchar charAt(int index) {
returntheChars[index];
}
// Change any and all occurrences of oldChar to newChar
publicvoid replace(char oldChar, char newChar) {
for (int i = 0; i < this.length(); i++) {
if (theChars[i] == oldChar)
theChars[i] = newChar;
}
}
2. Compute the Standard Deviation for a set of numbers, for example {1.0, 2.0, 3.0, 4.0, 5.0 } where the average is 3.0 and n is 5.
3. What should you return when n <= 1?
4. Consider an algorithm for the modes method, which isneeded on the current project:
// Return a String that has all modes in in ascending order separated by a space
// and rounded to one decimal place. The mode is the most frequently occurring number.
//
// If there are no numbers (size() == 0), there is no mode--return empty string "".
//
// If all numbers occur once, there is no mode--return empty string "".
//
// If one number occurs as frequently as exactly one other number, the data is
// said to be bimodal. In this case return a String with both numbers in order
// separated by a space.
//
// If three or more numbers occur the more frequent number of times, return
// a string with all modes in ascending order.
public String modes() {
@Test
publicvoid testFailedWhileTestingModeWhenEmpty() {
DescriptiveStatistics ds = new DescriptiveStatistics();
assertEquals("Failed modes() when there are no values", "", ds.modes().trim());
assertEquals("Failed number of modes after calling modes()", 0, ds.numberOfModes());
}
@Test
publicvoid testFailedWhileTestingModeWhenThereIsNoMode() {
DescriptiveStatistics ds = new DescriptiveStatistics();
ds.addInOrder(4.0);
ds.addInOrder(2.0);
ds.addInOrder(3.0);
ds.addInOrder(1.0);
assertEquals("Failed modes() when 4 values are unique", "", ds.modes().trim());
assertEquals("Failed number of modes after calling modes()", 0, ds.numberOfModes());
}
@Test
publicvoid testFailedWhileTestingModeWhenBiModal() {
ds = new DescriptiveStatistics();
ds.addInOrder(4.0);
ds.addInOrder(2.0);
ds.addInOrder(4.0);
ds.addInOrder(3.0);
ds.addInOrder(1.0);
ds.addInOrder(2.0);
assertEquals("Failed modes() when should be bimodal", "2.0 4.0", ds.modes());
assertEquals("Failed number of modes after calling modes()", 2, ds.numberOfModes());
}