diff --git a/BubbleSort.java b/BubbleSort.java new file mode 100644 index 0000000..ee8eb74 --- /dev/null +++ b/BubbleSort.java @@ -0,0 +1,43 @@ +public class BubbleSort { + public BubbleSort(Integer count, Integer[] passedArray){ + for(int i = 0; i < count; i++){ + for(int j = 0; j < i; j++){ + if(passedArray[i] <= passedArray[j]){ + ////Swapping using multiplication/division - buggy when any item(s) is/are 0 + //passedArray[i] = passedArray[i] * passedArray[j]; + //passedArray[j] = passedArray[i] / passedArray[j]; + //passedArray[i] = passedArray[i] / passedArray[j]; + + ////Swapping using XOR + //passedArray[i] = passedArray[i] ^ passedArray[j]; + //passedArray[j] = passedArray[i] ^ passedArray[j]; + //passedArray[i] = passedArray[i] ^ passedArray[j]; + + //Swapping using addition/substraction + passedArray[i] = passedArray[i] + passedArray[j]; + passedArray[j] = passedArray[i] - passedArray[j]; + passedArray[i] = passedArray[i] - passedArray[j]; + } + } + } + + for(int i = 0; i < count; i++){ + System.out.println(passedArray[i]); + } + } + + public static void main(String[] args){ + Integer numberOfItems = args.length; + Integer[] inputsToBeSorted = new Integer[numberOfItems]; + + for(int i = 0; i < numberOfItems; i++){ + inputsToBeSorted[i] = Integer.parseInt(args[i]); + } + + try { + new BubbleSort(numberOfItems, inputsToBeSorted); + } catch (Exception e) { + System.out.println("Empty argument!"); + } + } +} \ No newline at end of file diff --git a/CollectionsTest.java b/CollectionsTest.java new file mode 100644 index 0000000..6727c6c --- /dev/null +++ b/CollectionsTest.java @@ -0,0 +1,37 @@ +import java.util.*; + +public class CollectionsTest { + public static void main(String[] args){ + //ArrayList + List names = new ArrayList<>(); + names.add("ABC"); + names.add("Budi"); + names.add("Andy"); + names.add(null); + //names.add(stringSet); //Compile error due to different object type + + for(String name : names){ + System.out.println(name); + } + + //Set + Set stringSet = new HashSet<>(); + stringSet.add("String 1"); + stringSet.add("String 1"); + stringSet.add("String 2"); + stringSet.add(null); + //stringSet.add(names); //Compile error due to different object type + for(String str : stringSet){ + System.out.println(str); + } + + //Map + Map scoreByName = new HashMap<>(); + scoreByName.put("Oliver", 100); + scoreByName.put(null, 19); + //scoreByName.put(names, 21); //Compile error due to different object type + for (Map.Entry entry : scoreByName.entrySet()){ //Prints from last item + System.out.println(entry.getKey() + ": " + entry.getValue()); + } + } +} \ No newline at end of file diff --git a/DayPrinter.java b/DayPrinter.java new file mode 100644 index 0000000..aa43344 --- /dev/null +++ b/DayPrinter.java @@ -0,0 +1,38 @@ +public class DayPrinter { + public DayPrinter(Integer year, Integer month){ + switch(month){ + case 1: + case 3: + case 5: + case 7: + case 8: + case 10: + case 12: + System.out.println("31"); + break; + case 4: + case 6: + case 9: + case 11: + System.out.println("30"); + break; + case 2: + if(year % 4 == 0){ + System.out.println("29"); + } else { + System.out.println("28"); + } + break; + default: + System.out.println("Invalid input!"); + } + } + + public static void main(String[] args){ + try{ + new DayPrinter(Integer.parseInt(args[0]), Integer.parseInt(args[1])); + } catch (Exception e){ + System.out.println("Empty argument!"); + } + } +} \ No newline at end of file diff --git a/FactorialLooping.java b/FactorialLooping.java new file mode 100644 index 0000000..053f48e --- /dev/null +++ b/FactorialLooping.java @@ -0,0 +1,19 @@ +public class FactorialLooping { + public FactorialLooping(Integer number){ + Integer result = 1; + + for(int i = 1; i <= number; i++){ + result = result * i; + } + + System.out.println(result); + } + + public static void main(String[] args){ + try{ + new FactorialLooping(Integer.parseInt(args[0])); + } catch(Exception e){ + System.out.println("Empty argument"); + } + } +} \ No newline at end of file diff --git a/FactorialRecursive.java b/FactorialRecursive.java new file mode 100644 index 0000000..d343686 --- /dev/null +++ b/FactorialRecursive.java @@ -0,0 +1,22 @@ +public class FactorialRecursive { + private Integer recursive(Integer number){ + if(number > 0){ + return number * recursive(number - 1); + } + return 1; + } + + public FactorialRecursive(Integer number){ + Integer result = recursive(number); + + System.out.println(result); + } + + public static void main(String[] args){ + try{ + new FactorialRecursive(Integer.parseInt(args[0])); + } catch (Exception e){ + System.out.println("Empty argument!"); + } + } +} \ No newline at end of file diff --git a/MonthNamePrinter.java b/MonthNamePrinter.java new file mode 100644 index 0000000..56bb045 --- /dev/null +++ b/MonthNamePrinter.java @@ -0,0 +1,53 @@ +public class MonthNamePrinter { + public MonthNamePrinter(Integer monthNumber){ + switch(monthNumber){ + case 1: + System.out.println("January"); + break; + case 2: + System.out.println("February"); + break; + case 3: + System.out.println("March"); + break; + case 4: + System.out.println("April"); + break; + case 5: + System.out.println("May"); + break; + case 6: + System.out.println("June"); + break; + case 7: + System.out.println("July"); + break; + case 8: + System.out.println("August"); + break; + case 9: + System.out.println("September"); + break; + case 10: + System.out.println("October"); + break; + case 11: + System.out.println("November"); + break; + case 12: + System.out.println("December"); + break; + default: + System.out.println("Input invalid!"); + break; + } + } + + public static void main(String[] args){ + try{ + new MonthNamePrinter(Integer.parseInt(args[0])); + } catch(Exception e){ + System.out.println("Empty argument!"); + } + } +} \ No newline at end of file diff --git a/ScoreGradePrinter.java b/ScoreGradePrinter.java new file mode 100644 index 0000000..9fab721 --- /dev/null +++ b/ScoreGradePrinter.java @@ -0,0 +1,29 @@ +public class ScoreGradePrinter { + public ScoreGradePrinter(Integer score){ + System.out.print("Your grade: "); + if(score >= 0 && score <= 20){ + System.out.println("E"); + System.out.println("You Have to Study Extra Extra Hard!"); + } else if(score > 20 && score <= 40){ + System.out.println("D"); + System.out.println("You Have to Study Extra Hard!"); + } else if(score > 40 && score <= 60){ + System.out.println("C"); + System.out.println("You Have to Study Hard!"); + } else if(score > 60 && score <= 80){ + System.out.println("B"); + System.out.println("You Have to Study Harder!"); + } else if(score > 80 && score <= 100){ + System.out.println("A"); + System.out.println("You Have to Maintain Your Performance!"); + } + } + + public static void main(String[] args){ + try{ + new ScoreGradePrinter(Integer.parseInt(args[0])); + } catch(Exception e){ + System.out.println("Empty argument!"); + } + } +} \ No newline at end of file