OOP with Java

OOP with Java

I had a look at the solution for the answers, in the website already, but my lecturer wants us to do it in another method. Here goes the question.

Write a program to assign passengers seats in an airplane. Assume a small airplane with seat numberings as follows:
1 A B C D
2 A B C D
3 A B C D
4 A B C D
5 A B C D
6 A B C D
7 A B C D

The program should display the seat pattern, with an 'X' marking the seats already assigned. For example, after seats 1A, 2B, and 4C are taken, the display should look like:
1 X B C D
2 A X C D
3 A B C D
4 A B X D
5 A B C D
6 A B C D
7 A B C D

After displaying the seats available, the program prompts for the seat desired, the user types in a seat, and then the display of available seats is updated. This continues until all seats are filled or until the user signals that the program should end. If the user types in a seat that is already assigned, the program should say that that seat is occupied and ask for another choice.

This is part of my answer but I can't complete it any further. Mind if anyone could help out, please?

Here's my solution.

public class Question2 {

public static void main(String[] args) {

final int rows = 7, cols = 4;
char[][] seats = new char[rows][cols];
int i, j;
char seatLetter = 'A';

for (i=0; i <seats.length; i++) {
for(j=0; j < seats[i].length; j++)
seats[i][j] = seatLetter++;
seatLetter = 'A';
}

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

for (i=0; i < 7; i++) {
System.out.print("\nEnter your seat: ");

}
}
View Answers

January 26, 2010 at 11:33 PM

Hi Siu,

Below is my code for your requirement.
NOTE: I did not do any validation on inputs given by user.
So, you need to do all the validation part and I've used methods for code reusability. Hope this helps to start your project and if you have any queries you can contact me @ [email protected]

import java.util.Hashtable;
import java.util.Scanner;

public class Question2 {
final int rows = 7, cols = 4;
char[][] seats = new char[rows][cols];
Hashtable reservedSeats = new Hashtable();

public static void main(String[] args) {
Question2 q = new Question2();
q.buildSeats();
q.printSeats();
System.out.println("Enter Seat numbers:");
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
String s = scan.next();
int row = Integer.parseInt(s.toUpperCase().substring(0, 1));
char col = s.toUpperCase().charAt(1);
q.reserveSeat(row, col);
}
}

public void buildSeats() {
char seatLetter = 'A';
for (int i = 0; i < seats.length; i++) {
for (int j = 0; j < seats[i].length; j++)
seats[i][j] = seatLetter++;
seatLetter = 'A';
}
}

public void printSeats() {
System.out.println("Available Seats:");
for (int i = 0; i < seats.length; i++) {
System.out.print((i + 1) + " ");
for (int j = 0; j < seats[i].length; j++)
System.out.print(seats[i][j] + " ");
System.out.println();
}
}

public void reserveSeat(int row, char col) {
if (checkAvailability(row, col)) {
reservedSeats.put(new Integer(row), col);
for (int i = row - 1; i == row - 1; i++) {
for (int j = 0; j < seats[i].length; j++) {
if (seats[i][j] == col) {
seats[i][j] = 'X';
}
}
}
System.out.println(" Seat " + row + col + " is Reserved ");
} else {
System.out.println(" Sorry! The requested seat " + row + col
+ " is NOT Avialble.Please try another seat. ");
}
printSeats();
}

public boolean checkAvailability(int row, char col) {
boolean available = true;
if (reservedSeats.containsKey(new Integer(row))) {
if (reservedSeats.get(row).equals(col)) {
available = false;
}
}
return available;
}
}

January 27, 2010 at 12:10 PM

Hi Friend,

Try the following code:

import java.util.*;

