DOC PREVIEW
WVU CS 110 - Using Built-in Classes

This preview shows page 1-2-3 out of 9 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 3 – Using Built-in ClassesReading single characters as input:Static MethodsLecture 3 – Using Built-in ClassesThe fundamental notion in Java and in any object-oriented programming languages is the use andcreation 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 attributeso 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/apiand selecting “String” from the list of classes.**************************************/There are two ways to create an object of class String:String name = “Joe Brown”;OrString 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.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 e B r o w n| | | | | | | |0 1 2 3 4 5 6 7 8So 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 returnsan 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";int nlength = 0;nlength = name.length();System.out.println("the length of the string is " + nlength);}}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();Explicit parameters of method println, a method may have zero or more explicit parametersAn “implicit” parameterof println is the object to which it is applied.The return value is stored in nlengthnameJoe Brownwould 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 ofthe 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..WhileIndex = name.indexOf(“xyz”);Will return a value of -1Character 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 valuechar 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


View Full Document

WVU CS 110 - Using Built-in Classes

Download Using Built-in Classes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Using Built-in Classes 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 Using Built-in Classes 2 2 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?