Difference between revisions of "CSC212 Homework 1 Solutions 2014"
(Created page with "--~~~~ ---- <onlydft> =Problem #3= <br /> <source lang="java"> public class Cat extends Animal{ boolean[] legs = new boolean[4]; int numberOfLives = 9; boolean isStray = ...") |
|||
Line 6: | Line 6: | ||
<br /> | <br /> | ||
<source lang="java"> | <source lang="java"> | ||
+ | |||
public class Cat extends Animal{ | public class Cat extends Animal{ | ||
Line 36: | Line 37: | ||
public boolean isAlive() { | public boolean isAlive() { | ||
return numberOfLives > 0; | return numberOfLives > 0; | ||
+ | } | ||
+ | |||
+ | public boolean isAStrayCat() { | ||
+ | return isStray; | ||
} | } | ||
Line 48: | Line 53: | ||
if ( ! legs[i] ) count++; | if ( ! legs[i] ) count++; | ||
System.out.print( ", " + count + " bad leg(s)" ); | System.out.print( ", " + count + " bad leg(s)" ); | ||
+ | |||
+ | if ( isAStrayCat() ) | ||
+ | System.out.print( ", is stray" ); | ||
+ | else | ||
+ | System.out.print( ", has an owner"); | ||
+ | |||
+ | String life = "lives"; | ||
+ | if ( numberOfLives==1 ) life = "life"; | ||
+ | |||
if ( isAlive() ) | if ( isAlive() ) | ||
System.out.println( String.format( | System.out.println( String.format( | ||
− | ", has %d | + | ", has %d %s left, and is alive.", numberOfLives, life ) ); |
else | else | ||
System.out.println(", is a dead cat." ); | System.out.println(", is a dead cat." ); | ||
Line 62: | Line 76: | ||
for ( int i=0; i<10; i++ ) { | for ( int i=0; i<10; i++ ) { | ||
cat1.decrementLives(); | cat1.decrementLives(); | ||
+ | if ( i==5 ) cat1.changeStray( false ); | ||
cat1.displayBasicInfo(); | cat1.displayBasicInfo(); | ||
− | } | + | } |
+ | |||
} | } | ||
+ | |||
+ | |||
} | } | ||