Difference between revisions of "CSC212 Homework 1 2014"

From dftwiki3
Jump to: navigation, search
(Problem #1)
(Problem #1)
Line 13: Line 13:
 
=Problem #1=
 
=Problem #1=
 
<br />
 
<br />
Write a java program called '''Hw1_1.java''' that displays the 8th row of [http://www.mathsisfun.com/pascals-triangle.html Pascal's Triangle].  Your program must use an array, and initialize it with 1 in the first cell, and 0 in all the other cells.  The program must loop in order to compute the 2nd row, the 3rd row, the 4th, etc., until the 8th row.
+
Write a java program called '''Hw1_1.java''' that displays the 8th row of [http://www.mathsisfun.com/pascals-triangle.html Pascal's Triangle].  Your program must use an array, and initialize it with 1 in the first cell, and 0 in all the other cells.  The program must loop in order to compute the 2nd row, the 3rd row, the 4th, etc., until the 8th row.  Although you can do the computation with just one array, you may use two if you find it easier to do so.
 
<br />
 
<br />
 
The output should be  
 
The output should be  

Revision as of 11:16, 11 September 2014

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





This homework is concurrent with Lecture 3 (Thur 9/11). It deals with arrays, classes, and inheritance. Problems must be submitted on Moodle before 9/18/14, 1:00 p.m.



Problem #1


Write a java program called Hw1_1.java that displays the 8th row of Pascal's Triangle. Your program must use an array, and initialize it with 1 in the first cell, and 0 in all the other cells. The program must loop in order to compute the 2nd row, the 3rd row, the 4th, etc., until the 8th row. Although you can do the computation with just one array, you may use two if you find it easier to do so.
The output should be

1
6
15 
20
15
6
1

with no extra blank lines before or after.

Problem #2


  • Read Galen Long's Introduction to Java's section on User Input. You will see that getting input from the keyboard is quite tedious in Java. The good thing, though, is that it's quite mechanical to do, so I trust you will figure it out. Galen's introduction shows how to read a string from the keyboard. To read an int is slightly different, though no more complicated, as illustrated below:


import java.util.Scanner;

public class UserInput {

	public UserInput() {
	}

	public static void main(String[] args) {
	Scanner userInput;
        userInput = new Scanner(System.in);

        System.out.print("Please type your name: "); 
        String userName = userInput.nextLine();

        
        System.out.print( "Please enter your age: " );
        int age = userInput.nextInt();
        
        System.out.print("Your name is: " + userName);
        System.out.println( " and you were born in " + (2014-age) + ", right?");
	}

}


Your Assignment


  • Write a program called Hw1_2.java that asks the user for an integer. This integer indicates which row of the Pascal Triangle we want to see. The program then displays this row, one number per line, then quits.
  • Your program must use an array for computing the row of the Pascal Triangle. Only one row need to be stored at a given time. You may want to use 2 arrays if this makes the computation easier, but one array is sufficient. You won't lose points for using 2 arrays.
  • Below are some interactions with the program (the program prints the ">" character as a prompt):
> 1
1

> 0

> -1

> 5
1
4
6
4 
1

> 100

  • Your program will only display a row for inputs that are between 1 and 10, included. For all other integers, your program should not output anything. It is not very friendly to do so, but it is robust!
  • Your program can always expect the user (Moodle in this case) to use integers. It will never be fed strings or doubles.


Problem #3


Using the example programs seen in class, create a Java program called Cat.java that will extend Animal.java (you cannot change the Animal class), and that will contain additional fields (member variables) as well as methods. You have to figure out what they are and how they work. The only two pieces of information you are given are the code for the main() method of the Cat class, and its output. You should not modify the main() method given to you. Your program has to use it as is.

Source Code


	public static void main(String[] args) {
		Cat cat1 = new Cat( "Minou", 1 /* age */, true /* vaccinated */, 
                                              false /* tattooed */ , true  /* stray cat */ );
		cat1.setLegs( true, true, true, true );
		cat1.displayBasicInfo();
		
		for ( int i=0; i<10; i++ ) {
			cat1.decrementLives();
			if ( i==5 ) cat1.changeStray( false );
			cat1.displayBasicInfo();
		}	
	}


Output


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), is stray, has 7 lives left, and is alive.
Minou (1), not tattooed, vaccinated, 0 bad leg(s), is stray, has 6 lives left, and is alive.
Minou (1), not tattooed, vaccinated, 0 bad leg(s), is stray, has 5 lives left, and is alive.
Minou (1), not tattooed, vaccinated, 0 bad leg(s), is stray, has 4 lives left, and is alive.
Minou (1), not tattooed, vaccinated, 0 bad leg(s), has an owner, has 3 lives left, and is alive.
Minou (1), not tattooed, vaccinated, 0 bad leg(s), has an owner, has 2 lives left, and is alive.
Minou (1), not tattooed, vaccinated, 0 bad leg(s), has an owner, has 1 life left, and is alive.
Minou (1), not tattooed, vaccinated, 0 bad leg(s), has an owner, is a dead cat.
Minou (1), not tattooed, vaccinated, 0 bad leg(s), has an owner, is a dead cat.


Note 1
In the program output, there are no spaces before the 'M' of Minou.
Note 2
the program prints "lives" or "life" according to the number of lives the cat still has.

Submission


Submit your Cat.java code to Moodle. Moodle already has the Animal.class.