pert ii: basic java programming

Post on 25-Feb-2016

57 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Pert II: Basic Java Programming. Teguh Sutanto, M.Kom. teguh.sutanto@gmail.com teguh@stikom.edu http://blog.stikom.edu/teguh http://teguhsutanto.blogspot.com. Tujuan Pembelajaran. Pembelajar memahami struktur dasar pemrograman dengan bahasa Java Memahami variabel dan tipe data. - PowerPoint PPT Presentation

TRANSCRIPT

Pert II: Basic Java Programming

Teguh Sutanto, M.Kom.teguh.sutanto@gmail.com

teguh@stikom.edu http://blog.stikom.edu/teguh

http://teguhsutanto.blogspot.com

Tujuan Pembelajaran

• Pembelajar memahami struktur dasar pemrograman dengan bahasa Java

• Memahami variabel dan tipe data

Materi Hari ini

• Mengenal Java • Struktur dasar pemrograman Java• Penulisan kode program (coding)• Kompilasi program• Menjalankan program

Sumber/Ref

• www.ntu.edu.sg/home/ehchua/programming/java/J1a_Introduction.html

A computer program or application is a set of instructions,

written in a programminglanguage that enables a computer

to perform some specifi ed task

A program is a sequence of instructions (called programming statements),

executing one after another - usually in a sequential manne

Java?

• Java is a general-purpose language developed by Sun Microsystems in the early 1990s.

• Java was originally designed to program smart consumer electronic devices. Java’s creators identified three main goals for their new language:

1. Platform independence—Java programs should be capable of running on any computer.

2. Security—Java programs should not be susceptible to hackers’ code and dangerous viruses.

3. Reliability—Java programs should not “crash.”

JVM

• Java’s creative team designed an abstract computer implemented in software called the Java Virtual Machine (JVM). You cannot go to a store and buy a JVM computer. Instead you install software on your computer that simulates a JVM computer. The JVM is not a piece of hardware, but it pretends to be one. The machine language of the JVM is called bytecode. Java programs are fi rst compiled into bytecode, and then executed.

How to program

Desidn Code Compile

Struktur Dasar Pemrograman Java

Variable: The Holders of Information

• Sebuah tempat untuk menyimpan data, contoh:• Angka 365 merepresentasikan jumlah hari dalam

satu tahun• Angka 37 derajat celcius menunjukkan suhu

normal tubuh manusia• Nama seorang aktor favorit, Jet Lee

Variable di mata seorang programer

JumlahHari

suhuTubuh

aktorFavoritProgrammer

36537

“Jet Lee”

Variables in algebra are letters of the alphabet, like x and y, but in computer languages, they can also be any descriptive names like sum, answer, or first_value.

Note:

A variable is a named memory location capable of storing data of

a specified type

So...what is Variable?• Computer programs manipulate (or process) data. A variable is used to

store a piece of data for processing. It is called variable because you can change the value stored.

• More precisely, a variable is a named storage location, that stores a value of a particular data type. In other words, a variable has a name, a type and stores a value.

• A variable has a name (or identifier), e.g., radius, area, age, height. The name is needed to uniquely identify each variable, so as to assign a value to the variable (e.g., radius=1.2), and retrieve the value stored (e.g., radius*radius*3.1416).

• A variable has a type. Examples of type are:– int: for integers (whole numbers) such as 123 and -456;– double: for floating-point or real numbers, such as 3.1416, -55.66, having an

optional decimal point and fractional part;– String: for texts such as "Hello", "Good Morning!". Text strings are enclosed

within a pair of double quotes.

• A variable can store a value of that particular type. It is important to take note that a variable in most programming languages is associated with a type, and can only store value of the particular type. For example, a int variable can store an integer value such as 123, but NOT real number such as 12.34, nor texts such as "Hello". The concept of type was introduced into the early programming languages to simplify intrepretation of data made up of 0s and 1s.

Contoh Variable

Pembahasan

Deklarasi Variable• A variable must be declared before it can be used.• A variable declaration specifi es– the type of data that the variable can hold, for example int

or double , and– the name of the variable.

varType varName; // Declare a variable of a type varType varName1, varName2,...; // Declare multiple variables of the same type varType varName = initialValue; // Declare a variable of a type, and assign an initial value varType varName1 = initialValue1, varName2 = initialValue2,... ; // Declare variables with initial values

Pembahasan

Men

gisi

varia

ble

dari

luar

Pembahasan

• Line 7: int chirps;On line 7, we declare an integer variable, chirps , that is intended to hold the number of chirps per minute.

• Line 8: double temperature;The statement on line 8 is also a variable declaration. The variable temperature holds the air temperature. Because the computation of the temperature requires division by 6.6, temperature is declared as double .

• Line 9: Scanner input new Scanner(System.in) ;The name input refers to a “ Scanner object.”

• Line 10: System.out.print("Enter the number of chirps/minute: ");Line 10 is an output statement that prompts the user for data. A “user friendly” program should always supply a prompt when interactive input is required. It is also a good idea to remind the user of the type of units that are expected, that is, chirps/minute rather than chirps/second.

• Line 11: chirps input.nextInt();The statement on line 11 demonstrates the Scanner object in action. The Scanner object, input , accepts or reads one integer from the keyboard. In fact, the program pauses indefinitely until the user types an integer and presses the Enter key. Once the user supplies an integer, that number is assigned to the variable chirps. The Scanner object, input, expects an integer (input. nextInt() ). If the user enters a decimal number or a character other than whitespace (spaces, tabs, or new lines), a runtime error terminates the execution of the program and the system issues an error message. Because the Scanner object skips leading whitespace, a user can legally enter “ 77”—the spaces are ignored.

• Line 12: temperature chirps/6.6 4;The value stored in chirps is used to compute the air temperature. The result of the computation is assigned to the variable temperature.

• Line 13: System.out.println("The temperature is "temperature"C");The program displays the value stored in temperature along with some explanatory text.

top related