GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Mar-2005, 13:08
leanieleanz leanieleanz is offline
New Member
 
Join Date: Mar 2005
Posts: 10
leanieleanz is on a distinguished road

Convert Java to C++???


I have a program that I wrote in Java and need it converted to C++. It is a airplane seating assignment that uses arrays. Im so clueless when it comes to C++ arrays. Can someone help me by recommending a better converter then Microsoft's JCLA...its just not cutting it.

JAVA Code:
//Chapter 9: Programming Exercise 12

import java.io.*;

public class Ch9_PrExercise12
{
    static BufferedReader keyboard = new
           BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) throws IOException
    {
          char[][] seatPlan = new char[13][6];
          char resp;

          initiailzeSeatPlan(seatPlan);

          showMenu(seatPlan);

          System.out.println("To reserve a seat enter Y/y(Yes), N/n(No): ");
          resp = keyboard.readLine().charAt(0);
          System.out.println();

          while(resp == 'y' || resp == 'Y')
          {
                assignSeat(seatPlan);
                showMenu(seatPlan);
                System.out.println("Reserve another seat Y/y(Yes), N/n(No): ");
                resp = keyboard.readLine().charAt(0);
                System.out.println();
          }
    }

    public static void initiailzeSeatPlan(char[][] sPlan)
    {
          int i, j;

          for(i = 0; i < sPlan.length; i++)
              for(j = 0; j < sPlan[0].length; j++)
                  sPlan[i][j] = '*';
    }

    public static void showSeatAssignments(char[][] sPlan)
    {
          int i, j;

          System.out.println("        A B C  D E F");

          for(i = 0; i < sPlan.length; i++)
          {
            if( i < 9)
                  System.out.print("Row " + (i+1) + "   ");
            else
                System.out.print("Row " + (i+1) + "  ");

                for(j = 0; j < sPlan[0].length; j++)
                {
                      System.out.print(sPlan[i][j] + " ");
                      if(j == 2)
                          System.out.print(" ");
                }
                System.out.println();
          }

          System.out.println("* -- available seat");
          System.out.println("X -- occupied seat");
          System.out.println();
    }

    public static void assignSeat(char[][] sPlan) throws IOException
    {
          char ticketType;
          char resp;

          System.out.print("Enter ticket type: F/f (first class) "
                 + " E/e (Economy class): ");
          ticketType = keyboard.readLine().charAt(0);
          System.out.println();

          switch(ticketType)
          {
          case 'f':
          case 'F': if(!isFirstClassFull(sPlan))
                                assignFirstClassSeat(sPlan);
                          else
                          {
                                System.out.println("Sorry!!! First Class is Full");
                                System.out.print("Press Y/y to continue: ");
                                resp = keyboard.readLine().charAt(0);
                                System.out.println();
                          }

                          break;
          case 'e':
          case 'E': if(!isNonSmokingEconomicFull(sPlan) ||
                              !isSmokingEconomicFull(sPlan))
                                  assignEconomicSeat(sPlan);
                          else
                        {
                                System.out.println("Sorry!!! Economic Class is Full");
                                System.out.print("Press Y/y to continue: ");
                                resp = keyboard.readLine().charAt(0);
                                System.out.println();
                          }
          }

          showSeatAssignments(sPlan);
    }

    public static void showMenu(char[][] sPlan)
    {
          System.out.println("This program assigns seats for a commercial airplane.");
          System.out.println("The current seat assignments is as follows.");
          showSeatAssignments(sPlan);
          System.out.println("Rows 1 and 2 are for first class passengers");
          System.out.println("Rows 1 through 7 are nonsmoking");
    }

    public static boolean isFirstClassFull(char[][] sPlan)
    {
          int i, j;

          int assignedSeats = 0;

          for(i = 0; i < 2; i++)
              for(j = 0; j < sPlan[0].length; j++)
                    if(sPlan[i][j] == 'X')
                        assignedSeats++;

          return (assignedSeats == 2 * sPlan[0].length);
    }

    public static boolean isNonSmokingEconomicFull(char[][] sPlan)
    {
          int i, j;

          int assignedSeats = 0;

          for(i = 2; i < 7; i++)
                for(j = 0; j < sPlan[0].length; j++)
                      if(sPlan[i][j] == 'X')
                            assignedSeats++;

        return (assignedSeats == 5 * sPlan[0].length);
    }

    public static boolean isSmokingEconomicFull(char[][] sPlan)
    {
          int i, j;

          int assignedSeats = 0;

        for(i = 7; i < 13; i++)
                for(j = 0; j < sPlan[0].length; j++)
                      if(sPlan[i][j] == 'X')
                            assignedSeats++;

          return (assignedSeats == 6 * sPlan[0].length);
    }

    public static void selectSeatNumber(int startRows, int endRows,
                                IntClass rowNo, CharClass columnNo)  throws IOException
    {

          System.out.print("Enter Row number " + startRows + " - "
                       + endRows + ": ");
          rowNo.setNum(Integer.parseInt(keyboard.readLine()));
          System.out.println();

          while(rowNo.getNum() < startRows || rowNo.getNum() > endRows)
          {
                System.out.print("Enter Row number " + startRows + " - "
                            + endRows + ": ");
                rowNo.setNum(Integer.parseInt(keyboard.readLine()));
            System.out.println();
          }

          System.out.print("Enter seat number (A - F): ");
        columnNo.setChar(keyboard.readLine().charAt(0));
          System.out.println();

          while(columnNo.getChar() < 'A' || columnNo.getChar() > 'F')
          {
                System.out.print("Enter seat number (A - F): ");
                columnNo.setChar(keyboard.readLine().charAt(0));
                System.out.println();
          }
    }

    public static void assignFirstClassSeat(char[][] sPlan) throws IOException
    {
          IntClass rowNum = new IntClass();
          CharClass columnPos = new CharClass();

          if(!isFirstClassFull(sPlan))
          {
                selectSeatNumber(1, 2, rowNum, columnPos);

                while(sPlan[rowNum.getNum() - 1][(int)columnPos.getChar() - 65] != '*')
                {
                      System.out.println("*#*#*#*# This seat is occupied *#*#*#*#");
                      System.out.println("Make another selection");

                      showSeatAssignments(sPlan);

                      selectSeatNumber(1, 2, rowNum, columnPos);
                }

                sPlan[rowNum.getNum() - 1][(int)columnPos.getChar() - 65] = 'X';
                System.out.println("This seat is reserved for you");
          }
          else
                System.out.println("Sorry!!! First Class is Full");
    }

    public static void assignEconomicSeat(char[][] sPlan) throws IOException
    {
          char seatType;

          if(!isNonSmokingEconomicFull(sPlan) &&
              isSmokingEconomicFull(sPlan))
                      assignSeatNonSmokingEconomic(sPlan);
          else
                if(isNonSmokingEconomicFull(sPlan) &&
                      !isSmokingEconomicFull(sPlan))
                          assignSeatSmokingEconomic(sPlan);
                else
                      if(!isNonSmokingEconomicFull(sPlan) &&
                          !isSmokingEconomicFull(sPlan))
                      {
                            System.out.print("Enter seat type: smoking (S/s), nonsmoking (N/n): ");
                            seatType = keyboard.readLine().charAt(0);
                            System.out.println();

                            while(seatType != 'S' && seatType != 's' &&
                                    seatType != 'N' && seatType != 'n')
                            {
                                  System.out.println("Invalid seat type.");
                                  System.out.print("Enter smoking (S/s), nonsmoking (N/n): ");
                                  seatType = keyboard.readLine().charAt(0);
                                  System.out.println();
                            }

                          if(seatType == 'N' || seatType == 'n')
                                  assignSeatNonSmokingEconomic(sPlan);
                            else
                                  assignSeatSmokingEconomic(sPlan);
                      }
                      else
                            System.out.println("Economic Class is Full");
    }

    public static void assignSeatNonSmokingEconomic(char[][] sPlan) throws IOException
    {
          IntClass rowNum = new IntClass();
          CharClass columnPos = new CharClass();

          if(!isNonSmokingEconomicFull(sPlan))
          {
                selectSeatNumber(3, 7, rowNum, columnPos);

                while(sPlan[rowNum.getNum() - 1][(int)columnPos.getChar() - 65] != '*')
                {
                      System.out.println("*#*#*#*# This seat is occupied *#*#*#*#");
                      System.out.println("Make another selection");

                      showSeatAssignments(sPlan);

                      selectSeatNumber(3, 7, rowNum, columnPos);
                }

                sPlan[rowNum.getNum() - 1][(int)columnPos.getChar() - 65] = 'X';
                System.out.println("This seat is reserved for you");
          }
          else
                System.out.println("Non smoking section of economic class is Full");
    }

    public static void assignSeatSmokingEconomic(char[][] sPlan) throws IOException
    {
          IntClass rowNum = new IntClass();
          CharClass columnPos = new CharClass();

          if(!isSmokingEconomicFull(sPlan))
          {
                selectSeatNumber(8, 13, rowNum, columnPos);

                while(sPlan[rowNum.getNum() - 1][(int)columnPos.getChar() - 65] != '*')
                {
                      System.out.println("*#*#*#*# This seat is occupied *#*#*#*#");
                      System.out.println("Make another selection");

                      showSeatAssignments(sPlan);

                      selectSeatNumber(8, 13, rowNum, columnPos);
                }

                sPlan[rowNum.getNum() - 1][(int)columnPos.getChar() - 65] = 'X';
                System.out.println("This seat is reserved for you");
          }
          else
                System.out.println("Smoking section of economic class is Full");
    }
}
Last edited by LuciWiz : 26-Mar-2006 at 10:14. Reason: Please insert your Java code between [java] & [/java] tags
  #2  
Old 04-Mar-2005, 19:06
leanieleanz leanieleanz is offline
New Member
 
Join Date: Mar 2005
Posts: 10
leanieleanz is on a distinguished road
Thumbs up

Nevermind - done


Nevermind everyone...I got it done thanks anyways!
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Java] Eclipse.org's Java IDE JdS Java Forum 5 04-Dec-2005 09:41
Made program in Java, trying C++ now, file i/o problems technickel C++ Forum 4 19-Feb-2005 00:32
calling a java program from C Magicman C Programming Language 3 20-Oct-2004 07:43
convert long to pointer to char realpopeye C++ Forum 2 26-Sep-2003 10:22

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:54.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.