Packages, Characters, Strings Arguments to method main CS2110, Week 2 Recitation 1.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Getting Started with Java.
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Basic Java Constructs and Data Types – Nuts and Bolts
1 Packages: Putting Classes Together. 2 Introduction The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance)
Classes and Objects in Java
9 Copyright © 2005, Oracle. All rights reserved. Using Strings, String Buffer, Wrapper, and Text-Formatting Classes.
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
7 Copyright © 2005, Oracle. All rights reserved. Creating Classes and Objects.
6 Copyright © 2005, Oracle. All rights reserved. Building Applications with Oracle JDeveloper 10g.
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
Java
Data Structures ADT List
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Page 1 – Spring 2010Steffen Vissing Andersen Software Development with UML and Java 2 SDJ I2, Spring 2010 Agenda – week 7, 2010 • Pakages • Looking back.
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
CSci 1130 Intro to Programming in Java
25 seconds left…...
The Random and String Classes The import statement.
Introduction to Programming G51PRG University of Nottingham Revision 1
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Strings in Java 1. strings in java are handled by two classes String &
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.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
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.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
11-Jun-15 Using the Java API
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 ,
Using the Java API
Fundamental Programming Structures in Java: Strings.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
28-Jun-15 String and StringBuilder Part I: String.
29-Jun-15 Using the Java API
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.
Recitation 2 Main Method, API & Packages, Java Basics.
The java.lang Package chapter 8 Java Certification Study Group February 2, 1998 Seth Ladd.
String Manipulation Chapter 15 This chapter explains the String facilities. You have already seen some of the main methods of the String class.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Characters, Strings, Reading Files CS2110, Week 2 Recitation.
Chapter 7: Characters, Strings, and the StringBuilder.
3 - 1 Text Processing In Java: Characters and Strings Reading:Downey: Chapter 7 Problem Set:Assignment #1 due Tuesday, Feburary 13 Wellesley College CS230.
ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”
17-Feb-16 String and StringBuilder Part I: String.
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
Arguments to method main, Packages, Wrapper Classes, Characters, Strings CS2110, Recitation 2.
Arguments to method main Packages APIspecs Characters Strings
CS2110, Recitation 1 Arguments to method main, Packages, Wrapper Classes, Characters, Strings.
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
CS2110, Recitation 1 Arguments to method main, Packages, Wrapper Classes, Characters, Strings.
String and StringBuilder
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Main Method, API & Packages, Java Basics
Review for Exam 1 Spring 2007 CS 101/CS 101-E.
String and StringBuilder
CS2011 Introduction to Programming I Strings
String methods 26-Apr-19.
Switch, Strings, and ArrayLists in Java
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

Packages, Characters, Strings Arguments to method main CS2110, Week 2 Recitation 1

Package Package: See JavaSummary.pptx, slide 20 (1)Java classes that are contained in a specific directory on your hard drive (it may also contain sub-packages) or (2)Packages of Java classes that come with Java, e.g. packages java.lang, javax.swing. Consider first the packages that come with Java. We show you: (1)How to refer to them (2)How to find out how to use them, using the API (Application Programmer Interface) specifications. 2

API packages that come with Java Visit course webpage, click Links, then Java 1.7 API Specs scroll down in left col (Packages pane), click on java.langJava 1.7 API Specs 3

API packages that come with Java On page that opens, left col, you see Classes in package java.lang. Click on Character 4

Specs for Class Character 5 Main pane now contains description of class Character: 1.The header of its declaration. 2.A description, including info about Unicode 3.Nested class summary (skip it) 4.Field summary (skip it) 5.Constructor summary (read) 6.Method summary (read) 7.Field detail (skip it) 8.Method detail (read) Find method compareTo See a 1-sentence description Click on method name Takes you to a complete description in Method detail section More on class Character later

Package java.lang vs. other packages 6 You can use any class in package java.lang. Just use the class name, e.g. Character To use classes in other API packages, you have to give the whole name, e.g. javax.swing.JFrame So you have to write: javax.swing.JFrame jf= new javax.swing.JFrame();

