0% found this document useful (0 votes)
8 views24 pages

Java Programs for Wage Calculation and Reservations

java assignment solution

Uploaded by

iamfighter267
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)
8 views24 pages

Java Programs for Wage Calculation and Reservations

java assignment solution

Uploaded by

iamfighter267
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

Name:-Arvind kumar

Reg. No:- 24MCA0188

Q1. Write an interactive java program to compute the total wages based on the number of
hours worked. The wages are calculated at a rate of 8.25 per hour for hours less than 40 and
at the rate of 1.5 for any hours greater than 40. Capture the personal information of 3
labourers and display their wages along with the details captured. For example, if the
person worked for 45 hours the wages should be (40*8.25)+(5*1.5).

Solution:-
import [Link];

public class CalculateWages {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
final int NUM_LABORERS = 3;
String[] names = new String[NUM_LABORERS];
int[] ages = new int[NUM_LABORERS];
int[] hoursWorked = new int[NUM_LABORERS];
double[] totalWages = new double[NUM_LABORERS];

for (int i = 0; i < NUM_LABORERS; i++) {


[Link]("Enter details for laborer " + (i + 1) + ":");
[Link]("Name: ");
names[i] = [Link]();
[Link]("Age: ");
ages[i] = [Link]();
[Link]("Hours Worked: ");
hoursWorked[i] = [Link]();
[Link]();

totalWages[i] = calcWages(hoursWorked[i]);
[Link]();
}

[Link]("Details of all laborers:");


for (int i = 0; i < NUM_LABORERS; i++) {
[Link]("Name: " + names[i]);
[Link]("Age: " + ages[i]);
[Link]("Hours Worked: " + hoursWorked[i]);
[Link]("Total Wages: $" + totalWages[i]);
[Link]();
}
[Link]();
}

private static double calcWages(int hours) {

double rate = 8.25;


double overtimeRate = 1.5;
int regularHours = 40;

if (hours <= regularHours) {


return hours * rate;
} else {
int overtimeHours = hours - regularHours;
return (regularHours * rate) + (overtimeHours * overtimeRate);
}
}
}

Output:-

Q2. Write a Java program to compute the reverse of a number and check whether the
reversed number is prime or not. Capture the user input through Scanner class.
Solution:-
import [Link];

public class ReversePrime {


private static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i < num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);
[Link]("Enter the number which you want to reverse and check for prime:");
int input = [Link]();

int reverse = 0;
while (input > 0) {
int lastDigit = input % 10;
reverse = (reverse * 10) + lastDigit;
input = input / 10;
}
boolean prime = isPrime(reverse);
if (prime) {
[Link](reverse + " is a prime no. after reversed");
} else {
[Link](reverse + " is a not prime no. after reversed");
}
[Link]();
}
}

Output:-

Q3. Write a program to capture the name, age, gender, qualification, salary of five different
people and display number of persons whose age is greater than 40
Solution:-
import [Link];

public class DisplayPerson {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String[] name = new String[5];
int[] age = new int[5];
char[] gender = new char[5];
String[] qualification = new String[5];
long[] salary = new long[5];

for (int i = 0; i < 5; i++) {


[Link]("Enter the details of " + (i + 1) + "th person: ");
[Link]("Enter name: ");
name[i] = [Link]();
[Link]("Enter age: ");
age[i] = [Link]();
[Link]("Enter gender (M/F): ");
gender[i] = [Link]().charAt(0);
[Link]();
[Link]("Enter qualification: ");
qualification[i] = [Link]();
[Link]("Enter salary: ");
salary[i] = [Link]();
[Link]();
}

[Link]("The details of persons whose age is greater than 40 are: ");


for (int i = 0; i < 5; i++) {
if (age[i] > 40) {
[Link]("Name: " + name[i]);
[Link]("Age: " + age[i]);
[Link]("Gender: " + gender[i]);
[Link]("Qualification: " + qualification[i]);
[Link]("Salary: " + salary[i]);
[Link]();
}
}
[Link]();
}
}

Output:--
Q4. A small airline has just purchased the computer for its new automated reservations
system. You have been asked to program the new system in Java to assign seats on each
flight of the airline’s two planes, each of capacity: 10.

