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

Gym Registration and Employee Incentives

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
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)
97 views12 pages

Gym Registration and Employee Incentives

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

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 [Link].*;

import [Link].*;

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

[Link]("Name : "+AuthorName);

[Link]("Paper ID : "+PaperID);

[Link]("Date : "+RegistrationDate);

[Link]("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");

[Link]();

[Link]();

[Link]();

}
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 [Link].*;

import [Link].*;

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

{
[Link]("Name : "+Name);

[Link]("Paper ID : "+EmployeeId);

[Link]("Department : "+Department);

[Link]("Revised Salary : "+Salary);

[Link]("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");

[Link]();

[Link]();

[Link]();

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 [Link].

Code :

import [Link].*;

import [Link].*;

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([Link]);

[Link]("Length :");

length=[Link]();

[Link]("Width :");

width=[Link]();

[Link]("Height :");

height=[Link]();

wallArea=WallArea(length,width,height);

gallonReq=gallon(wallArea);

amount=finalPrice(gallonReq);

[Link]("Gallon Required : "+gallonReq);

[Link]("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 [Link].*;

import [Link].*;

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=[Link]();

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

[Link]("Name : "+name);

[Link]("Age : "+age);

[Link]("Seat No. : "+seatNo);

[Link]("Train No. : "+TrainNo);

[Link]("Source : "+source);

[Link]("Destination : "+destination);

[Link]("Fare : "+fare);

public static void main(String[] args){

Scanner scan=new Scanner([Link]);

[Link]("Source :");

String city1=[Link]();

[Link]("Destination :");

String city2=[Link]();

[Link]("Freedom Fighter Details:");

[Link]("Source :");

String city3=[Link]();

[Link]("Destination :");
String city4=[Link]();

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

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

[Link]();

[Link]();

Output:

Common questions

Powered by AI

Using Java classes provides a structured and modular approach to handle complex data scenarios by encapsulating data and behavior within objects. This not only helps to organize code logically but also allows for code reusability, evolution, and maintainability. Constructors, methods, and encapsulation in the given exercises enable clear initialization and modification procedures, such as applying discounts, calculating bonuses, or handling different pricing scenarios, which can be crucial for managing varying data manipulations and business rules effectively .

The `main()` method serves as the entry point in each Java program, initiating execution and handling user interactions by taking input, executing class functions, and displaying results. It shapes user interaction by guiding the sequence in which data is acquired and processed, offering an interface through console prompts and outputs, as seen in PaintCostFinder and RailwayReservation programs where user inputs define operational pathways and final outputs .

The primary challenge in using overloaded constructors and methods is ensuring that overloaded versions are intuitive and distinguishable, as improper design can lead to complexity and errors due to similar parameter lists. A solution to this challenge is to employ clear parameter naming, varied parameter types or counts, and comprehensive documentation. Proper testing and adhering to a consistent naming convention can also assist in maintaining clarity and integrity when customizing behavior through overloading .

The RailwayReservation class uses a switch statement within the `Ticket` method to determine the fare based on the concatenation of the lowercase names of the source and destination cities. Each case in the switch corresponds to a pair of cities with an associated ticket fare, like "chennaimumbai" for a fare of 500. These fares are automatically set when the passenger object is created .

Boolean parameters in constructor overloading provide a mechanism to trigger specific behaviors based on simple true/false states. In the Java examples provided, they allow for different paths of initialization or adjustments in methods, such as applying a membership discount in the ConferenceRegistration class or an additional bonus in the Employee class. The presence of these parameters adds flexibility, enhancing the capability of constructors to cater to different scenarios without the need for multiple separate methods, simplifying the class design and usage .

The Employee class uses the `incentive()` method to add a 10% bonus to all employees' salaries. For exceptional employees, method overloading is used through `incentive(boolean e)`, which adds an additional fixed bonus of Rs. 10,000 on top of the 10% salary bonus. This differentiation is done by calling the overloaded `incentive` method with a boolean parameter indicating exceptional performance .

Computing and returning fractional paint gallons in PaintCostFinder is beneficial because it allows buyers to purchase the exact necessary amount of paint, minimizing waste and reducing costs. Since the price structure supports fractional purchases at the same rate as whole gallons, this approach provides both economic and resource efficiency, ensuring resources are used precisely according to need .

The `offer()` method in the RailwayReservation class adjusts the ticket fare by reducing it by 50% if the passenger is a freedom fighter. This reduction is necessary to comply with the special discount policy intended for such individuals, promoting inclusivity and recognition of historical contributions by offering them fare concessions on the Indian Railways .

The PaintCostFinder application calculates the total paint cost by first determining the wall area using the `WallArea` method, which computes the area based on the dimensions of length, width, and height of the room. This area is then passed to the `gallon` method to calculate the number of gallons of paint needed (area divided by 300 square feet per gallon). Finally, the total paint cost is calculated in the `finalPrice` method by multiplying the number of gallons by the price per gallon ($30), and this cost is printed by the main method .

In the ConferenceRegistration class, constructor overloading is used to apply discounts for members of the XYZ Computer Society. When a participant is a member, a parameterized constructor with an additional boolean parameter is used, which sets the RegistrationAmount to 3500 instead of the default 7000. This directly includes the 50% discount during the object creation for members .

You might also like