class Passenger{
public int no;
public String seatA;
public String seatB;
public String seatC;
public String seatD;
public Passenger(){}
public Passenger(int no, String seatA,String seatB,String seatC,String seatD) {
this.no = no;
this.seatA = seatA;
this.seatB = seatB;
this.seatC = seatC;
this.seatD = seatD;
}
public int getNo() {
return no;
}
public void setNo(int no){
this.no=no;
}
public String getSeatA() {
return seatA;
}
public void setSeatA(String seatA){
this.seatA=seatA;
}
public String getSeatB() {
return seatB;
}
public void setSeatB(String seatB){
this.seatB=seatB;
}
public String getSeatC() {
return seatC;
}
public void setSeatC(String seatC){
this.seatC=seatC;
}
public String getSeatD() {
return seatD;
}
public void setSeatD(String seatD){
this.seatD=seatD;
}
}
public class AssignPassenger {

public static void main(String[] args) throws Exception {
List<Passenger> list = new ArrayList<Passenger>();
list.add(new Passenger(1, "A","B","C","D"));
list.add(new Passenger(2, "A","B","C","D"));
list.add(new Passenger(3, "A","B","C","D"));
list.add(new Passenger(4, "A","B","C","D"));
list.add(new Passenger(5, "A","B","C","D"));
list.add(new Passenger(6, "A","B","C","D"));
list.add(new Passenger(7, "A","B","C","D"));
for(int i=1;i<=5;i++){
System.out.print("Enter seat no: ");
Scanner input=new Scanner(System.in);
int num = input.nextInt();
String sn=input.next();
for (Passenger s : list){
if(num == s.getNo()){
if(sn.equals(s.getSeatA())){
s.setSeatA("X");
}
else if(sn.equals(s.getSeatB())){
s.setSeatB("X");
}
else if(sn.equals(s.getSeatC())){
s.setSeatC("X");
}
else if(sn.equals(s.getSeatD())){
s.setSeatD("X");
}
else{
System.out.println("Not Available");
}

}


System.out.println(s.getNo()+" "+s.getSeatA()+" "+s.getSeatB()+" "+s.getSeatC()+" "+s.getSeatD());
}
}
}
}

Hope that it will be helpful for you.
Thanks

January 28, 2010 at 10:06 AM

Hey siu,
The previous code that i posted fails in one case.(eg., 1a 1b 1a).
Sorry for that.So, i fixed the code and posting it again. Hope this works in all cases.
Please test all the cases before use.

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Scanner;

public class Question2 {
final int rows = 7, cols = 4;
char[][] seats = new char[rows][cols];
ArrayList<String> reservedSeats = new ArrayList<String>();

public static void main(String[] args) {
Question2 q = new Question2();
q.buildSeats();
q.printSeats();
System.out.println("Enter Seat numbers:");
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
String s = scan.next();
int row = Integer.parseInt(s.toUpperCase().substring(0, 1));
char col = s.toUpperCase().charAt(1);
q.reserveSeat(row, col);
}
}

public void buildSeats() {
char seatLetter = 'A';
for (int i = 0; i < seats.length; i++) {
for (int j = 0; j < seats[i].length; j++)
seats[i][j] = seatLetter++;
seatLetter = 'A';
}
}

public void printSeats() {
System.out.println("Available Seats:");
for (int i = 0; i < seats.length; i++) {
System.out.print((i + 1) + " ");
for (int j = 0; j < seats[i].length; j++)
System.out.print(seats[i][j] + " ");
System.out.println();
}
}

public void reserveSeat(int row, char col) {
String seatNo=String.valueOf(row)+col;
if (checkAvailability(seatNo)) {
reservedSeats.add(seatNo);
for (int i = row - 1; i == row - 1; i++) {
for (int j = 0; j < seats[i].length; j++) {
if (seats[i][j] == col) {
seats[i][j] = 'X';
}
}
}
System.out.println(" Seat " + seatNo + " is Reserved ");
}
else
System.out.println("Sorry! The Seat "+seatNo+" is NOT available.Please look up for another seat.");
printSeats();
}

