Name: Arpit Tripathi
Rollno: 2300290100075
ASSIGNMENGT -3
1. Num Class with Boolean Methods
public class Num {
private double value;
public Num(double value) {
[Link] = value;
}
public boolean isZero() {
boolean result = (value == 0);
[Link]("Zero = " + result);
return result;
}
public boolean isPositive() {
boolean result = (value > 0);
[Link]("Positive = " + result);
return result;
}
public boolean isNegative() {
boolean result = (value < 0);
[Link]("Negative = " + result);
return result;
}
public boolean isOdd() {
boolean result = (value % 2 != 0);
[Link]("Odd = " + result);
return result;
}
public boolean isEven() {
boolean result = (value % 2 == 0);
[Link]("Even = " + result);
return result;
}
public boolean isPrime() {
int num = (int) value;
if (num <= 1 || value != num) return false;
for (int i = 2; i <= [Link](num); i++) {
if (num % i == 0) return false;
}
[Link]("Prime = true");
return true;
}
public boolean isAmstrong() {
int num = (int) value;
if (value != num) return false;
int original = num, sum = 0, digits = [Link](num).length();
while (num != 0) {
int digit = num % 10;
sum += [Link](digit, digits);
num /= 10;
}
boolean result = (sum == original);
[Link]("Armstrong = " + result);
return result;
}
public static void main(String[] args) {
[Link]("Name: Arpit Tripathi");
[Link]("Rollno: 2300290100075\n");
Num n = new Num(153);
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
2. Vehicle Class Hierarchy
abstract class Vehicle {
String make, model, fuelType;
int year;
public Vehicle(String make, String model, int year, String fuelType) {
[Link] = make;
[Link] = model;
[Link] = year;
[Link] = 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) {
[Link]("Name: Arpit Tripathi");
[Link]("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");
[Link]("Car Distance: " + [Link](10));
[Link]("Truck Max Speed: " + [Link]());
[Link]("Bike Fuel Efficiency: " + [Link]());
}
}
3. Employee Class Hierarchy
abstract class Employee {
String name, address, jobTitle;
double salary;
public Employee(String name, String address, double salary, String jobTitle) {
[Link] = name;
[Link] = address;
[Link] = salary;
[Link] = jobTitle;
}
abstract double calculateBonus();
void generatePerformanceReport() {
[Link]("Performance report for " + name);
}
void manageProject(String project) {
[Link](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) {
[Link]("Name: Arpit Tripathi");
[Link]("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);
[Link]([Link] + " Bonus: " + [Link]());
[Link]();
[Link]("Inventory System");
}
}