important classes in java

Post on 05-Jan-2016

44 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Min Chen School of Computer Science and Engineering Seoul National University. Data Structure: Java Programming 2. Important Classes in java. Content. Inheritance implementation in Java Important Classes in Java Object Class Integer Class String Class StringTokenizer Class - PowerPoint PPT Presentation

TRANSCRIPT

IMPORTANT CLASSES IN JAVA

Data Structure: Java Programming 2

Min ChenSchool of Computer Science and Engineering

Seoul National University

Content

Inheritance implementation in Java Important Classes in Java

Object Class Integer Class String Class StringTokenizer Class ArrayList Class

Inheritance in Java

Example: An inheritance implementation

CEOsCompanyProducts

SportsmenEmployer

SportsLeague

StarsSchoolDrama

PeopleName

GenderAge

Place of Birth

People Class•Attributes• Name• Gender• Age• Place of Birth

•Methods• getName()• …

CEOs Class•Attributes• Company• Products

•Methods• getCompany()• …

People Class

Definition of Attributes

Definition of Methods

Constructed function

Necessary if you defined your own constructed function and

the class will be inherited.

CEO Class

Definition of Attributes

Definition of Methods

Constructed function

Modified HelloWorld Class

Hello World in Java!My name is Steve Jobs.My company is Apple.

Important classes in Java

Why java is so popular? Write Once, Run Everywhere Easy to Lean Easy to Use

Standard and Efficient Package Library!

Some Essential Classes in Java will make your programming much easier!

Concept of Package

In Java, a set of essential Classes with related characteristics are packed in packages

How to use a package?

How to create a package?

import java.util.*;

package mypackage;

Object Class

The MOTHER Class of the Java World All Classes in Java are subclass of Object

Class Extends the Object Class is default Unnecessary to declare when define a new

Class The methods in Object Class can be inherited

in all of the classes clone() equals() toString()

ClassName+@+hashCode

equals() in Object Class

Compare two objects When the two objects are actually one

instance, return true Else, return false

CEO steve=new CEO("Jobs","Male",54,"U.S.","Apple Inc.","Mac..."); CEO jobs=steve; CEO bill=new CEO("Jobs","Male",54,"U.S.","Apple Inc.","Mac...");

steve.equals(bill);Return false

steve.equals(jobs);Return true

Integer Class

Be Used To Define a integer Constructed Function

Integer(int) Integer(String)

Important Methods parseInt(String) toString()

Integer myInt = new Integer(“321”);

Integer myInt = Integer.parseInt(“321”);

String myStr = myInt.toString();

Static Method in Class

A static method can be called without creating the instance of the class

Integer myInt = Integer.parseInt(“321”);

public class Integer { …

public static Integer parseInt ( String ) { … } … }

String Class

Construct Function String( char[] )

Important Methods equals() toLowerCase() & toUpperCase() indexOf (String s) replace(char old ,char new) substring(int start ,int end) charAt (int index) getChars(int start,int end,char c[],int offset )

equals() in String Class

Has been overwritten Be used to compare the values of

two strings String a=“A String”; String b=“A String”; String c= “a string”

a.equals(b)Return true

a.equlas(c) Return false a. equalsIgnoreCase(c) Return true

StringTokenizer Class

Problem: How can we retrieval words from a

sentence? Example:

StringTokenizer Class address this problem Divide the sentence into tokens Retrieval words by the tokens

You adore the light, so you will never fear the darkness.

StringTokenizer Class(cont.) Defined in the package of java.utils Constructed Function

StringTokenizer( String text ) StringTokenizer( String text, String

delimiter ) Important Methods

boolean hasMoreTokens() String nextToken() int countTokens()

Provide a rule of dividing string

Divide the string by blanks

StringTokenizer Class(cont.)

Create two StringTokenizer instances for

the textLoop to print

out the tokens we can get

Import the package

ArrayList Class

Typical Features of Array Same Data Type Predefined Array Length

ArrayList Provide functionality of array Support different data type in one array Flexible Array Length

ArrayList Class(cont.)

Important Methods add(object o) & add(int index, object o)

Add a data into the ArrayList get(int index)

Retrieval a data from the ArrayList indexOf()

Search for the item Remove()

Remove a corresponding index or a value isEmpty()

ArrayList Class(cont.)Import the package

Create an instance of Array List

Add items to the ArrayList

Insert 23 into the index of 1Get items from

index

Search a predicted value

from the arraylist

Remove a predicted value

Result: Obama 23 0 steve is in the index of [3]

ArrayList Class(cont.)

Warning Message for ArrayList Type safety: The method add(Object)

belongs to the raw type ArrayList. References to generic type ArrayList<E> should be parameterized

The data type of the item we retrieval from the ArrayList is Object A transform is necessary

How to define a Type-Restricted ArrayList? ArrayList<String> myArr = new

ArrayList<String> ();

Thank you

top related