1 / 11

(Dreaded) Quiz 2

(Dreaded) Quiz 2. Next Monday. Strings Revisited. String Is a Type (Object) in JAVA Automatically imported in java.lang May Initialize or Assign with Double Quoted Values Can Add String to String with ‘+’ Operator CANNOT Compare using Equality Operators (uses .equals() method instead)

arnon
Download Presentation

(Dreaded) Quiz 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. (Dreaded) Quiz 2 • Next Monday

  2. Strings Revisited • String Is a Type (Object) in JAVA • Automatically imported in java.lang • May Initialize or Assign with Double Quoted Values • Can Add String to String with ‘+’ Operator • CANNOT Compare using Equality Operators (uses .equals() method instead) • Methods Available to • Access single characters • Get length of String • Find a character or substring in String • Convert to lower or upper case

  3. Objects • Objects: Types of Variables Made from Built-In Types • Variables Can be Declared as an Object Type • Objects Usually Have Methods/Functions Tied to Them • Member functions • Public functions • Interface functions • Methods • String Is an Example of an Object Type • StringVariable.length() Returns the Number of Characters in the String

  4. Methods • Also called Procedures, Functions • Have Parenthesis (e.g., method() ) • May be Passed Arguments, or Parameters Inside of Parenthesis • Parameters are values to initialize new variables in method • Parameters used to pass information to method

  5. String Methods • charAt(int index): return char at named index (starting at 0) • equals(String str): return boolean • indexOf(char): return int of first occurrence of char (index starts at 0) • indexOf(String substr): return int of first occurrence of String substr • length(): return int length of String • substring(int start, int end): return String between start and end • toLowerCase(), toUpperCase(): convert String

  6. String myString = "My Dog Sandy",compareString; • System.out.println("The fourth character is: " + myString.charAt(3)); • if (myString.equals("my dog sandy")) • System.out.println("Strings are equal"); • else • System.out.println("Strings are not equal"); • compareString = myString + myString; • System.out.println("Added strings are: " + compareString); • Output: • The fourth character is: D • Strings are not equal • Added strings are: My Dog SandyMy Dog Sandy

  7. compareString = myString.toLowerCase(); System.out.println("String is: " + compareString); System.out.println("Char S is at location: " + myString.indexOf('S')); System.out.println("substring og is at location: " + myString.indexOf("og")); System.out.println("Length of string is: " + myString.length()); System.out.println("substring between location 3 and 5 is: " + myString.substring(3,5)); Output: String is: my dog sandy Char S is at location: 7 Substring og is at location: 4 Length of string is: 12 Substring between location 3 and 5 is: Do

  8. import java.util.Scanner; public class MyProgram { public static void main( String [ ] args) { Scanner scan = new Scanner(System.in); String lastname; boolean instructor = false; System.out.print(“Enter last name:”); lastname = scan.next(); if(lastname.equals(“hanrath”)) instructor = true; // privileged else instructor = false; // not privileged } }

  9. More on Input • Input Failure Common • *MUST* Be Part of Test Plan • Pages 310-313 in Anderson • Check Input for Type Desired • If not, Notify User, and Get Rid of Line • Use scan.hasNextInt(), scan.hasNextDouble(), scan.hasNextFloat()

  10. Input Failure Example import java.util.*; class InputFailureExample { public static void main(String[] args) { int numE; String garbage; Scanner scan = new Scanner(System.in); System.out.print("Enter Integer: "); while (!scan.hasNextInt()) { garbage = scan.nextLine(); System.out.println("Please try again."); System.out.print("Enter Integer: "); } numE = scan.nextInt(); System.out.println("You entered: " + numE); } }

  11. Know for Quiz 2 • Everything through Exam 1 • Loops: while, for, do-while • switch-case Statement • String Methods • Input Failure Methods

More Related