Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Latihan Java #2

Uploaded by

rantitriana15
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Latihan Java #2

Uploaded by

rantitriana15
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Praktikum Pemrograman

--:: Tipe Data-Variabel-Ekspresi ::--

--java03--

package javadasar;
public class java03 {
public static void main(String[] args) {

//Mencetak tipe data langsung dgn Println


System.out.println('Q'); //tipe data char
System.out.println("Coba dulu baru
komen...."); //tipe data String
System.out.println(10);//tipe data int
System.out.println(70.9);//tipe data double
System.out.println(true);//tipe data
boolean
}
}

--java04--

package javadasar;
public class java04 {
public static void main(String[] args) {
/*
Membuat variabel dengan assignment
char huruf;
huruf = 'Z'; --> assignment
*/
char huruf = 'Z';
String nama = "Jagoan Neon";
int usia = 20;
double angka = 77.7;
boolean logik = true;

System.out.println("Variabel di Java");

System.out.println("------------------------");
System.out.println("Hurufnya\t: "+huruf);
System.out.println("Namanya\t: "+nama);
System.out.println("Usianya\t: "+usia+"
tahun");
System.out.println("Angkanya\t: "+angka);
System.out.println("Logikanya\t: "+logik);
}
}

Netbeans IDE - Java 3


Praktikum Pemrograman

--java05--

package javadasar;
public class java05 {
public static void main(String[] args) {
//siapkan variabel
int Z = 10, Y = 5, X;
//rumusnya
X = Z + Y;

System.out.println("Bilangan Z = "+Z);
System.out.println("Bilangan Y = "+Y);
System.out.println("Z + Y = X");
System.out.println(Z+" + "+Y+ " = "+X);
//tanpa variabel
System.out.println(Z+" + "+Y+ " = "+
(Z+Y));

}
}

--java06--

package javadasar;
public class java06 {
public static void main(String[] args) {
final double pi = 3.14;//nilai konstanta
double jari = 35.5, keliling;
//rumus
keliling = 2*pi*jari;
//tampilan
System.out.println("Menghitung Lingkaran");
System.out.println("PI = "+pi);
System.out.println("Jari-jari "+jari);
System.out.println("-----------------------
--");
System.out.println("Keliling Lingkaran =
"+keliling);
}
}

Netbeans IDE - Java 4


Praktikum Pemrograman

--:: Operator ::--

--java07--

package javadasar;
public class java07 {
public static void main (String[] args) {
//siapkan variabel
int a = 10, b = 2;
int tambah, kurang, kali, bagi, mod;
//operator aritmatika > + - * / %
tambah = a + b;
kurang = a - b;
kali = a * b;
bagi = a / b;
mod = a % b;

System.out.println("A = "+a);
System.out.println("B = "+b);
System.out.println("Operator aritmatika :
+ - * / %");
System.out.println(a+" + "+b+ " =
"+tambah);
System.out.println(a+" - "+b+ " =
"+kurang);
System.out.println(a+" x "+b+ " = "+kali);
System.out.println(a+" : "+b+ " = "+bagi);
System.out.println(a+" % "+b+ " = "+mod);
}
}
/*
Ekspresi = operand + operator
operand = nilai, variabel, angka, jumlah
operator aritmatika = + - * / %
*/

--java08a--

package javadasar;
public class java08a {
public static void main (String[] args) {
//operator increment operands++
int a = 10, b = 5;
//increment nilai a dan b
System.out.println("Nilai a = 10");
a++;
System.out.println("Nilai a++ = " +a);

System.out.println("Nilai b = 5");
b = b + 1;
System.out.println("Nilai b = b + 1 =
"+b);
}
}

Netbeans IDE - Java 5


Praktikum Pemrograman

--java08b--

package javadasar;
public class java08b {
public static void main (String[] args) {
//operator decrement operands--
int a = 10, b = 5;
//decrement nilai a dan b
System.out.println("Nilai a = 10");
a--;
System.out.println("Nilai a-- = " +a);

System.out.println("Nilai b = 5");
b = b - 1;
System.out.println("Nilai b = b - 1 =
"+b);
}
}

--java08c--

package javadasar;
public class java08c {
public static void main (String[] args) {
//operator penugasan +=, -=, *=, /=, %=
int a = 10, b = 5, c = 8, d = 2;
//penugasan nilai a dan b
System.out.println("Nilai a = 10, b = 5");
System.out.println("Nilai c = 8, d = 2");
a += b; //artinya a = a + b
System.out.println("Nilai a += b : " +a);
c -= d; //artinya c = c - d
System.out.println("Nilai c -= d : "+c);
//coba contoh lain utk penugasan yg
tersisa
}
}

--java09--

package javadasar;
public class java09 {
public static void main (String[] args) {
//operator relasi ==, !=, >, <, >=, <=
int a = 10, b = 5;

System.out.println("Nilai a == b : "+(a ==
b));
System.out.println("Nilai a != b : "+(a !=
b));
System.out.println("Nilai a > b : "+(a >
b));
System.out.println("Nilai a < b : "+(a <

Netbeans IDE - Java 6


Praktikum Pemrograman

b));
System.out.println("Nilai a >= b : "+(a >=
b));
System.out.println("Nilai a <= b : "+(a <=
b));

}
}

--java10--

package javadasar;
public class java10 {
public static void main (String[] args) {
//operator logika || && !
boolean a = true, b = true, c, d, e;
System.out.println("OR ||, AND &&, NEGASI
!");
c = a || b;
System.out.println("a || b = "+c); // ||
OR
d = a && b;
System.out.println("a && b = "+d); // &&
AND
e = !(a && b);
System.out.println("!(a && b) = "+
e); // ! Negasi
}
}

--java11--

package javadasar;
public class java11 {
public static void main (String[] args) {
//operator bitwise & | ^ ~ << >> --> untuk
bit
int a = 20; //biner 0001 0100
int b = 10; //biner 0000 1010
int c = 0, d; // biner 0000 0000

c = a & b;
d = a | b;
System.out.println(c);
System.out.println(d);
//untuk operator lainnya bisa dicoba
sendiri
}
}

Netbeans IDE - Java 7

You might also like