Lecture 221 CS110 Lecture 22 Tuesday, April 20, 2004 Announcements –hw10 due Thursday, April 22 –exam next Tuesday, April 27 Agenda –Questions –Error handling.

Slides:



Advertisements
Similar presentations
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Advertisements

Strings in Java 1. strings in java are handled by two classes String &
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Lecture 211 CS110 Lecture 21 Thursday, April 15, 2004 Announcements –hw9 due tonight –hw9 due Thursday, April 22 –exam Tuesday, April 27 Agenda –Questions.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
28-Jun-15 String and StringBuilder Part I: String.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
JAVA PROGRAMMING PART II.
L EC. 02: D ATA T YPES AND O PERATORS (1/2) Fall Java Programming.
CSC3170 Introduction to Database Systems
String Class in Java java.lang Class String java.lang.Object java.lang.String java.lang.Object We do not have to import the String class since it comes.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Chapter 6 Array-Based Lists.
Lecture 231 CS110 Lecture 23 Thursday, April 22, 2004 Announcements –hw10 due tonight –exam next Tuesday, April 27 –final exam Wednesday, May 20, 8:00.
The java.lang Package chapter 8 Java Certification Study Group February 2, 1998 Seth Ladd.
Chapter 2: Java Fundamentals
Lecture 131 CS110 Lecture 13 Thursday, March 11, 2004 Announcements –hw5 due tonight –Spring break next week –hw6 due two weeks from tonight Agenda –questions.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
CHAPTER 5 ArrayLists.
Working with string In java Four classes are provided to work with String:1) String 2)String Buffer 3)String Tokenizer 4)String Builder Note: An object.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Lecture 101 CS110 Lecture 10 Thursday, February Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,
Copyright Curt Hill Variables What are they? Why do we need them?
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
Lecture 6: More Java Advanced Programming Techniques Summer 2003.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes.
CS1101 Group1 Discussion 7 Lek Hsiang Hui comp.nus.edu.sg
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
17-Feb-16 String and StringBuilder Part I: String.
Data Types and Control Structures CS3250. Java Data Types Everything is an Object Except Primitive Data Types –For efficiency –Platform independent Portable.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Object Oriented Programming Lecture 2: BallWorld.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
The Methods and What You Need to Know for the AP Exam
Java: Base Types All information has a type or class designation
Java: Base Types All information has a type or class designation
Lecture 5: Some more Java!
EKT 472: Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
String and String Buffers
Exercise on Java Basics
Primitive Types Vs. Reference Types, Strings, Enumerations
An Introduction to Java – Part I
String and StringBuilder
Exercise on Java Basics
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Introduction to Java Programming
An Introduction to Java – Part I, language basics
String and StringBuilder
An overview of Java, Data types and variables
String and StringBuilder
Java – String Handling.
Chapter 6 Array-Based Lists.
String and StringBuilder
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
String methods 26-Apr-19.
Strings in Java.
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

Lecture 221 CS110 Lecture 22 Tuesday, April 20, 2004 Announcements –hw10 due Thursday, April 22 –exam next Tuesday, April 27 Agenda –Questions –Error handling (finally) –What’s on the exam?

Lecture 222 RumpelStiltskin Exception examples in a short standalone program The fairy tale: guess my name examples/RumpelStiltskin.java > Java RumpelStiltskin > Java RumpelStiltskin foo > Java RumpelStiltskin foo bar > Java RumpelStiltskin RumpelStiltskin

Lecture 223 Java RumpelStiltskin Design (pseudocode) If there is no command line argument print usage message end the program Two possible implementation strategies –test for args[0], proceed based on test result –assume args[0] is there, catch Exception if not

