Lab Activity 2. Kontrol Alur, Method dan Array
import
java.util.Scanner; public
class PenjualanTiket { public static void main(String[] args) { int sisaTiket = 10; int tiketTerjual = 0; int jumlahTiket = 10; int pembeli = 0; Scanner keyboard = new
Scanner(System.in); while (tiketTerjual < jumlahTiket
&& sisaTiket > 0) { System.out.print("Masukkan
jumlah tiket yang ingin dibeli: "); int jumlahBeli =
keyboard.nextInt(); if (jumlahBeli > 4) { System.out.println("Anda
tidak bisa membeli lebih dari 4 tiket."); } else if (sisaTiket <
jumlahBeli) {
System.out.println("Maaf, sisa tiket yang tersisa adalah " +
sisaTiket + " tiket."); } else { tiketTerjual += jumlahBeli; sisaTiket -= jumlahBeli; pembeli++; System.out.println("Anda
membeli sebanyak " + jumlahBeli + " tiket."); if (sisaTiket > 0) {
System.out.println("Sisa tiket yang tersedia: " + sisaTiket); } } } if (sisaTiket == 0) { System.out.println("Semua
tiket telah terjual kepada " + pembeli + " pembeli."); } } } |
public
static double getTotal(double[][] arr) { double total = 0.0; for (int row = 0; row < arr.length;
row++) { for (int col = 0; col <
arr[row].length; col++) { total += arr[row][col]; } } return total; } public
static double getAverage(double[][] arr) { double total = getTotal(arr); return total / (arr.length *
arr[0].length); } public
static double getRowTotal(double[][] arr, int row) { double total = 0.0; for (int col = 0; col <
arr[row].length; col++) { total += arr[row][col]; } return total; } public
static double getColumnTotal(double[][] arr, int col) { double total = 0.0; for (int row = 0; row < arr.length;
row++) { total += arr[row][col]; } return total; } public
static double getHighestInRow(double[][] arr, int row) { double highest = arr[row][0]; for (int col = 1; col <
arr[row].length; col++) { if (arr[row][col] > highest) { highest = arr[row][col]; } } return highest; } public
static double getLowestInRow(double[][] arr, int row) { double lowest = arr[row][0]; for (int col = 1; col <
arr[row].length; col++) { if (arr[row][col] < lowest) { lowest = arr[row][col]; } } return lowest; } |
import
java.util.Scanner; public
class NilaiUjian { public static double
hitungRatarata(double n1, double n2, double n3, double n4, double n5) { double ratarata = (n1 + n2 + n3 + n4
+ n5) / 5.0; return ratarata; } public static String tentukanGrade(double
nilai) { if (nilai >= 90) { return "A"; } else if (nilai >= 80) { return "B"; } else if (nilai >= 70) { return "C"; } else if (nilai >= 60) { return "D"; } else { return "E"; } } /**************************************** *
!!! JANGAN UBAH KODE DI BAWAH !!!
*
****************************************/ public static void main(String[] args) { double nilai1; double nilai2; double nilai3; double nilai4; double nilai5; double ratarata; Scanner keyboard = new
Scanner(System.in); System.out.print("Masukkan nilai
ujian ke-1: "); nilai1 = keyboard.nextDouble(); System.out.print("Masukkan nilai
ujian ke-2: "); nilai2 = keyboard.nextDouble(); System.out.print("Masukkan nilai
ujian ke-3: "); nilai3 = keyboard.nextDouble(); System.out.print("Masukkan nilai
ujian ke-4: "); nilai4 = keyboard.nextDouble(); System.out.print("Masukkan nilai
ujian ke-5: "); nilai5 = keyboard.nextDouble(); ratarata = hitungRatarata(nilai1,
nilai2, nilai3, nilai4, nilai5); System.out.println("Rata-rata
nilai ujian = " + ratarata); System.out.println("Grade Anda =
" + tentukanGrade(ratarata)); } } |
public
static int firstDigit(int num) { while
(num >= 10) { num
/= 10; } return
num; } public
static int lastDigit(int num) { return
num % 10; } public
static int banyakDigit(int num) { if
(num == 0) { return
1; } int
count = 0; while
(num != 0) { count++; num
/= 10; } return
count; } |
import
java.util.Scanner; public
class Main { public static void main(String[] args) { Scanner input = new
Scanner(System.in); System.out.print("Masukkan nilai
a: "); int a = input.nextInt(); System.out.print("Masukkan nilai
b: "); int b = input.nextInt(); System.out.print("Masukkan nilai
c: "); int c = input.nextInt(); double diskriminan = b*b - 4*a*c; double x1, x2; if (diskriminan >= 0) { x1 = (-b +
Math.sqrt(diskriminan)) / (2*a); x2 = (-b -
Math.sqrt(diskriminan)) / (2*a); System.out.printf("x1 =
%.2f\n", x1); System.out.printf("x2 =
%.2f\n", x2); } else {
System.out.println("Akar-akar dari persamaan kuadrat adalah
imajiner."); } } } |
public
static int alternatingSum(int[] arr) { int sum = 0; int sign = 1; for (int i = 0; i < arr.length; i++) { sum += sign * arr[i]; sign = -sign; } return sum; } |
import
java.util.Scanner; public
class Main { public static void main(String[] args) { Scanner input = new
Scanner(System.in); System.out.print("Masukkan
sebuah bilangan asli: "); int n = input.nextInt(); if (n <= 1) { System.out.println("Input
tidak valid. Bilangan harus lebih besar dari 1."); return; } for (int i = 2; i <= n; i++) { while (n % i == 0) { System.out.println(i); n /= i; } } } } |
import
java.util.Scanner; public
class Main { public static void main(String[] args) { Scanner input = new
Scanner(System.in); System.out.print("Masukkan berat
paket Anda (kg): "); double berat = input.nextDouble(); System.out.print("Masukkan jarak
pengiriman (km): "); double jarak = input.nextDouble(); int tarifPerKm = tarif(berat); double tarifPengiriman =
Math.floor(jarak) * tarifPerKm; System.out.printf("Tarif
Pengiriman = Rp.%,.2f\n", tarifPengiriman); input.close(); } public static int tarif(double berat) { if (berat <= 2) { return 1500; } if (2 < berat && berat
<= 6) { return 3000; } if (6 < berat && berat
<= 10) { return 5000; } else { return 5500; } } } |
Comments
Post a Comment