Difference between revisions of "CSC212 Lab 4 2014"
(→Private Member Variables) |
|||
Line 16: | Line 16: | ||
<br /> | <br /> | ||
<source lang="java"> | <source lang="java"> | ||
− | public class | + | public class Animal1 { |
boolean isVaccinated; | boolean isVaccinated; | ||
boolean isTattooed; | boolean isTattooed; | ||
Line 22: | Line 22: | ||
int age; | int age; | ||
− | + | Animal1( String n, int a, boolean v, boolean t ) { | |
name = n; | name = n; | ||
age = a; | age = a; | ||
Line 38: | Line 38: | ||
} | } | ||
− | + | } | |
− | + | </source> | |
− | + | <br /> | |
− | + | * In the same directory, create a second file called '''TestAnimal1.java''', which contains this code: | |
− | + | <br /> | |
− | + | <source lang="java"> | |
− | + | 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(); | ||
+ | } | ||
} | } | ||
</source> | </source> | ||
+ | <br /> |
Revision as of 20:23, 17 September 2014
--D. Thiebaut (talk) 21:12, 17 September 2014 (EDT)
Contents
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();
}
}