import java.util.Arrays;
import java.util.Random;
public class SeatingChart {
private String[][] seats;
// Constructor that initializes the seating chart with empty seats
public SeatingChart(int numRows, int numCols) {
seats = new String[numRows][numCols];
for (int i = 0; i < numRows; i++) {
Arrays.fill(seats[i], "");
}
}
// Method to print the current seating chart
public void printChart() {
System.out.println("Current Seating Chart:");
for (int i = 0; i < seats.length; i++) {
for (int j = 0; j < seats[i].length; j++) {
System.out.print(seats[i][j] + "\t");
}
System.out.println();
}
}
// Method to add a person to a specific seat
public void addPerson(String name, int row, int col) {
seats[row][col] = name;
}
// Method to shuffle the seats randomly
public void shuffleSeats() {
Random rand = new Random();
for (int i = 0; i < seats.length; i++) {
for (int j = 0; j < seats[i].length; j++) {
int randRow = rand.nextInt(seats.length);
int randCol = rand.nextInt(seats[i].length);
String temp = seats[i][j];
seats[i][j] = seats[randRow][randCol];
seats[randRow][randCol] = temp;
}
}
}
// Method to replace a person in a specific seat
public void replacePerson(String newName, int row, int col) {
seats[row][col] = newName;
}
public static void main(String[] args) {
SeatingChart chart = new SeatingChart(4, 5); // Create a 4x5 seating chart
chart.addPerson("Alice", 0, 0);
chart.addPerson("Bob", 1, 2);
chart.addPerson("Charlie", 2, 3);
chart.addPerson("David", 3, 1);
chart.printChart();
chart.shuffleSeats();
chart.printChart();
chart.replacePerson("Eve", 1, 2);
chart.printChart();
}
}
SeatingChart.main(null);
//create ArrayLists that satisfy the following
// a) that stores Boolean values
ArrayList<Boolean> boolList = new ArrayList<Boolean>();
// b) that stores Turtle Objects
ArrayList<Turtle> turtleList = new ArrayList<Turtle>();
// c) that initializes with 10 Strings
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"));
import java.util.ArrayList;
public class Hack2 {
public static void main(Integer[] args) {
ArrayList<Integer> randomNumbers = new ArrayList<Integer>();
randomNumbers.add(1);
randomNumbers.add(4);
randomNumbers.add(7);
randomNumbers.add(12);
randomNumbers.add(23);
System.out.println("ArrayList: " + randomNumbers);
;
;
;
}
}
Hack2.main(null);
public class Hack3 {
public static void main(String[] args) {
ArrayList<Integer> values = new ArrayList<Integer>();
values.add(1);
values.add(4);
values.add(7);
values.add(12);
values.add(23);
System.out.println("ArrayList: " + values);
int total = 0;
for (int i=0; i < values.size(); i++) {
total += values.get(i);
}
System.out.println("Total: " + total);
}
}
Hack3.main(null);
public class SelectionSort {
public static void main(String[] args) {
int[] arr = {64, 25, 12, 22, 11};
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[i]) {
// Swap the elements
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
System.out.print(arr[i] + " ");
}
}
}
SelectionSort.main(null);
public class ArrayIndexingExample {
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
System.out.println("Elements with even indexes:");
for (int i = 0; i < arr.length; i += 2) {
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println("Elements with odd indexes:");
for (int i = 1; i < arr.length; i += 2) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
ArrayIndexingExample.main(null);
public class Test1 {
public static void main(String[] args) {
int[][] array = { {1,2,3},{-1,-2,-3},{4,5,6} };
int sum = 0;
// Iterate over the diagonal of the array
for (int i = 0; i < array.length; i++) {
sum += array[i][i];
}
System.out.println("Sum of diagonal elements: " + sum);
}
}
Test1.main(null);
// FRQ Array Lists
//Part a
class MemberInfo {
private String name;
private int gradYear;
private boolean goodStanding;
public MemberInfo(String name, int gradYear, boolean goodStanding) {
this.name = name;
this.gradYear = gradYear;
this.goodStanding = goodStanding;
}
public int getGradYear() {
return gradYear;
}
public boolean inGoodStanding() {
return goodStanding;
}
public String getMemberName() {
return name;
}
}
//Part B
import java.util.ArrayList;
public class ClubMembers {
private ArrayList<MemberInfo> memberList;
public ClubMembers() {
memberList = new ArrayList<>();
}
/**
* Adds new club members to memberList.
* Precondition: names is a non-empty array.
*/
public void addMembers(String[] names, int gradYear) {
for (String name : names) {
MemberInfo member = new MemberInfo(name, gradYear, true);
memberList.add(member);
}
}
/**
* Removes members who have graduated and returns a list of members who have
* graduated and are in good standing.
*/
public ArrayList<MemberInfo> removeMembers(int year) {
ArrayList<MemberInfo> graduatedMembers = new ArrayList<>();
for (int i = 0; i < memberList.size(); i++) {
MemberInfo member = memberList.get(i);
if (member.getGradYear() == year && member.inGoodStanding()) {
graduatedMembers.add(member);
memberList.remove(i);
i--; // Decrement the index as the list size has changed
}
}
return graduatedMembers;
}
// Tester method
public static void main(String[] args) {
ClubMembers club = new ClubMembers();
// Adding members
String[] names = {"John", "Emily", "David"};
int gradYear = 2024;
club.addMembers(names, gradYear);
// Removing members who graduated in a specific year
int yearToRemove = 2024;
ArrayList<MemberInfo> removedMembers = club.removeMembers(yearToRemove);
// Displaying removed members
System.out.println("Removed members who graduated in " + yearToRemove + ":");
for (MemberInfo member : removedMembers) {
System.out.println(member.getGradYear() + ": " + member.getMemberName());
}
}
}
ClubMembers.main(null);