SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Struktur Data
Code Lecture SD[01..12]
   Hermawan, ST., Mkom
    T. Informatika - UTM
Topic
1. Primitive & Abstract Data Types
2. Lists, Stacks, and Queues
3. String & File
4. Trees
5. Balance Tree
6. Hashing
7. Heap
8. Sorting
9. Graph
10. Algoritma & Data Structure Analysis
Literature:
1. Data Structures and Algorithm Analysis in
   Java, Mark A l l e n Weiss, Florida
   International University 2012
2. Data Structures and Algorithms in Java Fifth
   Edition International Student Version ,
   Michael T. Goodrich, Department of
   Computer Science University of California,
   Irvine
Struktur Data
 Lecture SD-01
Hermawan, ST., Mkom
 T. Informatika - UTM
Primitive & Abstract Data Types
Generally data types of all common language
  divide by 2 types, they are:
• Primitive
• Abstract (Object)
Primitive data type
Primitive 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 )
Primitive data type
Integer & double Data types, example:
30 byte of double used:1.073741824E9
30 byte of integer used:1073741824
32 byte of double used:4.294967296E9
32 byte of integer used:2147483647
64 byte of double used:1.8446744073709552E19
64 byte of integer used:2147483647
Proof: Maximum value of integer = 2^31 – 1
                           = 2147483647
Primitive data type
Integers & 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);
  }
Primitive data type
Char & 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
Dynamic Memory Create of
            Primitive Data Type to Object
Base Type        Class Name   Creation                   Access

byte             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()
Java Primitive to Dynamic
               Object Casting
Usually, 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);
Java Primitive to Dynamic
                Object Casting
Dynamic Object Casting,
Example:
public class TestMemory{
// Object for abstract data type
   public Object read( ) {
     return storedValue;
   }
   public void write( Object x ) {
     storedValue = x;
   }
Java Primitive to Dynamic
                     Object Casting
private 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 );
    }
}
Collection of
             Object Data Types
Collection 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
Collection of
                            Array
public 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];
    }
}
Collection of
                                 Array
public 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];
    }
}
Collection of
                             Dynamic Array
public 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;
  }
Collection of List
                      Object Data Types
import 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;
  }
Collection of List
                 Object Data Types
public 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());
  }
}
Collection of Map
                   Object Data Types
public 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());
    }
}
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
Exercise
                   Quiz....!
If you have many class inside domain class like
   picture above, how you can configure Java
   Class to represent it?

Weitere ähnliche Inhalte

Was ist angesagt?

Authorship attribution pydata london
Authorship attribution   pydata londonAuthorship attribution   pydata london
Authorship attribution pydata london
kperi
 
5. c sharp language overview part ii
5. c sharp language overview   part ii5. c sharp language overview   part ii
5. c sharp language overview part ii
Svetlin Nakov
 
Types by Adform Research
Types by Adform ResearchTypes by Adform Research
Types by Adform Research
Vasil Remeniuk
 
Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]
Palak Sanghani
 
C# Language Overview Part II
C# Language Overview Part IIC# Language Overview Part II
C# Language Overview Part II
Doncho Minkov
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
Ravi Kant Sahu
 

Was ist angesagt? (19)

Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
Authorship attribution pydata london
Authorship attribution   pydata londonAuthorship attribution   pydata london
Authorship attribution pydata london
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
5. c sharp language overview part ii
5. c sharp language overview   part ii5. c sharp language overview   part ii
5. c sharp language overview part ii
 
Whiteboarding Coding Challenges in Python
Whiteboarding Coding Challenges in PythonWhiteboarding Coding Challenges in Python
Whiteboarding Coding Challenges in Python
 
C# overview part 2
C# overview part 2C# overview part 2
C# overview part 2
 
Second chapter-java
Second chapter-javaSecond chapter-java
Second chapter-java
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita Ganesan
 
Types by Adform Research
Types by Adform ResearchTypes by Adform Research
Types by Adform Research
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on Python
 
String Handling
String HandlingString Handling
String Handling
 
Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]
 
Object and class in java
Object and class in javaObject and class in java
Object and class in java
 
C# Language Overview Part II
C# Language Overview Part IIC# Language Overview Part II
C# Language Overview Part II
 
Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)
 
JSpiders - Wrapper classes
JSpiders - Wrapper classesJSpiders - Wrapper classes
JSpiders - Wrapper classes
 
