M3 notes - CSC 211 NOTES W i nt e r 2 0 1 1 : A mb e r S e...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CSC 211 NOTES Winter 2011: Amber Settle Module: Using predefined Java classes Using predefined classes There are a (large) number of classes defined in Java. A class is defined to create objects, which can be used in our program to help us solve problems. An object has three things: 1. Identity : We can distinguish multiple instances of the same kind (“class”) of object; this is done using the identifier for the object 2. State : Every object has certain properties that characterize it; these are the value(s) of the object 3. Behavior : Every object has certain actions that it can perform; these are the method(s) of the object To see what we mean by this, consider the String class. The String class Strings are objects representing character strings. To create a String object : String stringName = new String(“some character string”); Note: We must supply the text for the contents of the string. This is also a declaration and assignment of a variable, although not one of a primitive type. Examples : String name = new String(“Amber Settle”); String address = new String(“243 S. Wabash Ave.”); Once we have a string, what can we do with it? Some methods of the String class: int length() : returns the number of characters in the String char charAt(int index) : returns the character in the input position „index‟ int indexOf(char target) : returns the index of the first occurrence of the input character „target‟ or –1 if it does not occur in
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 the String; indexOf can also take a String as a parameter and it will return the index of the first occurrence of the parameter in the object. String substring(int start, int end) : returns a String that represents a range of characters from positions „start‟ to „end‟-1 In general, the form for describing a method (the header of the method) is: returnType methodName(types and names of parameters) Note: Indexing in strings (like all arrays) starts at 0. Example : See the recorded discussion of the StringDemo.java program. Obtaining characters as input We can use String method charAt to obtain a single character of input from the user of our programs. To do this, you input a String using either the Scanner or JOptionPane class and then use the charAt method to obtain the first character of the String. Example : See the recorded discussion of the StringDemo.java program. Invoking class methods In the example above, we saw how to use methods built into the String class. Here we will generalize that so that you understand how to call any method of a class (to which you have access). When we invoke (or call) a method of an object, we are asking the method to do something, usually to the information contained in the object. Consider an example:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/05/2011 for the course CSC 211 taught by Professor Chan during the Spring '11 term at CA Health Sciences.

Page1 / 12

M3 notes - CSC 211 NOTES W i nt e r 2 0 1 1 : A mb e r S e...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online