Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Variabel, Tipe Data & Operator Teguh Sutanto, M.Kom. Buku Hari ini Menu Hari ini • Review basic programing • Variable • Tipe Data • Operator 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. 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. 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. Legal namaDepan no_telp min123 Ilegal 123Nama no telp %area 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. Tipe Data Isi Data Boolean boolean Numerik Floating Point Integer Text String Char Memasukan Data Dari Luar Program 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 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. Operator di Java Kategori Operator postfix unary casting Multiplicative additive shift relational expr++ expr-++expr --expr +expr -expr ~ ! (type) */% +<< >> >>> < > <= >= instanceof Operator di Java (lanjutan…) Kategori Operator equality Bitwise AND bitwise exclusive OR bitwise inclusive OR Logical AND logical OR Ternary assignment == != & ^ | && || ?: = += -= *= /= %= &= ^= |= <<= >>= >>>= Opertor ++eks --eks +eks Nama Operator Prefix Increment Prefix Decrement Unary plus -eks ~ Unary minus Bitwise complement Logical complement Membalik true dan false ! Keterangan Penambanan 1 dari nilai ekspresi Pengurangan 1 dari nilai ekspresi Mengindikasikan sebuah bilangan positive Membuat negative nilai ekspresi Membalik nilai integer per bit Multiplicative Operator multiplication (*) division (/) modulus (%). 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. 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 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 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. 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), < (less than), >= (greater than or equal to), and <= (less than or equal to) all work on primitives, but they don't work on objects. RelationOprDemo.java Equality Operators • Java has two equality operators: • == (equals) • != (not equals). 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 Bitwise AND Variable Desimal Binary bil1 85 1 0 1 0 1 0 1 bil2 63 0 1 1 1 1 1 1 result 21 0 0 1 0 1 0 1 Bitwise Inclusive OR Operator (|) • The bitwise Inclusive OR operator (|) compares two binary values and sets each bit to 1 if either bit is 1 Bitwise OR Variable Desimal Binary bil1 85 1 0 1 0 1 0 1 bil2 63 0 1 1 1 1 1 1 result 127 1 1 1 1 1 1 1 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. Bitwise XOR Variable Desimal Binary bil1 85 1 0 1 0 1 0 1 bil2 63 0 1 1 1 1 1 1 result 106 1 1 0 1 0 1 0 Logical AND Operator (&&) • The logical AND operator (&&) returns true if both arguments are true and false if either one is false Logical OR Operator (||) • The logical AND operator (||) returns true if either of its arguments are true and false only if both arguments are false. 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. Operator = Description 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 + 2. -= 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. Operator &= Description 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 is the same as x = x >> y. >>>= Perform a bitwise signed right-shift operation and assign the result to the lefthand operator. x >>>= y is the same as x = x >>> y