Download strings, scope, comments - University of St. Thomas

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
QMCS 230: Today in Class
• Lab Status?
• Stuff from the reading
– Scope
– Documentation and comments
– Formatting
• The String Class
• Related Object Stuff
• Keyboard Input (if time)
March 2005
R. Smith - University of St Thomas - Minnesota
1/18
If you did NOT do the reading…
• Section 2.10 talked about “Scope”
–
–
–
–
Java analyzes the file in order from top to bottom
You must declare variables before use
Java lets you declare anywhere before the variable is used
STYLE: I want you to always declare at the top of the method
• Section 2.11 talked about Comments
• Section 2.12 talked about Style
– Follow the style recommendations in the book
– Follow its formatting style
– “Javadoc” is cool but not something we need
• I trust this book - follow its recommendations
March 2005
R. Smith - University of St Thomas - Minnesota
2/18
The String Class
• A Java “Class” is a set of methods
– The “main” we write is a method
– May also define a type of object
• The String Class = object type for “Strings”
• A collection of methods we can use
–
–
–
–
length() = calculates # characters
toLowerCase() = converts to all lower case
toUpperCase() = converts to all upper case
charAt(n) = returns the “char” at location “n”
March 2005
R. Smith - University of St Thomas - Minnesota
3/18
So what is a String?
• Not a primitive type; it’s an object
• String foo = “Hello, world!”
• foo itself isn’t big enough for the text
– It points to the string, which resides
elsewhere
• Objects are like that - arbitrarily large
– Their variables don’t contain, they point.
March 2005
R. Smith - University of St Thomas - Minnesota
4/18
The Whole Point of Objects
• Make it easier to write programs
– Re-use existing programs
– Catch bugs when using existing programs
– Catch bugs in newly written programs
• A “Class” is a way of defining new object types
– A new object is always based on an old one
• “Real” Java Programming
– Some “procedural programming” - what we do
– Mostly relies on existing “classes” to do the work
March 2005
R. Smith - University of St Thomas - Minnesota
5/18
Let’s try those String Operations
• The string methods
– length() = calculates # characters
– toLowerCase() = converts to all lower case
– toUpperCase() = converts to all upper case
– charAt(n) = returns the “char” at location “n”
• Syntax:
– objectName.methodName(args);
March 2005
R. Smith - University of St Thomas - Minnesota
6/18
Reading Keyboard Input
• “Scanner” class - a real class
– String has shortcuts built into Java
– Scanner is a completely “external” class
• Reading from “System.in”
– The opposite of our friend “System.out”
• Initializing an object variable
– Scanner keyboard = new Scanner (System.in);
• To Use, must be Defined
– import java.util.Scanner;
– Imports the class from the “util” package
– Put at start of file
March 2005
R. Smith - University of St Thomas - Minnesota
7/18
Scanner methods
•
•
•
•
•
•
•
•
Scanner Kb = new Scanner(…);
Kb.nextLine() - next line of text (string)
Kb.nextInt() - next integer
Kb.nextDouble() - larger floating point
Kb.nextFloat() - float sized fp
Kb.nextLong() - huge integer
Kb.nextByte() - next byte-sized number
Kb.nextShort() - next short-sized number
March 2005
R. Smith - University of St Thomas - Minnesota
8/18
Using NextLine
• Repeat those string experiments
• Print out upper, lower, length, and a
chosen char
• Time to do some arithmetic?
March 2005
R. Smith - University of St Thomas - Minnesota
9/18
Creative Commons License
This work is licensed under the Creative
Commons Attribution-Share Alike 3.0 United
States License. To view a copy of this license,
visit http://creativecommons.org/licenses/bysa/3.0/us/ or send a letter to Creative
Commons, 171 Second Street, Suite 300, San
Francisco, California, 94105, USA.
March 2005
R. Smith - University of St Thomas - Minnesota
10/18