MedianTest.java
From Huben's Wiki
(Created page with "import java.util.*; class MedianTest { static public void main(String[] args) { test1(); test2(); test3(); } static public void test1() { ArrayLis...") |
|||
Line 1: | Line 1: | ||
+ | <pre> | ||
+ | // The median method checks to see if there are an equal number of strings | ||
+ | // that would alphabetize before the test string and after the test string. | ||
+ | // If so, the test string is a median, and the median method should return true. | ||
+ | // Look at the methods of class String to see how to tell which would alphabetize first. | ||
+ | // You can write this with a for loop, and then try changing it to a for each loop. | ||
+ | |||
import java.util.*; | import java.util.*; | ||
class MedianTest | class MedianTest | ||
{ | { | ||
+ | |||
+ | static public boolean median(String s, ArrayList<String> a) | ||
+ | { | ||
+ | // INSERT YOUR CODE HERE AND FIX THE RETURN STATEMENT | ||
+ | return true; | ||
+ | } | ||
+ | |||
static public void main(String[] args) | static public void main(String[] args) | ||
{ | { | ||
Line 8: | Line 22: | ||
test2(); | test2(); | ||
test3(); | test3(); | ||
+ | test4(); | ||
} | } | ||
Line 14: | Line 29: | ||
ArrayList<String> a = new ArrayList<String>(); | ArrayList<String> a = new ArrayList<String>(); | ||
− | |||
a.add("dog"); | a.add("dog"); | ||
+ | a.add("bat"); | ||
String s = "cat"; // is this a median value? | String s = "cat"; // is this a median value? | ||
boolean r = true; // expected result | boolean r = true; // expected result | ||
Line 31: | Line 46: | ||
boolean r = false; // expected result | boolean r = false; // expected result | ||
boolean m = median(s, a); // actual result | boolean m = median(s, a); // actual result | ||
− | showResults(" | + | showResults("test2", m, r, s, a); |
} | } | ||
Line 43: | Line 58: | ||
boolean r = false; // expected result | boolean r = false; // expected result | ||
boolean m = median(s, a); // actual result | boolean m = median(s, a); // actual result | ||
− | showResults(" | + | showResults("test3", m, r, s, a); |
} | } | ||
+ | |||
+ | static public void test4() | ||
+ | { | ||
+ | ArrayList<String> a = new ArrayList<String>(); | ||
+ | |||
+ | a.add("cat"); | ||
+ | a.add("cat"); | ||
+ | String s = "cat"; // is this a median value? | ||
+ | boolean r = true; // expected result | ||
+ | boolean m = median(s, a); // actual result | ||
+ | showResults("test4", m, r, s, a); | ||
+ | } | ||
+ | |||
+ | // Should have tests for [ant, cat, cat, dog, fox] cat being true, but complex to explain. | ||
static public void showResults(String name, boolean m, boolean r, String s, ArrayList<String> a) | static public void showResults(String name, boolean m, boolean r, String s, ArrayList<String> a) | ||
Line 55: | Line 84: | ||
System.out.println(" Inputs " + a + ", \"" + s + "\", expected " + r); | System.out.println(" Inputs " + a + ", \"" + s + "\", expected " + r); | ||
} | } | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
+ | </pre> |