{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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 = ne w 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
Image of page 1

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

View Full Document Right 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: Strin g fullName = new String(“Tanya Amber Settle”); System.out.println(“Your full name is” + full Name); System.out.println(“Your name has “ + fullName.length() + “ characters, including spaces.”); To invoke a method i.e. fullName.length(): First, we give the name of the object: fullName Next, we put a “.” (
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern