|
home—info—labs—hws—exams
textbook—java.lang docs—java.util docs—archive
Write a class TresAmigos which contains three Strings, and has methods getFirst, getSecond, and getThird which returns the alphabetically3 first, second, and third name respectively. Work carefully, and indent well -- it is easy to get lost in the various cases. Don't wait until the last moment, to start this!
Use nested ifs4. For example, if ami1,ami2,ami3 are all Strings, your code might contain some lines like:
if (ami1.compareTo(ami2) < 0) { if (ami2.compareTo(ami3) < 0) { // If we reach here, we know ami1 < ami2 < ami3. } else { // If we reach here, we know ami2 is last (since ami1 < ami2, and ami3 <= ami2.) } } else // ami2 <= ami1. { // ... compare ami1 vs ami3, maybe? } |
Note that your class TresAmigos should not involve any Scanner or println. Here is a test-class:
class TresAmigosTester { static TresAmigos f1 = new TresAmigos( "Arturo", "Margarita", "Zorro" ); static TresAmigos f2 = new TresAmigos( "Arturo", "Zorro", "Margarita" ); static TresAmigos f3 = new TresAmigos( "Margarita", "Arturo", "Zorro" ); static TresAmigos f4 = new TresAmigos( "Margarita", "Zorro", "Arturo" ); static TresAmigos f5 = new TresAmigos( "Zorro", "Arturo", "Margarita" ); static TresAmigos f6 = new TresAmigos( "Zorro", "Margarita", "Arturo" ); static TresAmigos g1 = new TresAmigos( "Arturo", "Arturo", "Margarita" ); static TresAmigos g2 = new TresAmigos( "Arturo", "Margarita", "Arturo" ); static TresAmigos g3 = new TresAmigos( "Margarita", "Arturo", "Arturo" ); static TresAmigos h1 = new TresAmigos( "Zorro", "Zorro", "Zorro" ); public static void main(String[] args) { System.err.println("\nStarting tests:" ); // Call three of our own static methods: TresAmigosTester.testGetters(f1, "Arturo", "Margarita", "Zorro", "f1"); TresAmigosTester.testGetters(f2, "Arturo", "Margarita", "Zorro", "f2"); TresAmigosTester.testGetters(f3, "Arturo", "Margarita", "Zorro", "f3"); TresAmigosTester.testGetters(f4, "Arturo", "Margarita", "Zorro", "f4"); TresAmigosTester.testGetters(f5, "Arturo", "Margarita", "Zorro", "f5"); TresAmigosTester.testGetters(f6, "Arturo", "Margarita", "Zorro", "f6"); TresAmigosTester.testGetters(g1, "Arturo", "Arturo", "Margarita", "g1"); TresAmigosTester.testGetters(g2, "Arturo", "Arturo", "Margarita", "g2"); TresAmigosTester.testGetters(g3, "Arturo", "Arturo", "Margarita", "g3"); TresAmigosTester.testGetters(h1, "Zorro", "Zorro", "Zorro", "h1"); System.err.println("Finished tests." ); } public static void testGetters( TresAmigos f, String exp1, String exp2, String exp3, String posseName ) { TresAmigosTester.reportIfError( exp1, f.getFirst(), posseName, "First" ); TresAmigosTester.reportIfError( exp2, f.getSecond(), posseName, "Second" ); TresAmigosTester.reportIfError( exp3, f.getThird(), posseName, "Third" ); } public static void reportIfError( String expectedResult, String actualResult, String posseName, String methodName ) { if (! expectedResult.equals(actualResult)) { System.err.println( posseName + "." + "get" + methodName + " expected \"" + expectedResult + "\"" + " but got \"" + actualResult + "\"." ); } } } |
BlueJ tip: Make the terminal window active, and then select Options > Clear Screen at method call. This way, every time you call a method, it will erase any old (error) messages.
1Point values are merely a suggestion; your professor might assign points differently. ↩
3Or, as the book calls it, “lexicographically”. ↩
4Alternately, you are welcome to use if-else-ifs along with &&, ||, !. ↩
home—info—labs—hws—exams
textbook—java.lang docs—java.util docs—archive
©2008, Ian Barland, Radford University Last modified 2008.Nov.03 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |