Geoff Holmes Date Math Weighted Distr Strings String methods Tokenizers System Examples Utility Classes (Chapter 17) import java.util.*;

Slides:



Advertisements
Similar presentations
1 StringBuffer & StringTokenizer Classes Chapter 5 - Other String Classes.
Advertisements

COP 2800 Lake Sumter State College Mark Wilson, Instructor.
String Pemrograman Berbasis Obyek Oleh Tita Karlita.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Working with String Duo Wei CS110A_ Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Chapter 5 Functional Methods. © Daly and Wrigley Learning Java through Alice Objectives Properly construct and use methods when programming. Describe.
Java Programming Strings Chapter 7.
Chapter 10 Review. Write a method that returns true is s1 and s2 end with the same character; otherwise return false. Sample Answer: public boolean lastChar(String.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text I/O.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
1 String Buffer & String Tokenizer Overview l Overview of String Buffer class and Methods l Overview of String Tokenizer class and methods l Preview: Notions.
String Tokenization What is String Tokenization?
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
StringBuffer class  Alternative to String class  Can be used wherever a string is used  More flexible than String  Has three constructors and more.
Fundamental Programming Structures in Java: Strings.
1 StringTokenizer and StringBuffer classes Overview l StringTokenizer class l Some StringTokenizer methods l StringTokenizer examples l StringBuffer class.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
 Pearson Education, Inc. All rights reserved Strings, Characters and Regular Expressions.
Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
String Class. Objectives and Goals Identify 2 types of Strings: Literal & Symbolic. Learn about String constructors and commonly used methods Learn several.
Lecture 14 March 23, Exam Results Class Average was 69.4 –Median was 72.5 (which means there were some very low grades) Questions 31, 32, and 33.
The java.lang Package chapter 8 Java Certification Study Group February 2, 1998 Seth Ladd.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Computer Programming 2 Lecture 5: String Processing Part b: Class StringTokenizer Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;
Chapter 7: Characters, Strings, and the StringBuilder.
String Handling StringBuffer class character class StringTokenizer class.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
1 The String Class F Constructing a String: F Obtaining String length and Retrieving Individual Characters in a string F String Concatenation (concat)
Strings Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and.
1 CHAPTER 3 StringTokenizer. 2 StringTokenizer CLASS There are BufferedReader methods to read a line (i.e. a record) and a character, but not just a single.
Chapter 5 Programming with Objects and Classes OO Programming Concepts OO Programming Concepts Declaring and Creating Objects Declaring and Creating Objects.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
CIS Intro to JAVA Lecture Notes Set 5 26-May-05.
Object-Oriented Java Programming
Strings, Characters and Regular Expressions
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Strings.
EKT 472: Object Oriented Programming
String and String Buffers
Primitive Types Vs. Reference Types, Strings, Enumerations
String class in java Visit for more Learning Resources string.
Modern Programming Tools And Techniques-I Lecture 11: String Handling
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Part a: Fundamentals & Class String
Java – String Handling.
Lecture 07 String Jaeki Song.
CS2011 Introduction to Programming I Strings
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
Object-Oriented Java Programming
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

Geoff Holmes Date Math Weighted Distr Strings String methods Tokenizers System Examples Utility Classes (Chapter 17) import java.util.*;

Department of Computer Science2 Date Represents both times and dates class Date { public Date() // current time and date! public Date(int y, int m, int d, int h, int m, int s) public int getMonth(),.., getSeconds(), public int getDay() // day of the week public int getYear() // year – 1900 public long getTime() // milliseconds since epoch public void setMonth(int m),.., setSeconds(int s)

Department of Computer Science3 import java.util.*; public class DateTest { public static void main(String [] args) { Date s = new Date(); System.out.println("s toString = " + s.toString()); int j = 0; for (int i=0; i<100000; i++) j = j + i; Date e = new Date(); System.out.println("That took " + (e.getTime() - s.getTime())+ " msecs"); }

Department of Computer Science4 Math Supplies static constants and methods public static final double E // = public static final double PI // =  Trigonometric ops (double  double): sin, cos, tan, asin, acos, atan, atan2  Rounding ops: ceil, floor, rint, round  Exponentials: exp, pow, log, sqrt  Other: abs, max, min, random

Department of Computer Science5 Draw from weighted distribution static public int weightedDistribution (int[ ] weights) { int sum = 0; // sum of weights for(int i = 0; i < weights.length; i++) sum += weights[i]; int val = (int) Math.floor(Math.random()*sum+1); for(int i = 0; i < weights.length; i++) { val -= weights[i]; if (val < 0) return i; } return 0; // should never happen }  weights (1,3,2) will yield p(0)=1/6, p(1)=1/2, p(2)=1/3

Department of Computer Science6 String Immutable! i.e. cannot be changed String name = “John Smith”; char[] data = {‘q’,’e’,’d’}; String quod = new String(data); Concatenation: +, but be careful, groups from left: System.out.println(“Catch-” )  “Catch22” System.out.println( “warned”)  “4warned” System.out.println(“” “warned”)  “22warned” // trick: empty leading string

Department of Computer Science7 String methods Will return copies in case of modifications Constructors from Strings and StringsBuffer, char and byte arrays concat, replace (characters), (retrieve) substring, toLowerCase, toUpperCase, trim (whitespace), valueOf, compareTo, equalsIgnoreCase, endsWith, startsWith, indexOf, lastIndexOf

Department of Computer Science8 valueOf safer than toString public static String valueOf(Object o) { return (o == null) ? “null” : o.toString(); } Purely polymorphic and safe: Shape aShape = null; … String a = String.valueOf(aShape); // “null” String b = aShape.toString(); // nullPointerException

Department of Computer Science9 == on Strings String one = “One”; String two = new String(one); // copy of one String three = String.valueOf(one); // ref to one System.out.println((one == two)); // “false” System.out.println((one == three)); // “true”

Department of Computer Science10 StringBuffer More like strings in C (arrays of char), can be modified: StringBuffer strbuf = new StringBuffer(“hope”); strbuf.setCharAt(0,’c’); Constructors: StringBuffer(String initial), StringBuffer(int capacity) append, insert, and reverse modify buffer and return this thus allowing for cascaded calls: strbuf.append(“ with ”).append(“209”); setCharAt, charAt, length, setLength, ensureCapacity, toString

Department of Computer Science11 StringTokenizer Breaks a string into a sequence of tokens, tokens are defined by delimiters (e.g. space) Implements the Enumeration protocol public StringTokenizer(String s) public StringTokenizer(String s, String delims) public boolean hasMoreElements() public Object nextElement() public String nextToken() public int countTokens() // remaining tokens

Department of Computer Science12 StringTokenizer example public void readLines (DataInputStream input) throws IOException { String delims = “ \t\n.,!?;:”; for(int line = 1; true; line++) { String text = input.readLine(); if (text==null) return; text = text.toLowerCase(); StringTokenizer e = new StringTokenizer(text,delim); while( e.hasMoreElements()) }}

Department of Computer Science13 Parsing String Values For primitive data types wrapper classes provide parsing from strings and back: String dstr = “23.7”; Double dwrap = new Double(dstr); double dval = dwrap.doubleValue(); Instead of constructor: double dval = Double.parseDouble(“23.7”); enterWord(e.nextToken(), new Integer(line)); Similar for ints, booleans, longs, and floats

Department of Computer Science14 System Supplies system-wide resources:  Streams: System.in, System.out, System.err  System.exit(int) terminates a program SystemDemo.java

Department of Computer Science15 Examples  Write a program palindrome in two ways:  First, using StringBuffer (and reverse) public StringBuffer reverse( )  Second, using public char charAt(int index)  Eliza psychiatric help  Supply the name of a file as argument and count the number of lines, words and characters in the file (tips).