160 likes | 267 Views
CSCI S-1 Section 5. Deadlines for Problem Set 3. Part A Friday, July 10, 17:00 EST Parts B Tuesday, July 14, 17:00 EST Getting the code examples from lecture cp ~libs1/pub/ src /lectures/ leitner /July6*.java . The Scanner Class. A java library to take user input
E N D
Deadlines for Problem Set 3 • Part A • Friday, July 10, 17:00 EST • Parts B • Tuesday, July 14, 17:00 EST • Getting the code examples from lecture • cp ~libs1/pub/src/lectures/leitner/July6*.java .
The Scanner Class • A java library to take user input • Not included automatically • Unlike lang.System, for System.out.print() • Need to tell the compiler where the library is • So IMPORT the library at the very top of the program file • Import java.util.scanner; // includes definition for scanner only • Import java.util.* // includes all definitions in util library
Declaring Scanner Objects • Scanner scan = new Scanner(System.in); • “new” creates an instance (or “object”) of the java class Scanner • Now we can use predefined methods from the scanner class • Scan.nextInt(); // get an int • Scan.nextDouble(); // get a double • Scan.next() or scan.nextLine(); // get a string • Scan.nextShort(); // get a short int • Scan.nextByte(); // get a byte
Example 1 - Nint // take an int from user and print it back out Class Nint { public static void main (String[] args) { } }
Example 1 - Nint // take an int from user and print it back out import java.util.*; Class Nint { public static void main (String[] args) { System.out.print(“Please enter a number: ”) Scanner scan= new Scanner(System.in); int n= scan.nextInt(); System.out.printl(“You have entered the number ” + N + “.”); } }
Example 2 - Celsius // take a double (representing Celsius) and change it to a double for Fahrenheit Class Celsius { public static void main (String[] args) { } }
Example 2 - Celsius // take a double (representing Celsius) and change it to a double for Fahrenheit import java.util.*; Class Celsius { public static void main (String[] args) { System.out.print(“Please enter the temperature in Celsius outside:“ ) Scanner scan= new Scanner(System.in); double n= scan.nextDouble(); System.out.printl(“You current temperature in Fahrenheit is “ + (n*(9.0/5) + 32) + “.”); } }
Example 3 - Born // Take a String and print it back out class Born { public static void main(String[] args) { } }
Example 3 - Born // Take a String and print it back out import java.util.*; class Born { public static void main(String[] args) { System.out.print("Where were you born? "); Scanner scan = new Scanner(System.in); String s = scan.nextLine(); System.out.println("You were born in " + s + "! Wow, so was I!"); } }
Example 4 - Bad // Don’t try this at home class Bad { public static void main(String[] args) { System.out.print("I need some vital input here: "); Scanner scan = new Scanner(System.in); String n = scan.nextLine(); System.out.println("Wow, " + n + " was some vital input!"); } }
Conditional Statements • Use if-else conditions to create ‘branches’. Conditions evaluate to true or false. int x=3; if (x < 5) System.out.println("This is True"); if (x >= 5) System.out.println("This is False"); // alternatively if (x < 5) System.out.println("This is True"); else System.out.println("This is False");
Example 5 – Conditions Inside Loops // print odd digits up to N, as entered by user. print * for even digits class Condition1 { public static void main(String[] args) { } }
Example 5 – Conditions Inside Loops // print odd digits up to N, as entered by user. Print * for even digits import java.util.*; class Condition1 { public static void main(String[] args) { System.out.print("Enter a number. I am going to count odds: "); Scanner scan = new Scanner(System.in); int stop = scan.nextInt(); for (inti = 1; i <= stop; i++) if (i % 2 == 1) System.out.println(i); else System.out.println("*"); } }
Example 6 – Smallest // picks out the smallest number of ten numbers entered (all ints > 0) class Smallest { public static void main(String[] args) { } }
Example 6 – Smallest // picks out the smallest number of ten numbers entered (all ints > 0) import java.util.*; class Smallest { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int smallest = 0; for (inti = 1; i <= 10; i++) { System.out.println("Enter a number greater than 0: "); int n = scan.nextInt(); if (n < smallest || smallest == 0) smallest = n; } System.out.println("The smallest number was " + smallest + "."); } }