CS 2421 Object Oriented Programming in Java
  • CS 2421 Object Oriented Programming in Java
  • Module 1
    • Presentations
    • Lecture Videos
    • Source Code
    • Assignment
  • Module 2
    • Presentations
    • Lecture Videos
    • Source Code
    • Assignment
  • Module 3
    • Presentations
    • Lecture Videos
    • Source Code
    • Assignment
  • Module 4
    • Presentations
    • Lecture Videos
    • Source Code
    • Assignment
  • Module 5
    • Presentations
    • Videos
    • Source Code
    • Assignment
  • Module 6
    • Presentations
    • Videos
    • Source Code
    • Assignment
Powered by GitBook
On this page
  • Module6_1.java
  • Module6_2.java
  1. Module 6

Source Code

Module6_1.java

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class Module6_1 {
    public static void main(String[] args) {
        System.out.println("Module 6 Part 1 Examples");
        // Create new Scanner object (don't delete!)
        Scanner in = new Scanner(System.in);
        // menu variables
        boolean done = false;
        String choice;
        // do while menu loop
        do {
            System.out.println("E1 - Example 1");
            System.out.println("Q - Quit");
            System.out.print("Choice: ");
            choice = in.nextLine();
            switch (choice) {
                case "E1":
                    System.out.println("Example 1");
                    // example1 method call
                    example1();
                    break;
                case "E2":
                    System.out.println("Example 2");
                    // example2 method call
                    example2();
                    break;
                case "E3":
                    System.out.println("Example 3");
                    // example3 method call
                    example3();

                    break;
                case "E4":
                    System.out.println("Example 4");
                    // example4 method calls
                    // Part 1
                    
                    // Part 2
                    
                    break;
                case "E5":
                    System.out.println("Example 5");
                    // example5 method call
                    example5();
                    break;
                case "E6":
                    System.out.println("Example 6");
                    // example6 method call
                    try {
                        example6(15);
                    } catch (IllegalArgumentException e) {
                        System.out.println("Exception caught: " + e.getMessage());
                    }
                    break;
                case "E7":
                    System.out.println("Example 7");
                    example7();
                    break;
                // quit and default cases
                case "Q":
                    System.out.println("Quit");
                    done = true;
                    break;
                default:
                    System.out.println("Invalid Choice");
                    break;
            }
        } while (!done);

        // close the Scanner (don't delete!)
        in.close();
    }

    // example1 method
    public static void example1() {
        
    }

    // example2 method
    public static void example2() {
        
    }

    // example3 method
    public static void example3() {
        
    }

    // example4 method
    public static void example4(int[] numbers) {
        
    }

    // example5 method
    public static void example5() {
        
    }

    // example6 method
    public static void example6(int age) {
        
    }

    // example7 method
    public static void example7() {
        
    }
}

Module6_2.java

import java.util.*;

public class Module6_2 {
    public static void main(String[] args) {
        System.out.println("Module 6 Part 2 Examples");
        // Create new Scanner object (don't delete!)
        Scanner in = new Scanner(System.in);
        // menu variables
        boolean done = false;
        String choice;

        do {
            System.out.println("E1 - Example 1");
            System.out.println("Q - Quit");
            System.out.print("Choice: ");
            choice = in.nextLine();
            switch (choice) {
                case "E1":
                    System.out.println("Example 1");

                    break;
                case "E2":
                    System.out.println("Example 2");
   
                    break;
                case "E3":
                    System.out.println("Example 3");
 
                    break;
                case "E4":
                    System.out.println("Example 4");
                    
                    break;
                case "E5":
                    System.out.println("Example 5");
               
                    break;
                case "E6":
                    System.out.println("Example 6");
               
                    break;
                case "E7":
                    System.out.println("Example 7");
               
                    break;
                case "E8":
                    System.out.println("Example 8");
               
                    break;
                // quit and default cases
                case "Q":
                    done = true;
                    break;
                default:
                    System.out.println("Invalid Choice");
                    break;
            }
        } while (!done);
    }

    // example1() method


    // example2() method


    // example3() method
    
    
    // fibonacci method


    // recursive binary search method
    public static int recursiveBinarySearch(int[] array, int target, int start, int end) {
        int middle = (start + end) / 2;
        // base case: check middle element
        if (target == array[middle]) {
            return middle;
        }
        // base case: check if we've run out of elements
        if (end < start) {
            return -1; // not found
        }
        // recursive call: search start to middle
        if (target < array[middle]) {
            return recursiveBinarySearch(array, target, start, middle - 1);
        }
        // recursive call: search middle to end
        if (target > array[middle]) {
            return recursiveBinarySearch(array, target, middle + 1, end);
        }
        return -1;
    }

    // merge sort
    public static void mergeSort(int[] a) {
        // Base case: if the array is of length 0 or 1, it's already sorted
        if (a.length <= 1) {
            return;
        }

        // Creating two arrays to hold the two halves of the input array
        int[] first = new int[a.length / 2];
        int[] second = new int[a.length - first.length];

        // Copying the first half of the array 'a' into 'first'
        for (int i = 0; i < first.length; i++) {
            first[i] = a[i];
        }
        // Copying the second half of the array 'a' into 'second'
        for (int i = 0; i < second.length; i++) {
            second[i] = a[first.length + i];
        }

        // Recursively sort the first half
        mergeSort(first);
        // Recursively sort the second half
        mergeSort(second);

        // Merge the sorted halves back into the original array
        merge(first, second, a);
    }

    // Private helper method to merge two sorted arrays into a single sorted array
    private static void merge(int[] first, int[] second, int[] a) {
        int iFirst = 0; // Index into the first array
        int iSecond = 0; // Index into the second array
        int j = 0; // Index into the merged array

        // While both arrays have elements yet to be merged
        while (iFirst < first.length && iSecond < second.length) {
            // Determine which element from the two halves is smaller
            // and add it to the merged array
            if (first[iFirst] < second[iSecond]) {
                a[j] = first[iFirst];
                iFirst++;
            } else {
                a[j] = second[iSecond];
                iSecond++;
            }
            j++;
        }

        // If there are remaining elements in 'first', add them to 'a'
        while (iFirst < first.length) {
            a[j] = first[iFirst];
            iFirst++;
            j++;
        }

        // If there are remaining elements in 'second', add them to 'a'
        while (iSecond < second.length) {
            a[j] = second[iSecond];
            iSecond++;
            j++;
        }
    }
}
PreviousVideosNextAssignment

Last updated 1 year ago