NAMA : HAMZAH ALFARIANSYAH
KELAS : XI RPL 1
SCRIPT TIPE DATA DAN OPERATOR
Operator Aritmatika public static void main(String[] args) {
int a, b, c, d, e, f, g;
a = 10;
b = 5;
c = a + b;
d = a - b;
e = a * b;
f = a / b;
g = a % b;
System.out.println("Hasil dari penambahan "+c);
System.out.println("Hasil dari pengurangan "+d);
System.out.println("Hasil dari perkalian "+e);
System.out.println("Hasil dari pembagian "+f);
System.out.println("Hasil dari sisa bagi (modulus)"+g);
}
}
Operator Relasi
public static void main(String[] args) {
int operand1 = 48;
int operand2 = 70;
/**
* Demonstrasi penggunaan Logika AND, hasilnya akan true
* karena kedua operand tersebut bernilai true
*/
boolean test1 = operand1 > 20 && operand2 <= 100;
System.out.println(test1);
/**
* Demonstrasi penggunaan Logika AND, hasilnya akan flase
* karena ada salah satu operand yang bernilai false
*/
boolean test2 = operand1 < 20 && operand2 <= 100;
System.out.println(test2);
}
}
3. Operator Logika
Operator AND
public static void main(String[] args) {
int operand1 = 48;
int operand2 = 70;
/**
* Demonstrasi penggunaan Logika AND, hasilnya akan true
* karena kedua operand tersebut bernilai true
*/
boolean test1 = operand1 > 20 && operand2 <= 100;
System.out.println(test1);
/**
* Demonstrasi penggunaan Logika AND, hasilnya akan flase
* karena ada salah satu operand yang bernilai false
*/
boolean test2 = operand1 < 20 && operand2 <= 100;
System.out.println(test2);
}
}
Operator OR
public static void main(String[] args) {
int operand1 = 80;
int operand2 = 120;
//Demonstrasi1 || (Logika OR)
boolean test1 = (operand1 == 80) || (operand2 < 10);
System.out.println(test1);
//Demonstrasi2 || (Logika OR)
boolean test2 = (operand1 > 100) || (operand2 >= 200);
System.out.println(test2);
}
}
Operator Exclusive OR ( XOR )
public static void main(String[] args) {
int operand1 = 77;
int operand2 = 30;
//Demonstrasi1 ^ (boolean logika exclusive OR)
boolean test1 = (operand1 > 100) ^ (operand2 != operand1);
System.out.println(test1);
//Demonstrasi2 ^ (boolean logika exclusive OR)
boolean test2 = (operand1 < 10) ^ (operand2 > 40);
System.out.println(test2);
}
}
Operator NOT
public static void main(String[] args) {
int operand = 200;
//Demonstrasi1 ! (Logika NOT)
boolean test1 = (operand > 100);
System.out.println(test1);
//Demosntrasi2 ! (Logika NOT)
boolean test2 = (operand > 100);
System.out.println(!test2);
}
}
4. Operator Assignment
public static void main(String[] args) {
int a = 10;
int b, c, d, e, f;
b = 1; c = 2; d = 3; e = 4; f = 5;
b += a;
c -= a;
d *= a;
e /= a;
f %= a;
System.out.println("Hasil dari += adalah "+b);
System.out.println("Hasil dari -= adalah "+c);
System.out.println("Hasil dari *= adalah "+d);
System.out.println("Hasil dari /= adalah "+e);
System.out.println("Hasil dari %= adalah "+f);
}
public static void main(String[] args) {
int a = 10;
int b, c, d, e, f;
b = 1; c = 2; d = 3; e = 4; f = 5;
b += a;
c -= a;
d *= a;
e /= a;
f %= a;
System.out.println("Hasil dari += adalah "+b);
System.out.println("Hasil dari -= adalah "+c);
System.out.println("Hasil dari *= adalah "+d);
System.out.println("Hasil dari /= adalah "+e);
System.out.println("Hasil dari %= adalah "+f);
}
}
Komentar
Posting Komentar