LAB EXERCISE 3: Classes & Objects, Overloading
LAB EXERCISE 3: Classes & Objects, Overloading
1. Create a class named ConferenceRegistration that is used for getting the participant details. The
following details are to be gathered: Paper ID, Author Name, Registration Amount = 7000,
Registration Date. A discount of 50% is applicable for participants who are members of XYZ
Computer Society. The discount should be applicable while registering itself (Use parameterised
constructor / constructor overloading). Create three participant objects of whom one is a member of
XYZ Computer Society. Display the registration details of all three participants.
Code:
import java.io.*;
import java.util.*;
class JavaLab3_19BPS1127{
int PaperID;
String AuthorName;
String RegistrationDate;
boolean XYZ;
int RegistrationAmount;
PaperID=id;
AuthorName=name;
RegistrationDate=date;
RegistrationAmount=7000;
}
PaperID=id;
AuthorName=name;
RegistrationDate=date;
RegistrationAmount=3500;
System.out.println("Name : "+AuthorName);
System.out.println("Paper ID : "+PaperID);
System.out.println("Date : "+RegistrationDate);
r1.display();
r2.display();
r3.display();
}
Output:
2. Create a class named Employee where the details pertaining to employees such as Employee ID,
Name, Department, Salary and Designation are added. Create a member method named incentive
which will add 10% of salary as bonus to all employees. For some employees who have been
exceptional in their work an additional bonus of Rs. 10000 is given. Overload the incentive method
to perform this. Create three employee objects of whom one is an exceptional employee. Display
the revised salary details along with the employee id and name for all the three employees.
Code:
import java.io.*;
import java.util.*;
class JavaLab32_19BPS1127{
//Employee ID,
int EmployeeId;
String Name;
String Department;
double Salary;
String Designation;
boolean exp;
void incentive()
double inc=Salary/10;
Salary=Salary+inc;
void incentive(boolean e)
double inc=Salary/10;
Salary=Salary+inc+10000;
EmployeeId=id;
Name=name;
Department=dep;
Salary=sal;
Designation=des;
incentive();
EmployeeId=id;
Name=name;
Department=dep;
Salary=sal;
Designation=des;
incentive(k);
{
System.out.println("Name : "+Name);
System.out.println("Paper ID : "+EmployeeId);
System.out.println("Department : "+Department);
System.out.println("Designation : "+Designation);
r1.display();
r2.display();
r3.display();
Output:
3. Assume that a gallon of paint covers about 300 square feet of wall space. Create an application
with a main() method that prompts the user for the length, width, and height of a rectangular
room. Pass these three values to a method that does the following: Calculates the wall area for a
room, passes the calculated wall area to another method that calculates and returns the number
of gallons of paint needed, displays the number of gallons needed, computes the price based on
a paint price of $30 per gallon. Assuming that the painter can buy any fraction of a gallon of
paint at the same price as a whole gallon. Return the price to the main() method The main()
method displays the final price. Save the application as PaintCostFinder.java.
Code :
import java.io.*;
import java.util.*;
class PaintCostFinder{
double area=(2*length*height)+(2*width*height);
return area;
double paint=a/300;
return paint;
double price=a*30;
return price;
}
double length;
double width;
double height;
double wallArea;
double gallonReq;
double amount;
System.out.print("Length :");
length=scan.nextDouble();
System.out.print("Width :");
width=scan.nextDouble();
System.out.print("Height :");
height=scan.nextDouble();
wallArea=WallArea(length,width,height);
gallonReq=gallon(wallArea);
amount=finalPrice(gallonReq);
}
Output:
4. On the 75th Year of India's Independence the Railway department decides to offer special
discounts to Freedom Fighters on select trains between select cities. Create a class named
RailwayReservation which captures the details such as passenger name, age, seatno, train no,
freedom fighter (Y / N), source, destination and ticket fare. The source and destination details and
the corresponding fare are populated as below:
Source Station Destination Ticket Fare
Chennai Mumbai 500
Chennai Delhi 800
Chennai Kolkata 700
Delhi Mumbai 800
Delhi Kolkata 700
Mumbai Kolkata 800
These details should be initialized as the passenger object is created. While creating the object the
passenger will enter the source and destination and these details should be populated automatically
and the ticket fare should be returned. If the passenger is a freedom fighter he will pass an
additional parameter namely freedomFighter = Y while creating the object. The ticket fare should be
waived by 50% for the freedom fighter based on his choice of source and destination. Construct a
class with suitable constructors, methods, fields etc.
Code :
import java.io.*;
import java.util.*;
class RailwayReservation{
String name;
int age;
int seatNo;
String TrainNo;
String freedomFighter;
double fare;
String source;
String destination;
String temp=c1+c2;
temp=temp.toLowerCase();
switch(temp){
case "chennaimumbai":
return 500;
case "chennaidelhi":
return 800;
case "chennaikolkata":
return 700;
case "delhimumbai":
return 800;
case "delhikolkata":
return 700;
case "mumbaikolkata":
return 800;
default:
return 0;
void offer()
double newFare=fare-(fare/2);
fare= newFare;
name=n;
age=a;
seatNo=sn;
TrainNo=tn;
source=c1;
destination=c2;
fare=Ticket(c1,c2);
name=n;
age=a;
seatNo=sn;
TrainNo=tn;
source=c1;
destination=c2;
fare=Ticket(c1,c2);
offer();
void display()
System.out.println("Name : "+name);
System.out.println("Age : "+age);
System.out.println("Source : "+source);
System.out.println("Destination : "+destination);
System.out.println("Fare : "+fare);
System.out.print("Source :");
String city1=scan.next();
System.out.print("Destination :");
String city2=scan.next();
System.out.print("Source :");
String city3=scan.next();
System.out.print("Destination :");
String city4=scan.next();
p1.display();
p2.display();
Output: