Unformatted text preview:

Lecture 3 Using Built in Classes The fundamental notion in Java and in any object oriented programming languages is the use and creation of classes As defined in a previous lecture a class represents a group of similar real world things or objects which share a set of common properties called attributes and a common set of behaviors which define how objects of the class can be used which are implemented as methods A class definition in a program is a template containing both o A Data structure containing the variables which represent attributes o Methods implemented as functions which represent behavior Before we begin defining classes on our own we first need to learn how to read the documentation for the wealth of built in classes provided by Java Java has a huge library of classes that it makes available to programmers These benefit the programmer in 2 ways 1 By using these classes a novice programmer will be able to write more interesting and complex programs faster 2 It helps to keep programmers from reinventing the wheel They can reuse code that has already been written well tested and documented rather that writing the code from scratch We will begin our study with the built in class String A class is both a definition of a set of objects and an Abstract data Type An abstract data type ADT is a programmer defined data type which we can use to define objects of that type For example we can define string objects in our programs to represents lines of text consisting of multiple characters Internally String is implemented as a series of characters Variables of type String are reference variables as opposed to the primitive variables we have already studied Variables whose type is a class to not physically contain data values but rather contain an address called a reference that tells where in memory the physical object is stored We can view the methods and data variables in the class string via http java sun com j2se 1 5 0 docs api and selecting String from the list of classes There are two ways to create an object of class String String name Joe Brown Or String name new String Joe Brown The new operator invokes a constructor method which allocates storage space for the new object and stores the location of the data in the reference variable name and stores the value Joe Brown into space allocated for the string name Joe Brown The characters stored in a String variable have associated with them an index value which represents their position in the string This sequential index value starts at zero 0 For Example J o 0 1 e 2 3 B 4 r 5 o 6 w 7 n 8 So in this example the index values range from 0 8 and the string itself contains 9 characters This index value is used and returned by some of the methods provided for class String Handout page of documentation for Class String One of the methods of the class String is length which when applied to a string object returns an integer value which is a count of the number of characters in the string Example for using method length public class Stringtest public static void main String args String name Joe Brown The int nlength 0 return value is stored in nlength nlength name length System out println the length of the string is nlength An implicit parameter of println is the object to which it is applied Explicit parameters of method println a method may have zero or more explicit parameters Method length does not require any explicit parameters Some methods as we will see later do not require implicit parameters that means they are invoked independently of any object Another useful method is toLowerCase which returns a string object with all characters in the object to which the method is applied converted to lower case e g name name toLowerCase would modify the contents of name to joe brown Similarly there is a method toUpperCase As seen with println methods may have parameters which act to convey information to the method For example consider the method indexOf String str which requires one formal parameter of type string this is also called an explicit parameter The purpose of this method is to return the index or position of the specified string str inside of the string object to which it is applied If the string is not found it returns a value of 1 String name Joe Brown int index index name indexOf Bro will return the value 4 While Index name indexOf xyz Will return a value of 1 Character Data Type The char data type is an integer data type that contains a single character character literals are delimited by a single quote c Character values can be referred to by their literal representation or by their associated integer value char aletter r Several string methods reference the char data type and manipulate individual characters of the string For example the indexOf method we just used has two other definitions indexOf char ch and indexOf int ch which means in addition to being called with a string parameter it can also be called with a single character or integer as a parameter String name Joe Brown int index index name indexOf o returns 1 Overloaded method names indexOf is an example of an overloaded method name a method name is overloaded if two or more methods in a class have the same name but are distinguished by differing parameter profiles they have different types of parameters Some other useful string methods are charAt int index returns the character at the indicated position concat string str concatenates the string contained in the parameter with the string contained in the object to which the method is applied String name Joe name name concat Brown Produces Joe Brown Note that the operator can also be used to perform concatenation with strings indexOf int ch returns the location of the 1st occurrence of this character indexOf int ch int fromIndex returns the location of the 1st occurrence of this character beginning at the position in the string indicated by fromIndex indexOf String str int fromIndex returns the location of the 1st occurrence of this string in the string object beginning at the position in the string indicated by fromIndex replace char ch char ch2 replaces every occurrence of the first character with the second find the first two occurrences of the letter o String name Joe Brown int index index name indexOf o this will return 1 index increment to the next character position index name indexOf o index will return 6 Substring Methods The substring methods of class String are used to return a piece of


View Full Document

WVU CS 110 - Lecture 3

Loading Unlocking...
Login

Join to view Lecture 3 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 3 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?