Difference between revisions of "CSC212 Lab 4 2014"

From dftwiki3
Jump to: navigation, search
(Private Member Variables)
(Private Member Variables)
Line 16: Line 16:
 
<br />
 
<br />
 
<source lang="java">
 
<source lang="java">
 +
public class Animal {
 +
        boolean isVaccinated;
 +
        boolean isTattooed;
 +
        String name;
 +
        int age;
 +
       
 +
        Animal( String n, int a, boolean v, boolean t ) {
 +
                name            = n;
 +
                age              = a;
 +
                isVaccinated = v;
 +
                isTattooed  = t;
 +
        }
 +
       
 +
        public void displayBasicInfo( ) {
 +
                String v = "vaccinated";
 +
                if ( !isVaccinated ) v = "not " + v;
 +
                String t = "tattooed";
 +
                if ( !isTattooed ) t = "not " + t;
 +
                System.out.print( String.format( "%s (%d), %s, %s",
 +
                                name, age, t, v ) );
 +
        }
 +
       
 +
        public static void main(String[] args) {
 +
                Animal a = new Animal( "Max", 3, false, true );
 +
                a.displayBasicInfo();
 +
                System.out.println();
 +
        }
  
 +
}
 
</source>
 
</source>

Revision as of 20:13, 17 September 2014

--D. Thiebaut (talk) 21:12, 17 September 2014 (EDT)





Lab 4 deals with private member variables, and javadoc.


Private Member Variables


  • Login to beowulf2.csc.smith.edu, grendel.csc.smith.edu, or use one of the Linux Mint machines.
  • Create a new program called Animal1.java containing the code below:


public class Animal {
        boolean isVaccinated;
        boolean isTattooed;
        String name;
        int age;
        
        Animal( String n, int a, boolean v, boolean t ) {
                name             = n;
                age              = a;
                isVaccinated = v;
                isTattooed   = t;
        }
        
        public void displayBasicInfo( ) {
                String v = "vaccinated";
                if ( !isVaccinated ) v = "not " + v;
                String t = "tattooed";
                if ( !isTattooed ) t = "not " + t;
                System.out.print( String.format( "%s (%d), %s, %s", 
                                name, age, t, v ) );
        }
        
        public static void main(String[] args) {
                Animal a = new Animal( "Max", 3, false, true );
                a.displayBasicInfo();
                System.out.println();
        }

}