Difference between revisions of "CSC212 Lab 4 2014"

From dftwiki3
Jump to: navigation, search
(Private Member Variables)
Line 34: Line 34:
 
                 String t = "tattooed";
 
                 String t = "tattooed";
 
                 if ( !isTattooed ) t = "not " + t;
 
                 if ( !isTattooed ) t = "not " + t;
                 System.out.print( String.format( "%s (%d), %s, %s",  
+
                 System.out.println( String.format( "%s (%d), %s, %s",  
 
                                 name, age, t, v ) );
 
                                 name, age, t, v ) );
 
         }
 
         }
Line 41: Line 41:
 
                 Animal a = new Animal( "Max", 3, false, true );
 
                 Animal a = new Animal( "Max", 3, false, true );
 
                 a.displayBasicInfo();
 
                 a.displayBasicInfo();
                 System.out.println();
+
                 a.v = true;
 +
                a.age = 5;
 +
                a.displayBasicInfo();
 
         }
 
         }
  
 
}
 
}
 
</source>
 
</source>

Revision as of 20:15, 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.println( 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();
                a.v = true;
                a.age = 5;
                a.displayBasicInfo();
        }

}