Main class/** * The class TrainClient tests Train class. A number of orders is made and then carriage map is printed. **/ public class TrainClient { /** Number of rows in a carriage **/ public static final int NUMBER_OF_ROWS = 3; /** Number of columns in a carriage **/ public static final int NUMBER_OF_COLUMNS = 4; public static void main(String[] args) { // On this train the ordering of seats is made Train train = new Train(); // Set carriages for(int i = 0; i < Train.MAX_NUMBER_OF_CARRIAGES;i++){ train.setInCarriage(new Carriage(NUMBER_OF_ROWS,NUMBER_OF_COLUMNS)); } // orderMap shows what orders have to be made in the train. //In each row, the first value is the number of the carriag and the secind value is the column number. int[][] orderMap = {{0,0}, {1,1}, {1,1}, {0,2}, {2,3}, {2,0}, {3,4}, {-1,-1}, {0,0}, {0,0}, {0,0}, {0,-1}, {0,5}, {2,1} }; // Does the orders in orderMap for(int i = 0; i < orderMap.length;i++){ int carriageNr = orderMap[i][0]; int column = orderMap[i][1]; train.orderSeat(carriageNr,column); } System.out.println(); System.out.println("Seatmap for the train after reservation"); // Prints out carriagemap for all carriages. for(int i = 0; i < Train.MAX_NUMBER_OF_CARRIAGES;i++){ System.out.printf("%nCarriage %d:%n",i); train.printOutMap(i); } } }
Help classes
Train
/** * The class train is administering the ordering of seats. * - A train is implemented as an array of carriages. * - The number of carriages is relative to how many carriages are set. **/ public class Train { /** Max number of carriages */ public final static int MAX_NUMBER_OF_CARRIAGES = 3; private Carriage[] carriages; // Array of carriages private int numberOfCarriages; // Number of carriages /** * Konstruktøren oppretter en tabell for vogner. **/ public Train() { numberOfCarriages = 0; carriages = new Carriage[MAX_NUMBER_OF_CARRIAGES]; } public int getTheNumberOfCarriages() { return numberOfCarriages; } /** * @param carriage Carriage to set into the array of carriages. * @return true if the given carriage was sat in, otherwise false. **/ public boolean setInCarriage(Carriage carriage) { // WHAT IS THE CODE FOR THIS METHOD? } /** * Method does the following * - If CarriageNr is not valid, the program gives an error message. * - Reserves the first empty seat in the given column. * - Prints out the row, column and the carriageNr of the reserved seat. * For example: * Carriage Nr. 2, Seat[1, 3]. * @param CarriageNr Number of the carriage where the seat is ordered. * @param column preferred column. * @return true if the seat has been ordered. **/ public boolean orderSeat(int carriageNr, int column) { // WHAT IS THE CODE FOR THIS METHOD? } /** * Prints out the seatmap of the carriage with the given carriage Nr. * @param carriageNr The number of the carriage to print out the seatmap for. * @return true if the seatmap has been printed, false if there is no carriage with the given Nr. **/ public boolean printOutMap(int carriageNr){ if(carriageNr >= numberOfCarriages || carriageNr < 0){ return false; } carriages[carriageNr].drawSeatMap(); return true; } }
Carriage
/** * The Class Carriage represents a carriage in a train. * - A carriage has a plan (given with a number of rows and a number of columns). * - A 2D array holds the order of how many empty seats in every column. */ public class Carriage { private int numberOfRows; // Number of rows in a carriage private int numberOfColumns; // Number of columns in the carriage private boolean[][] carriagePlan; // Defines what seats are taken private int[] numberOfEmptySeats; // Number of empty seats for every column. /** * The constructor does the following: * - makes the plan (where no seats are reserved). * - initialises the array of the number of empty seats in every column. * - initialising of the field variables * @param rows gives the number of rows in the carriage. * @param columns gives the number of columns in the carriage. */ public Carriage(int rows, int columns) { // WHAT IS THE CODE FOR THIS CONSTRUCTOR? } /** * Sets the place given by (i,j)-pair i the plan to "taken". * Creates the number of available seats of the column given by a column nr. * @param i shows the row nr of this seat. * @param j shows the column nr for this seat. **/ private void setTaken(int i, int j) { // WHAT IS THE CODE FOR THIS METHOD? } /** * Deloppgave 2c * @return nr of the first empty seat available. * if there are no empty seats in the column return -1. **/ private int findRowNrOfTheFirstEmptySeat(int column) { // WHAT IS THE CODE FOR THIS METHOD? } /** * Metoden reserverer den første ledige plassen av den angitte kolonnen. * This method reserves the first empty seat of the given column. * If the given column is not valid or if there are no empty seats, an error message is given. * and -1 is returned. * @return Row nr of the first empty seat of the given column that was reserved, otherwise -1. **/ public int reserveTheFirstEmptySeat(int column) { // WHAT IS THE CODE FOR THIS METHOD? } /** * Draws the plan for the carriage. * A taken seat is given by "+", an empty seat is given by "-". * For example: * 012 * 0 -++ * 1 -+- **/ public void drawSeatMap() { // WHAT IS THE CODE FOR THIS METHOD? } }
How do I code the missing methods and the constructor? What the methods and the constructor needs to do, is written as comments in the classes. I really apreaciate your help!