0% found this document useful (0 votes)
22 views

Arvind Java Assignment

java assignment solution

Uploaded by

iamfighter267
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Arvind Java Assignment

java assignment solution

Uploaded by

iamfighter267
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

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 java.util.Scanner;

public class CalculateWages {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
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++) {


System.out.println("Enter details for laborer " + (i + 1) + ":");
System.out.print("Name: ");
names[i] = scanner.nextLine();
System.out.print("Age: ");
ages[i] = scanner.nextInt();
System.out.print("Hours Worked: ");
hoursWorked[i] = scanner.nextInt();
scanner.nextLine();

totalWages[i] = calcWages(hoursWorked[i]);
System.out.println();
}

System.out.println("Details of all laborers:");


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

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 java.util.Scanner;

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(System.in);
System.out.println("Enter the number which you want to reverse and check for prime:");
int input = sc.nextInt();

int reverse = 0;
while (input > 0) {
int lastDigit = input % 10;
reverse = (reverse * 10) + lastDigit;
input = input / 10;
}
boolean prime = isPrime(reverse);
if (prime) {
System.out.println(reverse + " is a prime no. after reversed");
} else {
System.out.println(reverse + " is a not prime no. after reversed");
}
sc.close();
}
}

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 java.util.Scanner;

public class DisplayPerson {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
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++) {


System.out.println("Enter the details of " + (i + 1) + "th person: ");
System.out.print("Enter name: ");
name[i] = sc.nextLine();
System.out.print("Enter age: ");
age[i] = sc.nextInt();
System.out.print("Enter gender (M/F): ");
gender[i] = sc.next().charAt(0);
sc.nextLine();
System.out.print("Enter qualification: ");
qualification[i] = sc.nextLine();
System.out.print("Enter salary: ");
salary[i] = sc.nextLong();
sc.nextLine();
}

System.out.println("The details of persons whose age is greater than 40 are: ");


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

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 java.util.Scanner;
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) {

this.name = name;

this.mobno = 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";

p.Class = "First";

p.seatNo = i + 1;

p.fName = "Flight1";

return 1;

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

flight2[i] = "Reserved";

p.Class = "First";

p.seatNo = i + 1;

p.fName = "Flight2";

return 1;

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

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

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

flight1[i] = "Reserved";

p.Class = "Economy";

p.seatNo = i + 1;

p.fName = "Flight1";

return 1;

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


flight2[i] = "Reserved";

p.Class = "Economy";

p.seatNo = i + 1;

p.fName = "Flight2";

return 1;

return -1;

public void displayBoardingPass() {

System.out.println("Passenger Details: ");

System.out.println("Passenger name: " + name);

System.out.println("Mob number: " + mobno);

System.out.println("Flight name: " + fName);

System.out.println("Seat No: " + seatNo);

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

public static void main(String args[]) {


String name;

String mobno;

char clas;

Scanner sc = new Scanner(System.in);

Passenger[] p = new Passenger[20];

int choice;

for (int i = -1;;) {

System.out.println("1. Display reserved Passenger Details ");

System.out.println("2. Reserve a seat ");

System.out.println("3. Stop ");

System.out.println("Enter your choice: ");

choice = sc.nextInt();

sc.nextLine();

if (choice == 3)

break;

switch (choice) {

case 1:
if (i < 0) {

System.out.println("Not any passenger details ");

} else {

p[i].displayBoardingPass();

break;

case 2:

if (i == 19) {

System.out.println("Next Flight leaves in 3 hours");

} else {

System.out.println("Enter Passenger name: ");

name = sc.nextLine();

System.out.println("Enter mobile no: ");

mobno = sc.nextLine();

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

System.out.println("Select your choice:- Press E for


Economy and F for First class: ");

clas = sc.nextLine().charAt(0);

int status = Passenger.doBooking(clas, p[i]);

if (status == -1) {
System.out.println("No seat Available");

} else {

System.out.println("Booking confirmed");

break;

default:

System.out.println("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 java.util.Scanner;
public class Password {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter your password: ");

String password = sc.nextLine();

if (isValidPassword(password)) {

System.out.println("Valid Password");

} else {

System.out.println("Invalid Password!");

sc.close();

public static boolean isValidPassword(String password) {

if (password.length() < 8) {

return false;

int digitCount = 0;

for (int i = 0; i < password.length(); i++) {


char ch = password.charAt(i);

if (!Character.isLetterOrDigit(ch)) {

return false;

if (Character.isDigit(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 java.util.Scanner;

abstract class Employee {

float salary;

String name;

String emp_id;

public Employee(String name, String emp_id) {

this.name = name;

this.emp_id = emp_id;

abstract void calc_salary();


public void display() {

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

System.out.println("Employee ID: " + emp_id);

System.out.println("Salary: $" + salary);

class Salary extends Employee {

float weeklySalary;

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

super(name, emp_id);

this.weeklySalary = 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);

this.wage = wage;

this.hoursWorked = 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);

this.grossSales = grossSales;

this.commissionRate = 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);

this.baseSalary = baseSalary;
this.grossSales = grossSales;

this.commissionRate = commissionRate;

@Override

void calc_salary() {

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

public class Company {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

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

salariedEmployee.display();

hourlyEmployee.display();

commissionEmployee.display();

salCommissionEmployee.display();

sc.close();

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

System.out.println(PI * (radius) * (radius));

class Cone implements Shape {

float radius;

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

radius = r;

height = h;

public void CalculateArea() {

System.out.println(((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;

System.out.print("Area of Cylinder:- ");

s.CalculateArea();

System.out.print("Area of Cone:- ");

s = c;

s.CalculateArea();

}
}

Output:-

You might also like