0% found this document useful (0 votes)
12 views8 pages

Week 3

Uploaded by

billionoid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

Week 3

Uploaded by

billionoid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Date: SheetNo:

WEEK-3
1. A travel agent book tickets in rail to Mumbai to its customers.
Create a class Railway with the variables
pass_name,age,no_of_tickets,price,total amount . The manager of
the travel agent wants to know how many tickets and how many
customers the agent has booked
Program:
import java.util.Scanner;
class Railway {
String pass_name;
int age;
int no_of_tickets;
double price;
double total_amount;
void inputDetails() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter passenger name: ");
pass_name = sc.nextLine();
System.out.print("Enter age: ");
age = sc.nextInt();
System.out.print("Enter number of tickets: ");
no_of_tickets = sc.nextInt();
System.out.print("Enter price per ticket: ");
price = sc.nextDouble();
total_amount = no_of_tickets * price;
}
void displayDetails() {
System.out.println("Passenger Name: " + pass_name);
System.out.println("Age: " + age);
System.out.println("Number of Tickets: " + no_of_tickets);
System.out.println("Price per Ticket: " + price);
System.out.println("Total Amount: " + total_amount);
}
}
class Manager {
int totalTickets = 0;
int totalCustomers = 0;
void updateDetails(Railway r) {
totalTickets += r.no_of_tickets;
totalCustomers++;
}
Date: SheetNo:
void displaySummary() {
System.out.println("\nTotal Tickets Booked: " + totalTickets);
System.out.println("Total Customers: " + totalCustomers);
}
}
public class RailwayTicketBooking {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Manager manager = new Manager();
System.out.print("Enter the number of customers: ");
int numCustomers = sc.nextInt();
for (int i = 1; i <= numCustomers; i++) {
System.out.println("\nCustomer " + i);
Railway railway = new Railway();
railway.inputDetails();
railway.displayDetails();
manager.updateDetails(railway);
}
manager.displaySummary();
}
}
Output:
Input:
Enter the number of customers: 2

Customer 1
Enter passenger name: Alice
Enter age: 25
Enter number of tickets: 2
Enter price per ticket: 1000.0

Customer 2
Enter passenger name: Bob
Enter age: 30
Enter number of tickets: 3
Enter price per ticket: 1200.0

Output:
Passenger Name: Alice
Age: 25
Number of Tickets: 2
Price per Ticket: 1000.0
Total Amount: 2000.0
Date: SheetNo:
Passenger Name: Bob
Age: 30
Number of Tickets: 3
Price per Ticket: 1200.0
Total Amount: 3600.0

Total Tickets Booked: 5


Total Customers: 2
Date: SheetNo:

2. Write a Java program for For MOVIE TICKET RESERVATION


assuming that movie is A rated movie and it shouldn't allow the
children below 18 and identify the current status of the seats
available and should also display when the house is full.
Program:
import java.util.Scanner;
public class MovieTicketReservation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int age;
int totalSeats = 100;
int availableSeats = totalSeats;
System.out.print("Enter your age: ");
age = scanner.nextInt();
if (age < 18) {
System.out.println("Sorry, this movie is A-rated. You must be 18 or older.");
} else {
if (availableSeats > 0) {
System.out.println("Booking confirmed! Enjoy the show.");
availableSeats--;
System.out.println("Available seats: " + availableSeats);
} else {
System.out.println("House full! Sorry, no tickets available.");
}
}
}
}
Output:
Enter your age: 25
Booking confirmed! Enjoy the show.
Available seats: 99
Date: SheetNo:

3. Write a Java program to find out the number of votes polled by


party 'X' and 'Y' assuming a general election at Vijayawada.Find
out the Winner of the election
Program:
import java.util.Scanner;

public class ElectionResult {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int votesForX, votesForY;

System.out.print("Enter the number of votes polled for party X: ");


votesForX = scanner.nextInt();

System.out.print("Enter the number of votes polled for party Y: ");


votesForY = scanner.nextInt();

if (votesForX > votesForY) {


System.out.println("Party X wins the election!");
} else if (votesForY > votesForX) {
System.out.println("Party Y wins the election!");
} else {
System.out.println("The election is a tie!");
}
}
}
Output:
Enter the number of votes polled for party X: 10000
Enter the number of votes polled for party Y: 9500
Party X wins the election!
Date: SheetNo:

4. Write a Java program for movie ticket reservation.Assuming that


the number of tickets available are 10.Use the codisplay(),to
display the availability of tickets .The data members are
name ,movie name ,number of ticket needed ,cofull
display house full.
Program:
import java.util.Scanner;
class MovieTicketReservation {
String name;
String movieName;
int numberOfTicketsNeeded;
static int availableTickets = 10;
void inputDetails() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
name = scanner.nextLine();
System.out.print("Enter the movie name: ");
movieName = scanner.nextLine();
System.out.print("Enter the number of tickets needed: ");
numberOfTicketsNeeded = scanner.nextInt();
}

void bookTicket()
{
if (numberOfTicketsNeeded <= availableTickets) {
availableTickets -= numberOfTicketsNeeded;
System.out.println("Tickets booked successfully!");
} else {
System.out.println("Sorry, not enough tickets available.");
}
}

void display() {
System.out.println("Available tickets: " + availableTickets);
if (availableTickets == 0) {
System.out.println("House full!");
}
}
}

public class Main {


public static void main(String[] args) {
Date: SheetNo:
MovieTicketReservation reservation = new MovieTicketReservation();

reservation.inputDetails();
reservation.bookTicket();
reservation.display();
}
}
Output:
Enter your name: John Doe
Enter the movie name: Inception
Enter the number of tickets needed: 3

Tickets booked successfully!


Available tickets: 7
Date: SheetNo:

5.Write a Java program to count the number of object


created using static.
Program:
public class ObjectCounter {
private static int objectCount = 0;
public ObjectCounter() {
objectCount++;
}
public static void main(String[] args) {
ObjectCounter obj1 = new ObjectCounter();
ObjectCounter obj2 = new ObjectCounter();
ObjectCounter obj3 = new ObjectCounter();

System.out.println("Number of objects created: " + objectCount);


}
}
Output:
Number of objects created : 3

You might also like