1.
// Nama file: BitwiseAnd.java
// Contoh penggunaan operator bitwise And
// -------------------------------------------------------------------------------------------
public class BitwiseAnd
{
public static void main (String[ ] args)
{
int x = 133; // pendefinisian variabel
int y = 35;
int z;
z = x & y;
System.out.println ("Nilai x = " + x);
System.out.println ("Nilai y = " + y);
System.out.println ("x & y = " + z);
}
}
2.
// Nama file: BitwiseOr.java
// Contoh penggunaan operator bitwise Or
// -------------------------------------------------------------------------------------------
public class BitwiseOr
{
public static void main (String[ ] args)
{
int x = 133; // pendefinisian variabel
int y = 35;
int z;
z = x | y;
System.out.println ("Nilai x = " + x);
System.out.println ("Nilai y = " + y);
System.out.println ("x | y = " + z);
}
}
3.
// Nama file: BitwiseXor.java
// Contoh penggunaan operator bitwise Xor
// -------------------------------------------------------------------------------------------
public class BitwiseXor
{
public static void main (String[ ] args)
{
int x = 133; // pendefinisian variabel
int y = 35;
int z;
z = x ^ y;
System.out.println ("Nilai x = " + x);
System.out.println ("Nilai y = " + y);
System.out.println ("x ^ y = " + z);
}
}
4.
// Nama file: Concat.java
// Contoh penggunaan operator penghubung (concatenation)
// -------------------------------------------------------------------------------------------
public class Concat
{
public static void main (String[ ] args)
{
String kata, kata1, kata2, kata3; // pendefinisian variabel
kata1 = "Bahasa ";
kata2 = "Pemrograman ";
kata3 = "Java";
kata = kata1 + kata2 + kata3;
System.out.println (kata);
}
}
5.
// Nama file: Determinan.java
// Perhitungan determinan dengan operator aritmatika
// -------------------------------------------------------------------------------------------
public class Determinan
{
public static void main (String[ ] args)
{
int a, b, c, D; // pendefinisian variabel
a = 2;
b = 3;
c = 10;
D = b * b - 4 * a * c;
System.out.print ("D = " + D);
}
}
6.
// Nama file: GeserKanan.java
// Contoh penggunaan operator bitwise geser kanan (>>)
// -------------------------------------------------------------------------------------------
public class GeserKanan
{
public static void main (String[ ] args)
{
int x = 133; // pendefinisian variabel
int y;
y = x >> 1; // geser kanan 1 bit
System.out.println ("Nilai x semula = " + x);
System.out.println ("Setelah digeser ke kanan 1 bit = " + y);
}
}
7.
// Nama file: GeserKananZ.java
// Contoh penggunaan operator bitwise geser kanan
// -------------------------------------------------------------------------------------------
public class GeserKananZ
{
public static void main (String[ ] args)
{
int x = 133; // pendefinisian variabel
int y;
String hasil = "Setelah digeser kanan 1 bit dan ditambah bit 0 = ";
y = x >>> 1;
System.out.println ("Nilai x semula = " + x);
System.out.println (hasil + y);
}
}
8.
// Nama file: GeserKiri.java
// Contoh penggunaan operator bitwise geser kiri
// ------------------------------------------------------------------------------------------------
public class GeserKiri
{
public static void main (String[ ] args)
{
int x = 133; // pendefinisian variabel
int y;
y = x << 1;
System.out.println ("Nilai x semula = " + x);
System.out.println ("Nilai x setelah digeser ke kiri 1 bit = " + y);
}
}
9.
// Nama file: IncrementDecrement.java
// Contoh penggunaan operator increment dan decrement
// -------------------------------------------------------------------------------------------
public class IncrementDecrement
{
public static void main (String[ ] args)
{
int a, b, c, d; // pendefinisian variabel
int x = 5;
int y = 10;
a = ++x;
b = x++;
c = -- y;
d = y --;
System.out.println (a);
System.out.println (b);
System.out.println (c);
System.out.println (d);
}
}
10.
// Nama file: KonversiTemperatur.java
// Input temperatur Celcius, kemudian dikonvesi ke Reamur dan Fahrenheit
// ---------------------------------------------------------------------------------------
import javax.swing.*;
public class KonversiTemperatur
{
public static void main (String[ ] args)
{
double tempC, tempR, tempF;
String bilangan = JOptionPane.showInputDialog("Input Celcius:");
System.out.println("Input angka: " + bilangan);
tempC = Double.parseDouble(bilangan);
tempR = tempC * 4 / 5 ;
tempF = tempC * 9 / 5 + 32.0;
System.out.println ("Temperatur Celcius = " + tempC);
System.out.println ("Temperatur Reamur = " + tempR);
System.out.println ("Temperatur Fahrenheit = " + tempF);
System.exit(0);
}
}
11.
// Nama file:Logika.java
// Contoh penggunaan operator logika
// -------------------------------------------------------------------------------------------
public class Logika
{
public static void main (String[ ] args)
{
int x = 5; // pendefinisian variabel
int y = 3;
int z = 1;
boolean nilai;
System.out.println ("x = 5, y = 3, z = 1");
nilai = (x > y && z == 1);
System.out.println ("x > y && z == 1 " + nilai);
nilai = (x > y || z > 5);
System.out.println ("x > y || z > 5 " + nilai);
nilai = ( ! (z == 1));
System.out.println ("! (z == 1)) " + nilai);
}
}
12.
// Nama file: Modulus.java
// Penggunaan operator modulus (sisa pembagian)
// -------------------------------------------------------------------------------------------
public class Modulus
{
public static void main (String[ ] args)
{
int a, b; // pendefinisian variabel
double c, d;
a = 9 % 2;
b = 6 % 3;
c = 9.7 % 2;
d = 6 % 3.2;
System.out.println ("9 modulus 2 hasilnya adalah = " + a);
System.out.println ("6 modulus 3 hasilnya adalah = " + b);
System.out.println ("9.7 modulus 2 hasilnya adalah = " + c);
System.out.println ("6 modulus 3.2 hasilnya adalah = " + d);
}
}
13.
// Nama file: Prioritas.java
// Contoh penggunaan operator sehubungan prioritasnya
// ------------------------------------------------------------------------------------------
public class Prioritas
{
public static void main (String[ ] args)
{
int x, y, z; // pendefinisian variabel
x = 4 + 3 * 5;
y = 4 * 3 % 5;
z = 10 / (5 - 3);
System.out.println (x);
System.out.println (y);
System.out.println (z);
}
}
14.
// Nama file: Relasi.java
// Contoh penggunaan operator relasional
// -------------------------------------------------------------------------------------------
public class Relasi
{
public static void main (String[ ] args)
{
boolean nilai; // pendefinisian variabel
nilai = 10 > 5;
System.out.println ("10 > 5 " + nilai);
nilai = 10 < 5;
System.out.println ("10 < 5 " + nilai);
nilai = 10 == 5;
System.out.println ("10 == 5 " + nilai);
nilai = 10 != 5;
System.out.println ("10 != 5 " + nilai);
nilai = 10 <= 5;
System.out.println ("10 <= 5 " + nilai);
nilai = 10 >= 5;
System.out.println ("10 >= 5 " + nilai);
}
}
// Nama file: BitwiseAnd.java
// Contoh penggunaan operator bitwise And
// -------------------------------------------------------------------------------------------
public class BitwiseAnd
{
public static void main (String[ ] args)
{
int x = 133; // pendefinisian variabel
int y = 35;
int z;
z = x & y;
System.out.println ("Nilai x = " + x);
System.out.println ("Nilai y = " + y);
System.out.println ("x & y = " + z);
}
}
2.
// Nama file: BitwiseOr.java
// Contoh penggunaan operator bitwise Or
// -------------------------------------------------------------------------------------------
public class BitwiseOr
{
public static void main (String[ ] args)
{
int x = 133; // pendefinisian variabel
int y = 35;
int z;
z = x | y;
System.out.println ("Nilai x = " + x);
System.out.println ("Nilai y = " + y);
System.out.println ("x | y = " + z);
}
}
3.
// Nama file: BitwiseXor.java
// Contoh penggunaan operator bitwise Xor
// -------------------------------------------------------------------------------------------
public class BitwiseXor
{
public static void main (String[ ] args)
{
int x = 133; // pendefinisian variabel
int y = 35;
int z;
z = x ^ y;
System.out.println ("Nilai x = " + x);
System.out.println ("Nilai y = " + y);
System.out.println ("x ^ y = " + z);
}
}
4.
// Nama file: Concat.java
// Contoh penggunaan operator penghubung (concatenation)
// -------------------------------------------------------------------------------------------
public class Concat
{
public static void main (String[ ] args)
{
String kata, kata1, kata2, kata3; // pendefinisian variabel
kata1 = "Bahasa ";
kata2 = "Pemrograman ";
kata3 = "Java";
kata = kata1 + kata2 + kata3;
System.out.println (kata);
}
}
5.
// Nama file: Determinan.java
// Perhitungan determinan dengan operator aritmatika
// -------------------------------------------------------------------------------------------
public class Determinan
{
public static void main (String[ ] args)
{
int a, b, c, D; // pendefinisian variabel
a = 2;
b = 3;
c = 10;
D = b * b - 4 * a * c;
System.out.print ("D = " + D);
}
}
6.
// Nama file: GeserKanan.java
// Contoh penggunaan operator bitwise geser kanan (>>)
// -------------------------------------------------------------------------------------------
public class GeserKanan
{
public static void main (String[ ] args)
{
int x = 133; // pendefinisian variabel
int y;
y = x >> 1; // geser kanan 1 bit
System.out.println ("Nilai x semula = " + x);
System.out.println ("Setelah digeser ke kanan 1 bit = " + y);
}
}
7.
// Nama file: GeserKananZ.java
// Contoh penggunaan operator bitwise geser kanan
// -------------------------------------------------------------------------------------------
public class GeserKananZ
{
public static void main (String[ ] args)
{
int x = 133; // pendefinisian variabel
int y;
String hasil = "Setelah digeser kanan 1 bit dan ditambah bit 0 = ";
y = x >>> 1;
System.out.println ("Nilai x semula = " + x);
System.out.println (hasil + y);
}
}
8.
// Nama file: GeserKiri.java
// Contoh penggunaan operator bitwise geser kiri
// ------------------------------------------------------------------------------------------------
public class GeserKiri
{
public static void main (String[ ] args)
{
int x = 133; // pendefinisian variabel
int y;
y = x << 1;
System.out.println ("Nilai x semula = " + x);
System.out.println ("Nilai x setelah digeser ke kiri 1 bit = " + y);
}
}
9.
// Nama file: IncrementDecrement.java
// Contoh penggunaan operator increment dan decrement
// -------------------------------------------------------------------------------------------
public class IncrementDecrement
{
public static void main (String[ ] args)
{
int a, b, c, d; // pendefinisian variabel
int x = 5;
int y = 10;
a = ++x;
b = x++;
c = -- y;
d = y --;
System.out.println (a);
System.out.println (b);
System.out.println (c);
System.out.println (d);
}
}
10.
// Nama file: KonversiTemperatur.java
// Input temperatur Celcius, kemudian dikonvesi ke Reamur dan Fahrenheit
// ---------------------------------------------------------------------------------------
import javax.swing.*;
public class KonversiTemperatur
{
public static void main (String[ ] args)
{
double tempC, tempR, tempF;
String bilangan = JOptionPane.showInputDialog("Input Celcius:");
System.out.println("Input angka: " + bilangan);
tempC = Double.parseDouble(bilangan);
tempR = tempC * 4 / 5 ;
tempF = tempC * 9 / 5 + 32.0;
System.out.println ("Temperatur Celcius = " + tempC);
System.out.println ("Temperatur Reamur = " + tempR);
System.out.println ("Temperatur Fahrenheit = " + tempF);
System.exit(0);
}
}
11.
// Nama file:Logika.java
// Contoh penggunaan operator logika
// -------------------------------------------------------------------------------------------
public class Logika
{
public static void main (String[ ] args)
{
int x = 5; // pendefinisian variabel
int y = 3;
int z = 1;
boolean nilai;
System.out.println ("x = 5, y = 3, z = 1");
nilai = (x > y && z == 1);
System.out.println ("x > y && z == 1 " + nilai);
nilai = (x > y || z > 5);
System.out.println ("x > y || z > 5 " + nilai);
nilai = ( ! (z == 1));
System.out.println ("! (z == 1)) " + nilai);
}
}
12.
// Nama file: Modulus.java
// Penggunaan operator modulus (sisa pembagian)
// -------------------------------------------------------------------------------------------
public class Modulus
{
public static void main (String[ ] args)
{
int a, b; // pendefinisian variabel
double c, d;
a = 9 % 2;
b = 6 % 3;
c = 9.7 % 2;
d = 6 % 3.2;
System.out.println ("9 modulus 2 hasilnya adalah = " + a);
System.out.println ("6 modulus 3 hasilnya adalah = " + b);
System.out.println ("9.7 modulus 2 hasilnya adalah = " + c);
System.out.println ("6 modulus 3.2 hasilnya adalah = " + d);
}
}
13.
// Nama file: Prioritas.java
// Contoh penggunaan operator sehubungan prioritasnya
// ------------------------------------------------------------------------------------------
public class Prioritas
{
public static void main (String[ ] args)
{
int x, y, z; // pendefinisian variabel
x = 4 + 3 * 5;
y = 4 * 3 % 5;
z = 10 / (5 - 3);
System.out.println (x);
System.out.println (y);
System.out.println (z);
}
}
14.
// Nama file: Relasi.java
// Contoh penggunaan operator relasional
// -------------------------------------------------------------------------------------------
public class Relasi
{
public static void main (String[ ] args)
{
boolean nilai; // pendefinisian variabel
nilai = 10 > 5;
System.out.println ("10 > 5 " + nilai);
nilai = 10 < 5;
System.out.println ("10 < 5 " + nilai);
nilai = 10 == 5;
System.out.println ("10 == 5 " + nilai);
nilai = 10 != 5;
System.out.println ("10 != 5 " + nilai);
nilai = 10 <= 5;
System.out.println ("10 <= 5 " + nilai);
nilai = 10 >= 5;
System.out.println ("10 >= 5 " + nilai);
}
}
0 komentar:
Posting Komentar