Define a user defined class to represent the reservation details like passenger name, mobile
number, flight number and reserved seat number.

Keep the flight details in two static String arrays for each flight. The first five seats (index 0
to 4) represent the First Class whereas the next five seats (index 5 to 9) represent
the Economy Class. Initially, both the arrays should be assigned with the
value Available through static block so, no booking has done. It should be updated
as Reserved for each corresponding booking.

Define a static method to display the flight details. Sample is here:

Flight-1 Flight-2
1-Reserved 1-Available
2-Reserved 2-Reserved
3-Available 3-Available
4-Available 4-Reserved
5-Reserved 5-Available
6-Reserved 6-Available
7-Available 7-Available
8-Reserved 8-Reserved
9-Available 9-Available
10-Available 10-Available

Define a constructor with the parameters passenger name, mobile number

Create a static method booking for every reservation. It should get the flight number and
travel class (First or Economy) as parameters. If the seat is available in the corresponding
flight it should return the seat number, otherwise -1. Also, the status of the corresponding
flight seat should be updated as “Reserved” when it is available.

Create a non-static method to display the reservation details.

Create a demo class which contains main method. Declare array of objects with the size 20 to
store the reservation details. Create a menu driven loop to do the following with the choices
from 1 to 4.

1. Display Reserved Passenger Details

3. Reserve a seat

4. Stop

The flight details should be displayed when the user press 1. The reservation details
should be displayed when the user press 2. If the user press 3, the system should get the
flight number and travel class as input. Then it should check the availability of the seat. If
it is available, then the system collects the user name and mobile number. Now, it should
create an object belonging to reservation class with complete details. Suppose the seat is
not available, print the message “Next Flight leaves in 3 hours”.

Stop this iteration when user press 4. Display ‘choice is wrong, try again’ when user
didn’t press the correct choice.

Solution:-

