CSC212 Homework 1 2014

From dftwiki3
Revision as of 10:20, 11 September 2014 by Thiebaut (talk | contribs) (Created page with "--~~~~ ---- <br /> __TOC__ <br /> <br /> <bluebox> This homework is concurrent with Lecture 3 (Thur 9/11). It deals with arrays, classes, and inheritance. Problems must be ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--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, 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?");
	}

}