important classes in java

22
IMPORTANT CLASSES IN JAVA Data Structure: Java Programming 2 Min Che School of Computer Science and Engineerin Seoul National Universit

Upload: china

Post on 05-Jan-2016

40 views

Category:

Documents


0 download

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

Page 1: Important Classes in java

IMPORTANT CLASSES IN JAVA

Data Structure: Java Programming 2

Min ChenSchool of Computer Science and Engineering

Seoul National University

Page 2: Important Classes in java

Content

Inheritance implementation in Java Important Classes in Java

Object Class Integer Class String Class StringTokenizer Class ArrayList Class

Page 3: Important Classes in java

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()• …

Page 4: Important Classes in java

People Class

Definition of Attributes

Definition of Methods

Constructed function

Necessary if you defined your own constructed function and

the class will be inherited.

Page 5: Important Classes in java

CEO Class

Definition of Attributes

Definition of Methods

Constructed function

Page 6: Important Classes in java

Modified HelloWorld Class

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

Page 7: Important Classes in java

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!

Page 8: Important Classes in java

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;

Page 9: Important Classes in java

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

Page 10: Important Classes in java

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

Page 11: Important Classes in java

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();

Page 12: Important Classes in java

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 ) { … } … }

Page 13: Important Classes in java

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 )

Page 14: Important Classes in java

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

Page 15: Important Classes in java

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.

Page 16: Important Classes in java

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

Page 17: Important Classes in java

StringTokenizer Class(cont.)

Create two StringTokenizer instances for

the textLoop to print

out the tokens we can get

Import the package

Page 18: Important Classes in java

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

Page 19: Important Classes in java

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()

Page 20: Important Classes in java

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]

Page 21: Important Classes in java

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> ();

Page 22: Important Classes in java

Thank you