Source Code
Session10.java
import java.util.*;
public class Session10 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String myName = ""; // Add your name
System.out.println("Session 10 - " + myName);
// menu variables
boolean done = false;
String choice;
do {
// Menu
System.out.println("Menu");
System.out.println("E1 - Example 1");
// optionally, add new menu items
System.out.println("Q - Quit");
System.out.print("Choice: ");
choice = in.nextLine();
switch (choice) {
case "E1":
System.out.println("Example 1");
example1();
break;
case "E2":
System.out.println("Example 2");
example2();
break;
case "E3":
System.out.println("Example 3");
example3();
break;
// quit and default cases
case "Q":
System.out.println("Quit");
done = true;
break;
default:
System.out.println("Invalid Choice");
break;
}
} while (!done);
}
// example1 method definition
public static void example1() {
// Creating a HashMap
// adding key-value pairs
// retrieving values
// updating values
// Iterating using keySet()
}
// example2 method definition
public static void example2() {
// Creating a TreeMap
// adding key-value pairs
// Iterating using keySet()
}
// example3 method definition
public static void example3() {
// Creating a TreeMap
// adding key-value pairs
// Iterating using keySet()
}
// example4 method definition
public static void example4() {
}
}
Vehicle.java
public class Vehicle {
private String model;
private String manufacturer;
private int year;
public Vehicle(String model, String manufacturer, int year) {
this.model = model;
this.manufacturer = manufacturer;
this.year = year;
}
public String getModel() {
return model;
}
public String getManufacturer() {
return manufacturer;
}
public int getYear() {
return year;
}
@Override
public String toString() {
return year + " " + manufacturer + " " + model;
}
}
Recursion.java
import java.util.*;
public class Recursion {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String myName = ""; // Add your name
System.out.println("Name: " + myName);
// menu variables
boolean done = false;
String choice;
do {
System.out.println("E1 - Example 1");
System.out.println("E2 - Example 2");
System.out.println("E3 - Example 3");
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");
int[] arrMerge = {86, 3, 43, 5};
System.out.println(Arrays.toString(arrMerge));
mergeSort(arrMerge);
System.out.println(Arrays.toString(arrMerge));
break;
case "E6":
System.out.println("Example 6");
// 3 discs
Hanoi towersofHanoi = new Hanoi(3);
break;
case "E7":
System.out.println("Example 7");
// 64 discs
Hanoi towersofHanoi = new Hanoi(64);
break;
// quit and default cases
case "Q":
done = true;
break;
default:
System.out.println("Invalid Choice");
break;
}
} while (!done);
}
// example1() method
public static void example1() {
}
// example2() method
public static void example2(int n) {
}
// example3() method
public static int example3(int n) {
}
// fibonacci method
// recursive binary serach 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++;
}
}
}
Hanoi.java
public class Hanoi {
private int numDiscs; // Number of discs
public Hanoi(int n) {
// Assign the number of discs.
numDiscs = n;
// Move the number of discs from peg 1 to peg 3
// using peg 2 as a temporary storage location.
moveDiscs(numDiscs, 1, 3, 2);
}
/**
* The moveDiscs method accepts the number of
* discs to move, the peg to move from, the peg
* to move to, and the temporary peg as arguments.
* It uses recursion to display the necessary
* disc moves.
*/
private void moveDiscs(int num, int fromPeg, int toPeg, int tempPeg) {
if (num > 0) {
moveDiscs(num - 1, fromPeg, tempPeg, toPeg);
System.out.println("Move a disc from peg " + fromPeg + " to peg " + toPeg);
moveDiscs(num - 1, tempPeg, toPeg, fromPeg);
}
}
}
Last updated