// FILE: statexam.java // Written by Michael Main (main@colorado.edu)for C++ // Modified for java by Barbara Boucher Owens // This program calls five test functions to test the Statistician class. // Maximum number of points from this program is 83 // Program as of January 14, 2003 import java.io.*; class statexam{ // Test program for basic Statistician functions. // Returns 62 if everything goes okay; otherwise returns 0. public static int test1( ) { Statistician s = new Statistician(); Statistician t = new Statistician(); int i; double r = 0; if (s.length( )>0 || t.length( )>0) return 0; if (s.sum( ) >0 || t.sum( )>0) return 0; for (i = 1; i <= 10000; i++) { s.next(i); r += i; }; if (t.length( )>0 || t.sum( )>0) return 0; if (s.length( ) != 10000) return 0; if (s.sum( ) != r) return 0; if (s.mean( ) != r/10000) return 0; // Reset and then retest everything s.clear( ); t.clear( ); r = 0; if (s.length( )>0 || t.length( )>0) return 0; if (s.sum( ) >0|| t.sum( )>0) return 0; for (i = 1; i <= 10000; i++) { s.next(i); r += i; }; if (t.length( )>0 || t.sum( )>0) return 0; if (s.length( ) != 10000) return 0; if (s.sum( ) != r) return 0; if (s.mean( ) != r/10000) return 0; return 62; } public static int test2( ) { // Test program for minimum/maximum Statistician functions. // Returns 7 if everything goes okay; otherwise returns 0. Statistician s= new Statistician(); Statistician t= new Statistician(); Statistician u= new Statistician(); double r = 1000; String n = "10000000000000"; if (s.length( ) >0|| t.length( )>0) return 0; if (s.sum( )>0 || t.sum( )>0) return 0; r = 1/r; s.next(r); if ((s.minimum( ) != r) || (s.maximum( ) != r)) return 0; r *= -1; t.next(r); if ((t.minimum( ) != r) || (t.maximum( ) != r)) return 0; u.next(100); u.next(-1); u.next(101); u.next(3); if ((u.minimum( ) != -1) || (u.maximum( ) != 101)) return 0; return 7; } public static int test3( ) { // Test program for + operator of the Statistician // Returns 7 if everything goes okay; otherwise returns 0. Statistician s= new Statistician(); Statistician t= new Statistician(); Statistician u= new Statistician(); Statistician v= new Statistician(); if (s.length( ) >0|| t.length( )>0) return 0; if (s.sum( ) >0|| t.sum( )>0) return 0; t.next(5); u.next(0); u.next(10); u.next(10); u.next(20); v = Statistician.union(s,s); //if (v.length( )>0 || v.sum( )>0) return 0; v = Statistician.union(s,u); //if (!(u == v)) System.out.println("2");//return 0; v = Statistician.union(t,s); //if (!(t == v)) System.out.println("3");//return 0; v = Statistician.union(t,u); if (v.length( ) != 5) return 0; if (v.sum( ) != 45) return 0; if (v.minimum( ) != 0) return 0; if (v.maximum( ) != 20) return 0; if (v.mean( ) != 45.0/5) return 0; v = Statistician.union(v,t); if (v.length( ) != 6) return 0; if (v.sum( ) != 50) return 0; if (v.minimum( ) != 0) return 0; if (v.maximum( ) != 20) return 0; if (v.mean( ) != 50.0/6) return 0; return 7; } public static int test5( ) { // Test program for == operator of the Statistician. // Returns 7 if everything goes okay; otherwise returns 0. Statistician t= new Statistician(); Statistician u= new Statistician(); Statistician v= new Statistician(); Statistician s= new Statistician(); Statistician x= new Statistician(); Statistician w= new Statistician(); if (s.length( )>0 || t.length( )>0) return 0; if (s.sum( )>0 || t.sum( )>0) return 0; t.next(10); u.next(0); u.next(10); u.next(10); u.next(20); v.next(5); v.next(0); v.next(20); v.next(15); w.next(0); x.next(0); x.next(0); if (!s.equals(s)) return 0; if (s.equals(t)) return 0; if (t.equals(s)) return 0; if (u.equals(t)) return 0; if (!u.equals(v)) return 0; if (w.equals(x)) return 0; return 7; } public static void main (String[] arg) {{ int value = 0; int result; System.out.println("Running Statistician tests:" + "\nTEST 1:"); System.out.println("\nTesting next, reset, length, sum, and mean (62 points).\n"); result = test1( ); value += result; if (result > 0) System.out.println("Test 1 passed.\n"); else System.out.println("Test 1 failed.\n\n"); System.out.println("\nTEST 2:\n"); System.out.println("Testing minimum and maximum member functions (7 points).\n"); result = test2( ); value += result; if (result > 0) System.out.println("Test 2 passed.\n\n"); else System.out.println("Test 2 failed.\n\n"); System.out.println("\nTEST 3:\n"); System.out.println("Testing the union operator (7 points).\n"); result = test3( ); value += result; if (result > 0) System.out.println("Test 3 passed.\n\n"); else System.out.println("Test 3 failed.\n\n"); /*System.out.println("\nTEST 4:\n"); System.out.println("Testing the * operator (7 points).\n"); result = test4( ); value += result; if (result > 0) System.out.println("Test 4 passed.\n\n"); else System.out.println("Test 4 failed.\n\n"); */ System.out.println("\nTEST 5:\n"); System.out.println("Testing the equals operator (7 points).\n"); result = test5( ); value += result; if (result > 0) System.out.println("Test 5 passed.\n\n"); else System.out.println("Test 5 failed.\n\n"); System.out.println("If you submit the Statistician to Dora now, this part of the\n"); System.out.println("grade will be " + value +" points out of 83.\n"); } } }