import [Link];
public class Airlines {

static class Passenger {

String name;

String mobno;

String Class;

int seatNo;

String fName;

static String[] flight1 = { "Unreserved", "Unreserved", "Unreserved",


"Unreserved", "Unreserved", "Unreserved",

"Unreserved", "Unreserved", "Unreserved", "Unreserved" };

static String[] flight2 = { "Unreserved", "Unreserved", "Unreserved",


"Unreserved", "Unreserved", "Unreserved",

"Unreserved", "Unreserved", "Unreserved", "Unreserved" };

Passenger(String name, String mobno) {

[Link] = name;

[Link] = mobno;

public static int doBooking(char cl, Passenger p) {

if (cl == 'F' || cl == 'f') {

for (int i = 0; i < 5; i++) {

if (flight1[i] == "Unreserved") {
flight1[i] = "Reserved";

[Link] = "First";

[Link] = i + 1;

[Link] = "Flight1";

return 1;

} else if (flight2[i] == "Unreserved") {

flight2[i] = "Reserved";

[Link] = "First";

[Link] = i + 1;

[Link] = "Flight2";

return 1;

} else if (cl == 'E' || cl == 'e') {

for (int i = 5; i < 10; i++) {

if (flight1[i] == "Unreserved") {

flight1[i] = "Reserved";

[Link] = "Economy";

[Link] = i + 1;

[Link] = "Flight1";

return 1;

} else if (flight2[i] == "Unreserved") {


flight2[i] = "Reserved";

[Link] = "Economy";

[Link] = i + 1;

[Link] = "Flight2";

return 1;

return -1;

public void displayBoardingPass() {

[Link]("Passenger Details: ");

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

[Link]("Mob number: " + mobno);

[Link]("Flight name: " + fName);

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

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

public static void main(String args[]) {


String name;

String mobno;

char clas;

Scanner sc = new Scanner([Link]);

Passenger[] p = new Passenger[20];

int choice;

for (int i = -1;;) {

[Link]("1. Display reserved Passenger Details ");

[Link]("2. Reserve a seat ");

[Link]("3. Stop ");

[Link]("Enter your choice: ");

choice = [Link]();

[Link]();

if (choice == 3)

break;

switch (choice) {

case 1:
if (i < 0) {

[Link]("Not any passenger details ");

} else {

p[i].displayBoardingPass();

break;

case 2:

if (i == 19) {

[Link]("Next Flight leaves in 3 hours");

} else {

[Link]("Enter Passenger name: ");

name = [Link]();

[Link]("Enter mobile no: ");

mobno = [Link]();

p[++i] = new Passenger(name, mobno);

[Link]("Select your choice:- Press E for


Economy and F for First class: ");

clas = [Link]().charAt(0);

int status = [Link](clas, p[i]);

if (status == -1) {
[Link]("No seat Available");

} else {

[Link]("Booking confirmed");

break;

default:

[Link]("choice is wrong, try again");

Output:-
Q5. Understanding Strings

Some Websites impose certain rules for passwords. Write a method that
checks whether a string is a valid password. Suppose the password rule is
as follows:
A password must have at least eight characters.
A password consists of only letters and digits.
A password must contain at least two digits.
Write a program that prompts the user to enter a password and displays
"Valid Password" if the rule is followed or "Invalid Password" otherwise.

Solution:-

import [Link];
public class Password {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter your password: ");

String password = [Link]();

if (isValidPassword(password)) {

[Link]("Valid Password");

} else {

[Link]("Invalid Password!");

[Link]();

public static boolean isValidPassword(String password) {

if ([Link]() < 8) {

return false;

int digitCount = 0;

for (int i = 0; i < [Link](); i++) {


char ch = [Link](i);

if (![Link](ch)) {

return false;

if ([Link](ch)) {

digitCount++;

if (digitCount < 2) {

return false;

return true;

}Output:-
Q6. Understanding Inheritance

A company pays its employees on a weekly basis. The company has four
types of employees: salaried employees, who are paid a fixed weekly
salary regardless of the number of hours worked; hourly employees, who
are paid by the hour and receive overtime pay; commission employees,
who are paid a percentage of their sales; and salaried commission
employees, who receive a base salary plus a percentage of their sales. For
a current pay period, the company has decided to reward salaried
commission employees by adding 10% to their salaries. The company
wants to implement a java application that performs its payroll
calculations polymorphically.

Solution:-

import [Link];

abstract class Employee {

float salary;

String name;

String emp_id;

public Employee(String name, String emp_id) {

[Link] = name;

this.emp_id = emp_id;

abstract void calc_salary();


public void display() {

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

[Link]("Employee ID: " + emp_id);

[Link]("Salary: $" + salary);

class Salary extends Employee {

float weeklySalary;

public Salary(String name, String emp_id, float weeklySalary) {

super(name, emp_id);

[Link] = weeklySalary;

@Override

void calc_salary() {

salary = weeklySalary;

class Hourly extends Employee {


float wage;

float hoursWorked;

public Hourly(String name, String emp_id, float wage, float hoursWorked) {

super(name, emp_id);

[Link] = wage;

[Link] = hoursWorked;

@Override

void calc_salary() {

if (hoursWorked <= 40) {

salary = wage * hoursWorked;

} else {

salary = (40 * wage) + ((hoursWorked - 40) * wage * 1.5f);

class Commission extends Employee {

float grossSales;

float commissionRate;
public Commission(String name, String emp_id, float grossSales, float commissionRate) {

super(name, emp_id);

[Link] = grossSales;

[Link] = commissionRate;

@Override

void calc_salary() {

salary = grossSales * commissionRate;

class Salcommission extends Employee {

float baseSalary;

float grossSales;

float commissionRate;

public Salcommission(String name, String emp_id, float baseSalary, float grossSales, float
commissionRate) {

super(name, emp_id);

[Link] = baseSalary;
[Link] = grossSales;

[Link] = commissionRate;

@Override

void calc_salary() {

salary = baseSalary + (grossSales * commissionRate * 1.1f);

public class Company {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

Salary salariedEmployee = new Salary("Rohit kumar", "E100", 800);

Hourly hourlyEmployee = new Hourly("Arvind kumar", "E101", 20, 45);

Commission commissionEmployee = new Commission("Ritik kumar", "E102", 10000, 0.06f);

Salcommission salCommissionEmployee = new Salcommission("Vivek kumar", "E103", 500,


8000, 0.04f);

salariedEmployee.calc_salary();

hourlyEmployee.calc_salary();

commissionEmployee.calc_salary();
salCommissionEmployee.calc_salary();

[Link]();

[Link]();

[Link]();

[Link]();

[Link]();

Output:-

Q7. Understanding Interfaces


Develop a java program consisting of Shape hierarchy and a specialized
Cylinder and Cone classes with appropriate functionality for computing
the area of respective shape. Use Interfaces and implement runtime
polymorphic behaviour in it.
Solution:-

interface Shape {

float PI = 3.14f;

public void CalculateArea();

class Cylinder implements Shape {

float radius;

Cylinder(float r) {

radius = r;

public void CalculateArea() {

[Link](PI * (radius) * (radius));

class Cone implements Shape {

float radius;

int height;
Cone(float r, int h) {

radius = r;

height = h;

public void CalculateArea() {

[Link](((float) 1 / (float) 3) * PI * (radius) * (radius) * (height));

public class Interface {

public static void main(String args[]) {

Cylinder cy = new Cylinder(5.2f);

Cone c = new Cone(5.2f, 6);

Shape s;

s = cy;

[Link]("Area of Cylinder:- ");

[Link]();

[Link]("Area of Cone:- ");

s = c;

[Link]();

}
}

Output:-

Common questions

Powered by AI

The payroll application utilizes polymorphism by creating an abstract `Employee` class and having specific employee types such as `Salary`, `Hourly`, `Commission`, and `Salcommission` extend it. Each type implements its `calc_salary` method depending on the pay structure, showcasing polymorphism. Salaried Commission employees receive a bonus of 10% to their commission in their salary calculation, allowing differentiated reward systems without altering other employee types .

The program uses a `Shape` interface to define a contract for calculating areas, which `Cylinder` and `Cone` classes implement. Utilizing interfaces facilitates polymorphism, enabling runtime determination of object-specific behaviors while maintaining a unified method structure across shapes. This ensures reliable modifications and feature extensions in geometric calculations .

To calculate the total wages, the program captures personal details for three laborers, including their names, ages, and hours worked using a for-loop with Scanner input . Each laborer's wages are computed based on the logic defined in the `calcWages` method. This method computes wages at 8.25 per hour for hours <= 40. For hours > 40, wages for the first 40 hours are calculated at 8.25 per hour; any overtime hours are calculated at a rate of 1.5 times per hour beyond 40 . Finally, the details and calculated wages are printed for each laborer .

The airline reservation system uses a static class and arrays to manage seat reservations. Flights have two arrays each for First and Economy class. Initially, all seats are 'Unreserved'. The `doBooking` static method attempts to reserve a seat based on the flight class. It searches arrays for the first 'Unreserved' seat, updates its status to 'Reserved', and assigns relevant booking details to a Passenger object. If no seats are available, it returns -1 indicating a wait of three hours for the next available flight .

Challenges include proper abstraction of diverse employee pay types, correct overloading of salary calculators, and ensuring extendability. The program uses an abstract `Employee` class with specific subclasses (`Salary`, `Hourly`, `Commission`, `Salcommission`). Each subclass defines a `calc_salary` method suited for its type, introducing polymorphism and allowing different implementations to manage variable pay structures uniformly .

The program checks password validity by confirming it has at least eight characters, only comprises letters and digits, and contains at least two digits. The `isValidPassword` method iterates over each character to count digits and checks for illegal characters. Failing any condition yields an 'Invalid Password' response; otherwise, it validates the password .

The application uses a `Passenger` class with attributes for reservation details and static arrays for seat statuses. A menu-driven loop processes reservations, where valid seats trigger a new `Passenger` instance creation, if available. It confirms bookings and stores details in an array; if not, it opts out for later sessions. This systematic use of object management and status updates ensures accurate handling of reservations .

The program receives input through the Scanner class and reverses the number by extracting digits using modulo and division in a while-loop. The reversed number is then checked for primality using the `isPrime` method, which returns false for numbers less than or equal to 1 and checks divisibility for numbers greater than 1 . If the reversed number is prime, it prints the number and confirms its primality; otherwise, it indicates it is not prime .

The password constraints require at least eight characters, consist solely of letters and digits, and contain at least two digits. The program counts the number of digits and checks each character for invalid symbols. If any condition is not met, the password is declared invalid. Otherwise, the password is confirmed as valid .

You might also like