L9 wrapper classes
L9 wrapper classesL9 wrapper classes
L9 wrapper classes
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Java Day-5
Java Day-5Java Day-5
Java Day-5
 

Ähnlich wie Struktur data 1

Lecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptxLecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptx
ShahinAhmed49
 

Ähnlich wie Struktur data 1 (20)

CH1 ARRAY (1).pptx
CH1 ARRAY (1).pptxCH1 ARRAY (1).pptx
CH1 ARRAY (1).pptx
 
DAY_1.3.pptx
DAY_1.3.pptxDAY_1.3.pptx
DAY_1.3.pptx
 
Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................
 
Lecture20 vector
Lecture20 vectorLecture20 vector
Lecture20 vector
 
L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)
 
Lecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptxLecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptx
 
Introducing scala
Introducing scalaIntroducing scala
Introducing scala
 
Scala Bootcamp 1
Scala Bootcamp 1Scala Bootcamp 1
Scala Bootcamp 1
 
Computer programming 2 Lesson 12
Computer programming 2  Lesson 12Computer programming 2  Lesson 12
Computer programming 2 Lesson 12
 
Lecture 3.pptx
Lecture 3.pptxLecture 3.pptx
Lecture 3.pptx
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
L10 array
L10 arrayL10 array
L10 array
 
130717666736980000
130717666736980000130717666736980000
130717666736980000
 
Arrays
ArraysArrays
Arrays
 
C data types, arrays and structs
C data types, arrays and structsC data types, arrays and structs
C data types, arrays and structs
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Aj unit2 notesjavadatastructures
Aj unit2 notesjavadatastructuresAj unit2 notesjavadatastructures
Aj unit2 notesjavadatastructures
 
vectors.(join ALL INDIA POLYTECHNIC (AICTE)).pptx
vectors.(join ALL INDIA POLYTECHNIC (AICTE)).pptxvectors.(join ALL INDIA POLYTECHNIC (AICTE)).pptx
vectors.(join ALL INDIA POLYTECHNIC (AICTE)).pptx
 
GenericsFinal.ppt
GenericsFinal.pptGenericsFinal.ppt
GenericsFinal.ppt
 
C# p9
C# p9C# p9
C# p9
 

Struktur data 1

  • 1. Struktur Data Code Lecture SD[01..12] Hermawan, ST., Mkom T. Informatika - UTM
  • 2. Topic 1. Primitive & Abstract Data Types 2. Lists, Stacks, and Queues 3. String & File 4. Trees 5. Balance Tree 6. Hashing 7. Heap 8. Sorting 9. Graph 10. Algoritma & Data Structure Analysis
  • 3. Literature: 1. Data Structures and Algorithm Analysis in Java, Mark A l l e n Weiss, Florida International University 2012 2. Data Structures and Algorithms in Java Fifth Edition International Student Version , Michael T. Goodrich, Department of Computer Science University of California, Irvine
  • 4. Struktur Data Lecture SD-01 Hermawan, ST., Mkom T. Informatika - UTM
  • 5. Primitive & Abstract Data Types Generally data types of all common language divide by 2 types, they are: • Primitive • Abstract (Object)
  • 6. Primitive data type Primitive 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. Primitive data type Integer & double Data types, example: 30 byte of double used:1.073741824E9 30 byte of integer used:1073741824 32 byte of double used:4.294967296E9 32 byte of integer used:2147483647 64 byte of double used:1.8446744073709552E19 64 byte of integer used:2147483647 Proof: Maximum value of integer = 2^31 – 1 = 2147483647
  • 8. Primitive data type Integers & 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. Primitive data type Char & 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. Dynamic Memory Create of Primitive Data Type to Object Base Type Class Name Creation Access byte 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. Java Primitive to Dynamic Object Casting Usually, 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. Java Primitive to Dynamic Object Casting Dynamic Object Casting, Example: public class TestMemory{ // Object for abstract data type public Object read( ) { return storedValue; } public void write( Object x ) { storedValue = x; }
  • 13. Java Primitive to Dynamic Object Casting private 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. Collection of Object Data Types Collection 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. Collection of Array public 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. Collection of Array public 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. Collection of Dynamic Array public 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. Collection of List Object Data Types import 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. Collection of List Object Data Types public 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. Collection of Map Object Data Types public 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. 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. Exercise Quiz....! If you have many class inside domain class like picture above, how you can configure Java Class to represent it?