Session 16
Source Code
import java.util.*;
public class Week16 {
public static void main(String[] args) {
String myName = ""; // add your name
System.out.println("Week 16" + " - " + myName);
// Menu
System.out.println("Menu");
System.out.println("E1 - Example 1");
System.out.println("Q - Quit");
// setup Scanner
Scanner in = new Scanner(System.in);
System.out.print("Choice: ");
String choice = in.nextLine();
// switch choices
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");
example5();
break;
case "Q":
System.out.println("Quitting..!");
break;
}
}
// example1 method
public static void example1() {
// create an array of friends
// for loop
// enhanced for loop
}
// example2 method
public static void example2() {
// create an array of horses
// enhanced for loop
}
// example3 method
public static void example3() {
// create array
int[] values = { 6, 2, 1, 7, 12, 5 };
// assign first value
int first = values[0];
// loop
// if it's not the last element, copy the next one over
// last element gets first
// print
}
// example4 method
public static void example4() {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = in.nextInt();
int[] values = new int[n];
System.out.print("Enter the elements:");
for (int i = 0; i < n; i++) {
values[i] = in.nextInt();
}
// Checking for adjacent duplicates
}
// example5 method
public static void example5() {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = in.nextInt();
int[] values = new int[n];
System.out.print("Enter the elements:");
for (int i = 0; i < n; i++) {
values[i] = in.nextInt();
}
// Checking for any duplicates in the array
}
// Method to check for adjacent duplicates
public static boolean hasAdjacentDuplicates(int[] values) {
for (int i = 0; i < values.length - 1; i++) {
if (values[i] == values[i + 1]) {
return true;
}
}
return false;
}
// Method to check for duplicates anywhere in the array
public static boolean hasAnyDuplicates(int[] values) {
for (int i = 0; i < values.length; i++) {
for (int j = i + 1; j < values.length; j++) {
if (values[i] == values[j]) {
return true;
}
}
}
return false;
}
// printArray method
public static void printArray(int[] arr) {
System.out.println("Print Array");
System.out.println("Index\tValue");
for (int i = 0; i < arr.length; i++) {
System.out.println(i + "\t" + arr[i]);
}
}
}
CommandLineArgs.java
CommandLineSum.java
W16Problem1.java
W16Problem2.java
W16Problem3.java
W16Problem4.java
W16Problem5.java
W16Problem6.java
Horse.java
HorseBarn.java
Vehicle.java
ParkingGarage.java
Last updated
Was this helpful?