Name: Arpit Tripathi
Rollno: 2300290100075
ASSIGNMENGT -3
1. Num Class with Boolean Methods
public class Num {
private double value;
public Num(double value) {
this.value = value;
}
public boolean isZero() {
boolean result = (value == 0);
System.out.println("Zero = " + result);
return result;
}
public boolean isPositive() {
boolean result = (value > 0);
System.out.println("Positive = " + result);
return result;
}
public boolean isNegative() {
boolean result = (value < 0);
System.out.println("Negative = " + result);
return result;
}
public boolean isOdd() {
boolean result = (value % 2 != 0);
System.out.println("Odd = " + result);
return result;
}
public boolean isEven() {
boolean result = (value % 2 == 0);
System.out.println("Even = " + result);
return result;
}
public boolean isPrime() {
int num = (int) value;
if (num <= 1 || value != num) return false;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) return false;
}
System.out.println("Prime = true");
return true;
}
public boolean isAmstrong() {
int num = (int) value;
if (value != num) return false;
int original = num, sum = 0, digits = String.valueOf(num).length();
while (num != 0) {
int digit = num % 10;
sum += Math.pow(digit, digits);
num /= 10;
}
boolean result = (sum == original);
System.out.println("Armstrong = " + result);
return result;
}
public static void main(String[] args) {
System.out.println("Name: Arpit Tripathi");
System.out.println("Rollno: 2300290100075\n");
Num n = new Num(153);
n.isZero();
n.isPositive();
n.isNegative();
n.isOdd();
n.isEven();
n.isPrime();
n.isAmstrong();
}
}
2. Vehicle Class Hierarchy
abstract class Vehicle {
String make, model, fuelType;
int year;
public Vehicle(String make, String model, int year, String fuelType) {
this.make = make;
this.model = model;
this.year = year;
this.fuelType = fuelType;
}
abstract double calculateFuelEfficiency();
abstract double distanceTraveled(double fuel);
abstract double maxSpeed();
}
class Car extends Vehicle {
public Car(String make, String model, int year, String fuelType) {
super(make, model, year, fuelType);
}
double calculateFuelEfficiency() {
return 15.0;
}
double distanceTraveled(double fuel) {
return calculateFuelEfficiency() * fuel;
}
double maxSpeed() {
return 180;
}
}
class Truck extends Vehicle {
public Truck(String make, String model, int year, String fuelType) {
super(make, model, year, fuelType);
}
double calculateFuelEfficiency() {
return 6.0;
}
double distanceTraveled(double fuel) {
return calculateFuelEfficiency() * fuel;
}
double maxSpeed() {
return 120;
}
}
class Motorcycle extends Vehicle {
public Motorcycle(String make, String model, int year, String fuelType) {
super(make, model, year, fuelType);
}
double calculateFuelEfficiency() {
return 40.0;
}
double distanceTraveled(double fuel) {
return calculateFuelEfficiency() * fuel;
}
double maxSpeed() {
return 160;
}
}
public class VehicleDemo {
public static void main(String[] args) {
System.out.println("Name: Arpit Tripathi");
System.out.println("Rollno: 2300290100075\n");
Vehicle car = new Car("Toyota", "Corolla", 2020, "Petrol");
Vehicle truck = new Truck("Volvo", "FH16", 2018, "Diesel");
Vehicle bike = new Motorcycle("Yamaha", "R15", 2021, "Petrol");
System.out.println("Car Distance: " + car.distanceTraveled(10));
System.out.println("Truck Max Speed: " + truck.maxSpeed());
System.out.println("Bike Fuel Efficiency: " + bike.calculateFuelEfficiency());
}
}
3. Employee Class Hierarchy
abstract class Employee {
String name, address, jobTitle;
double salary;
public Employee(String name, String address, double salary, String jobTitle) {
this.name = name;
this.address = address;
this.salary = salary;
this.jobTitle = jobTitle;
}
abstract double calculateBonus();
void generatePerformanceReport() {
System.out.println("Performance report for " + name);
}
void manageProject(String project) {
System.out.println(name + " is managing project: " + project);
}
}
class Manager extends Employee {
public Manager(String name, String address, double salary) {
super(name, address, salary, "Manager");
}
double calculateBonus() {
return salary * 0.20;
}
}
class Developer extends Employee {
public Developer(String name, String address, double salary) {
super(name, address, salary, "Developer");
}
double calculateBonus() {
return salary * 0.15;
}
}
class Programmer extends Employee {
public Programmer(String name, String address, double salary) {
super(name, address, salary, "Programmer");
}
double calculateBonus() {
return salary * 0.10;
}
}
public class EmployeeDemo {
public static void main(String[] args) {
System.out.println("Name: Arpit Tripathi");
System.out.println("Rollno: 2300290100075\n");
Employee e1 = new Manager("Alice", "New York", 90000);
Employee e2 = new Developer("Bob", "LA", 80000);
Employee e3 = new Programmer("Charlie", "Chicago", 70000);
System.out.println(e1.jobTitle + " Bonus: " + e1.calculateBonus());
e2.generatePerformanceReport();
e3.manageProject("Inventory System");
}
}