Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variabel, Tipe Data & Operator Teguh Sutanto, M.Kom.

Similar presentations


Presentation on theme: "Variabel, Tipe Data & Operator Teguh Sutanto, M.Kom."— Presentation transcript:

1 Variabel, Tipe Data & Operator Teguh Sutanto, M.Kom.

2 Buku Hari ini

3 Menu Hari ini Review basic programing Variable Tipe Data Operator

4 Statement & Ekspression Java statements appear inside methods and classes; they describe all activities of a Java program. Expressions produce values; an expression is evaluated to produce a result that is to be used as part of another expression or in a statement.

5 Variable A variable is a named memory location capable of storing data of a specified type A variable must be declared before it can be used. A variable declaration specifies: the type of data that the variable can hold, for example int or double, and the name of the variable. When naming a variable, you should choose a name that is meaningful. A variable may be declared and given an initial value with a single initialization statement.

6 Declaring Variables must start with a letter (A to Z, a to z) or (unusually) an underscore ‘_’; can contain any number of letters or digits (a digit is 0 to 9); can contain the underscore ‘_’; can be up to 255 characters long. LegalIlegal namaDepan123Nama no_telpno telp min123%area

7 Constant A constant is a value that never changes In Java, a constant is a field with a particular set of modifiers: static and final. Most are also public, though it is possible (and sometimes useful) to have private, package, and protected constants.

8 Tipe Data Isi Data Text CharString Numerik Integer Floating Point Boolean boolean

9 Memasukan Data Dari Luar Program

10 Menggunakan class java.util.Scanner Include the import statement: import java.util.*; Declare a Scanner object as Scanner name = new Scanner(System.in) Methods name.nextLong() //membaca data dengan tipe long name.nextDouble() //membaca data dengan tipe double name.nextFloat() //membaca data dengan tipe float name.nextBoolean() //membaca data dengan tipe boolean name.nextInt() //membaca data dengan tipe int name.nextShort() //memcaca data dengan tipe short

11 Problem Statement Write an application that prompts for the number of pizzas and the number of toppings. The program should calculate the price of the pizza (including sales tax) and print a receipt.

12

13

14 Operator di Java KategoriOperator postfixexpr++ expr-- unary++expr --expr +expr -expr ~ ! casting(type) Multiplicative* / % additive+ - shift > >>> relational = instanceof

15 Operator di Java ( lanjutan …) KategoriOperator equality== != Bitwise AND& bitwise exclusive OR^ bitwise inclusive OR| Logical AND&& logical OR|| Ternary? : assignment= += -= *= /= %= &= ^= |= >= >>>=

16 OpertorNama OperatorKeterangan ++eksPrefix IncrementPenambanan 1 dari nilai ekspresi --eksPrefix DecrementPengurangan 1 dari nilai ekspresi +eksUnary plusMengindikasikan sebuah bilangan positive -eksUnary minusMembuat negative nilai ekspresi ~Bitwise complement Membalik nilai integer per bit !Logical complementMembalik true dan false

17 Multiplicative Operator multiplication (*) division (/) modulus (%).

18 Lat2: Celsius to Fahrenheit The temperature F in Fahrenheit equals (9 / 5) C +32 where C is the Celsius temperature. Write a program that computes and displays the temperatures in Fahrenheit for Celsius values 5, 0, 12, 68, 22.7, 100, and 6.

19 Lat 2: JamMenitDetik Buatlah program yang meminta user untuk memasukan jumlah detik, kemudian program akan menghitung berapa jam, berapa menit dan berapa detik. Diskusikan dengan kelompok anda, kemudian buatlah programnya

20 Lat 3:Uptime The uptime command of the UNIX operating system displays the number of days, hours, and minutes since the operating system was last started. For example, the UNIX command uptime might return the string Up 53 days 12:39 Write a program that converts the 53 days, 12 hours, and 39 seconds to the number of seconds that have elapsed since the operating system was last started. Catatan masukan variable: totalHari totalJam totalMenit

21 Casting Software developers often find that they need a variable of one type to be a variable of another type. In Java (and most programming languages), you can't change the type of a variable. Instead, you should create a new variable and convert the existing variable into the new variable's type. That process is called casting, and Java provides an operator of sorts for doing it. I say, “of sorts,” because the casting operator differs according to the data type to which you're casting.

22 Relational Operators The relational operators compare things to one another. In particular, they determine whether one value is greater than, less than, equal to, or not equal to another value. For the relational operators to work, the items being compared have to be comparable. > (greater than), = (greater than or equal to), and <= (less than or equal to) all work on primitives, but they don't work on objects.

23 RelationOprDemo.java

24 Equality Operators Java has two equality operators: == (equals) != (not equals).

25 Bitwise AND Operator (&) The bitwise AND operator (&) compares two binary values and sets each bit to 1 where each bit in the two values being compared is 1

26 Bitwise AND VariableDesimalBinary bil bil result

27 Bitwise Inclusive OR Operator (|) The bitwise Inclusive OR operator (|) compares two binary values and sets each bit to 1 if either bit is 1

28 Bitwise OR VariableDesimalBinary bil bil result

29 Bitwise Exclusive OR Operator (^) The bitwise Exclusive OR (often shortened to XOR) operator (^) compares two binary values and sets each bit to 1 if the bits differ.

30 Bitwise XOR VariableDesimalBinary bil bil result

31 Logical AND Operator (&&) The logical AND operator (&&) returns true if both arguments are true and false if either one is false

32 Logical OR Operator (||) The logical AND operator (||) returns true if either of its arguments are true and false only if both arguments are false.

33 Assignment Operators The assignment operators set values and assign object references. The basic assignment operator (=) is by far the most often used operator Every time we put something like int = 0 (assigning a value to aprimitive) or Date now = new Date() (assigning an object reference to a variable) into an example, we use the basic assignment operator.

34 OperatorDescription = This is the basic assignment operator. += Add the right-hand operand to the left-hand operand and assign the result to the left-hand operand. x += 2 is the same as x = x = Subtract the right-hand operand from the left-hand operand and assign the result to the lefthand operand. x -= 2 is the same as x = x – 2. *= Multiply the right-hand operand by the left-hand operand and assign the result to the lefthand operand. x *= 2 is the same as x = x * 2. /= Divide the right-hand operand by the left-hand operand and assign the result to the left-hand operand. x /= 2 is the same as x = x / 2. %= Use the right-hand operand to determine the modulus of the left-hand operator and assign the result to the left-hand operator. x %= 2 is the same as x = x % 2.

35 OperatorDescription &= Perform a bitwise and operation and assign the result to the left-hand operator. x &= y is the same as x = x & y. |= Perform a bitwise inclusive operation and assign the result to the left-hand operator. x |= y is the same as x = x | y. ^= Perform a bitwise exxclusive or operation and assign the result to the left-hand operator. x ^= y is the same as x = x ^ y. <<= Perform a bitwise left-shift operation and assign the result to the left-hand operator. x <<= y is the same as x = x << y. >>= Perform a bitwise right-shift operation and assign the result to the left-hand operator. x > y. >>>= Perform a bitwise signed right-shift operation and assign the result to the left- hand operator. x >>>= y is the same as x = x >>> y


Download ppt "Variabel, Tipe Data & Operator Teguh Sutanto, M.Kom."

Similar presentations


Ads by Google