1 / 16

Introduction to Java

Introduction to Java. The String class Input and Output. Classes. A class is a type used to produce objects. An object is an entity that stores data and can take actions defined by methods . For example: An object of the String class stores data consisting of a sequence of characters.

rosie
Download Presentation

Introduction to Java

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. Introduction to Java The String class Input and Output

  2. Classes • A classis a type used to produce objects. • An object is an entity that stores data and can take actions defined by methods. • For example: • An object of the String class stores data consisting of a sequence of characters. • example: length() method returns the number of characters in a particular String object. String solution = ″ten″ int howMany = solution.length()

  3. Objects, Methods, and Data • Objects within a class • have the same methods • have the same kind(s) of data but the data can have different values. • Remember, primitive types have values, but no methods.

  4. Some String Methods • s.length() • s.equals(s1) • s.toLowerCase • s.trim() • s.charAt(n) • s.substring(start,end) • s.indexOf(substring, start) • s.compareTo(s1) • (+ if s > s1; if s1 > s; else 0) • see book p. 80-81

  5. String Objects vs. Variables • No methods allow you to change the value of a String object. • But you can change the value of a String variable. value ofpause String pause = “ Hmm“; Hmm pause = pause.trim() Hmm pause = pause + “mmm!”; Hmmmmm pause = “Ahhh”; Ahhh

  6. Escape Characters • How would you print “Java” refers to a language.? • The compiler needs to be told that the quotation marks (“) do not signal the start or end of a string, but instead are to be printed. System.out.println( “\”Java\” refers to a language.”);

  7. Escape Characters • Each escape sequence is a single character even though it is written with two symbols.

  8. Keyboard and ScreenI/O: Outline The two issues are • Screen Output – how to write to the screen • Keyboard Input – how to read from the keyboard.

  9. Screen Output • System.out is an object that is part of Java. • println() is one of the methods available to the System.out object. System.out.println(“Hi!“);

  10. print and println methods • Alternatively, use print() System.out.print(“When everything “); System.out.print(“does not fit on “); System.out.print(“one line, use the “); System.out.print(“\”print\” ”); System.out.println(“statement”); ending with aprintln(). • System.out.println(“c” + “o” + “c” +”a” );

  11. Keyboard Input • Java 5.0 keyboard input facilities are provided by the Scanner class • The Scanner class is stored in the java.util package. • remember: A package is a library of classes.

  12. Using the Scanner Class • Near the beginning of your program, insert import java.util.* • Create an object of the Scanner class Scanner keyboard = new Scanner (System.in) • Read data (an int or a double, for example) int n1 = keyboard.nextInt(); double d1 = keyboard,nextDouble();

  13. Some Scanner Class Methods • syntax Int_Variable = Object_Name.nextInt(); Double_Variable = Object_Name.nextDouble(); String_Variable = Object_Name.next(); String_Variable = Object_Name.nextLine(); • see also p 93

  14. nextLine()Method Use with Caution • The nextLine() method reads the remainder of the current line, even if it is empty. n = scanner.nextInt() s1 = scanner.nextLine() s2 = scanner.nextLine() • 42 is the answerand don’t youforget it • 42and don’t youforget it

  15. The Empty String • A string can have any number of characters, including zero. • The string with zero characters is called the empty string. • The empty string is useful and can be created in many ways including String s3 = “”; • class DelimitersDemo

  16. Documentation and Style: Outline Points • Use Meaningful Names • Self-Documentation and Comments // this line is a comment /* this line and this line are comments*/ • Use Indentation • Use Named Constantspublic static final PI = 3.142

More Related