Difference between revisions of "CSC212 Homework 1 Solutions 2014"

From dftwiki3
Jump to: navigation, search
(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 = ...")
 
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:
 
----
 
----
 
<onlydft>
 
<onlydft>
 +
=Problem #1=
 +
 +
=Problem #2=
 +
  
 
=Problem #3=
 
=Problem #3=
 
<br />
 
<br />
 
<source lang="java">
 
<source lang="java">
 +
  
 
public class Cat extends Animal{
 
public class Cat extends Animal{
Line 36: Line 41:
 
public boolean isAlive() {
 
public boolean isAlive() {
 
return numberOfLives > 0;
 
return numberOfLives > 0;
 +
}
 +
 +
public boolean isAStrayCat() {
 +
return isStray;
 
}
 
}
 
 
Line 48: Line 57:
 
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 lives left, and is alive.", numberOfLives ) );
+
", 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 80:
 
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();
}
+
}
 +
 
}
 
}
 +
 +
 
}
 
}
  
Line 70: Line 92:
 
<br />
 
<br />
 
</onlydft>
 
</onlydft>
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
[[Category:CSC212]][[Category:Java]][[Category:Homework]]

Latest revision as of 11:21, 11 September 2014

--D. Thiebaut (talk) 12:05, 11 September 2014 (EDT)



...