Difference between revisions of "CSC212 Homework 1 2014"

From dftwiki3
Jump to: navigation, search
(Problem #3)
(Problem #3)
Line 85: Line 85:
 
=Problem #3=
 
=Problem #3=
 
<br />
 
<br />
Using the [[CSC212_Examples_for_Lecture_3|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.  You are given the ''main()'' method of the Cat class, and its output.  See below.
+
Using the [[CSC212_Examples_for_Lecture_3|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 youYour program has to use it as is.
 
<br />
 
<br />
 
===Source Code===
 
===Source Code===
Line 120: Line 120:
  
 
<br />
 
<br />
 +
===Submission===
 
<br />
 
<br />
 +
Submit your Cat.java code to Moodle.  Moodle already has the Animal.class.
 
<br />
 
<br />
 
<br />
 
<br />

Revision as of 10:58, 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.
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, true, false, true );
		cat1.setLegs( true, true, true, true );
		cat1.displayBasicInfo();
		
		for ( int i=0; i<10; i++ ) {
			cat1.decrementLives();
			cat1.displayBasicInfo();
		}
		
	}


Output


Minou (1), not tattooed, vaccinated, 0 bad leg(s), has 9 lives left, and and is alive.
Minou (1), not tattooed, vaccinated, 0 bad leg(s), has 8 lives left, and and is alive.
Minou (1), not tattooed, vaccinated, 0 bad leg(s), has 7 lives left, and and is alive.
Minou (1), not tattooed, vaccinated, 0 bad leg(s), has 6 lives left, and and is alive.
Minou (1), not tattooed, vaccinated, 0 bad leg(s), has 5 lives left, and and is alive.
Minou (1), not tattooed, vaccinated, 0 bad leg(s), has 4 lives left, and and is alive.
Minou (1), not tattooed, vaccinated, 0 bad leg(s), has 3 lives left, and and is alive.
Minou (1), not tattooed, vaccinated, 0 bad leg(s), has 2 lives left, and and is alive.
Minou (1), not tattooed, vaccinated, 0 bad leg(s), has 1 lives left, and and is alive.
Minou (1), not tattooed, vaccinated, 0 bad leg(s), is a dead cat.
Minou (1), not tattooed, vaccinated, 0 bad leg(s), is a dead cat.


Submission


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