In this lesson, you will learn
package ch7.l7;
class Seat {
int row;
int column;
boolean isBooked;
// Constructor to initialize the seat
public Seat(int r, int c) {
row = r;
column = c;
isBooked = false; // All seats are initially not booked
}
// Method to book the seat
public void bookSeat() {
isBooked = true;
}
// Method to check if the seat is booked
public boolean isBooked() {
return isBooked;
}
@Override
public String toString() {
return "Seat: " + row + "," + column + " - "
+(isBooked ? "Booked" : "Available");
}
}
class CinemaHall {
// Method to generate and return a 2D array of Seat objects
public static Seat[][] initializeSeating(int rows, int cols) {
Seat[][] seats = new Seat[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
seats[i][j] = new Seat(i + 1, j + 1);
// Creating seat objects with 1-based indexing
// for rows and cols
}
}
return seats;
}
}
public class CinemaHallMain {
public static void main(String[] args) {
// Initialize the seating arrangement for a cinema
// hall with 5 rows and 8 columns
Seat[][] cinemaSeats = CinemaHall.initializeSeating(5, 8);
// Example: Book some seats
cinemaSeats[2][3].bookSeat(); // Booking seat at row 3, column 4
cinemaSeats[4][7].bookSeat(); // Booking seat at row 5, column 8
// Print the seating arrangement
for (Seat[] row : cinemaSeats) {
for (Seat seat : row) {
System.out.println(seat);
}
}
}
}
Seat: 1,1 - Available
Seat: 1,2 - Available
Seat: 1,3 - Available
Seat: 1,4 - Available
Seat: 1,5 - Available
Seat: 1,6 - Available
Seat: 1,7 - Available
Seat: 1,8 - Available
Seat: 2,1 - Available
Seat: 2,2 - Available
Seat: 2,3 - Available
Seat: 2,4 - Available
Seat: 2,5 - Available
Seat: 2,6 - Available
Seat: 2,7 - Available
Seat: 2,8 - Available
Seat: 3,1 - Available
Seat: 3,2 - Available
Seat: 3,3 - Available
Seat: 3,4 - Booked
Seat: 3,5 - Available
Seat: 3,6 - Available
Seat: 3,7 - Available
Seat: 3,8 - Available
Seat: 4,1 - Available
Seat: 4,2 - Available
Seat: 4,3 - Available
Seat: 4,4 - Available
Seat: 4,5 - Available
Seat: 4,6 - Available
Seat: 4,7 - Available
Seat: 4,8 - Available
Seat: 5,1 - Available
Seat: 5,2 - Available
Seat: 5,3 - Available
Seat: 5,4 - Available
Seat: 5,5 - Available
Seat: 5,6 - Available
Seat: 5,7 - Available
Seat: 5,8 - Booked
package ch7.l7;
class Student{
int rollno;
String name, city;
Student(int roll, String n, String c){
rollno=roll;
name=n;
city=c;
}
public static void main(String args[]){
Student s1=new Student(1001,"Somya","Delhi");
Student s2=new Student(1002,"James","USA");
System.out.println(s1);//compiler writes here s1.toString()
System.out.println(s2);//compiler writes here s2.toString()
}
}
ch7.l7.Student@34a245ab
ch7.l7.Student@7cc355be
In the above example, the compiler prints the hashcode of the corresponding objects instead of printing its values. Java Compiler calls the toString() method internally to print the same.
package ch7.l7;
class Student{
int rollno;
String name, city;
Student(int roll, String n, String c){
rollno=roll;
name=n;
city=c;
}
@Override
public String toString() {
return rollno+","+name+","+city;
}
public static void main(String args[]){
Student s1=new Student(1001,"Somya","Delhi");
Student s2=new Student(1002,"James","USA");
System.out.println(s1);//compiler writes here s1.toString()
System.out.println(s2);//compiler writes here s2.toString()
}
}
1001,Somya,Delhi
1002,James,USA
In the above example, the Java compiler internally calls the toString() method, overriding this method will return the specified values of the s1 and s2 objects of the Student class.
There are no reviews yet. Be the first one to write one.
You must be logged in to submit a review.