Lecture 224 Test first strategy if (args.length == 0 ) { System.out.println( "usage: java RumpelStiltskin guess"); System.exit(0); // leave program gracefully } // continue normal processing

Lecture 225 Exception strategy try { System.out.println(”Are you " + args[0] +'?'); rumpelstiltskin.guessName(args[0]); System.out.println("Yes! How did you guess?"); System.exit(0); // leave program gracefully } // come here right away if there is no args[0] catch (IndexOutOfBoundsException e) { System.out.println( "usage: java RumpelStiltskin guess"); System.exit(0); // leave program gracefully }

Lecture 226 java RumpelStiltskin foo sorry - foo is not my name Intentionally generate a NullPointerException, see what the Exception's toString method returns java.lang.NullPointerException Experiment with the printStackTrace() method: BadGuessException at java.lang.Throwable. (Compiled Code) at java.lang.Exception. (Compiled Code) at BadGuessException. (Compiled Code) at Wizard.guessName(Compiled Code) at Wizard.makeMischief(Compiled Code) at RumpelStiltskin.main(Compiled Code) Look for a second command line argument, see what happens if it's not there: java.lang.ArrayIndexOutOfBoundsException: 1 at RumpelStiltskin.main(Compiled Code)

Lecture 227 Equality In Java, “==” means “two variables have same value” Box and arrow pictures help: –same value for primitive types is just what you expect –same value for reference types: arrow points to the same Object In Object public boolean equals(Object o) { return this == o; } Override equals when you have a better idea about what equality should mean

Lecture 228 Equals in AbstractList.java public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof List)) return false; ListIterator e1 = listIterator(); ListIterator e2 = ((List) o).listIterator(); while(e1.hasNext() && e2.hasNext()) { Object o1 = e1.next(); Object o2 = e2.next(); if (!(o1==null ? o2==null : o1.equals(o2))) return false; } return !(e1.hasNext() || e2.hasNext()); }

Lecture 229 How ArrayLists work private Object elementData[]; private int size; public ArrayList() { this(10); } public boolean add(Object o) { ensureCapacity(size + 1); elementData[size++] = o; return true; }

Lecture 2210 How ArrayLists grow public void ensureCapacity(int minCapacity) { int oldCapacity = elementData.length; if (minCapacity > oldCapacity) { Object oldData[] = elementData; int newCapacity = (oldCapacity * 3)/2 + 1; if (newCapacity < minCapacity) newCapacity = minCapacity; elementData = new Object[newCapacity]; System.arraycopy(oldData, 0, elementData, 0, size); }

Lecture 2211 hw10 Test scripts Error handling for all shell commands Error handling to register and login Count fraction of Juno that’s there for error handling (class Profile)

Lecture 2212 Characters Type char is primitive in Java A char is really an int In the old days characters were just small integers The ASCII character set contains 128 characters numbered one byte, 8 bits: to in binary (0-255 decimal) ascii codes are the bytes with the high bit 0 Googling for ASCII code will find lots of information

Lecture 2213 Characters (continued) Printable characters are (decimal) – other bytes are –visible in emacs (look at a class file) –used for emacs commands, like ^S To represent them in Java use escape character: \ ‘\ddd’ // ddd is base 8 number < 256 System.out.println(‘\007’); //ring bell ‘\n’, ‘\b’, ‘\t’, ‘\”’, ‘\\’ See Escape.java in joi/examples/

Lecture 2214 Unicode Unicode extends character set to 16 bits (0 to ) for kanji, Arabic, Hebrew, mathematics, … Type char in Java really is a 16 bit int We usually write these values as hexadecimal strings: 16 bits is four hex digits ‘\uXXXX’ (X = 0, 1, …, 9, A, …, F) Internationalization (I18N) –locale –collation sequence –time, date, number format

Lecture 2215 class Character Wrapper class for primitive type char Static methods to process char values Use Character to save char in a Collection Character(char ch) // constructor public char charValue() static int getNumericValue(char ch) // unicode value static boolean isDigit(char ch) static char toUpperCase(char ch) … see API for more

Lecture 2216 Strings... The String API - lots there to use when needed –constructors –equality –comparisons –substrings –character contents –changing String contents (not) Read (some of) String.java

Lecture 2217 String constructors String s; s = “hello”; //common and convenient s = new String(“hello”); char[ ] charArray = {‘O’, ‘K’} ; s = new String( charArray ); String t = new String(s);

Lecture 2218 String matches and searches boolean equals(String anotherString); boolean equalsIgnoreCase(String anotherString); int compareTo(String anotherString); // +,-,0 boolean startsWith(String prefix); boolean endsWith(String suffix); int indexOf(int ch); int indexOf(String str); int indexOf(..., int fromIndex); int lastIndexOf(...);