Difference between revisions of "CSC212 Examples for Lecture 3"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- =Student Class= <br /> <source lang="java"> public class Student { String name = ""; int age = 0; static int MAX = 5; Student( String n, int a ) { name = n...")
 
(Student Class)
 
(One intermediate revision by the same user not shown)
Line 4: Line 4:
 
<br />
 
<br />
 
<source lang="java">
 
<source lang="java">
 +
/**
 +
* Student: A class implementing a student with a name and age.
 +
*/
 
public class Student {
 
public class Student {
 
String name = "";
 
String name = "";
 
int age = 0;
 
int age = 0;
static int MAX = 5;
+
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 ) {
 
Student( String n, int a ) {
 
name = n;
 
name = n;
Line 14: Line 22:
 
}
 
}
 
 
 +
/**
 +
* displays the name and age nicely formatted.
 +
*/
 
public void display() {
 
public void display() {
 
System.out.println(  
 
System.out.println(  
Line 20: Line 31:
 
}
 
}
 
 
 +
/**
 +
* main entry point.
 +
* @param args
 +
*/
 
public static void main(String[] args) {
 
public static void main(String[] args) {
 +
//--- array of students ---
 
Student[] students = new Student[MAX];
 
Student[] students = new Student[MAX];
 +
 +
//--- Create MAX students and store in array students ---
 
int i;
 
int i;
 
char n;
 
char n;
Line 27: Line 45:
 
students[i] = new Student( "" + n, i+20 ); // trick: string + char --> string
 
students[i] = new Student( "" + n, i+20 ); // trick: string + char --> string
 
}
 
}
for ( i=0; i<MAX; i++ ) {
+
 +
//--- display all students ---
 +
for ( i=0; i<MAX; i++ )  
 
students[i].display();
 
students[i].display();
 +
}
 +
 +
}
 +
 +
</source>
 +
<br />
 +
=Animal Class=
 +
<br />
 +
<source lang="java">
 +
 +
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();
 +
}
 +
 +
}
 +
 +
</source>
 +
<br />
 +
=Bird Class=
 +
<br />
 +
<source lang="java">
 +
 +
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();
 
}
 
}
 +
}
 +
}
 +
 +
</source>
 +
<br />
 +
=Dog Class=
 +
<br />
 +
<source lang="java">
 +
 +
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();
 
}
 
}
  
 
}
 
}
 +
 
</source>
 
</source>
 +
<br />

Latest revision as of 12:14, 10 September 2014

--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();
	}

}