Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with methods and constructors please!

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help with methods and constructors please!

    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!


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with methods and constructors please!

    What have you tried?
    Do you have any errors?

    As it is your post is basically "here's my assignment, write it for me"

    If you have a specific question then people are more than willing to help.
    Improving the world one idiot at a time!

Similar Threads

  1. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  2. Constructors
    By av8 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 19th, 2011, 06:40 PM
  3. constructors in servlets
    By the light in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: June 29th, 2011, 10:32 AM
  4. [SOLVED] Overloading constructors(Multiple Constructors)
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 11th, 2011, 12:55 PM
  5. Write methods and constructors from Javadocs
    By smashX in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 20th, 2011, 10:23 PM

Tags for this Thread