Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#Compiled class file
*.class
41 changes: 41 additions & 0 deletions ArrayBubbleSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
public class ArrayBubbleSort {
public static void main (String[] args) {
if (args.length < 1) {
System.err.println("Array of Number to be sorted");
return;
}

//https://en.wikipedia.org/wiki/Bubble_sort

// int[] arrayBubble -> Mendeklarasi variabel array
// arrayBubble = new int[10] -> Menginstansiasi object array
int[] arrayBubble = new int[args.length+1];
for (int i = 0; i < args.length; i++) {
arrayBubble[i] = Integer.parseInt(args[i]);
}

//boolean swapped = false;
int swapped;
do {
swapped = 0;
for (int i = 0; i < arrayBubble.length - 1; i++) {
if (arrayBubble[i] > arrayBubble[i+1] ) {
// sort tanpa temp -> pakai XOR
int temp = arrayBubble[i];
arrayBubble[i] = arrayBubble[i+1];
arrayBubble[i+1] = temp;
swapped ++;
}
}

// for (int i = 1; i < arrayBubble.length; i++) {
// System.out.print(arrayBubble[i] + " ");
//}
//System.out.println("");
} while (swapped != 0);

for (int i = 1; i < arrayBubble.length; i++) {
System.out.print(arrayBubble[i] + " ");
}
}
}
16 changes: 16 additions & 0 deletions ForFactorial.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class ForFactorial {
public static void main (String[] args){
if (args.length != 1) {
System.err.println("ForFactorial <number>");
return;
}


int number = Integer.parseInt(args[0]);
long ansFactorial = 1;
for (long i = 1; i <= number ; i++ ) {
ansFactorial *= i;
}
System.out.println("Factorial: " + ansFactorial);
}
}
28 changes: 28 additions & 0 deletions ForFactorialRecursive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
public class ForFactorialRecursive {
public static void main (String[] args){
if (args.length != 1) {
System.err.println("ForFactorial <number>");
return;
}

int number = Integer.parseInt(args[0]);
ForFactorialRecursive hello = new ForFactorialRecursive();
int ans = hello.factorial(number);
System.out.println("Factorial Recursive: " + ans);
}

/*cara memanggil fungsi dalam main:
https://www.homeandlearn.co.uk/java/java_method_calling.html
1. function dibuat static
2. memanggil di main dengan "new HelloWorld().factorial"
*/

public int factorial (int n) {
if (n >= 1) {
return (n * factorial(n-1));
}
else {
return 1;
}
}
}
5 changes: 5 additions & 0 deletions HelloWorld.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class HelloWorld {
public static void main (String[] args){
System.out.println("Hello World!");
}
}
37 changes: 37 additions & 0 deletions IfNilai.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
public class IfNilai {
// ambil inputan melalui args

public static void main(String[] args){
int score;

if (args.length != 1){
System.out.println("IfNilai <nilai>");
}
//else {
score = Integer.parseInt(args[0]);
//}




//if 0-20 E, 21-40 D, 41-60 C, 61-80 B, 81-100 A
if (score >=0 && score <=20) {
System.out.println("Nilai E");
}
else if (score >=21 && score <=40) {
System.out.println("Nilai D");
}
else if (score >=41 && score <=60) {
System.out.println("Nilai C");
}
else if (score >=61 && score <=80) {
System.out.println("Nilai B");
}
else if (score >=81 && score <=100) {
System.out.println("Nilai A");
}
else {
System.out.println("Nilai tidak tahu!");
}
}
}
30 changes: 30 additions & 0 deletions ListTestCollection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.util.*;

public class ListTestCollection {
public static void main (String[] args) {
List<String> names = new ArrayList<>();
names.add("ABC");
names.add("Budi");
names.add("Andy");

for (String name : names) {
System.out.println(name);
}

Set<String> stringSet = new HashSet<>();
stringSet.add("String 1");
stringSet.add("String 1");
for (String string : stringSet) {
System.out.println(string);
}

Map<String, Integer> scoreByName = new HashMap<>();
scoreByName.put("Budi", 100);
scoreByName.put(null, 2);
scoreByName.put("Budi", 43);

for(Map.Entry<String, Integer> entry : scoreByName.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
47 changes: 47 additions & 0 deletions SwitchJumlahHari.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
public class SwitchJumlahHari {
public static void main (String[] args){
if (args.length != 2){
System.err.println("SwitchJumlahHari <bulan> <tahun> dalam angka");
return;
}

//jumlah hari dalam bulan (termasuk kabisat hanya kelipatan 4)
int bulan = Integer.parseInt(args[0]);
int tahun = Integer.parseInt(args[1]);
int jumlahHari;

// https://www.geeksforgeeks.org/switch-statement-in-java/

switch (bulan){
case 4:
case 6:
case 9:
case 11:
jumlahHari = 30;
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
jumlahHari = 31;
break;
case 2:
//https://en.wikipedia.org/wiki/Leap_year
// if (tahun % 4 == 0) jumlahHari = 29;
// else jumlahHari = 28;
if ((tahun % 4 == 0) && !(tahun % 100 == 0) && (tahun % 400 == 0)) {
jumlahHari = 29;
}
else jumlahHari = 28;

break;
default :
jumlahHari = 0;
}

System.out.println("Terdapat " + jumlahHari + " hari");
}
}
55 changes: 55 additions & 0 deletions SwitchKalender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
public class SwitchKalender {
public static void main(String[] args){

if (args.length != 1) {
System.out.println("Masukkan bulan");
}

int bulan = Integer.parseInt(args[0]);
String namaBulan;
switch(bulan) {
case 1 :
namaBulan = "Januari";
break;
case 2 :
namaBulan = "Februari";
break;
case 3 :
namaBulan = "Maret";
break;
case 4 :
namaBulan = "April";
break;
case 5 :
namaBulan = "Mei";
break;
case 6 :
namaBulan = "Juni";
break;
case 7 :
namaBulan = "Juli";
break;
case 8 :
namaBulan = "Agustus";
break;
case 9 :
namaBulan = "September";
break;
case 10 :
namaBulan = "Oktober";
break;
case 11 :
namaBulan = "November";
break;
case 12 :
namaBulan = "Desember";
break;
default :
namaBulan = "Tidak ada";
}

System.out.println("Bulan: " + namaBulan);


}
}