CSC212 Lab 4 2014

From dftwiki3
Revision as of 20:23, 17 September 2014 by Thiebaut (talk | contribs) (Private Member Variables)
Jump to: navigation, search

--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 Animal1 {
        boolean isVaccinated;
        boolean isTattooed;
        String name;
        int age;
        
        Animal1( 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 ) );
        }
        
}


  • In the same directory, create a second file called TestAnimal1.java, which contains this code:


class TestAnimal1 {

   TestAnimal1() { 
   }

   public static void main(String[] args) {
           // create a new animal
           Animal a = new Animal1( "Max", 3, false, true );
           a.displayBasicInfo();

           // modify it.  Then display it.
           a.displayBasicInfo();
           a.isVaccinated = true;
           a.age = 5;
           a.displayBasicInfo();

           // modify it some more, then display it.
           a.isTattooed = true;
           a.age = a.age + 1;
           a.displayBasicInfo();
   }
}