1 / 26

Pert II: Basic Java Programming

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.

fadey
Download Presentation

Pert II: Basic Java Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. Tujuan Pembelajaran • Pembelajar memahami struktur dasar pemrograman dengan bahasa Java • Memahami variabel dan tipe data

  3. Materi Hari ini • Mengenal Java • Struktur dasar pemrograman Java • Penulisan kode program (coding) • Kompilasi program • Menjalankan program

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

  5. A computer program or application is a set of instructions, written in a programminglanguage that enables a computer to perform some specifiedtask

  6. A program is a sequence of instructions (called programming statements), executing one after another - usually in a sequentialmanne

  7. 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 creatorsidentified three main goals for their new language: • Platform independence—Java programs should be capable of running on any computer. • Security—Java programs should not be susceptible to hackers’ code and dangerous viruses. • Reliability—Java programs should not “crash.”

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

  9. How to program

  10. Struktur Dasar Pemrograman Java

  11. 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

  12. Variable di mata seorang programer 365 JumlahHari 37 suhuTubuh “Jet Lee” Programmer aktorFavorit Note: Variables in algebra are letters of the alphabet, like x and y, but in computer languages, they canalso be any descriptive names like sum, answer, or first_value.

  13. A variable is a named memory location capable of storing data of a specified type

  14. 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.

  15. 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.

  16. Contoh Variable

  17. Pembahasan

  18. Deklarasi Variable • A variable must be declared before it can be used. • A variable declarationspecifi es • the type of data that the variable can hold, for example int or double , and • the name of the variable. • varTypevarName; // Declare a variable of a type • varTypevarName1, varName2,...; // Declare multiple variables of the same type • varTypevarName = initialValue; // Declare a variable of a type, and assign an initial value • varTypevarName1 = initialValue1, varName2 = initialValue2,... ; // Declare variables with initial values

  19. Pembahasan

  20. Mengisi variable dari luar

  21. 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 theair 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 inputrefers to a “ Scanner object.”

  22. 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” programshould always supply a prompt when interactive input is required. It is also a good ideato 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 suppliesan 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 characterother than whitespace (spaces, tabs, or new lines), a runtime error terminates the executionof the program and the system issues an error message. Because the Scanner objectskips 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 computationis 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.

More Related