Mock exam attempt: CSE1100 2018/2019 week 1.6

This commit is contained in:
steven@devbox 2025-07-19 01:38:13 +00:00
commit 6d14bc3b9c
13 changed files with 931 additions and 0 deletions

View file

@ -0,0 +1,49 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java application project to get you started.
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.14.3/userguide/building_java_projects.html in the Gradle documentation.
*/
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// Use JUnit Jupiter for testing.
testImplementation libs.junit.jupiter
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
// This dependency is used by the application.
implementation libs.guava
}
// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
application {
// Define the main class for the application.
mainClass = 'org.example.App'
}
tasks.named('test') {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}
jar {
manifest {
attributes 'Main-Class': 'org.example.App'
}
}

View file

@ -0,0 +1,190 @@
/*
* This source file was generated by the Gradle 'init' task
*/
package org.example;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class App {
ArrayList<Train> trains;
enum TrainType {
INTERCITY,
SPRINTER,
}
class Station {
String name;
Boolean isIntercity;
public Station(String name, Boolean isIntercity) {
this.name = name;
this.isIntercity = isIntercity;
}
public String getName() {
return this.name;
}
public Boolean isIntercity() {
return this.isIntercity;
}
public String toString() {
if (this.isIntercity) {
return "(" + this.name + ")";
}
return this.name;
}
}
class Train {
TrainType type;
String departTime;
String arrivalTime;
Station[] stops;
public Train(TrainType type, String departTime, String arrivalTime, Station[] stops) {
this.type = type;
this.departTime = departTime;
this.arrivalTime = arrivalTime;
this.stops = stops.clone();
}
}
class LineData {
private String name;
private String data;
public String getName() {
return this.name;
}
public String getData() {
return this.data;
}
public LineData(String name, String data) {
this.name = name;
this.data = data;
}
}
LineData getDataFromLine(String textLine) {
String[] result = textLine.split(": ");
LineData data = new LineData(result[0], result[1]);
return data;
}
ArrayList<Train> trainsFromTo(String fromStationName, String toStationName) {
ArrayList<Train> matches = new ArrayList<>();
for (Train train : this.trains) {
Boolean hasFromStation = false;
Boolean hasToStation = false;
for (Station stop : train.stops) {
if (train.type == TrainType.INTERCITY) {
if (stop.isIntercity() && stop.getName().equals(fromStationName) && !hasFromStation) {
hasFromStation = true;
}
if (stop.isIntercity() && stop.getName().equals(toStationName) && hasFromStation && !hasToStation) {
hasToStation = true;
}
} else {
if (stop.getName().equals(fromStationName) && !hasFromStation) {
hasFromStation = true;
}
if (stop.getName().equals(toStationName) && hasFromStation && !hasToStation) {
hasToStation = true;
}
}
}
if (hasFromStation && hasToStation) {
matches.add(train);
}
}
return matches;
}
void readTrains(String filepath) throws FileNotFoundException {
ArrayList<Train> trains = new ArrayList<>();
Scanner sc = new Scanner(new File("test.txt"));
while (sc.hasNext()) {
String trainTypeString = getDataFromLine(sc.nextLine()).data;
TrainType type;
if (trainTypeString.equals("INTERCITY")) {
type = TrainType.INTERCITY;
} else {
type = TrainType.SPRINTER;
}
String departTime = getDataFromLine(sc.nextLine()).data;
String arrivalTime = getDataFromLine(sc.nextLine()).data;
ArrayList<Station> stops = new ArrayList<>();
while (sc.hasNext()) {
String line = sc.nextLine();
if (line.equals("END")) {
break;
}
LineData lineData = getDataFromLine(line);
Boolean isIntercityStation;
if (lineData.name.equals("IC-STATION")) {
isIntercityStation = true;
} else {
isIntercityStation = false;
}
String stationName = lineData.data;
Station stop = new Station(stationName, isIntercityStation);
stops.add(stop);
}
Station[] allStations = new Station[stops.size()];
Train train = new Train(type, departTime, arrivalTime, stops.toArray(allStations));
trains.add(train);
}
sc.close();
this.trains = trains;
}
public String getGreeting() {
return "Hello World!";
}
public static void main(String[] args) {
String filepath;
if (args.length < 1) {
System.out.print("Filename: ");
Scanner sc = new Scanner(System.in);
filepath = sc.next();
System.out.println();
sc.close();
} else {
filepath = args[0];
}
App a = new App();
System.out.println("Reading input from: " + filepath);
ArrayList<Train> trains;
try {
a.readTrains(filepath);
trains = a.trains;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
trains = null;
}
if (trains == null) {
return;
}
Scanner sc = new Scanner(System.in);
System.out.println("Get from: ");
String fromStationName = sc.nextLine();
System.out.println("Get to: ");
String toStationName = sc.nextLine();
ArrayList<Train> trains2 = a.trainsFromTo(fromStationName, toStationName);
for (Train t : trains2) {
System.out.println("found " + t.type.toString());
}
sc.close();
}
}

View file

@ -0,0 +1,14 @@
/*
* This source file was generated by the Gradle 'init' task
*/
package org.example;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class AppTest {
@Test void appHasAGreeting() {
App classUnderTest = new App();
assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
}
}