1 / 30

Session 5

Session 5. java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic. java.lang package. The package consists classes and interface which are fundamental to Java programming All the program automaticlly import java.lang package It contains.

Download Presentation

Session 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic

  2. java.lang package • The package consists classes and interface which are fundamental to Java programming • All the program automaticlly import java.lang package • It contains

  3. Wrapper classes

  4. Wrapper classes (cont…)

  5. Common methods

  6. Math class • Defining methods for basic numeric operations as well as trigonomethic functions • All the methods are static • This is final class • Methods • public static double pow(double x, double y) • public static double exp(double x) • public static double log(double x)

  7. Methods of Math class • Trigonometric methods • public static double sin(double x) • public static double cos(double x) • public static double tan(double x) • Arirthmetic methods

  8. Array • Stored many values • Elements in array have the same datatype • Elements in array are accessed throught by subscript • Types of array: • Single dimension array (one dimension array) • Multi demension array

  9. One Dimensional Array Declarations • Three ways to declare an array are: • datatype identifier [ ]; int a[]; or int [] a; • datatype identifier [ ] = new datatype[size]; char ch[] = new char[10]; • datatype identifier [ ] = {value1,value2,….valueN}; byte b[] = {12, 3, 5};

  10. Initialize elements in array Init value for each element a[0] = 12; a[1] = 15; Init when creating array int a[] = {12, 15, 17}; Using loop for(int i = 0 ; i < a.length ; i++) a[i] = …;

  11. Example class ArrDemo { public static void main(String [] arg) { double nums[] = {10.1, 11.3, 12.5,13.7, 14.9}; System.out.println(" The value at location 3 is : " + nums[3]); } }

  12. Two dimension array Array that has more than one dimention Syntax: int a[][]; int []a[] = new int[2][3]; int [][]a = {{1, 2}, {3, 4}, {-9, 0}};

  13. Initialize 2 dimension array Initialize for each elements a[0][0] = ‘c’; a[0][1] = ‘e’; Initialize when declaring array int a[][] = {{1, 2}, {13, -78}}; Using loop for(int i = 0 ; i < n ; i++) for(int j = 0 ; j < m ; j++) a[i][j] = …;

  14. String class • In Java, a string literal is an object of type String class. • Hence manipulation of strings will be done through the use of the methods provided by the String class. • String class is in java.lang package • Every time we need an altered version of the String, a new String object is created with the modifications in it.

  15. Compare two String • String length(): This method determines the length of a string. • The == operator and equals(), compareTo() method can be used for strings comparison. • The == operator checks if the two operands being used are one and the same object. • The equals() method checks if the contents of the two operands are the same. • compareTo() return value = 0 if s1 = s2, return value >0 if s1 > s2, return value < 0 if s1 < s2

  16. Methods of String • trim () • substring (int start, int end) • equals (Object s) • equalsIgnoreCase (String s) • charAt(inti) • endsWith (String s) • startsWith (String s) • indexOf (String s) • lastIndexOf (String s) • toLowerCase () • toUpperCase ()

  17. Example class StringTest { public static void main(String[] args) { String name = args[0]; if(name.startsWith("M")) System.out.println("Hey my name also starts with an M! "); int length = name.length(); System.out.println("Your name has "+length+" characters"); String name_in_caps = name.toUpperCase(); System.out.println(name_in_caps); } }

  18. Convert String into number String s = “12”; String to int int a = Interger.parseInt(s); String to float float f = Float.parseFloat(s);

  19. Array of Strings String s[]; s = new String[3]; Or String s[] = new String[3]; Or String []s = {“a”, “b”, “c”};

  20. Immutability of String • Strings in Java once created cannot be changed directly. • This is known as immutability in Strings.

  21. Example class Testing { public static void main(String[] args) { String str = "Hello"; str.concat("And Goodbye"); System.out.println(str); } }

  22. java.util package • Contains useful classes providing a broard range of functionality • Collection classes are useful for working with groups of objects • Contains classes that provides date and time, calendar, dictionary facilities

  23. StringTokenizer • Placed at java.util • Seperated String into token • Methods • countTokens() • hasMoreElements() • hasMoreTokens() • nextElement() • nextToken() • Example: s = “22+3-5/3” • Token: 22 3 5 3 • Separator: + - /

  24. ArrayList • An ArrayList object is a variable length array of object references. • Used to create dynamic arrays • ExtendsAbstractList and implementsListinterface. • ArrayLists are created with an initial size. • As elements are added, size increases and the array expands.

  25. ArrayList (cont…) • Constructors • Methods • size() • add(int index, E element) • get(int index) • indexOf(Object elem) • lastIndexOf(Object elem) • remove(int index) • remove(Object o) • set(int index, E element)

  26. Vector class • Similar to ArrayList class, allows to implement dynamic array • Storing an array of objects that size can increase or decrease • At any given point of time, an instance of type Vector has the capacity to hold a certain number of elements. • When it becomes full, its capacity is incremented by an amount specific to that Vector object. • Diffirence between Vector and ArrayList is that methods of Vector are synchronized and are thread-safe

  27. Vector class (cont…)

  28. Generic • Earlier Collection treated elements as a collection of objects. • To retrieve an element from a Collection required an explicit cast • Thus, there was always a risk of runtime exception, ClassCastException • Generic allows the programmer to communicate the type of a collection to the compiler so that it can be checked. • The compiler consistently checks for the element type of the collection. • And inserts the correct cast on elements being taken out of the collection.

  29. Example

  30. Example of Generic Code required explicit cast Type of element No required explicit cast

More Related