Difference between revisions of "CSC212 Lab 4 2014"
(→Private Member Variables) |
(→Javadoc) |
||
Line 128: | Line 128: | ||
<br /> | <br /> | ||
=Javadoc= | =Javadoc= | ||
+ | <br /> | ||
+ | * Locate your old '''Animal.java''' class from a previous lab, or copy/paste it from [[CSC212_Examples_for_Lecture_3 | here]]. | ||
+ | * Don't worry if the member variables are not private. In this section we're interested in generating the '''javadoc''' for this program. | ||
+ | * Add documentation to your program using the same format as shown below. While it might be tempting to copy/paste it, | ||
+ | please type it. It's the best way to remember how the javadoc syntax works: | ||
+ | <br /> | ||
+ | ::<source lang="java"> | ||
+ | /** | ||
+ | * implements a base class for an animal. | ||
+ | * @author thiebaut | ||
+ | * | ||
+ | */ | ||
+ | public class Animal { | ||
+ | |||
+ | private boolean isVaccinated; // boolean holding the property for vaccination | ||
+ | private boolean isTattooed; // boolean holding the property for tattoo | ||
+ | private String name; // contains the animal's name | ||
+ | private int age; // equal to the animal's age | ||
+ | |||
+ | /** | ||
+ | * constructor | ||
+ | * @param n the name of the animal | ||
+ | * @param a the age of the animal | ||
+ | * @param v true if animal is vaccinated, false otherwise | ||
+ | * @param t true if animal is tattooed, false otherwise | ||
+ | */ | ||
+ | Animal( String n, int a, boolean v, boolean t ) { | ||
+ | name = n; | ||
+ | age = a; | ||
+ | isVaccinated = v; | ||
+ | isTattooed = t; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * displays all the information about the animal. | ||
+ | * <P> | ||
+ | * Format:<br /> | ||
+ | * Rex (3), tattoed, not vaccinated | ||
+ | */ | ||
+ | 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 ) ); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Main entry point. | ||
+ | * @param args | ||
+ | */ | ||
+ | public static void main(String[] args) { | ||
+ | Animal a = new Animal( "Max", 3, false, true ); | ||
+ | a.displayBasicInfo(); | ||
+ | System.out.println(); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </source> | ||
<br /> | <br /> |
Revision as of 21:01, 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 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 Animal1 a = new Animal1( "Max", 3, false, true ); a.displayBasicInfo(); // modify it. Then display it. a.isVaccinated = true; a.isTattooed = false; a.age = 5; a.displayBasicInfo(); // modify it some more, then display it. a.isTattooed = ! a.isTattooed; a.age = a.age + 1; a.displayBasicInfo(); } }
- Compile and run both:
javac Animal1.java TestAnimal1.java java TestAnimal1
- Verify that you get the following output:
Max (3), tattooed, not vaccinated Max (5), not tattooed, vaccinated Max (6), tattooed, vaccinated
Modification
- Modify the Animal1.java program and make all its member variables private.
private boolean isVaccinated; private boolean isTattooed; private String name; private int age;
- Recompile both java programs. Notice that, now, TestAnimal1.java generates many errors. Why?
- If you answer that it is because TestAnimal1 cannot access the member variables of a any longer, you are right!
- Modify both Animal1 and TestAnimal1 so that
- Animal1 will now have mutator and inspector methods to access its member functions, and
- TestAnimal1 will access the member variables of a by using the new accessors and mutators
- Here is an example of a modification you can make on Animal1.java
// mutator public void setAge( int n ) { age = n; } // ispector public int getAge( ) { return age; }
- And here is an example of a modification you can make on TestAnimal1.java:
a.setAge( 5 );
- When you have been able to get rid of all the compiler errors for TestAnimal1.java, run the program TestAnimal1 and verify that you get the same output as before:
Max (3), tattooed, not vaccinated Max (5), not tattooed, vaccinated Max (6), tattooed, vaccinated
Javadoc
- Locate your old Animal.java class from a previous lab, or copy/paste it from here.
- Don't worry if the member variables are not private. In this section we're interested in generating the javadoc for this program.
- Add documentation to your program using the same format as shown below. While it might be tempting to copy/paste it,
please type it. It's the best way to remember how the javadoc syntax works:
/** * implements a base class for an animal. * @author thiebaut * */ public class Animal { private boolean isVaccinated; // boolean holding the property for vaccination private boolean isTattooed; // boolean holding the property for tattoo private String name; // contains the animal's name private int age; // equal to the animal's age /** * constructor * @param n the name of the animal * @param a the age of the animal * @param v true if animal is vaccinated, false otherwise * @param t true if animal is tattooed, false otherwise */ Animal( String n, int a, boolean v, boolean t ) { name = n; age = a; isVaccinated = v; isTattooed = t; } /** * displays all the information about the animal. * <P> * Format:<br /> * Rex (3), tattoed, not vaccinated */ 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 ) ); } /** * Main entry point. * @param args */ public static void main(String[] args) { Animal a = new Animal( "Max", 3, false, true ); a.displayBasicInfo(); System.out.println(); } }