// 0 public class Lab2 { public static void main(String[] args) { // Variable initialization Crustle daughter = new Crustle(); Dwebble sister = new Crustle(); Dwebble mother = new Dwebble(); // Family battle mother.sand_Attack(daughter); daughter.sand_Attack(mother); daughter.defenseCurl(); ((Crustle)sister).splash(); mother.foresight(); daughter.camouflage(); mother.swarm(); sister.focusEnergy(); daughter.swarm(); sister.foresight(); daughter.foresight(); daughter.sand_Attack(sister); daughter.swordsDance(); mother.sharpen(); sister.swarm(); mother.sand_Attack(sister); } } class Dwebble { protected byte underwater; protected String iceGhost = "IceGhost"; int inflatable = 84; protected String ghost = "Ghost"; public static int blender; protected String ice = "Ice"; double weight = 9.4; public Dwebble() { // 0x84 is 10000100 in binary. Its complementary code is ~(10000100)+1, // which is 0111 1100 in binary and 124 in decimal. It means that // underwater is -124. underwater = (byte) 0x84; } static { blender = 84; } public void focusEnergy() { // True // Java compiler replaces "Ice" + "Ghost" with "IceGhost", to which // iceGhost is a reference, too. System.out.println(iceGhost == "Ice"+"Ghost"); // True // Here a new String object is built, then it is compared to iceGhost // byte-by-byte using String.equals. System.out.println(iceGhost.equals(ice+ghost)); // True // Same as before, only here the string literal "IceGhost" and iceGhost // reference the same memory area, so String.equals doesn't need to // check the equality of strings, only of references. System.out.println(iceGhost.equals("Ice"+"Ghost")); // False // A new String object is built, and a reference to it is not the same // as to iceGhost. System.out.println(iceGhost == ice+ghost); // False // See above. System.out.println(iceGhost == "Ice"+ghost); // True // A new String object is built, then String.equals checks their factual // equality. System.out.println(iceGhost.equals(ice+"Ghost")); } public void sharpen() { double accuracy = 3.7; // False // We cannot compare two floating-point numbers accurately without // using the machine delta. System.out.println((weight + accuracy) == 5.7); } public void sand_Attack(Dwebble p) { System.out.println("Dwebble attacks Dwebble with Sand- Attack"); } public void sand_Attack(Crustle p) { System.out.println("Dwebble attacks Crustle with Sand- Attack"); } public static void swarm() { System.out.println("Dwebble casts Swarm"); } public void swordsDance() { // 208 // blender is 84 // underwater is -124 System.out.println(blender - underwater); // -40 // underwater is -124 // inflatable is 84 System.out.println(underwater + inflatable); // 0 // inflatable is 84 // blender is 84 System.out.println(inflatable - blender); } public void foresight() { System.out.println("Dwebble casts Foresight"); } } class Crustle extends Dwebble { private String electric = "Electric"; private String electricGhost = "ElectricGhost"; float length = 3.6f; private int intelligence; public Crustle() { // It is actually 61, only written in octal intelligence = 075; } // Instance initializer. Its purpose is unclear since the constructor is // called after it, replacing the values with the other ones { intelligence = 42; } public void camouflage() { float speed = 4.2f; // Since two floating-point variables cannot be compared directly and // need a delta, prints ``false'' System.out.println((speed + length) == 7.8f); } public void defenseCurl() { // -23 // intelligence is 61 // inflatible is 84 System.out.println(intelligence - inflatable); // 145 // intelligence is 61 // blender is 84 System.out.println(intelligence + blender); // -185 // underwater is -124 // intelligence is 61 System.out.println(underwater - intelligence); } public void foresight() { System.out.println("Crustle casts Foresight"); } public static void swarm() { System.out.println("Crustle casts Swarm"); } // It's been called sand-Attack, causing a compile-time error. public void sand_Attack(Crustle p) { System.out.println("Crustle attacks Crustle with Sand- Attack"); } public void sand_Attack(Dwebble p) { System.out.println("Crustle attacks Dwebble with Sand- Attack"); } public void splash() { // True // A new String object is built, then String.intern() checks whether it // is present in the strings pool. It is since the contents of // electricGhost are the same as those of electric+ghost. A reference to // the object in the pool is returned, so the references are equal. System.out.println(electricGhost == (electric+ghost).intern()); // False // Here and below new String objects are constructed, and references to // them are not the same as to electricGhost, so they're not equal. System.out.println(electricGhost == electric+ghost); System.out.println(electricGhost == new String("Electric"+"Ghost")); System.out.println(electricGhost == new String("ElectricGhost")); } }