Class Notes (809,444)
COMP 1405 (38)
Lecture

# comp1005/1405_c2_14092010.pdf

6 Pages
173 Views

School
Carleton University
Department
Computer Science
Course
COMP 1405
Professor
Christine Laurendeau
Semester
Fall

Description
Lecture 2 Program 1 //This program will ask you to input String and Int Data //Display the Data in the output window import java.util.Scanner; //import Scanner() function from the java library class Ch1_4ScannerInput { public static void main (String args[]) { System.out.println("What is your name?"); //prints out the String "What is your name?" System.out.println("Hello, " + new Scanner(System.in).nextLine()); //Prints out "Hello, " + your name in String. //Since it does not know your name in String yet, before printing //it's going to ask you to type an String input System.out.println("What is your age?"); //prints out the String "What is your age?" System.out.println("You're, " + new Scanner(System.in).nextInt() + " years old"); //Prints out "You're " + your age in int + " years old". //Since it does not know your age in int yet, before printing //it's going to ask you to type an int input } } /* What's the difference between Scanner(System.in).nextLine() * and Scanner(System.in).nextInt()? * * Scanner(System.in).nextLine() is for String inputs and * Scanner(System.in).nextInt() is for int inputs. * * If an int was typed into the Scanner(System.in).nextLine(), it will * automatically be converted to String. As in result you will not be able * to use it for calculation (unless if you convert it back first) * * If an String was typed into the Scanner(System.in).nextInt(), you * will get an compile error. (java.util.InputMismatchException) * * */ Lecture 2 Program 2 //This program will take 3 of your (int) inputs //Display the average the input in the output window import java.util.Scanner; //imports the Scanner() function from the java library class Ch1_5Calculator { public static void main (String args[]) { System.out.println("Enter three numbers:"); //prints enter 3 numbers System.out.println("The average of these 3 numbers is: " + new Scanner(System.in).nextInt() + new Scanner(System.in).nextInt() + new Scanner(System.in).nextInt() / 3.0); // 1. Gets the input of your 3 numbers // 2. Takes the total of the 3 numbers and divide by 3 // 3. prints out "The average of these 3 numbers is: " + the average } } Lecture 2 Program 3 //This program will display your input using Graphic User Interference Window import javax.swing.JOptionPane; //Import javax.swing.JOptionPane from the java library //Used for the GUI windows class Ch1_6GUIInput { public static void main (String args[]) { System.out.println("Hello, " + JOptionPane.showInputDialog("What is your name?")); /* The JOptionPane.showInputDialog() function will display * a message and get your input in a GUI window. * * The Program will print "Hello, " + your name, but first * It needs to get your name. * * - JOptionPane.showInputDialog(message) * the message section is where you usually store your message *
More Less

Related notes for COMP 1405

OR

Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Join to view

OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.