02 java language and oop part ii lab
DESCRIPTION
Java Language And OOP Part II LABTRANSCRIPT
Java Language and OOP Part II LAB
By Hari Christian
Agenda
• 01 Operator in Java• 02 Operator - Assignment• 03 Operator - Relational• 04 Operator - Arithmetic• 05 Operator - Conditional• 06 Operator - Bitwise• 07 Operator - Logical• 08 Operator - Precedence• 09 Operator - Associativity
Agenda
• 10 Selection Statement - If• 11 Selection Statement - Switch• 12 Selection Statement - Conditional• 13 Looping Statement - For• 14 Looping Statement - While• 15 Looping Statement - Do While• 16 Continue Statement• 17 Break Statement• 18 Basic of Arrays• 19 Arrays of Arrays
Lab1 – Operator Arithmetic
public class Lab1 {
public static void main(String[] args) {
int a = 5;
int b = 10;
int tambah = a + b;
int kurang = a – b;
int kali = a * b;
int bagi = a / b;
}
}
Lab2 – Operator Arithmetic
public class Lab2 {
public static void main(String[] args) {
int a = 5;
int tambah = a += 5;
int kurang = a –= 2;
int kali = a *= 5;
int bagi = a /= 1;
}
}
Lab3 – Operator Relational
public class Lab3 {
public static void main(String[] args) {
int num1 = 5;
if (num1 < 5) {
System.out.println(“Less than 5”);
}
if (num1 > 5) {
System.out.println(“More than 5”);
}
}
}
Lab4 – Operator Relational
public class Lab4 {
public static void main(String[] args) {
int num1 = 5;
if (num1 <= 5) {
System.out.println(“Less than or equal 5”);
}
if (num1 >= 5) {
System.out.println(“More than or equal 5”);
}
}
}
Lab5 – Operator Relational
public class Lab5 {
public static void main(String[] args) {
int num1 = 5;
if (num1 == 5) {
System.out.println(“Equal 5”);
}
if (num1 != 5) {
System.out.println(“Not equal 5”);
}
}
}
Lab6 – Operator Ternary
public class Lab6 {
public static void main(String[] args) {
int a = 5;
int num1 = (a == 5) ? 0 : 1;
int num2 = (a != 5) ? 0 : 1;
int num3 = (a < 5) ? 0 : 1;
int num4 = (a > 5) ? 0 : 1;
}
}
Lab7 – Operator Bitwise
public class Lab7 {
public static void main(String[] args) {
int a = 60; /* 60 = 0011 1100 */
int b = 13; /* 13 = 0000 1101 */
int c = 0;
c = a & b; /* 12 = 0000 1100 */
c = a | b; /* 61 = 0011 1101 */
c = a ^ b; /* 49 = 0011 0001 */
c = ~a; /*-61 = 1100 0011 */
c = a << 2; /* 240 = 1111 0000 */
c = a >> 2; /* 215 = 1111 */
c = a >>> 2; /* 215 = 0000 1111 */
}
}
Lab8 – Operator Logical And
public class Lab8 {
public static void main(String[] args) {
int num = 5;
if (num == 6 && num > 10) { // false and false
System.out.println(“Hari Christian”);
}
if (num == 6 && num > 2) { // false and true
System.out.println(“Hari Christian”);
}
}
}
Lab9 – Operator Logical And
public class Lab9 {
public static void main(String[] args) {
int num = 5;
if (num == 5 && num > 10) { // true and false
System.out.println(“Hari Christian”);
}
if (num == 5 && num > 2) { // true and true
System.out.println(“Hari Christian”);
}
}
}
Lab10 – Operator Logical Or
public class Lab10 {
public static void main(String[] args) {
int num = 5;
if (num == 6 || num > 10) { // false and false
System.out.println(“Hari Christian”);
}
if (num == 6 || num > 2) { // false and true
System.out.println(“Hari Christian”);
}
}
}
Lab11 – Operator Logical Or
public class Lab11 {
public static void main(String[] args) {
int num = 5;
if (num == 5 || num > 10) { // true and false
System.out.println(“Hari Christian”);
}
if (num == 5 || num > 2) { // true and true
System.out.println(“Hari Christian”);
}
}
}
Lab12 – Selection If
public class Lab12 {
public static void main(String[] args) {
int number = 5;
if (number == 5) {
System.out.println(“1”);
}
if (number > 2) {
System.out.println(“2”);
}
if (number > 5) {
System.out.println(“3”);
} else {
System.out.println(“4”);
}
}
}
Lab13 – Selection If
public class Lab13 {
public static void main(String[] args) {
int number = 5;
if (number == 5) {
System.out.println(“1”);
} else if (number > 2) {
System.out.println(“2”);
} else if (number > 4) {
System.out.println(“3”);
} else {
System.out.println(“4”);
}
}
}
Lab14 – Selection Switch
public class Lab14 {
public static void main(String[] args) {
int number = 2;
String a = “”;
switch (x) {
case 1: a = “1”; break;
case 2: a = “2”; break;
case 3: a = “3”; break;
case 4: a = “4”; break;
case 5: a = “5”; break;
default: a = “6”; break;
}
}
}
Lab15 – Looping For
public class Lab15 {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println(“Print ke = ” + i);
}
}
}
Lab16 – Looping While
public class Lab16 {
public static void main(String[] args) {
int i = 0;
while (i < 10) {
System.out.println(“Print ke = ” + i);
i++;
}
}
}
Lab17 – Looping Do While
public class Lab17 {
public static void main(String[] args) {
int i = 0;
do {
System.out.println(“Print ke = ” + i);
i++;
} while (i < 10) ;
}
}
Lab18 – Looping Continue
public class Lab18 {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
System.out.println(“Print ke = ” + i);
}
}
}
Lab19 – Looping Break
public class Lab19 {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
System.out.println(“Print ke = ” + i);
}
}
}
Thank You