Skip to content

Instantly share code, notes, and snippets.

@lnfel
Last active April 22, 2023 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lnfel/5e6ac6f4e1413d8b4fc5255a34d9fd73 to your computer and use it in GitHub Desktop.
Save lnfel/5e6ac6f4e1413d8b4fc5255a34d9fd73 to your computer and use it in GitHub Desktop.
Java Exercise

Java Exercise

This are collection of java exercises. It was kinda fun ngl.

package com.sample.bus.reservation;
import java.util.Scanner;
import java.util.Arrays;
/**
*
* @author lnfel
*/
public class BusReservation {
/**
* Display reserved seats
*
* @linkplain https://stackoverflow.com/questions/15961130/align-printf-output-in-java
* @param bus Matrix of bus seat rows
* @return void
*/
static void displayBusSeats(String[][] bus) {
System.out.println("Bus seat reservation.");
System.out.printf("%-9s", "");
System.out.printf("%s", "Col 1 Col 2 Col 3 Col 4");
for (int i = 0; i < bus.length; i++) {
System.out.println("");
int row = i + 1;
System.out.printf("Row %-5s", row);
for (String seat : bus[i]) {
System.out.printf("%-6s", " " + seat);
}
}
System.out.println("");
}
/**
* Check if bus is fully booked
*
* @param bus Matrix of bus seat rows
*/
static boolean notFullyBooked(String[][] bus) {
Long availableSeats = Arrays.stream(bus)
.flatMap(Arrays::stream)
.filter(value -> value.equals("*"))
.count();
return availableSeats > 0;
}
/**
* Seat Coordinates object
*/
static public class SeatCoordinates {
public int row = 0;
public int column = 0;
public SeatCoordinates(int rowChosen, int columnChosen) {
row = rowChosen -1;
column = columnChosen -1;
}
}
/**
* Get input from user
* User must provide a space separated integer to reserve a seat on the bus.
* Example: 5 3
* The above input reserves the bus seat 3 from row 5
*
* If a user inputs a negative integer without a second input separated by space, the program will close.
*
* @return Array of seatCoordinate input
*/
static String[] getInput() {
// Initialize scanner
Scanner sc = new Scanner(System.in);
System.out.print("Enter row and seat number separated by space to reserve a seat on the bus: ");
// Get input and split by space character
String[] input = sc.nextLine().split(" ");
// If input has is not separated by space, check if it is a negative number and terminate program
if (input.length == 1 && Integer.parseInt(input[0]) < 0) {
System.out.println("Exiting program, goodbye!");
System.exit(0);
}
// Loop over asking for valid seatCoordinates until user provides one.
while (input.length < 2) {
System.out.println("To reserve a bus seat, please provide a seat row and column separated by space.");
System.out.println("Example: to reserve seat 3 in row 5, enter 5 3");
input = sc.nextLine().split(" ");
}
return input;
}
/**
* Validate SeatCoordinate based on selected bus rows and columns
* by referencing the bus matrix values.
*
* @param seatCoordinates The selected seat coordinates to be reserved
* @param bus The bus matrix that holds the seat plan
* @return Returns whether SeatCoordinates are valid
*/
static boolean validateSeatCoordinates(SeatCoordinates seatCoordinates, String[][] bus) {
if (seatCoordinates.row > bus.length - 1) {
System.out.println("Sorry the bus only has 10 rows, please select a row from 1 - 10.");
return false;
}
if (seatCoordinates.column > bus[seatCoordinates.row].length - 1) {
System.out.printf("Sorry, bus row %d has %d seats, please select a seat from 1 - %d. %n", seatCoordinates.row + 1, bus[seatCoordinates.row].length, bus[seatCoordinates.row].length);
return false;
}
return true;
}
public static void main(String args[]) {
String[][] bus = {
{"*", "*", "*", "*"},
{"*", "*", "*", "*"},
{"*", "*", "*", "*"},
{"*", "*", "*", "*"},
{"*", "*", "*", "*"},
{"*", "*", "*", "*"},
{"*", "*", "*", "*"},
{"*", "*", "*", "*"},
{"*", "*", "*", "*"},
{"*", "*", "*", "*"},
};
displayBusSeats(bus);
while (notFullyBooked(bus)) {
System.out.println("");
String[] input = getInput();
SeatCoordinates seatCoordinates = new SeatCoordinates(Integer.parseInt(input[0]), Integer.parseInt(input[1]));
System.out.printf("Reserved row %d column %d%n", seatCoordinates.row + 1, seatCoordinates.column + 1);
boolean validSeatCoordinates = validateSeatCoordinates(seatCoordinates, bus);
if (validSeatCoordinates) {
bus[seatCoordinates.row][seatCoordinates.column] = "x";
displayBusSeats(bus);
} else {
}
}
// Test while loop by automatically filling all seats until bus is full
//while (notFullyBooked(bus)) {
// for (int i = 0; i < bus.length; i++) {
// for(int j = 0; j < bus[i].length; j++) {
// bus[i][j] = "x";
// }
// displayBusSeats(bus);
// System.out.println("");
// }
//}
if (!notFullyBooked(bus)) {
System.out.println("Bus is full, departing shortly.");
}
}
}
import java.util.Random;
import java.util.Scanner;
/**
*
* @author lnfel
*/
public class wordle {
/**
* Display game stats
*
* @param correctGuesses Array of booleans based on the length of the word to guess
* @param lives Remaining tries to guess the word
* @return void
*/
static void displayCorrectGuesses(boolean[] correctGuesses, int lives) {
System.out.print("Guessed characters: ");
for (boolean guessed: correctGuesses) {
System.out.printf("%-2s", guessed ? "✓" : "x");
}
System.out.printf("Lives left: %-2d", lives);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
String[] words = {"apple", "banana", "cherry", "orange", "pear"};
Random random = new Random();
int index = random.nextInt(words.length);
String wordToGuess = words[index];
int currentLetter = 0;
boolean[] correctGuesses = new boolean[wordToGuess.length()];
int lives = 8;
Scanner scanner = new Scanner(System.in);
while (lives > 0) {
displayCorrectGuesses(correctGuesses, lives);
System.out.print("Guess a letter: ");
String guess = scanner.nextLine();
boolean foundLetter = wordToGuess.charAt(currentLetter) == guess.charAt(0);
if (foundLetter) {
correctGuesses[currentLetter] = true;
currentLetter++;
} else {
lives--;
}
if (currentLetter == wordToGuess.length()) {
break;
}
if (lives == 0) {
System.out.println("Game Over");
}
}
if (lives > 0) {
System.out.println("You win!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment