0% found this document useful (0 votes)
375 views

Computer Applications ICSE Worksheet

The document describes a RailwayTicket class with methods to accept passenger details like name, coach, mobile number and ticket amount. The update method calculates the total amount based on the coach selected by adding the appropriate surcharge. The display method prints the passenger details and total amount. The main method creates a RailwayTicket object and calls the accept, update and display methods.

Uploaded by

Sneha Bhatia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
375 views

Computer Applications ICSE Worksheet

The document describes a RailwayTicket class with methods to accept passenger details like name, coach, mobile number and ticket amount. The update method calculates the total amount based on the coach selected by adding the appropriate surcharge. The display method prints the passenger details and total amount. The main method creates a RailwayTicket object and calls the accept, update and display methods.

Uploaded by

Sneha Bhatia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Question 1.

Design a class Railway Ticket with following description : [15]


Instance variables/s data members :
String name : To store the name of the customer
String coach : To store the type of coach customer wants to travel
long mobno : To store customer’s mobile number
int amt : To store basic amount of ticket
int totalamt : To store the amount to be paid after updating the original amount

Member methods
void accept ( ) — To take input for name, coach, mobile number and amount
void update ( )— To update the amount as per the coach selected

Type of Coaches Amount


First_ AC 700
Second_AC 500
Third _AC 250
sleeper None

void display( ) — To display all details of a customer such as name, coach, total
amount and mobile number.
Write a main method to create an object of the class and call the above member
methods.

Solution:
import java.io.*;
import java.util.Scanner;
class RailwayTicket
{
String name, coach;
long mobno;
int amt, totalamt;
void accept( )
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter Passenger’s Name: “);
name = sc.next( );
System.out.print(“Enter Mobile Number:”);
mobno = sc.nextlnt( );
Systein.out.print(“Enter Coach (FirstAC/SecondAC/ThirdAC/sleeper):”);
coach = sc.next( );
System.out.print(“Enter basic amount of ticket:”);
amt = sc.nexdnt( );
}
void update()
{
if (coach.equals(“First_AC”))
totalamt = amt + 700;
else
if (coach.equals(“Second_AC”))
totalamt = amt + 500; .
else
if (coach.equals!”Third_AC”))
totalamt = amt + 250;
else
totalamt = amt;
}
void display()
{
System.out.println(“\n\n Name :” +name);
System.out.println(“Coach :” +coach);
System.out.prindn(”Total Amount:” +totalaint);
System.out.prindn(“Mobile No.:” +name);
}
public static void main (String args[ ])
{
RailwayTicket t = new RailwayTicket!);
t.accept();
t.update();
t.display();
}
}

You might also like