Download
data structure java programming 2 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Important Classes in java PowerPoint Presentation
Download Presentation
Important Classes in java

Important Classes in java

126 Views Download Presentation
Download Presentation

Important Classes in java

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Java Programming 2 Important Classes in java

  2. Content • Inheritance implementation in Java • Important Classes in Java • Object Class • Integer Class • String Class • StringTokenizer Class • ArrayList Class

  3. Inheritance in Java • People Class • Attributes • Name • Gender • Age • Place of Birth • Methods • getName() • … • Example: • An inheritance implementation People Name • CEOs Class • Attributes • Company • Products • Methods • getCompany() • … Gender Age Place of Birth CEOs Sportsmen Stars School Employer Company Drama Sports Products League

  4. People Class Definition of Attributes Necessary if you defined your own constructed function and the class will be inherited. Definition of Methods Constructed function

  5. CEO Class Definition of Attributes Constructed function Definition of Methods

  6. Modified HelloWorld Class Hello World in Java! My name is Steve Jobs. My company is Apple.

  7. 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!

  8. 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;

  9. 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

  10. 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

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

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

  13. 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(intstart,intend,char c[],int offset )

  14. 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

  15. StringTokenizerClass • 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.

  16. StringTokenizer Class(cont.) Divide the string by blanks • Defined in the package of java.utils • Constructed Function • StringTokenizer( String text ) • StringTokenizer( String text, String delimiter ) • Important Methods • booleanhasMoreTokens() • String nextToken() • intcountTokens() Provide a rule of dividing string

  17. StringTokenizer Class(cont.) Import the package Create two StringTokenizer instances for the text Loop to print out the tokens we can get

  18. 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

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

  20. Search a predicted value from the arraylist ArrayList Class(cont.) Get items from index Remove a predicted value Create an instance of Array List Insert 23 into the index of 1 Import the package Add items to the ArrayList Result: Obama 23 0 steve is in the index of [3]

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

  22. Thank you