Source Code
Module4.java
import java.util.*;
public class Module4 {
public static void main(String[] args) {
System.out.println("Module 4 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();
break;
case "E2":
System.out.println("Example 2");
example2();
break;
case "E3":
System.out.println("Example 3");
example3();
break;
case "E4":
System.out.println("Example 4");
example4();
break;
case "E5":
System.out.println("Example 5");
// declare arr array
// example5 method call
break;
case "E6":
System.out.println("Example 6");
// print command line arguments
case "E7":
System.out.println("Example 7");
example7();
break;
case "E8":
System.out.println("Example 8");
example8();
break;
case "E9":
System.out.println("Example 9");
example9();
break;
case "E10":
System.out.println("Example 10");
example10();
break;
case "E11":
System.out.println("Example 11");
example11();
break;
case "E12":
System.out.println("Example 12");
example12();
break;
case "E13":
System.out.println("Example 13");
example13();
break;
case "E14":
System.out.println("Example 14");
example14();
break;
case "E15":
System.out.println("Example 15");
example15();
break;
case "E16":
System.out.println("Example 16");
example16();
break;
case "E17":
System.out.println("Example 17");
example17();
break;
case "E18":
System.out.println("Example 18");
example18();
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() {
// declares an array of integers
// create an empty array of size 3
// print the values of in the scores array
// declare an array of 3 doubles
// assign values to each index position
// print the values of the array
}
// example2 method
public static void example2() {
// declare grades array
// print the values of the array
// print the number of elements in the array
}
// example3 method
public static void example3() {
}
// example4 method
public static void example4() {
// declare array
}
// example5 method
public static void example5() {
}
// example7 method
public static void example7() {
}
// example8 method
public static void example8() {
}
// example9 method
public static void example9() {
}
// example10 method
public static void example10() {
}
// example11 method
public static void example11() {
}
// example12 method
public static void example12() {
}
// example13 method
public static void example13() {
}
// example14 method
public static void example14() {
}
// example 15 method
public static void example15() {
}
// example 16 method
public static void example16() {
}
// example 17 method
public static void example17() {
}
// example 18 method
public static void example18() {
}
/**
* Finds the index of a value in an array of integers.
*
* @param elements an array containing the items to be searched.
* @param target the item to be found in elements.
* @return an index of target in elements if found; -1 otherwise.
*/
public static int linearArraySearch(int[] elements, int target) {
for (int j = 0; j < elements.length; j++) {
if (elements[j] == target) {
return j;
}
}
return -1;
}
/**
* Finds the index of a value in an ArrayList of integers.
*
* @param elements an array containing the items to be searched.
* @param target the item to be found in elements.
* @return an index of target in elements if found; -1 otherwise.
*/
public static int linearArrayListSearch(ArrayList<Integer> elements, int target) {
for (int j = 0; j < elements.size(); j++) {
if (elements.get(j) == target) {
return j;
}
}
return -1;
}
/**
* Performs a linear search on an array of strings to find the index of a given
* target string.
*
* @param elements An array of strings in which the search is to be performed.
* @param target The string to search for within the array.
* @return The index of the first occurrence of the target string within the
* array. If the target string
* is not found in the array, the method returns -1.
*/
public static int linearStringSearch(String[] elements, String target) {
for (int j = 0; j < elements.length; j++) {
if (elements[j].equals(target)) {
return j;
}
}
return -1;
}
/**
* Performs a binary search on an array of integers
*
* @param elements An array of integers, sorted in ascending order, in which the
* search is to be performed.
* The array should not be null and must be sorted for the
* binary search algorithm to work correctly.
* @param target The integer to search for within the array.
* @return The index of the target integer within the array if found. If the
* target integer is not found,
* the method returns -1.
*/
public static int binarySearch(int[] elements, int target) {
int left = 0;
int right = elements.length - 1;
while (left <= right) {
int middle = (left + right) / 2;
if (target < elements[middle]) {
right = middle - 1;
} else if (target > elements[middle]) {
left = middle + 1;
} else {
return middle;
}
}
return -1;
}
/**
* Sorts an array of integers in ascending order using the selection sort
* algorithm.
*
* @param elements The array of integers to be sorted. The array is modified in
* place,
* and the sorted array is the same as the input array with its
* elements rearranged.
*/
public static void selectionSort(int[] elements) {
for (int j = 0; j < elements.length - 1; j++) {
int minIndex = j;
for (int k = j + 1; k < elements.length; k++) {
if (elements[k] < elements[minIndex]) {
minIndex = k;
}
}
int temp = elements[j];
elements[j] = elements[minIndex];
elements[minIndex] = temp;
System.out.println(Arrays.toString(elements));
}
}
}
Dragon.java
public class Dragon {
// Attributes of a dragon
private String name;
private int age;
private String color;
// Constructor
public Dragon(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
// Getter and Setter methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
// toString method to return a String representation of the dragon
@Override
public String toString() {
return "Dragon Details:" + "\n" +
"Name: " + name + "\n" +
"Age: " + age + " years" + "\n" +
"Color: " + color;
}
}
ListWorker.java
import java.util.*;
public class ListWorker {
// private instance variable
// Constructor
// removeName method
// toString method
@Override
public String toString() {
return "Values: " + nameList;
}
}
Last updated