Struktur data 1

902 views
822 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
902
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
29
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Struktur data 1

  1. 1. Struktur DataCode Lecture SD[01..12] Hermawan, ST., Mkom T. Informatika - UTM
  2. 2. Topic1. Primitive & Abstract Data Types2. Lists, Stacks, and Queues3. String & File4. Trees5. Balance Tree6. Hashing7. Heap8. Sorting9. Graph10. Algoritma & Data Structure Analysis
  3. 3. Literature:1. Data Structures and Algorithm Analysis in Java, Mark A l l e n Weiss, Florida International University 20122. Data Structures and Algorithms in Java Fifth Edition International Student Version , Michael T. Goodrich, Department of Computer Science University of California, Irvine
  4. 4. Struktur Data Lecture SD-01Hermawan, ST., Mkom T. Informatika - UTM
  5. 5. Primitive & Abstract Data TypesGenerally data types of all common language divide by 2 types, they are:• Primitive• Abstract (Object)
  6. 6. Primitive data typePrimitive data type is type of data which have static size of memory. Common this data types include:• Integer : byte (8 byte), short (16 b), int (32 b), long (64 b)• Floating point: float (32 byte), double(64 b), decimal(128 b), bigDecimal(256 b)• Booleans: (1 bit)• Characters: (1 byte)• String: (n collection of character-size byte )
  7. 7. Primitive data typeInteger & double Data types, example:30 byte of double used:1.073741824E930 byte of integer used:107374182432 byte of double used:4.294967296E932 byte of integer used:214748364764 byte of double used:1.8446744073709552E1964 byte of integer used:2147483647Proof: Maximum value of integer = 2^31 – 1 = 2147483647
  8. 8. Primitive data typeIntegers & float Data types, example:public static void main(String[] args) { // TODO code application logic here double d = Math.pow(2, 30); int i = (int)d; System.out.println("30 byte of integer used:"+i); System.out.println("30 byte of integer used:"+i); d = Math.pow(2, 32); i = (int)d; System.out.println("32 byte of integer used:"+i); d = Math.pow(2, 64); i = (int)d; System.out.println("64 byte of integer used:"+i); }
  9. 9. Primitive data typeChar & String Data Type, example: char ch_a = A; char ch_b = B; System.out.println("Char: A("+ch_a+") Char B:"+ch_b); String s = Character.toString(ch_a)+Character.toString(ch_b); System.out.println("String S:"+s); ch_a = s.charAt(0); System.out.println("Char A:"+ch_a); ch_b = s.charAt(1); System.out.println("Char B:"+ch_b);Proof : String is collection of characters
  10. 10. Dynamic Memory Create of Primitive Data Type to ObjectBase Type Class Name Creation Accessbyte Byte n new Byte((byte}34); n.byteValueOf()short Short n new Short((short}100); n.shortValueOf()int Integer n = new Integer(1045); n.intValueOf()long Long n = new Long(10849L); n.longValueOf()float Float n = new Float(3.934F); n.floatValueOf()double Double n new Double{3.934}; n.doubleValueOf()
  11. 11. Java Primitive to Dynamic Object CastingUsually, primitive data type is statically typing for casting each others, but with using of Object type can to cast dinamically. Example: int i = 100; String s = String.valueOf(i); System.out.println(s);
  12. 12. Java Primitive to Dynamic Object CastingDynamic Object Casting,Example:public class TestMemory{// Object for abstract data type public Object read( ) { return storedValue; } public void write( Object x ) { storedValue = x; }
  13. 13. Java Primitive to Dynamic Object Castingprivate Object storedValue; public static void main(String[] args) { TestMemory t = new TestMemory(); t.write( 33 ); String vi = (String) t.read().toString(); System.out.println( "Contents of String for integer are: " + vi ); TestMemory m = new TestMemory(); m.write( "my age: " ); String vs = (String) m.read(); System.out.println( "Contents of String are: " + vs + vi ); }}
  14. 14. Collection of Object Data TypesCollection is set of object with dynamic type inside.Type of collection in Java include:• Array, static collection of the same primitive data types with index.• List, dynamic collection of objects with index• Map, dynamic collection of objects with key & value
  15. 15. Collection of Arraypublic class Array{ int index = 10; int [] arr= new int[index]; public void setArray(int index, int val){ for (int i=0;i<row;i++){ arr[i]= val; } } public int getArray(int index){ return arr[index]; }}
  16. 16. Collection of Arraypublic class Matrik { int bar = 10; int kol = 10; double [][] mtr = new double[bar][kol]; public void setMatrix(int row,int col, double val){ for (int i=0;i<row;i++){ for (int j=0;j<col;j++){ mtr[i][j]= val; } } } public double getMatrix(int row, int col){ return mtr[row][col]; }}
  17. 17. Collection of Dynamic Arraypublic void setValue(int row, int col, double value) { if (row >= matrix.length) { double[][] tmp = matrix; matrix = new double[row + 1][]; System.arraycopy(tmp, 0, matrix, 0, tmp.length); for (int i = row; i < row + 1; i++) { matrix[i] = new double[col]; } } if (col >= matrix[row].length) { double[] tmp = matrix[row]; matrix[row] = new double[col + 1]; System.arraycopy(tmp, 0, matrix[row], 0, tmp.length); } matrix[row][col] = value; }
  18. 18. Collection of List Object Data Typesimport java.util.ArrayList;import java.util.List;public class Student { String name; String faculty; int level; static List listOfStudents = new ArrayList(); public String toString(){ return this.name+"|"+this.faculty+"|"+this.level; } Student(String pname, String pfaculty, int plevel){ this.name=pname; this.faculty=pfaculty; this.level=plevel; }
  19. 19. Collection of List Object Data Typespublic static void main(String[] args) { Student s = new Student("adi", "technic", 1); s.listOfStudents.add(s); System.out.println(s.toString()); s = new Student("wati", "economics", 3); s.listOfStudents.add(s); System.out.println(s.toString()); s.listOfStudents.add("gatot"); s.listOfStudents.add(1000); for(int i=0; i<s.listOfStudents.size(); i++) System.out.println(s.listOfStudents.get(i).toString()); }}
  20. 20. Collection of Map Object Data Typespublic static void main(String[] args) { HashMap<String, List> mapOfStudents = new HashMap<String, List>(); List<Student> ls = new ArrayList<Student>(); Student s = new Student("adi", "technic", 1); ls.add(s); s = new Student("eko", "technic", 3); ls.add(s); s = new Student("zaid", "technic", 5); ls.add(s); mapOfStudents.put("mahasiswa teknik", ls); List result = mapOfStudents.get("mahasiswa teknik"); for(int i=0; i<result.size(); i++){ System.out.println(result.get(i).toString()); }}
  21. 21. Exercise Problem...?class domain books Publisher - address: String - city: String - country: String - email: email - name: String - state_province: String - website: url 1 is published by 0..* Author Book Chapter are wrote by contains - abstract: String - authId: String - authors: Author - email: email - publication_date: Date 1 - book: Book 1..* 0..* 1..* - first_name: String - publisher: Publisher - chapter: String - last_name: String - title: String - content: String
  22. 22. Exercise Quiz....!If you have many class inside domain class like picture above, how you can configure Java Class to represent it?

×