CSC212 Homework 1 Solution 2014

From dftwiki3
Revision as of 11:05, 20 September 2014 by Thiebaut (talk | contribs) (Problem 3)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut (talk) 10:50, 20 September 2014 (EDT)


Problem 2


import java.util.Scanner;

/**
 * displays the nth row of Pascal's triangle.
 */
public class Hw1_1 {

	/**
	 * This is the main entry point
	 * @param args the command line arguments.
	 */
	public static void main(String[] args) {
		int[] pascal = new int[10];
		Scanner userInput;
        	userInput = new Scanner(System.in);

	        // ask user for row number
	        System.out.println( "> " );
	        int n = userInput.nextInt();
        
	        // compute Row 1 to n, included, starting at the end of the row.
		for (int row = 1; row <= n; row++) {
			for (int i = pascal.length - 1; i >= 1; i--)
				pascal[i] = pascal[i - 1] + pascal[i];
			pascal[0] = 1;
		}

	        // display the nth row, one number per line.
		for (int i = 0; i < n; i++)
			System.out.println(pascal[i] + " ");
		System.out.println();
	}

}


Problem 3


/**
 * class that extends animal and defines a cat with 9 lives.
 *  
 * @author thiebaut
 *
 */
public class Cat extends Animal {

	private boolean isStray = false;
	private boolean[] legs;
	private int noLives = 0;
	
	/**
	 * constructs a cat with a name, age, vaccinated, tattooed and stray status
	 * @param n the name
	 * @param a the age
	 * @param vac whether vaccinated (true) or not (false)
	 * @param tat whether tattooed (true) or not (false)
	 * @param stra whether stray (true) or not (false)
	 */
	Cat(String n, int a, boolean vac, boolean tat, boolean stra) {
		super(n, a, vac, tat);
		isStray = stra;
		legs = new boolean[] { true, true, true, true };
		noLives = 9;
	}

	/**
	 * sets the validity of the legs.  True means a good leg.  False means broken or missing.  
	 * @param frontLeft just what the name means. True for good leg, false otherwise.
	 * @param frontRight 
	 * @param backLeft
	 * @param backRight
	 */
	public void setLegs(boolean frontLeft, boolean frontRight,
			boolean backLeft, boolean backRight) {
		legs[0] = frontLeft;
		legs[1] = frontRight;
		legs[2] = backLeft;
		legs[3] = backRight;
	}

	/**
	 * decrements number of lives by 1.
	 */
	public void decrementLives() {
		noLives--;
		if ( noLives < 0 ) noLives = 0;
	}

	public void changeStray(boolean b) {
		isStray = b;
	}

	private String strayStatus() {
		if ( isStray ) return "is stray";
		return "has an owner";
	}
	
	private String lifeStatus() {
		if ( noLives == 1 ) return "has " + noLives + " life left, and is alive.";
		if ( noLives <= 0 ) return "is a dead cat.";
		return "has " + noLives + " lives left, and is alive.";
	}
	/**
	 * displays cat information, nicely formatted:
	 * Examples:
	 * Minou (1), not tattooed, vaccinated, 0 bad leg(s), is stray, has 9 lives left, and is alive. 
	 * Minou (1), not tattooed, vaccinated, 0 bad leg(s), is stray, has 8 lives left, and is alive.
	 *  ...
	 * Minou (1), not tattooed, vaccinated, 0 bad leg(s), has an owner, is a dead cat. 
	 */
	 public void displayBasicInfo( ) {
               super.displayBasicInfo();
               int count=0;
               for ( int i=0; i<legs.length; i++ )
                     if ( ! legs[i] ) count++;
               System.out.print( ", " + count + " bad leg(s), " + strayStatus());
               System.out.println( ", " + lifeStatus() );        
	}
	 
	/**
	 * The main entry point.
	 * @param args the command line arguments.
	 */
	public static void main(String[] args) {
		
		// create a cat object 
		Cat cat1 = new Cat("Minou", 1 /* age */, true /* vaccinated */,
				false /* tattooed */, true /* stray cat */);
		
		// it has 4 good legs
		cat1.setLegs(true, true, true, true);
		cat1.displayBasicInfo();

		// slowly decrement its lives...
		for (int i = 0; i < 10; i++) {
			cat1.decrementLives();
			
			// after 5 iterations, somebody adopts it...
			if (i == 5)
				cat1.changeStray(false);
			cat1.displayBasicInfo();
		}
	}


}