Difference between revisions of "CSC212 Homework 1 2014"
(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 ...") |
(→Problem #2) |
||
Line 31: | Line 31: | ||
* Read [http://www.amoonki.com/introtojava/part_1/getting_input.html 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: | * Read [http://www.amoonki.com/introtojava/part_1/getting_input.html 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: | ||
<br /> | <br /> | ||
− | <source lang="java" highlight="1,8,9, | + | <source lang="java" highlight="1,8,9,13,17"> |
import java.util.Scanner; | import java.util.Scanner; | ||
Revision as of 10:20, 11 September 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, 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?");
}
}