0% found this document useful (0 votes)
194 views12 pages

LAB EXERCISE 3: Classes & Objects, Overloading

The document describes 4 Java programming exercises involving classes and objects - creating a conference registration class with constructor overloading, an employee class with method overloading to calculate bonuses, a paint cost calculator class, and a train reservation class that offers discounts to freedom fighters. Code solutions are provided for each exercise.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
194 views12 pages

LAB EXERCISE 3: Classes & Objects, Overloading

The document describes 4 Java programming exercises involving classes and objects - creating a conference registration class with constructor overloading, an employee class with method overloading to calculate bonuses, a paint cost calculator class, and a train reservation class that offers discounts to freedom fighters. Code solutions are provided for each exercise.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 12

LAB EXERCISE 3: Classes & Objects, Overloading

Faculty Name: Dr. Balasundaram A


Slot: L9+L10
Class Number: CH2022231000321
Date: 16/08/2022 Due Date: 17/08/2022
Name : Viresh Bhurke
Reg no.:19BPS1133

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;

JavaLab3_19BPS1127(int id,String name,String date)

PaperID=id;

AuthorName=name;

RegistrationDate=date;

RegistrationAmount=7000;
}

JavaLab3_19BPS1127(int id,String name,String date,boolean t)

PaperID=id;

AuthorName=name;

RegistrationDate=date;

RegistrationAmount=3500;

public void display()

System.out.println("Name : "+AuthorName);

System.out.println("Paper ID : "+PaperID);

System.out.println("Date : "+RegistrationDate);

System.out.println("Registration Amount : "+RegistrationAmount);

public static void main(String[] args){

JavaLab3_19BPS1127 r1=new JavaLab3_19BPS1133(123,"Viresh","1/1/2001");

JavaLab3_19BPS1127 r2=new JavaLab3_19BPS1133(456,"Akash","6/4/2003",true);

JavaLab3_19BPS1127 r3=new JavaLab3_19BPS1133(456,"Sahil","9/2/2005");

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,

//Name, Department, Salary and Designation

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;

JavaLab32_19BPS112(7int id,String name,String dep,double sal,String des)

EmployeeId=id;

Name=name;

Department=dep;

Salary=sal;

Designation=des;

incentive();

JavaLab32_19BPS1127(int id,String name,String dep,double sal,String des,boolean k)

EmployeeId=id;

Name=name;

Department=dep;

Salary=sal;

Designation=des;

incentive(k);

public void display()

{
System.out.println("Name : "+Name);

System.out.println("Paper ID : "+EmployeeId);

System.out.println("Department : "+Department);

System.out.println("Revised Salary : "+Salary);

System.out.println("Designation : "+Designation);

public static void main(String[] args){

JavaLab32_19BPS1127 r1=new JavaLab32_19BPS1127(123,"Viresh","Developer",10000,"lvl 1");

JavaLab32_19BPS1127 r2=new JavaLab32_19BPS1127(4321,"Akash","Manager",30000,"lvl 5",true);

JavaLab32_19BPS1127 r3=new JavaLab32_19BPS1127(8764,"Sahil","Design",20000,"lvl 3");

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{

static double WallArea(double length,double width,double height)

double area=(2*length*height)+(2*width*height);

return area;

static double gallon(double a)

double paint=a/300;

return paint;

static double finalPrice(double a)

double price=a*30;

return price;
}

public static void main(String[] args){

double length;

double width;

double height;

double wallArea;

double gallonReq;

double amount;

Scanner scan=new Scanner(System.in);

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);

System.out.println("Gallon Required : "+gallonReq);

System.out.println("Total amount : "+amount);

}
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;

double Ticket(String c1,String c2)

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;

RailwayReservation(String n,int a,int sn,String tn,String c1,String c2)

name=n;

age=a;

seatNo=sn;

TrainNo=tn;

source=c1;

destination=c2;

fare=Ticket(c1,c2);

RailwayReservation(String n,int a,int sn,String tn,String c1,String c2,String fredomFighter)

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("Seat No. : "+seatNo);

System.out.println("Train No. : "+TrainNo);

System.out.println("Source : "+source);

System.out.println("Destination : "+destination);

System.out.println("Fare : "+fare);

public static void main(String[] args){

Scanner scan=new Scanner(System.in);

System.out.print("Source :");

String city1=scan.next();

System.out.print("Destination :");

String city2=scan.next();

System.out.println("Freedom Fighter Details:");

System.out.print("Source :");

String city3=scan.next();

System.out.print("Destination :");
String city4=scan.next();

RailwayReservation p1=new RailwayReservation("Viresh",25,4,"TN21",city1,city2);

RailwayReservation p2=new RailwayReservation("Atharva",34,2,"TN45",city3,city4,"Y");

p1.display();

p2.display();

Output:

You might also like