Professional Documents
Culture Documents
Kelas DDP 2 – D
Pudy Prima
Objectives
○ Understand the basic principle of learning programming languages
○ Understand the difference between Python and Java
○ Learn the basic of Java programming language
○ Understand types of programming errors
Credits
● Liang, Introduction to Java Programming, 11 th Edition, Ch. 1
● Downey & Mayfield, Think Java: How to Think Like a Computer Scientist, Ch.
1
● Slide Kuliah Dasar-Dasar Pemrograman 2 Semester Genap 2019/2020
Outline
○ Motivation: Programming, Java vs Python
○ Introduction to Java
○ Compiling & running a Java program
○ Java Program Structure
○ Java programming convention
○ Programming Errors
Problem Solving
● The single most important skill for a computer scientist is
problem solving.
● It's the ability to:
○ formulate problems,
○ think creatively about solutions, and
○ express solutions clearly and accurately.
● As it turns out, the process of learning to program is an excellent
opportunity to develop problem solving skills.
What did you learn in DDP1?
print('Hello, Python!')
$ python hello.py
What is the output?
Python Program: 2
''' greet.py ''' $ python greet.py
#!/usr/bin/env python3
What is the output?
import sys
for n in numbers:
if n <= minimum:
minimum = n
print('Minimum: {}'.format(str(minimum)))
except ValueError:
print('Error: invalid type of argument')
Procedural Programming
Tipe Data Functions, methods, classes, Java has two kinds of data types:
code blocks, namespaces, 1. primitive types
numbers, strings, and so 2. reference types
forth, are all treated as
Objects. Java supports the following
primitive data types:
Python supports the 1. byte- 8-bit integers
following built-in data types: 2. short- 16-bit integers
1. Plain integers 3. int- 32-bit integers
2. Long integers 4. long- 64-bit integers
3. Booleans 5. float- 32-bit
4. Real numbers 6. double- 32-bit
5. Complex numbers 7. boolean- (false and true)
8. char- a single character
Java vs Python (4)
Python Java
Jalankan compiler
Source code
Java Program Structure
● Suatu program terbuat dari satu atau lebih class
● Suatu class terdiri satu atau lebih method
● Suatu method terdiri dari statement program
● Suatu aplikasi Java selalu terdiri sebuah method bernama main
A Simple Java Program
Listing 1.1
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
A Simple Java Program
Suatu program terbuat dari satu atau lebih class
Suatu class terdiri satu atau lebih method
Suatu method terdiri dari statement program
Suatu aplikasi Java selalu terdiri sebuah method main
COMMENTS BLOCKS
Class Name
● Every Java program must have at least one class.
● Each class has a name.
● By convention, class names start with an uppercase letter.
45
Special Symbols
Character Name Description
" " Opening and closing Enclosing a string (i.e., sequence of characters).
quotation marks
; Semicolon Marks the end of a statement.
Find the Special Symbols
● { … }, ( … ), [ … ], //, “ … ”, ;
• Eclipse
• Intellij
• NetBeans
• ..
• And so many more….
Writing a Java Program
● We could write something like this, but..
● Spacing
End-of-line
style
public class Test {
public static void main(String[] args) {
System.out.println("Block Styles");
}
}
Error types
>>javac ShowSyntaxErrors.java
ShowSyntaxErrors.java:3: error: unclosed string literal
System.out.println("Welcome to Java);
^
1 error
>>java ShowRuntimeErrors
Exception in thread "main" java.lang.ArithmeticException: / by zero
at ShowRuntimeErrors.main(ShowRuntimeErrors.java:3)
Errors occur during running
Logic Errors
public class ShowLogicErrors {
public static void main(String[] args) { It will compile
System.out.println("Average of 1, 2, and 3 is:");
System.out.println(1 + 2 + 3 / 3);
and run
}
}
>>java ShowLogicErrors
Average of 1, 2, and 3 is:
4