Вы находитесь на странице: 1из 11

Nama: Jansutris Apriten Purba

NIM: 2201866806

Mata Kuliah: Introduction to Programming

Kode: OS1 - 1822 - DQBA

Tugas Personal ke-1

(Minggu 2 / Sesi 3)

1. Jelaskan dan berikan contoh Operator Logical

2. Jelaskan dan berikan contoh operator Relational

3. Berapakah ouput dari program berikut :

Int A=10,B=2, C=4


D = A & B + A++ * C;
System.out.println(“Nilai D = ”+D);
D = ++A % B | C;
System.out.println(“Nilai D = ”+D);
D = A++ % B- - * ++C;
System.out.println(“Nilai D = ”+D);
D = C & B / A;
System.out.println(“Nilai D = ”+D);

4. Buatlah Program yang dapat menerima input dari user berupa dua bilangan, dan
menghitung :

a. Nilai terbesar
b. Nilai terkecil
c. Hasil pangkat dari bilangan 1 dipangkat bilangan 2
d. Hasil kuadrat dari bilangan 1 dan bilangan 2

5. Buatlah program yang dapat menerima input dari user kemudian menghasilkan bilangan
random . Apabila bilangan random yang dihasilkan adalah bilangan ganjil, maka bilangan
tersebut ditambah dengan 1

Contoh Tampilan

COMP6598 – Introduction to Programming


Jawaban

1. The Logical Operators


The following table lists the logical operators:

Operator Description Example

Called Logical AND operator. If both the operands


&& (logical and) (A && B) is false
are non-zero, then the condition becomes true.

Called Logical OR Operator. If any of the two


|| (logical or) operands are non-zero, then the condition becomes (A || B) is true
true.

Called Logical NOT Operator. Use to reverses the


! (logical not) logical state of its operand. If a condition is true !(A && B) is true
then Logical NOT operator will make false.

Example: Assume Boolean variables A holds true and variable B holds false, then:

package binus_online_learning;

/**

* @author COMPUTER

*/

COMP6598 – Introduction to Programming


public class Logical_Operator {

public static void main (String[] args)

boolean a = true;

boolean b = false;

System.out.println("a && b = " +(a&&b));

System.out.println("a|| b = " +(a || b));

System.out.println("!a && b = " + !(a && b));

Output:

2. The Relational Operators


There are following relational operators supported by Java language.

Assume variable A holds 10 and variable B holds 20, then:

Operator Description Example

Checks if the values of two operands are equal or not, if yes (A == B) is not
== (equal to)
then condition becomes true. true.

Checks if the values of two operands are equal or not, if


!= (not equal to) (A != B) is true.
values are not equal then condition becomes true.

Checks if the value of left operand is greater than the value (A > B) is not
> (greater than)
of right operand, if yes then condition becomes true. true.

COMP6598 – Introduction to Programming


Checks if the value of left operand is less than the value of
< (less than) (A < B) is true.
right operand, if yes then condition becomes true.

Checks if the value of left operand is greater than or equal


>= (greater than (A >= B) is not
to the value of right operand, if yes then condition becomes
or equal to) true.
true.

Checks if the value of left operand is less than or equal to


<= (less than or (A <= B) is
the value of right operand, if yes then condition becomes
equal to) true.
true.

Example: Assume variable A holds 10 and variable B holds 20:

COMP6598 – Introduction to Programming


package binus_online_learning;

/**

* @author COMPUTER

*/

public class Relational_Operator {

public static void main (String[] args)

int a = 10;

int b = 20;

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

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

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

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

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

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

COMP6598 – Introduction to Programming


Output:

3. Kode Program

package binus_online_learning;

/**

* @author COMPUTER

*/

public class Soal_3 {

public static void main (String[] args)

int a =10, b=2, c=4, d;

d = a&b + a++ * c;

System.out.println("Nilai D= "+d);

d= ++a %b | c;

System.out.println("Nilai D= "+d);

d= a++ %b-- * ++c;

System.out.println("Nilai D= " +d);

d= c & b/a;

COMP6598 – Introduction to Programming


System.out.println("Nilai D= " +d);

Output:

4. Kode Program

package binus_online_learning;

import java.util.Scanner;

import java.math.*;

import static java.time.Clock.system;

/**

* @author COMPUTER

*/

public class Soal_4 {

public static void main (String[] args)

int a,b, hasil_Pangkat, a_Kuadrat, b_Kuadrat;

Scanner sc = new Scanner (System.in);

System.out.println("Masukkan nilai A: ");

COMP6598 – Introduction to Programming


a = sc.nextInt();

System.out.println("Masukkan nilai B: ");

b = sc.nextInt();

/* Jawaban dari Soal 4a dan 4b */

if (a>b)

System.out.println("A adalah Nilai yang terbesar");

System.out.println("B adalah Nilai yang terkecil");

else{

System.out.println("B adalah Nilai yang terbesar");

System.out.println("A adalah Nilai yang terkecil");

/* Jawaban dari soal 4c */

hasil_Pangkat = (int) Math.pow(a, b);

System.out.println("Hasil Perpangangkatan dari bilangan 1


dipangkat bilangan 2= " +hasil_Pangkat);

/* Jawaban dari soal 4d */

a_Kuadrat= (int) Math.pow(a,2);

b_Kuadrat= (int) Math.pow(b,2);

System.out.println("Hasil kuadrat dari bilangan 1= "+a_Kuadrat);

System.out.println("Hasil kuadrat dari bilangan 2= "+b_Kuadrat);

COMP6598 – Introduction to Programming


}

Output:

5. Kode Program

package binus_online_learning;

import java.util.Scanner;

import java.math.*;

import java.util.Random;

/**

* @author COMPUTER

*/

public class Soal_5 {

public static void main (String[] args)

int a,b, random_plus_satu, hasil_akhir;

Scanner sc = new Scanner(System.in);

COMP6598 – Introduction to Programming


System.out.println("Masukkan bilangan A = ");

a = sc.nextInt();

System.out.println("Masukkan bilangan B = ");

b = sc.nextInt();

Random angkaRandom = new Random();

int hasil;

/* Melakukan loop 1 kali */

for ( int counter = 0; counter <= 0; counter++ ){

/*

Random dari 1-9

*/

hasil = 1 + angkaRandom.nextInt(100);

/* Method nextInt() akan menghasilkan angka random berkisar

dari range –2,147,483,648 sampai

+2,147,483,647*/

/*

hasil = angkaRandom.nextInt();

*/

System.out.printf("Angka random = %d\n", hasil);

if(hasil %2 == 1)

hasil_akhir = hasil+1;

COMP6598 – Introduction to Programming


System.out.println("\nHasil Random Bilangan Genap =
"+hasil_akhir);

Output:

COMP6598 – Introduction to Programming

Вам также может понравиться