CSC212 Homework 1 2014
--D. Thiebaut (talk) 11:20, 11 September 2014 (EDT)
Contents
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.
- 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, the program does 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.