public boolean checkAvailability(String seatNo) {
boolean available = true;
for(int i=0;i<reservedSeats.size();i++){
if(reservedSeats.get(i).equalsIgnoreCase(seatNo)){
available = false;
}
}
return available;
}
}

Regards,
javaquest2010









Related Tutorials/Questions & Answers:
java oop
java oop  Design and implement a class called DayType that implements the day of the week in a program. The class DayType should store the day, such as Sun for Sunday, Mon for Monday and so on and so forth. The program should
Java with OOP assignment (Eclipse)
Java with OOP assignment (Eclipse)  How do i control the length of characters in an input? E.g.: Enter your name > Hi * Name too short
Advertisements
OOP - Java Beginners
OOP Design Pattern  What is the oops design patterns
OOP JAVa - Java Interview Questions
OOP JAVa  Is java is 100% OOP.?Plz giv full explanation.  hi Sanjay,dis is Srinivas(MCA from Andhra University).....Java is not an 100 % OOPL,because of the availability of Primitive Data Types
oop answers - Java Beginners
oop answers  Consider the following declarations: { private int.... Write a Java statement that prints the values of the instance variables of x. i. Write a Java statement that creates the XClass object t and initializes
OOP questions - Java Beginners
OOP questions  Consider the following declarations: { private... of a and the instance variable w is initialized to the value of b. h. Write a Java statement that prints the values of the instance variables of x. i. Write a Java
OOP - Java Beginners
OOP  i have problem with OOP hope u'll help me write a program that allows the user to enter student' names followed by their test scores and outputs...://www.roseindia.net/java/master-java/java-object-oriented-language.shtml
OOP questions - Java Beginners
OOP questions  Consider the following declarations: { private int u; private double w; public XClass() { } public XClass(int a, double...; } For more information on Java visit to : http://www.roseindia.net/java
OOP with Java - Java Beginners
OOP with Java  I had a look at the solution for the answers, in the website already, but my lecturer wants us to do it in another method. Here goes...) { Question2 q = new Question2(); q.buildSeats(); q.printSeats
java"oop" - Java Beginners
Java OOPs Concept  What is OOPs programming and what it has to do with the Java?  Hi i hope you understand it.//To print the even numbers...){ System.out.println(e); } }}Java program to display all even numbers http
java oop - Java Beginners
java oop  Consider the following declarations: { private int u... variable w is initialized to the value of b. h. Write a Java statement that prints the values of the instance variables of x. i. Write a Java statement
JAVA OOP - Java Beginners
JAVA OOP  Employees in a company are divided into the classes Employee, HourlyPaid, Salescommissioned and Executive for the purpose of calculating their weekly wages or monthly salaries. The data to be maintained for each class
OOP - Java Beginners
OOP  Which one is better to learn object oriented progamming java or c++? thaks, Bet.  Hello, As of my concern both have different scope and use, you can not merge these. As i java person so i will show you java
OOP - Java Beginners
); } For more information on Java visit to : http://www.roseindia.net/java/ http://www.roseindia.net/java/beginners/Construct.shtml Thanks
oop
oop  can you tell me about why java is not 100% object oriented language
OOP using Java - Java Beginners
OOP using Java  Can you write a Java statement that creates the object mysteryClock of the Clock type, and initialize the instance variables hr,min...); } } For more information on Java visit to : http://www.roseindia.net/java
OOP with Java 2 - Java Beginners
OOP with Java 2  Define a class called BogEntry that could be used to store an entry for a Web log. The class should have member variables to store the poster's username, text of entry, and the date of the entry using the Date
OOP Using JAVA - Java Beginners
OOP Using JAVA  OBJECT ORIENTED PROGRAMMING USING JAVA (hope guys u will help me please i need your help,thank you so much) Create a Java program...(); oops.OopType(); } } For more information on Java visit to : http
OOP with Java 3 - Java Beginners
OOP with Java 3  Write a Temperature class that has two instances variables: temperature value (a floating-point number) and a character for the scale, wither C for Celsius or F for Fahrenheit. the class should have four
OOP with Java-Array - Java Beginners
OOP with Java-Array  Write a program to assign passengers seats in an airplane. Assume a small airplane with seat numberings as follows: 1 A B C D 2 A B C D 3 A B C D 4 A B C D 5 A B C D 6 A B C D 7
Java with OOP assignment (Eclipse) - Java Beginners
Java with OOP assignment (Eclipse)  "THREE Ts GAME" *Description* A "tic-tac-toe" game is a two player's board game where the game board..." games using Java. When your game application started, the players will be able
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.16.0 is released. Learn to use oop-lib-jvm version 0.16.0 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.16.0 in Java projects. Follow the step by step...Maven dependency for  it.unibo.tuprolog  - Version 0.16.0 of oop-lib-jvm released The developers of   it.unibo.tuprolog - oop-lib-jvm
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.17.1 is released. Learn to use oop-lib-jvm version 0.17.1 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.17.1 in Java projects. Follow the step by step...Maven dependency for  it.unibo.tuprolog  - Version 0.17.1 of oop-lib-jvm released The developers of   it.unibo.tuprolog - oop-lib-jvm
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.17.3 is released. Learn to use oop-lib-jvm version 0.17.3 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.17.3 in Java projects. Follow the step by step...Maven dependency for  it.unibo.tuprolog  - Version 0.17.3 of oop-lib-jvm released The developers of   it.unibo.tuprolog - oop-lib-jvm
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.17.4 is released. Learn to use oop-lib-jvm version 0.17.4 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.17.4 in Java projects. Follow the step by step...Maven dependency for  it.unibo.tuprolog  - Version 0.17.4 of oop-lib-jvm released The developers of   it.unibo.tuprolog - oop-lib-jvm
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.18.1 is released. Learn to use oop-lib-jvm version 0.18.1 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.18.1 in Java projects. Follow the step by step...Maven dependency for  it.unibo.tuprolog  - Version 0.18.1 of oop-lib-jvm released The developers of   it.unibo.tuprolog - oop-lib-jvm
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.30.2 is released. Learn to use oop-lib-jvm version 0.30.2 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.30.2 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.30.2 java library in your project. ADS...Maven dependency for  it.unibo.tuprolog  - Version 0.30.2 of oop-lib
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.30.0 is released. Learn to use oop-lib-jvm version 0.30.0 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.30.0 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.30.0 java library in your project. ADS...Maven dependency for  it.unibo.tuprolog  - Version 0.30.0 of oop-lib
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.20.2 is released. Learn to use oop-lib-jvm version 0.20.2 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.20.2 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.20.2 java library in your project. ADS...Maven dependency for  it.unibo.tuprolog  - Version 0.20.2 of oop-lib
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.20.7 is released. Learn to use oop-lib-jvm version 0.20.7 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.20.7 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.20.7 java library in your project. ADS...Maven dependency for  it.unibo.tuprolog  - Version 0.20.7 of oop-lib
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.20.0 is released. Learn to use oop-lib-jvm version 0.20.0 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.20.0 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.20.0 java library in your project. ADS...Maven dependency for  it.unibo.tuprolog  - Version 0.20.0 of oop-lib
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.15.0 is released. Learn to use oop-lib-jvm version 0.15.0 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.15.0 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.15.0 java library in your project. ADS...Maven dependency for  it.unibo.tuprolog  - Version 0.15.0 of oop-lib
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.14.11 is released. Learn to use oop-lib-jvm version 0.14.11 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.14.11 in Java projects. Follow the step...; it.unibo.tuprolog - oop-lib-jvm version 0.14.11 java library in your project...Maven dependency for  it.unibo.tuprolog  - Version 0.14.11 of oop
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.16.1 is released. Learn to use oop-lib-jvm version 0.16.1 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.16.1 in Java projects. Follow the step by step...Maven dependency for  it.unibo.tuprolog  - Version 0.16.1 of oop-lib-jvm released The developers of   it.unibo.tuprolog - oop-lib-jvm
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.17.0 is released. Learn to use oop-lib-jvm version 0.17.0 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.17.0 in Java projects. Follow the step by step...Maven dependency for  it.unibo.tuprolog  - Version 0.17.0 of oop-lib-jvm released The developers of   it.unibo.tuprolog - oop-lib-jvm
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.17.2 is released. Learn to use oop-lib-jvm version 0.17.2 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.17.2 in Java projects. Follow the step by step...Maven dependency for  it.unibo.tuprolog  - Version 0.17.2 of oop-lib-jvm released The developers of   it.unibo.tuprolog - oop-lib-jvm
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.18.0 is released. Learn to use oop-lib-jvm version 0.18.0 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.18.0 in Java projects. Follow the step by step...Maven dependency for  it.unibo.tuprolog  - Version 0.18.0 of oop-lib-jvm released The developers of   it.unibo.tuprolog - oop-lib-jvm
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.20.9 is released. Learn to use oop-lib-jvm version 0.20.9 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.20.9 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.20.9 java library in your project. ADS...Maven dependency for  it.unibo.tuprolog  - Version 0.20.9 of oop-lib
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.30.4 is released. Learn to use oop-lib-jvm version 0.30.4 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.30.4 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.30.4 java library in your project. ADS...Maven dependency for  it.unibo.tuprolog  - Version 0.30.4 of oop-lib
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.20.4 is released. Learn to use oop-lib-jvm version 0.20.4 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.20.4 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.20.4 java library in your project. ADS...Maven dependency for  it.unibo.tuprolog  - Version 0.20.4 of oop-lib
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.20.3 is released. Learn to use oop-lib-jvm version 0.20.3 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.20.3 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.20.3 java library in your project. ADS...Maven dependency for  it.unibo.tuprolog  - Version 0.20.3 of oop-lib
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.30.1 is released. Learn to use oop-lib-jvm version 0.30.1 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.30.1 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.30.1 java library in your project. ADS...Maven dependency for  it.unibo.tuprolog  - Version 0.30.1 of oop-lib
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.30.3 is released. Learn to use oop-lib-jvm version 0.30.3 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.30.3 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.30.3 java library in your project. ADS...Maven dependency for  it.unibo.tuprolog  - Version 0.30.3 of oop-lib
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.20.1 is released. Learn to use oop-lib-jvm version 0.20.1 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.20.1 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.20.1 java library in your project. ADS...Maven dependency for  it.unibo.tuprolog  - Version 0.20.1 of oop-lib
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.15.2 is released. Learn to use oop-lib-jvm version 0.15.2 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.15.2 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.15.2 java library in your project. ADS...Maven dependency for  it.unibo.tuprolog  - Version 0.15.2 of oop-lib
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.19.0 is released. Learn to use oop-lib-jvm version 0.19.0 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.19.0 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.19.0 java library in your project. ADS...Maven dependency for  it.unibo.tuprolog  - Version 0.19.0 of oop-lib
Maven dependency for it.unibo.tuprolog - oop-lib-jvm version 0.18.2 is released. Learn to use oop-lib-jvm version 0.18.2 in Maven based Java projects
; it.unibo.tuprolog - oop-lib-jvm version 0.18.2 in Java projects. Follow the step by step...; it.unibo.tuprolog - oop-lib-jvm version 0.18.2 java library in your project. ADS...Maven dependency for  it.unibo.tuprolog  - Version 0.18.2 of oop-lib
oop concept
oop concept  why Object class is super class for all class? 1.to use the Object class methods in subclass directly or 2.the Object class is usefull at the time of object create of subclass what
OOP - Java Beginners
OOP

Ads