Basic Java Dan ADT

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 52

Data Structure

and Algorithms

Abstract Data Type (ADT)


Ramaditia D
rama@unram.ac.id

V1.0
Outline

1. Dasar Java (Review Materi AlPro)

2. Abstract Data Type (ADT)

3. Latihan membuat program dengan


Java.
DASAR JAVA
Tipe Data Primitif
boolean • a boolean value: true or false

char • 16-bit Unicode character

byte • 8-bit signed two’s complement integer

short • 16-bit signed two’s complement integer

int • 32-bit signed two’s complement integer

long • 64-bit signed two’s complement integer

float • 32-bit floating-point number (IEEE 754-1985)

double • 64-bit floating-point number (IEEE 754-1985)


Tipe Data Primitif

Contoh deklarasi dan inisialisasi variable:


Java Hello World
Classes and Objects
In more complex Java programs, the primary
“actors” are objects

Members of a class
in Java:
Class in Java

Instance
Method
variable
Class and Objects Example
An web application with some of web robot.

object object

- name: “tono” - name: “tini”


- color: “green” - color: “orange”

wr1 = + sayHello( ) + sayHello( )


wr2 =
+ introduceSelf( ) + introduceSelf( )
Class and Objects Example

Class (object blueprint)

- name:
- color:

+ sayHello( )
+ introduceSelf( )

WebRobot
Contoh Lain Pendefinisian Class

A Counter class for a simple counter, which can be


queried, incremented, and reset.

Classes are known as reference types in Java


Contoh Lain Penggunaan Object

A demonstration of the use of Counter instances.


Reference type Reference variable

For accessing the methods and instance variable are performed with the dot (“.”)
operator
Reference Illustration
Modifiers

Access • Public
Control • Protected
• Private
Modifier

• Static
Other
• Abstract
Modifiers • Final
Constructors

• A constructor is a special kind of method


that is used to initialize a newly created
instance of the class so that it will be in
a consistent and stable initial state.
Constructor
Wrapper Types
STRING CLASS
Creating String

Direct way to create a string:

Create String objects by using the new keyword and a constructor


String Methods

Boolen
int length() String concat()
equals(Object)

static String
char charAt(int String
copyValueOf(char[]
index) toLowerCase()
data)

String
String trim()
toUpperCase()
Review Materi AlPro

CONTROL FLOW DAN


PERULANGAN
If Statement
Control Flow
Yang dieksekusi?

int snowLevel = 3;
Switch Statements
Loop

• Repeats a statement or group of statements while a given

while loop condition is true. It tests the condition before executing the
loop body.

for loop
• Execute a sequence of statements multiple times and
abbreviates the code that manages the loop variable.

do..while loop
• Like a ‘while’ statement, except that it tests the condition at
the end of the loop body.

For-Each
• This is mainly used to traverse collection of elements
including arrays.
For-Each Syntax
Loop Control Statements

• Terminates the loop or switch statement

break and transfers execution to the statement


immediately following the loop or switch.

• Causes the loop to skip the remainder of its

continue body and immediately retest its condition


prior to reiterating.
Review Materi AlPro

METHODS
Creating Method
Method is a collection of statements that are grouped
together to perform an operation.

Syntax:

Other syntax:
Method Example
Method Calling
For using a method, it should be called. There are two
ways in which a method is called i.e., method returns a
value or returning nothing (no return value).

Method calling example


Review Materi AlPro

ARRAY
Array

Array is a sequenced collection of variables all of the same type. Each


variable, or cell, in an array has an index, which uniquely refers to the
value stored in that cell. The cells of an array a are numbered 0, 1, 2,
and so on.
Declaring and Constructing Arrays

• declare a variable (or parameter) to have an array type

• create an array is to use an assignment to a literal form when initially


declaring the array

• create an array is to use the new operator


Data structure

ABSTRACT DATA TYPE


Abstraction

v Deciding what detail to be highlight and what


detail can be ignore

v Underlies computational thinking.


computational thinking and thinking about
computing (The Royal Society, 2008)
Important of Abstraction

Layering • Structure large system into layered components

Reusable • Allow programs code to be more generic/reusable

Separation of • Allow focus of what (specification) to be separated from


how (implementation)
concerns

• Dividing a system into components or modules, each of


Modularity which can be designed, implemented and tested
Procedural Abstraction

Achieve by:
– Threat function as black box
– Give specification of its purpose

input output
Black box

Don’t need to know detail algorithm


Data Abstraction

Focus on what the data structure can do,


rather than how it can be represented

Hidden data public method


public method representation
Key Technique

Data Keeping data and


operation together in
encapsulation one place

Restricting visibilities
Information of implementation
details to where
Hiding needed
Parts of ADT

Declaration of Declaration of
data (values) operations
Primitive Data Type

+ *
integer

- /

~ boolean
||
&&
Primitive ADT

• Types of Operation
– Constructor: object creator
int x[ ] = {1,2,3,4}
– Mutator: object updater
x[3] = 10
– Accessor: object querier
int y = … x[3] …
– Destroyer:object terminator
garbage colector
ADT Example

real

add(c)
Complex(r,i)

complex
minus(c)

times(c)

Imajiner

Complex number
ADT Example

Class Complex {
private … constructor

Complex( real r, real i); // this.r = r; this.i = I


add(Complex c); // this.add(c)
minus(Complex c); // this.minus(c) mutator
times(Complex c); // this.times(c)
getReal(); // return this.r
accessor
getImajiner(); // return this.i
}
ADT PADA STRUKTUR DATA
Data Structure

Data Structure is a way to


store and organize data so
that it can be used
efficiently.
Data Structure Classification
Operations on data structure
1. Traversing: Every data structure contains the set of
data elements. Traversing the data structure means
visiting each element of the data structure in order to
perform some specific operation like searching or
sorting.
2. Insertion: Insertion can be defined as the process of
adding the elements to the data structure at any
location.
3. Deletion: The process of removing an element from
the data structure is called Deletion. We can delete
an element from the data structure at any random
location.
Operations on data structure (2)
4. Searching: The process of finding the location of an
element within the data structure is called Searching.
There are two algorithms to perform searching,
Linear Search and Binary Search. We will discuss each
one of them later in this tutorial.
5. Sorting: The process of arranging the data structure
in a specific order is known as Sorting. There are
many algorithms that can be used to perform
sorting, for example, insertion sort, selection sort,
bubble sort, etc.
6. Merging: When two lists List A and List B of size M
and N respectively, of similar type of elements,
clubbed or joined to produce the third list, List C of
size (M+N), then this process is called merging
Latihan

Buat beberapa program ini dengan


menggunakan Java!
• Pengecekan String palindrome
• Mencari nilai max, min, dan mean pada
array
Next week…

• Konsep Linked List ADT


• Implementasi Linked List

TERIMA KASIH
References

• Data Structures and Algorithms in Java (Sixth Edition),


by Michael T. Goodrich, Roberto Tamassia, Michael H.
Goldwasser

• https://www.tutorialåspoint.com/java/index.htm

You might also like