1 / 12

Building Java Programs Chapter 3

Building Java Programs Chapter 3. The string class. The equals method. Strings and Objects are compared using a method named equals . Scanner console = new Scanner(System.in); System.out.print ("What is your name? "); String name = console.next (); if ( name.equals ("Barney ") ) {

dean-knight
Download Presentation

Building Java Programs Chapter 3

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. Building Java ProgramsChapter 3 The string class

  2. The equals method Strings and Objects are compared using a method named equals. Scanner console = new Scanner(System.in); System.out.print("What is your name? "); String name = console.next(); if (name.equals("Barney")) { System.out.println("I love you, you love me,"); System.out.println("We're a happy family!"); }

  3. Indexes Characters of a string are numbered with 0-based indexes: String name = "R. Kelly"; First character's index : 0 Last character's index : 1 less than the string's length The individual characters are values of type char (seen later)

  4. String methods These methods are called using the dot notation: String gangsta = "Dr. Dre"; System.out.println(gangsta.length()); // 7

  5. String method examples // index 012345678901 String s1 = "Stuart Reges"; String s2 = "Marty Stepp"; System.out.println(s1.length()); // 12 System.out.println(s1.indexOf("e")); // 8 System.out.println(s1.substring(7, 10)); // "Reg" String s3 = s2.substring(1, 7); System.out.println(s3.toLowerCase()); // "arty s" Given the following string: // index 0123456789012345678901 String book = "Building Java Programs"; How would you extract the word "Java" ?

  6. Modifying strings Methods like substring and toLowerCase build and return a new string, rather than modifying the current string. String s = "lil bow wow"; s.toUpperCase(); System.out.println(s); // lil bow wow To modify a variable's value, you must reassign it: String s = "lil bow wow"; s = s.toUpperCase(); System.out.println(s); // LIL BOW WOW An object that can never be modified after creation is called an immutable object. Strings are immutable.

  7. String test methods

  8. Try this Write a method that accepts a string parameter, and returns the string in reverse order. Example: reverse(“Good day”)should return “yaddooG“

  9. Scanner For most objects (including Scanner objects), we create a new instance with the new keyword: TypeNamemyInstance = newTypeName(any, parameters); For a Scanner, it looks like this: Scanner console = newScanner(System.in);

  10. A bit more magic: import There’s one other thing we have to do before we can start using our Scanner. We have to tell Java where it can find it! We do this with one more magic Java keyword, import, at the top of the Java source code file: importjava.util.*;

  11. Scanner importjava.util.*; public classMyInteractiveProgram { public static voidmain(String[]args) { Scanner console = newScanner(System.in); System.out.print("Type something: "); String word = console.next(); System.out.print("The first word was: " + word); } } What will this output? I don’t know! It depends on what you type at runtime!

  12. Let’s try it! Start Jgrasp and create a “GeometryHelper” project and class. Use Scanner’s nextDouble() method to ask for a radius, and then print the circumference, area, and volume for a circle and sphere with that radius: What is the radius? 3 A circle with radius 3.0 has circumference 18.8495559215 A circle with radius 3.0 has area 28.27433388230 A sphere with radius 3.0 has volume 113.0973355292

More Related