Arvind Java Assignment
Arvind Java Assignment
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;
totalWages[i] = calcWages(hoursWorked[i]);
System.out.println();
}
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;
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;
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.
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
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 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.
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 {
String name;
String mobno;
String Class;
int seatNo;
String fName;
this.name = name;
this.mobno = mobno;
if (flight1[i] == "Unreserved") {
flight1[i] = "Reserved";
p.Class = "First";
p.seatNo = i + 1;
p.fName = "Flight1";
return 1;
flight2[i] = "Reserved";
p.Class = "First";
p.seatNo = i + 1;
p.fName = "Flight2";
return 1;
if (flight1[i] == "Unreserved") {
flight1[i] = "Reserved";
p.Class = "Economy";
p.seatNo = i + 1;
p.fName = "Flight1";
return 1;
p.Class = "Economy";
p.seatNo = i + 1;
p.fName = "Flight2";
return 1;
return -1;
String mobno;
char clas;
int choice;
choice = sc.nextInt();
sc.nextLine();
if (choice == 3)
break;
switch (choice) {
case 1:
if (i < 0) {
} else {
p[i].displayBoardingPass();
break;
case 2:
if (i == 19) {
} else {
name = sc.nextLine();
mobno = sc.nextLine();
clas = sc.nextLine().charAt(0);
if (status == -1) {
System.out.println("No seat Available");
} else {
System.out.println("Booking confirmed");
break;
default:
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 {
if (isValidPassword(password)) {
System.out.println("Valid Password");
} else {
System.out.println("Invalid Password!");
sc.close();
if (password.length() < 8) {
return false;
int digitCount = 0;
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;
float salary;
String name;
String emp_id;
this.name = name;
this.emp_id = emp_id;
float weeklySalary;
super(name, emp_id);
this.weeklySalary = weeklySalary;
@Override
void calc_salary() {
salary = weeklySalary;
float hoursWorked;
super(name, emp_id);
this.wage = wage;
this.hoursWorked = hoursWorked;
@Override
void calc_salary() {
} else {
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() {
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() {
salariedEmployee.calc_salary();
hourlyEmployee.calc_salary();
commissionEmployee.calc_salary();
salCommissionEmployee.calc_salary();
salariedEmployee.display();
hourlyEmployee.display();
commissionEmployee.display();
salCommissionEmployee.display();
sc.close();
Output:-
interface Shape {
float PI = 3.14f;
float radius;
Cylinder(float r) {
radius = r;
float radius;
int height;
Cone(float r, int h) {
radius = r;
height = h;
Shape s;
s = cy;
s.CalculateArea();
s = c;
s.CalculateArea();
}
}
Output:-