CSC212 Examples for Lecture 3
--D. Thiebaut (talk) 11:14, 10 September 2014 (EDT)
Student Class
/**
* Student: A class implementing a student with a name and age.
*/
public class Student {
String name = "";
int age = 0;
static int MAX = 5; // max number of students in array
/**
* Constructor: initializes both the name and age
* @param n: the name
* @param a: the age
*/
Student( String n, int a ) {
name = n;
age = a;
}
/**
* displays the name and age nicely formatted.
*/
public void display() {
System.out.println(
String.format( "Student: %5s: %d years old",
name, age ) );
}
/**
* main entry point.
* @param args
*/
public static void main(String[] args) {
//--- array of students ---
Student[] students = new Student[MAX];
//--- Create MAX students and store in array students ---
int i;
char n;
for ( i=0, n='a'; i<MAX; n++, i++ ) {
students[i] = new Student( "" + n, i+20 ); // trick: string + char --> string
}
//--- display all students ---
for ( i=0; i<MAX; i++ )
students[i].display();
}
}
Animal Class
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.print( 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();
System.out.println();
}
}
Bird Class
public class Bird extends Animal{
int wingSpan;
Bird(String n, int a, boolean v, boolean t, int w) {
super(n, a, v, t);
wingSpan = w;
}
public void displayBasicInfo() {
super.displayBasicInfo();
System.out.print( String.format( ", wing-span: %d", wingSpan ) );
}
public static void main( String[] args ) {
Bird[] birds = new Bird[2];
birds[0] = new Bird( "Toto", 23, true, true, 34 );
birds[1] = new Bird( "Coco", 17, true, false, 29 );
for ( int i=0; i<birds.length; i++ ) {
birds[i].displayBasicInfo();
System.out.println();
}
}
}
Dog Class
public class Dog extends Animal {
boolean[] legs = new boolean[4];
public Dog(String n, int a, boolean v, boolean t) {
super(n, a, v, t);
for (int i=0; i<4; i++ ) legs[i] = true;
}
public void setLegs( boolean frontLeft, boolean frontRight,
boolean backLeft, boolean backRight ) {
legs[0] = frontLeft;
legs[1] = frontRight;
legs[2] = backLeft;
legs[3] = backRight;
}
public void setLegs( boolean[] l ) {
legs = l;
}
public void displayBasicInfo( ) {
super.displayBasicInfo();
int count=0;
for ( int i=0; i<legs.length; i++ )
if ( ! legs[i] ) count++;
System.out.println( ", " + count + " bad leg(s)" );
}
public static void main(String[] args) {
Dog d1 = new Dog( "Rintintin", 34, true, false );
d1.setLegs( true, true, false, false );
d1.displayBasicInfo();
Dog d2 = new Dog( "Rex", 4, true, true );
d2.setLegs( new boolean[] {true, true, true, false} );
d2.displayBasicInfo();
}
}