Difference between revisions of "CSC212 Lab 4 2014"
(→Private Member Variables) |
(→Private Member Variables) |
||
Line 15: | Line 15: | ||
* Create a new program called '''Animal1.java''' containing the code below: | * Create a new program called '''Animal1.java''' containing the code below: | ||
<br /> | <br /> | ||
− | <source lang="java"> | + | ::<source lang="java"> |
public class Animal1 { | public class Animal1 { | ||
boolean isVaccinated; | boolean isVaccinated; | ||
Line 43: | Line 43: | ||
* In the same directory, create a second file called '''TestAnimal1.java''', which contains this code: | * In the same directory, create a second file called '''TestAnimal1.java''', which contains this code: | ||
<br /> | <br /> | ||
− | <source lang="java"> | + | ::<source lang="java"> |
class TestAnimal1 { | class TestAnimal1 { | ||
Line 51: | Line 51: | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
// create a new animal | // create a new animal | ||
− | + | Animal1 a = new Animal1( "Max", 3, false, true ); | |
a.displayBasicInfo(); | a.displayBasicInfo(); | ||
Line 61: | Line 61: | ||
// modify it some more, then display it. | // modify it some more, then display it. | ||
− | a.isTattooed = | + | a.isTattooed = false; |
a.age = a.age + 1; | a.age = a.age + 1; | ||
a.displayBasicInfo(); | a.displayBasicInfo(); | ||
Line 68: | Line 68: | ||
</source> | </source> | ||
<br /> | <br /> | ||
+ | * Compile and run both: | ||
+ | |||
+ | javac Animal1.java TestAnimal1.java | ||
+ | java TestAnimal1 | ||
+ | |||
+ | * Verify that you get the following output: | ||
+ | |||
+ | Max (3), tattooed, not vaccinated | ||
+ | Max (3), tattooed, not vaccinated | ||
+ | Max (5), not tattooed, vaccinated | ||
+ | Max (6), tattooed, vaccinated | ||
+ | |||
+ | <br /> | ||
+ | ==Modification== | ||
+ | <br /> | ||
+ | * Modify the '''Animal1.java''' program and make all its member variables private. | ||
+ | <br /> | ||
+ | ::<source lang="java"> | ||
+ | private boolean isVaccinated; | ||
+ | private boolean isTattooed; | ||
+ | private String name; | ||
+ | private int age; | ||
+ | </source> | ||
+ | <br /> | ||
+ | * Recompile both java programs. Notice that, now, '''TestAnimal1.java''' generates many errors. Why? | ||
+ | * Add ''mutator'' and ''inspector'' methods to '''Animal1.java''' so that its member variables can be accessed by '''TestAnimal1.java'''. Here is an example of a mutator and inspector for '''age''': | ||
+ | <br /> | ||
+ | ::<source lang="java"> | ||
+ | public void setAge( int n ) { | ||
+ | age = n; | ||
+ | } | ||
+ | |||
+ | public int getAge( ) { | ||
+ | return age; | ||
+ | } | ||
+ | </source> |
Revision as of 20:33, 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.displayBasicInfo(); a.isVaccinated = true; a.age = 5; a.displayBasicInfo(); // modify it some more, then display it. a.isTattooed = false; 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 (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?
- Add mutator and inspector methods to Animal1.java so that its member variables can be accessed by TestAnimal1.java. Here is an example of a mutator and inspector for age:
public void setAge( int n ) { age = n; } public int getAge( ) { return age; }