CHAPTER -11
PG- 195,196,197
1. Design a class RailwayTicket with following description:
Instance variables / data members
Data
Purpose
Members
String name To store the name of the customer
String coach To store the type of coach customer wants to travel
long mob no To store customer's mobile number
int amt To store basic amount of ticket
To store the amount to be paid after updating the original
int totalamt
amount
Member
Purpose
Methods
void accept() To take input for name, coach, mobile number and amount
Member
Purpose
Methods
To update the amount as per the coach selected (extra amount
void update()
to be added in the amount as per the table below)
To display all details of a customer such as name, coach, total
void display()
amount and mobile number
Type of
Amount
Coaches
First_AC ₹700
Second_AC ₹500
Third_AC ₹250
Sleeper None
Write a main method to create an object of the class and call the above member
methods.
Programming Code
import java.util.Scanner;
public class RailwayTicket
String name,coach;
long mobno;
int amt,totalamt;
public void accept()
Scanner in = new Scanner(System.in);
System.out.print("Enter name: ");
name = in.nextLine();
System.out.print("Enter coach: ");
coach = in.nextLine();
System.out.print("Enter mobile no: ");
mobno = in.nextLong();
System.out.print("Enter amount: ");
amt = in.nextInt();
public void update()
if(coach.equalsIgnoreCase("First_AC"))
totalamt = amt + 700;
else if(coach.equalsIgnoreCase("Second_AC"))
totalamt = amt + 500;
else if(coach.equalsIgnoreCase("Third_AC"))
totalamt = amt + 250;
else if(coach.equalsIgnoreCase("Sleeper"))
totalamt = amt;
public void display()
System.out.println("Name: " + name);
System.out.println("Coach: " + coach);
System.out.println("Total Amount: " + totalamt);
System.out.println("Mobile number: " + mobno);
public static void main()
RailwayTicket obj = new RailwayTicket();
obj.accept();
obj.update();
obj.display();
}
Output
Enter name: Rakesh
Enter coach: FIRST_AC
Enter mobile no: 9874562312
Enter amount: 1500
Name: Rakesh
Coach: FIRST_AC
Total Amount: 2200
Mobile number: 9874562312
2. Define a class ElectricBill with the following specifications:
class : ElectricBill
Instance variables / data member:
String n — to store the name of the customer
int units — to store the number of units consumed
double bill — to store the amount to be paid
Member methods:
void accept( ) — to accept the name of the customer and number of units
consumed
void calculate( ) — to calculate the bill as per the following tariff:
Number of
Rate per unit
units
First 100 units Rs.2.00
Next 200 units Rs.3.00
Above 300 units Rs.5.00
A surcharge of 2.5% charged if the number of units consumed is above 300 units.
void print( ) — To print the details as follows:
Name of the customer: ………………………
Number of units consumed: ………………………
Bill amount: ………………………
Write a main method to create an object of the class and call the above member
methods.
Programming code
import java.util.Scanner;
public class ElectricBill1
String n;
int units;
double bill,amt,surcharge;
public void accept()
Scanner in = new Scanner(System.in);
System.out.print("Enter customer name: ");
n = in.nextLine();
System.out.print("Enter units consumed: ");
units = in.nextInt();
public void calculate()
if (units <= 100)
bill = units * 2;
else if (units <= 300)
bill = 200 + (units - 100) * 3;
else
amt = 200 + 600 + (units - 300) * 5;
surcharge = (amt * 2.5) / 100.0;
bill = amt + surcharge;
public void print() {
System.out.println("Name of the customer : " + n);
System.out.println("Number of units consumed : " + units);
System.out.println("Bill amount : " + bill);
public static void main()
ElectricBill1 obj = new ElectricBill1();
obj.accept();
obj.calculate();
obj.print();
Output
Enter customer name: Tuhin
Enter units consumed: 562
Name of the customer : Tuhin
Number of units consumed : 562
Bill amount : 2162.75
3. Define a class called BookFair with the following description:
Data Members Purpose
String Bname stores the name of the book
double price stores the price of the book
Member
Purpose
Methods
BookFair( ) Constructor to initialize data members
void input( ) To input and store the name and price of the book
void To calculate the price after discount. Discount is calculated as
calculate( ) per the table given below
void display( ) To display the name and price of the book after discount
Price Discount
Less than or equal to ₹1000 2% of price
Price Discount
More than ₹1000 and less than or equal to
10% of price
₹3000
More than ₹3000 15% of price
Write a main method to create an object of the class and call the above member
methods.
Programming code
import java.util.Scanner;
public class BookFair
String bname;
double price;
public BookFair()
bname = "";
price = 0.0;
}
public void input()
Scanner in = new Scanner(System.in);
System.out.print("Enter name of the book: ");
bname = in.nextLine();
System.out.print("Enter price of the book: ");
price = in.nextDouble();
public void calculate()
double disc;
if (price <= 1000)
disc = price * 0.02;
else if (price <= 3000)
disc = price * 0.1;
else
disc = price * 0.15;
price -= disc;
}
public void display()
System.out.println("Book Name: " + bname);
System.out.println("Price after discount: " + price);
public static void main()
BookFair obj = new BookFair();
obj.input();
obj.calculate();
obj.display();
Output
Enter name of the book: knowledge bank
Enter price of the book: 3500
Book Name: knowledge bank
Price after discount: 2975.0
4. Write a program to input and store roll number, name and marks in 3 subjects
of one student and display the remark based on average marks as given below:
Average
Remark
Marks
85 — 100 Excellent
75 — 84 Distinction
60 — 74 First Class
40 — 59 Pass
Less than 40 Poor
Programming code
import java.util.Scanner;
public class AvgMarks
{
int roll,s1,s2,s3;
String name,remark;
double avg;
void input()
Scanner in = new Scanner(System.in);
System.out.println(" Enter Name of student: ");
name = in.nextLine();
System.out.println(" Enter Roll Number of student: ");
roll = in.nextInt();
System.out.println(" Enter Subject 1 Marks: ");
s1= in.nextInt();
System.out.println(" Enter Subject 2 Marks: ");
s2= in.nextInt();
System.out.println(" Enter Subject 3 Marks: ");
s3= in.nextInt();
avg= (s1+s2+s3)/3.0;
void calculation()
if (avg< 40)
remark = "Poor";
else if (avg < 60)
remark = "Pass";
else if (avg < 75)
remark = "First Class";
else if (avg < 85)
remark = "Distinction";
else
remark = "Excellent";
void output()
System.out.println( " Roll Number of student : " +roll);
System.out.println( " Name of student : " +name);
System.out.println( " Marks of subject1 : " +s1);
System.out.println( " Marks of subject2 : " +s2);
System.out.println( " Marks of subject3 : " +s3);
System.out.println( " Remark of student : " +remark);
public static void main()
AvgMarks obj= new AvgMarks();
obj.input();
obj.calculation();
obj.output();
Output
Enter Name of student:
raju
Enter Roll Number of student:
23
Enter Subject 1 Marks:
89
Enter Subject 2 Marks:
85
Enter Subject 3 Marks:
88
Roll Number of student : 23
Name of student : raju
Marks of subject1 : 89
Marks of subject2 : 85
Marks of subject3 : 88
Remark of student : Excellent
5. Define a class called ParkingLot with the following description:
Data
Purpose
Members
int vno To store the vehicle number
To store the number of hours the vehicle is parked in the
int hours
parking lot
double bill To store the bill amount
Member
Purpose
Methods
void input( ) To input the vno and hours
Member
Purpose
Methods
To compute the parking charge at the rate ₹3 for the first hour
void
or the part thereof and ₹1.50 for each additional hour or part
calculate( )
thereof.
void display() To display the detail
Write a main method to create an object of the class and call the above methods.
Programming code
import java.util.Scanner;
public class ParkingLot
int vno,hours;
double bill;
public void input()
Scanner in = new Scanner(System.in);
System.out.print("Enter vehicle number: ");
vno = in.nextInt();
System.out.print("Enter hours: ");
hours = in.nextInt();
public void calculate()
if (hours <= 1)
bill = 3;
else
bill = 3 + (hours - 1) * 1.5;
public void display()
System.out.println("Vehicle number: " + vno);
System.out.println("Hours: " + hours);
System.out.println("Bill: " + bill);
public static void main()
ParkingLot obj = new ParkingLot();
obj.input();
obj.calculate();
obj.display();
Output
Enter vehicle number: 7025
Enter hours: 7
Vehicle number: 7025
Hours: 7
Bill: 12.0
6. Rewrite the following program segment using if-else statements instead of
the ternary operator:
String grade = (marks>=90)?"A": (marks>=80)? "B": "C";
String grade;
if (marks >= 90)
grade = "A";
else if (marks >= 80)
grade = "B";
else
grade = "C";
7. Define a class named movieMagic with the following description:
Data Members Purpose
int year To store the year of release of a movie
String title To store the title of the movie
To store the popularity rating of the movie
float rating
(minimum rating=0.0 and maximum rating=5.0)
Member
Purpose
Methods
Default constructor to initialize numeric data members to 0
movieMagic()
and String data member to "".
void accept() To input and store year, title and rating
void display() To display the title of the movie and a message based on the
Member
Purpose
Methods
rating as per the table given below
Ratings Table
Rating Message to be displayed
0.0 to
Flop
2.0
2.1 to
Semi-Hit
3.4
3.5 to
Hit
4.4
4.5 to
Super-Hit
5.0
Write a main method to create an object of the class and call the above member
methods.
Programming code
import java.util.Scanner;
public class movieMagic
int year;
String title;
float rating;
public movieMagic()
year = 0;
title = "";
rating = 0.0f;
public void accept()
Scanner in = new Scanner(System.in);
System.out.print("Enter Title of Movie: ");
title = in.nextLine();
System.out.print("Enter Year of Movie: ");
year = in.nextInt();
System.out.print("Enter Rating of Movie: ");
rating = in.nextFloat();
public void display()
String message = "Invalid Rating";
if (rating <= 2.0f)
message = "Flop";
else if (rating <= 3.4f)
message = "Semi-Hit";
else if (rating <= 4.4f)
message = "Hit";
else if (rating <= 5.0f)
message = "Super-Hit";
System.out.println(title);
System.out.println(message);
public static void main()
movieMagic obj = new movieMagic();
obj.accept();
obj.display();
Output
Enter Title of Movie: TRZONE
Enter Year of Movie: 2001
Enter Rating of Movie: 5.6
TRZONE
Invalid Rating
9. Given below is a hypothetical table showing rates of income tax for male
citizens below the age of 65 years:
Taxable income (TI) in ₹ Income Tax in ₹
Does not exceed Rs. 1,60,000 Nil
Is greater than Rs. 1,60,000 and less than or equal to
(TI - 1,60,000) x 10%
Rs. 5,00,000.
Taxable income (TI) in ₹ Income Tax in ₹
Is greater than Rs. 5,00,000 and less than or equal to [(TI - 5,00,000) x 20%] +
Rs. 8,00,000 34,000
[(TI - 8,00,000) x 30%] +
Is greater than Rs. 8,00,000
94,000
Write a program to input the age, gender (male or female) and Taxable Income of
a person.
If the age is more than 65 years or the gender is female, display “wrong
category”. If the age is less than or equal to 65 years and the gender is male,
compute and display the income tax payable as per the table given above.
Programming code
import java.util.Scanner;
public class IncomeTax
public static void main()
Scanner in = new Scanner(System.in);
System.out.print("Enter Gender(male/female): ");
String gender = in.nextLine();
System.out.print("Enter Age: ");
int age = in.nextInt();
System.out.print("Enter Taxable Income: ");
double ti = in.nextDouble();
double tax = 0.0;
if (age > 65 || gender.equalsIgnoreCase("female"))
System.out.print("Wrong Category");
else {
if (ti <= 160000)
tax = 0;
else if (ti <= 500000)
tax = (ti - 160000) * 10 / 100;
else if (ti <= 800000)
tax = 34000 + ((ti - 500000) * 20 / 100);
else
tax = 94000 + ((ti - 800000) * 30 / 100);
System.out.println("Tax Payable: " + tax);
}
}
Output
Enter Gender(male/female): male
Enter Age: 65
Enter Taxable Income: 500000
Tax Payable: 34000.0
10. Design a class to overload a function compare( ) as follows:
1. void compare(int, int) — to compare two integers values and print the
greater of the two integers.
2. void compare(char, char) — to compare the numeric value of two
characters and print with the higher numeric value.
3. void compare(String, String) — to compare the length of the two strings
and print the longer of the two.
Programming code
import java.util.Scanner;
public class Compare
public void compare(int a, int b)
{
if (a > b)
System.out.println(a);
else
System.out.println(b);
public void compare(char a, char b)
int x = (int)a;
int y = (int)b;
if (x > y) {
System.out.println(a);
else {
System.out.println(b);
}
public void compare(String a, String b)
int l1 = a.length();
int l2 = b.length();
if (l1 > l2) {
System.out.println(a);
else {
System.out.println(b);
public static void main() {
Scanner in = new Scanner(System.in);
Compare obj = new Compare();
System.out.print("Enter first integer: ");
int n1 = in.nextInt();
System.out.print("Enter second integer: ");
int n2 = in.nextInt();
obj.compare(n1, n2);
System.out.print("Enter first character: ");
char c1 = in.next().charAt(0);
System.out.print("Enter second character: ");
char c2 = in.next().charAt(0);
in.nextLine();
obj.compare(c1, c2);
System.out.print("Enter first string: ");
String s1 = in.nextLine();
System.out.print("Enter second string: ");
String s2 = in.nextLine();
obj.compare(s1, s2);
}
}