class Robot { String shape; boolean isTransformed; boolean isEvil; String id; Robot( String i, String s, boolean t, boolean e ) { this.id = i; this.shape = s; this.isTransformed = t; this.isEvil = e; } /** Return a description of a Robot's current form. * @param bot The Robot to describe. * @return a description of a Robot's current form. */ String currentForm( ) { if (this.isTransformed) { return this.shape; } else { return "Tall 'bot"; } } static void testRobots() { Robot r1 = new Robot( "X6", "Corvette", true, false); Robot r2 = new Robot( "Y7", "Mack Truck", false, true); testEqualStrings( r1.currentForm(), "Corvette" ); testEqualStrings( r2.currentForm(), "Tall 'bot" ); testEqualStrings( new Robot( "A1", "phone", true, true).currentForm( ), "phone" ); testEqualBools( r1.willBattle(r2), false ); testEqualBools( r2.willBattle(r1), false ); testEqualBools( r2.willBattle( new Robot( "Z0", "pencil",false,false) ), true ); testEqualBools( (new Robot("Z0", "pencil",false,false)).willBattle(r2), false ); testEqualBools( r1.willBattle(r1), false ); // more tests... testEqualRobots( r1.evilCopy(), new Robot("X1", "Corvette",true,true) ); testEqualRobots( r2.evilCopy(), r2 ); } /** Will one robot initiate battle w/ a second, given the chance? * @param bot1 The first Robot. * @param bot2 The second Robot. * @return true iff `bot1` will initiate a battle with `bot2`. * bot1 will initiate a battle if bot1 is evil * and bot2 is good+non-transformed. * [Alternate: two evil robots battle as well!] */ boolean willBattle( /* Robot this, */ Robot that ) { /* Inventory: * this.isEvil (boolean) * bot2.isTransformed (boolean) && || ! == if-else * bot2.isEvil */ return this.isEvil && !that.isTransformed && !that.isEvil; } /** Which of two robots is cooler? * @param bot1 The first Robot. * @param bot2 The second Robot. * @return Either bot1 or bot2, depening on who is cooler. * A robot with a *shorter* transformed-shape description is cooler; * in case of ties, the evil one is cooler. * */ /* static Robot coolerOf( Robot bot1, Robot bot2 ) { if ( length(bot1.shape) < length(bot2.shape) ) { ... } else { ... } } */ /** Make a new, evil copy of a given Robot. * @param aBot The Robot to make an evil copy of. * @return a new, evil copy of `aBot`. * (Evil copies have are always evil; * they have the same disguise and transformation status. */ Robot evilCopy( /* Robot this */ ) { return new Robot( this.id, this.shape, this.isTransformed, true ); } static boolean equals( Robot thisss, Robot thattt ) { return (thisss.isEvil == thattt.isEvil) && (thisss.isTransformed == thattt.isTransformed) && (equals(thisss.shape, thattt.shape)) && (equals(thisss.id, thattt.id)); } /** If two booleans aren't equal, print an error message. * Intended for use as a test-helper. * @param act The actual boolean returned by a test call. * @param exp The expected boolean to be returned by a test call. */ static void testEqualBools(boolean act, boolean exp) { if (act==exp) { System.out.println("(test passed)"); } else { System.out.println( "Actual: " + act + "\nExpect: " + exp ); } } /** If two Robots aren't equal, print an error message. * Intended for use as a test-helper. * @param act The actual Robot returned by a test call. * @param exp The expected Robot to be returned by a test call. */ static void testEqualRobots(Robot act, Robot exp) { if (act.equals(exp)) { System.out.println("(test passed)"); } else { System.out.println( "Actual: " + act + "\nExpect: " + exp ); } } /** If two Strings aren't equal, print an error message. * Intended for use as a test-helper. * @param act The actual String returned by a test call. * @param exp The expected String to be returned by a test call. */ static void testEqualStrings(String act, String exp) { if (act.equals(exp)) { System.out.println("(test passed)"); } else { System.out.println( "Actual: " + act + "\nExpect: " + exp ); } } }