/** * ============================================================ * CS 231 — Data Structures & Algorithms * Project B: Modeling Elephants * * ProjectBTester * * BASIC STUDENT SELF-CHECK * * This tester is intended to help you make progress while * developing Elephant.java and Tracker.java. It checks useful, * observable behavior, but it does not check every project * requirement. Passing every check does not guarantee full credit. * * Run with: * javac *.java * java ProjectBTester * ============================================================ */ public class ProjectBTester { private static int passed = 0; private static int checks = 0; public static void main(String[] args) { System.out.println("CS231 Project B Basic Tester"); System.out.println("============================"); testTrackerConstructors(); testTrackerSetters(); testTrackerMovement(); testTrackerInsideOutside(); testElephantConstructors(); testElephantSetters(); testElephantBehavior(); testToStringFormats(); System.out.println("\n============================"); System.out.println("Basic checks passed: " + passed + " of " + checks); if (passed == checks) { System.out.println("All basic checks passed."); } else { System.out.println("Use the FAIL messages above to decide what to work on next."); } System.out.println( "Passing all basic checks does not guarantee that every project requirement is satisfied."); } private static void report(boolean condition, String description) { checks++; if (condition) { passed++; System.out.println(" PASS: " + description); } else { System.out.println(" FAIL: " + description); } } // -------------------------------------------------------- // Tracker // -------------------------------------------------------- private static void testTrackerConstructors() { System.out.println("\nTracker constructors"); try { Tracker t = new Tracker(); report(t.getDistanceFromCenter() == 0.0, "default distance is 0.0 km"); report(t.getParkRadius() == 30.0, "default park radius is 30.0 km"); } catch (Exception ex) { report(false, "default Tracker constructor runs"); } try { Tracker t = new Tracker(12.0, 40.0); report(t.getDistanceFromCenter() == 12.0, "full constructor stores the distance"); report(t.getParkRadius() == 40.0, "full constructor stores the park radius"); Tracker negative = new Tracker(-5.0, 40.0); report(negative.getDistanceFromCenter() >= 0.0, "full constructor never allows a negative distance"); } catch (Exception ex) { report(false, "full Tracker constructor runs"); } } private static void testTrackerSetters() { System.out.println("\nTracker setters"); Tracker t = new Tracker(); t.setDistanceFromCenter(18.0); report(t.getDistanceFromCenter() == 18.0, "distance setter updates the distance"); t.setDistanceFromCenter(-5.0); report(t.getDistanceFromCenter() >= 0.0, "distance is never allowed to become negative"); t.setParkRadius(45.0); report(t.getParkRadius() == 45.0, "park-radius setter updates the radius"); } private static void testTrackerMovement() { System.out.println("\nTracker movement"); Tracker t = new Tracker(20.0, 30.0); t.move(5.0); double after = t.getDistanceFromCenter(); report(after == 15.0 || after == 25.0, "move(5.0) changes 20.0 km to either 15.0 or 25.0 km"); // A large inward move must still never produce a negative distance. boolean stayedNonNegative = true; for (int i = 0; i < 20; i++) { t.move(100.0); if (t.getDistanceFromCenter() < 0.0) { stayedNonNegative = false; break; } } report(stayedNonNegative, "movement never produces a negative distance"); } private static void testTrackerInsideOutside() { System.out.println("\nTracker park-boundary behavior"); Tracker t = new Tracker(29.0, 30.0); report(t.isInsidePark(), "29 km is inside a park with radius 30 km"); t.setDistanceFromCenter(31.0); report(!t.isInsidePark(), "31 km is outside a park with radius 30 km"); } // -------------------------------------------------------- // Elephant // -------------------------------------------------------- private static void testElephantConstructors() { System.out.println("\nElephant constructors"); try { Elephant e = new Elephant(); report(e.getAge() >= 1 && e.getAge() <= 60, "default age is between 1 and 60"); report(e.getSex() == 'm' || e.getSex() == 'f', "default sex is 'm' or 'f'"); report(e.getTracker() != null, "default Elephant has a Tracker"); } catch (Exception ex) { report(false, "default Elephant constructor runs"); } try { Tracker t = new Tracker(10.0, 35.0); Elephant e = new Elephant(25, 'f', t); report(e.getAge() == 25, "full constructor stores the age"); report(e.getSex() == 'f', "full constructor stores the sex"); report(e.getTracker() == t, "full constructor stores the supplied Tracker"); } catch (Exception ex) { report(false, "full Elephant constructor runs"); } } private static void testElephantSetters() { System.out.println("\nElephant setters"); Elephant e = new Elephant(); e.setAge(40); report(e.getAge() == 40, "age setter updates a valid age"); e.setAge(0); report(e.getAge() >= 1 && e.getAge() <= 60, "age remains between 1 and 60 when given 0"); e.setAge(61); report(e.getAge() >= 1 && e.getAge() <= 60, "age remains between 1 and 60 when given 61"); e.setSex('f'); report(e.getSex() == 'f', "sex setter updates a valid sex"); e.setSex('x'); report(e.getSex() == 'm' || e.getSex() == 'f', "sex remains 'm' or 'f' when given an invalid value"); Tracker replacement = new Tracker(8.0, 25.0); e.setTracker(replacement); report(e.getTracker() == replacement, "Tracker setter stores the supplied Tracker"); } private static void testElephantBehavior() { System.out.println("\nElephant movement and park behavior"); Tracker t = new Tracker(20.0, 30.0); Elephant e = new Elephant(10, 'm', t); e.move(5.0); double after = t.getDistanceFromCenter(); report(after == 15.0 || after == 25.0, "Elephant.move(5.0) changes its Tracker from 20.0 km to 15.0 or 25.0 km"); t.setDistanceFromCenter(29.0); report(e.isInsidePark() == t.isInsidePark(), "Elephant and its Tracker agree when inside the park"); t.setDistanceFromCenter(31.0); report(e.isInsidePark() == t.isInsidePark(), "Elephant and its Tracker agree when outside the park"); } // -------------------------------------------------------- // toString() // -------------------------------------------------------- private static void testToStringFormats() { System.out.println("\ntoString()"); Tracker t = new Tracker(12.0, 30.0); String expectedTracker = "Tracker[distance=12.0 km, radius=30.0 km, inside=true]"; report(t.toString().equals(expectedTracker), "Tracker.toString() matches the required format"); Elephant e = new Elephant(10, 'm', t); String expectedElephant = "Elephant[age=10, sex=m, " + expectedTracker + "]"; report(e.toString().equals(expectedElephant), "Elephant.toString() matches the required format"); } }