Use the import statement! 7 To be able to use just JFrame, put an import statement before the class definition: import javax.swing.JFrame; public class C { … public void m(…) { JFrame jf= new JFrame(); … } Imports only class JFrame. Use the asterisk, as in line below, to import all classes in package: import javax.swing.*;

Other packages on you hard drive One can put a bunch of logically related classes into a package, which means they will all be in the same directory on hard drive. Reasons for doing this? We may discuss much later. 8 Image of Eclipse Package Explorer: 3 projects: project has default package and package pack1 Default package has 2 classes: Rec02, Rec02Tester pack1 has 1 class: C

Hard drive Eclipse Package Explorer 9 Eclipse Hashing I03Demo recitation02 src Rec02.java Rec02Tester.java pack1 C.java Eclipse does not make a directory for the default package; its classes go right in directory src

Importing the package 10 import pack1.*; public class Rec02 { public Rec02() { C v= new C(); } package pack1; public class C { /** Constructor: */ public C() { } Every class in package pack1 must start with the package statement Every class outside the package should import its classes in order to use them

Primitive type char char fred= 'a'; char wilma= 'b'; System.out.println(fred); Unicode: 2-byte representation Visit to see all unicode charswww.unicode.org/charts/ a Use single quotes 11

Special chars worth knowing about ' ' - space '\t' - tab character '\n' - newline character '\'' - single quote character '\"' - double quote character '\\' - backslash character '\b' - backspace character - NEVER USE THIS '\f' - formfeed character - NEVER USE THIS '\r' - carriage return - NEVER USE THIS Backslash, called the escape character 12

Casting char values (int) 'a' gives 97 (char) 97 gives 'a' (char) 2384 gives ' ॐ ' Cast a char to an int using unary prefix operator (int), Gives unicode representation of char, as an int No operations on chars (values of type char)! BUT, if used in relation or arith, a char is automatically cast to type int. Relations = == != == 'a' < 'b' same as 97 < 98, i.e. false 'a' + 1 gives 98 Om, or Aum, the sound of the universe (Hinduism) 13

Class Character An object of class Character wraps a single char (has a field that contains the char) Character c1= new Character('b'); Character c2= new Character('c'); ??? 'b' charValue() compareTo(Character) equals(Object) ??? 'c' charValue() compareTo(Character) equals(Object) c1 c2 Don’t know field name 14

Class Character Each instance of class Character wraps an int value —has a field that contains an int value. Allows a char value to be treated as an object Find methods in each object by looking at API specs on web: docs.oracle.com/javase/7/docs/api/java/lang/Character.html c.charValue() c.equals(c1) c.compareTo(c1) c.toString() … c’s wrapped char, as a char True iff c1 is a Character and wraps same char 0 if c==c1. 0 if c > c1 c’s wrapped char, as a String … 15

Static methods in class Character Lots of static functions. You have to look to see what is available. Below are examples isAlphabetic(c) isDigit(c) isLetter(c) isLowerCase(c) isUpperCase(c) isWhitespace(c) toLowerCase(c) toUpperCase(c) These return the obvious boolean value for parameter c, a char Whitespace chars are the space ‘ ‘, tab char, line feed, carriage return, etc. These return a char. 16

== versus equals c1 == c2 c3 == c1 c1 == c1 c1.equals(c2) c3.equals(c1) ??? 'b' charValue() compareTo(Character) equals(Object) ??? 'b' charValue() compareTo(Character) equals(Object) c1 c2 null c3 true iff c1, c2 contain same values true iff c2 is also a Character object and contains same char as c1 false true Error!!! 17

“CS211” Class String String s = “CS211”; ??? String: special place in Java: no need for a new-expression. String literal creates object. s length() charAt(int) subString(int) subString(int, int) equals(Object) trim() contains(String) indexOf(String) startsWith(String) endsWith(String) … more … Find out about method of class String: docs.oracle.com/javase/7/docs/api/ index.html?java/lang/String.html Lots of methods. We explain basic ones Important: String object is immutable: can’t change its value. All operations/functions create new String objects 18

Operator + "abc" + "12$" evaluates to "abc1 2$" Catenation, or concatenation + is overloaded (1 + 2) + "ab$" evaluates to "3ab$ " If one operand of catenation is a String and the other isn’t, the other is converted to a String. Sequence of + done left to right "ab$ " evaluates to " ab$12 " 19

Operator + System.out.println("c is: " + c + “, d is: " + d + “, e is: " + e); c 32 d -3 e 201 Output: c is: 32, d is: -3, e is: 201 Can use + to advantage in println statement. Good debugging tool. Note how each output number is annotated to know what it is. Using several lines increases readability 20

s.substring(i): new String containing chars at positions from i to end — s.substring(2) is ' 13 ' Picking out pieces of a String s.length(): number of chars in s — 5 " CS 13 " ? s length() charAt(int) subString(int) subString(int, int) … more … " CS 13 " Numbering chars: first one in position 0 s.char(i): char at position i of s — s.charAt(3) is ' 1 ' s.substring(i,j): new String containing chars at positions i..(j-1) — s.substring(2,4) is ' 13 ' Be careful: Char at j not included! 21

Other useful String functions s.trim() – s but with leading/trailing whitespace removed s.indexOf(s1) – position of first occurrence of s1 in s (-1 if none) s.lastIndexOf(s1) – similar to s.indexOf(s1) s.contains(s1) – true iff String s1 is contained in s2 s.startsWith(s1) – true iff s starts with String s1 s.endsWith(s1) – true iff s ends with String s1 s.compareTo(s1) – 0 if s and s1 contain the same string, 0 if s is greater (dictionary order) There are more functions! Look at the API specs! 22

Given method main an argument public static void main(String[] args) { … } Parameter: String array In Eclipse, when you do menu item Run -> Run or Run -> Debug Eclipse calls method main. Default is main(null); To tell Eclipse what array of Strings to give as the argument, Use menu item Run -> Run Configurations… or Run -> Debug Configuration… (see next slide) 23

Window Run Configurations This Arguments pane of Run Configurations window gives argument array of size 3: args[0]: “SpeciesData/a0.dat” args[1]: “2” args[2]: “what for?” Click Arguments pane Quotes needed because of space char Quotes OK, but not needed 24