Question 1:
Write a java program to find the addition, subtraction, multiplication and division of two complex
numbers using inheritance.
Create abstract class Complex.
From this extends a child class Addition, Subtraction, Multiplication and Division.
All of these child classes should contain same methods.
Input Format
Input consists of four double type variables.
Which denotes the real and imaginary parts of the first complex number,
Followed by the real and imaginary parts of the second complex number.
Output Format
Output consists of the Addition, Subtraction, Multiplication and Division of the two input complex
numbers.
Title for Question 1: Abstract-Multiplication
Solution:
import [Link].*;
import [Link].*;
import [Link].*;
abstract class Complex{
abstract float Real(float real1,float real2);
abstract float Imaginary(float imag1,float imag2);
abstract float Real1(float real1,float real2,float imag1,float imag2);
abstract float Imaginary1(float real1,float real2,float imag1,float imag2);
}
class Addition extends Complex{
float Real(float real1,float real2){
return real1+real2;
}
float Imaginary(float imag1,float imag2){
return imag1+imag2;
}
float Imaginary1(float real1,float real2,float imag1,float imag2){return 0;}
float Real1(float real1,float real2,float imag1,float imag2){return 0;}
}
class Subtraction extends Complex{
float Real(float real1,float real2){
return real1-real2;
}
float Imaginary(float imag1,float imag2){
return imag1-imag2;
}
float Imaginary1(float real1,float real2,float imag1,float imag2){return 0;}
float Real1(float real1,float real2,float imag1,float imag2){return 0;}
}
class Multiplication extends Complex{
float Real1(float real1,float real2,float imag1,float imag2){
return ((real1*real2)-(imag1*imag2));
}
float Imaginary1(float real1,float real2,float imag1,float imag2){
return ((real1*imag2)+(imag1*real2));
}
float Imaginary(float imag1,float imag2){return 0;}
float Real(float real1,float real2){return 0;}
}
class Division extends Complex{
float Real1(float real1,float real2,float imag1,float imag2){
return (((real1*real2)+(imag1*imag2))/((real2*real2)+(imag2*imag2)));
}
float Imaginary1(float real1,float real2,float imag1,float imag2){
return (((imag1*real2)-(real1*imag2))/((real2*real2)+(imag2*imag2)));
}
float Imaginary(float imag1,float imag2){return 0;}
float Real(float real1,float real2){return 0;}
}
public class Main{
public static void main(String args[]){
Scanner sc =new Scanner([Link]);
float real1=[Link]();
float imag1=[Link]();
float real2=[Link]();
float imag2=[Link]();
Complex b;
b=new Addition();
[Link]("Addition:\n\t%.4f",[Link](real1,real2));
if([Link](imag1,imag2)>=0){[Link](" +");}
else{[Link](" ");}
[Link]("%.4f i",[Link](imag1,imag2));
b=new Subtraction();
[Link]("\nSubtraction:\n\t%.4f",[Link](real1,real2));
if([Link](imag1,imag2)>=0){[Link](" +");}
else{[Link](" ");}
[Link]("%.4f i",[Link](imag1,imag2));
b=new Multiplication();
[Link]("\nMultiplication:\n\t%.4f",b.Real1(real1,real2,imag1,imag
if(b.Imaginary1(real1,real2,imag1,imag2)>=0){[Link](" +");}
else{[Link](" ");}
[Link]("%.4f i",b.Imaginary1(real1,real2,imag1,imag2));
b=new Division();
[Link]("\nDivision:\n\t%.4f",b.Real1(real1,real2,imag1,imag2));
if(b.Imaginary1(real1,real2,imag1,imag2)>=0){[Link](" +");}
else{[Link](" ");}
[Link]("%.4f i",b.Imaginary1(real1,real2,imag1,imag2));
}}
TestCases:
[Link] Inputs Outputs
000 Addition: 0.0000 +0.0000 i Subtraction: 0.0000 +0.0000 i Multiplication:
1
0 0.0000 +0.0000 i Division: NaN NaN i
112 Addition: 3.0000 +3.0000 i Subtraction: -1.0000 -1.0000 i Multiplication:
2
2 0.0000 +4.0000 i Division: 0.5000 +0.0000 i
032 Addition: 2.0000 +10.0000 i Subtraction: -2.0000 -4.0000 i Multiplication: -
3
7 21.0000 +6.0000 i Division: 0.3962 +0.1132 i
963 Addition: 12.0000 +13.0000 i Subtraction: 6.0000 -1.0000 i Multiplication: -
4
7 15.0000 +81.0000 i Division: 1.1897 -0.7759 i
952 Addition: 11.0000 +13.0000 i Subtraction: 7.0000 -3.0000 i Multiplication: -
5
8 22.0000 +82.0000 i Division: 0.8529 -0.9118 i
264 Addition: 6.0000 +8.0000 i Subtraction: -2.0000 +4.0000 i Multiplication: -
6
2 4.0000 +28.0000 i Division: 1.0000 +1.0000 i
Question 2:
The Java code demonstrates a vehicle management application utilizing abstract classes. The
abstract class "Vehicle" defines common attributes and an abstract method
"calculateFuelEfficiency." How does this abstraction promote code reusability? In the concrete
classes "Car" and "Motorcycle," how are specific fuel efficiency calculations implemented? User
input is collected to create instances of vehicles; can you explain how the "displayInfo" method
contributes to the application's functionality? To extend the application, how would you integrate a
new vehicle type, like a "Truck"? What advantages does using an abstract class offer in this
context, and how might you validate user inputs? In the "Motorcycle" class, what does the
"hasSideCar" attribute represent? How could the code be enhanced to manage multiple vehicles
collectively and perform actions like starting the engine?
Input Format:
The program expects the user to provide input for creating a car and a motorcycle. The input
should follow this format:
Car details:
Car model (String)
Car manufacturer (String)
Number of doors (integer)
Motorcycle details:
Motorcycle model (String)
Motorcycle manufacturer (String)
Whether the motorcycle has a sidecar (true/false)
Output Format:
The program outputs information about the created car and motorcycle, including their details and
calculated fuel efficiency. The output follows this format:
Car Information:
Model: [Car Model] Manufacturer: [Car Manufacturer]
Fuel Efficiency: [Calculated Fuel Efficiency]
Motorcycle Information: Model: [Motorcycle Model]
Manufacturer: [Motorcycle Manufacturer]
Fuel Efficiency: [Calculated Fuel Efficiency]
Note:
Replace [Car Model], [Car Manufacturer], [Calculated Fuel Efficiency], [Motorcycle Model],
[Motorcycle Manufacturer], and [Calculated Fuel Efficiency] with the actual values obtained from
user input and calculations
Title for Question 2: Calculate Fuel Efficiency
Solution:
import [Link];
// Abstract class for Vehicle
abstract class Vehicle {
private String model;
private String manufacturer;
public Vehicle(String model, String manufacturer) {
[Link] = model;
[Link] = manufacturer;
}
// Abstract method for calculating fuel efficiency
public abstract double calculateFuelEfficiency();
public void displayInfo() {
[Link]("Model: " + model);
[Link]("Manufacturer: " + manufacturer);
}
}
// Concrete class for Car
class Car extends Vehicle {
private int numberOfDoors;
public Car(String model, String manufacturer, int numberOfDoors) {
super(model, manufacturer);
[Link] = numberOfDoors;
}
// Implementing the abstract method
@Override
public double calculateFuelEfficiency() {
// Perform calculation specific to cars
return 25.0; // Example value
}
}
// Concrete class for Motorcycle
class Motorcycle extends Vehicle {
private boolean hasSideCar;
public Motorcycle(String model, String manufacturer, boolean hasSideCar)
super(model, manufacturer);
[Link] = hasSideCar;
}
// Implementing the abstract method
@Override
public double calculateFuelEfficiency() {
// Perform calculation specific to motorcycles
return 50.0; // Example value
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
//[Link]("Enter car model: ");
String carModel = [Link]();
//[Link]("Enter car manufacturer: ");
String carManufacturer = [Link]();
//[Link]("Enter number of doors: ");
int carDoors = [Link]();
[Link](); // Consume the newline
//[Link]("Enter motorcycle model: ");
String motorcycleModel = [Link]();
//[Link]("Enter motorcycle manufacturer: ");
String motorcycleManufacturer = [Link]();
//[Link]("Does the motorcycle have a sidecar? (true/false):
boolean motorcycleSideCar = [Link]();
Car car = new Car(carModel, carManufacturer, carDoors);
Motorcycle motorcycle = new Motorcycle(motorcycleModel, motorcycleMa
[Link]("\nCar Information:");
[Link]();
[Link]("Fuel Efficiency: " + [Link]
//[Link]("\nMotorcycle Information:");
[Link]();
[Link]("Fuel Efficiency: " + [Link]
[Link]();
}
}
TestCases:
[Link] Inputs Outputs
Car Information: Model: Sedan Manufacturer: Toyota Fuel
Sedan Toyota 4 Cruiser
1 Efficiency: 25.0 Model: Cruiser Manufacturer: Harley-
Harley-Davidson false
Davidson Fuel Efficiency: 50.0
Hatchback Honda 5 Car Information: Model: Hatchback Manufacturer: Honda
2 Sportster Harley- Fuel Efficiency: 25.0 Model: Sportster Manufacturer:
Davidson false Harley-Davidson Fuel Efficiency: 50.0
Car Information: Model: Convertible Manufacturer: BMW
Convertible BMW 2
3 Fuel Efficiency: 25.0 Model: Chopper Manufacturer:
Chopper Indian true
Indian Fuel Efficiency: 50.0
Car Information: Model: SUV Manufacturer: Ford Fuel
SUV Ford 4 Cruiser
4 Efficiency: 25.0 Model: Cruiser Manufacturer: Yamaha
Yamaha false
Fuel Efficiency: 50.0
Sedan Honda 4 Car Information: Model: Sedan Manufacturer: Honda Fuel
5 Sportster Harley- Efficiency: 25.0 Model: Sportster Manufacturer: Harley-
Davidson false Davidson Fuel Efficiency: 50.0
Hatchback Toyota 5 Car Information: Model: Hatchback Manufacturer: Toyota
6 Chopper Harley- Fuel Efficiency: 25.0 Model: Chopper Manufacturer:
Davidson false Harley-Davidson Fuel Efficiency: 50.0
Question 3:
create an abstract class marks with the following method
voidgetPercentage();
Create a class A that extends marks and has 3 attributes marks1, marks2, and marks3 and a
method getPercentage that calculates and prints the percentage of the student.
Create a class B that extends marks and has 4 attributes marks1, marks2, marks3, and marks4
and a method getPercentage that calculates and prints the percentage of the student.
Round off the output to two decimal places.
Input Format
The first line of the input consists of three integers i.e., the marks scored by student A.
The second line of the input consists of four integers i.e., the marks scored by student B.
Output Format
The first line prints the percentage of A.
The second line prints the percentage of B.
Title for Question 3: Calculate Student MarkList
Solution:
import [Link].*;
import [Link].*;
import [Link].*;
abstract class marks {
abstract public void getPercentage();
}
class A extends marks {
DecimalFormat d = new DecimalFormat("0.00");
public int marks1;
public int marks2;
public int marks3;
A() {
this.marks1=0;
this.marks2=0;
this.marks3=0;
}
A(int m1,int m2,int m3) {
this.marks1 = m1;
this.marks2 = m2;
this.marks3 = m3;
}
public void getPercentage() {
int total = marks1+marks2+marks3;
double percent = (total/300.0)*100.0;
[Link]([Link](percent));
}
}
class B extends marks {
DecimalFormat d = new DecimalFormat("0.00");
public int marks1;
public int marks2;
public int marks3;
public int marks4;
B() {
this.marks1=0;
this.marks2=0;
this.marks3=0;
this.marks4=0;
}
B(int m1,int m2,int m3,int m4) {
this.marks1 = m1;
this.marks2 = m2;
this.marks3 = m3;
this.marks4 = m4;
}
public void getPercentage() {
int total = marks1+marks2+marks3+marks4;
double percent = (total/400.0)*100.0;
[Link]([Link](percent));
}
}
public class Main {
public static void main(String args[]) {
A a = new A();
Scanner sc = new Scanner([Link]);
a.marks1 = [Link]();
a.marks2 = [Link]();
a.marks3 = [Link]();
[Link]();
B b = new B();
b.marks1 = [Link]();
b.marks2 = [Link]();
b.marks3 = [Link]();
b.marks4 = [Link]();
[Link]();
}
}
TestCases:
[Link] Inputs Outputs
1 95 94 98 85 86 95 98 95.67 91.00
2 99 98 97 88 87 86 85 98.00 86.50
3 95 86 74 75 65 84 50 85.00 68.50
4 90 80 60 55 66 77 88 76.67 71.50
5 95 85 75 85 77 92 93 85.00 86.75
6 78 85 70 84 91 79 93 77.67 86.75
Question 4:
create an abstract class marks with the following method
voidgetPercentage();
Create a class A that extends marks and has 3 attributes marks1, marks2, and marks3 and a
method getPercentage that calculates and prints the percentage of the student.
Create a class B that extends marks and has 4 attributes marks1, marks2, marks3, and marks4
and a method getPercentage that calculates and prints the percentage of the student.
Round off the output to two decimal places.
Input Format
The first line of the input consists of three integers i.e., the marks scored by student A.
The second line of the input consists of four integers i.e., the marks scored by student B.
Output Format
The first line prints the percentage of A.
The second line prints the percentage of B.
Title for Question 4: Prints the Percentage of the student.
Solution:
import [Link].*;
import [Link].*;
import [Link].*;
abstract class marks {
abstract public void getPercentage();
}
class A extends marks {
DecimalFormat d = new DecimalFormat("0.00");
public int marks1;
public int marks2;
public int marks3;
A() {
this.marks1=0;
this.marks2=0;
this.marks3=0;
}
A(int m1,int m2,int m3) {
this.marks1 = m1;
this.marks2 = m2;
this.marks3 = m3;
}
public void getPercentage() {
int total = marks1+marks2+marks3;
double percent = (total/300.0)*100.0;
[Link]([Link](percent));
}
}
class B extends marks {
DecimalFormat d = new DecimalFormat("0.00");
public int marks1;
public int marks2;
public int marks3;
public int marks4;
B() {
this.marks1=0;
this.marks2=0;
this.marks3=0;
this.marks4=0;
}
B(int m1,int m2,int m3,int m4) {
this.marks1 = m1;
this.marks2 = m2;
this.marks3 = m3;
this.marks4 = m4;
}
public void getPercentage() {
int total = marks1+marks2+marks3+marks4;
double percent = (total/400.0)*100.0;
[Link]([Link](percent));
}
}
public class Main {
public static void main(String args[]) {
A a = new A();
Scanner sc = new Scanner([Link]);
a.marks1 = [Link]();
a.marks2 = [Link]();
a.marks3 = [Link]();
[Link]();
B b = new B();
b.marks1 = [Link]();
b.marks2 = [Link]();
b.marks3 = [Link]();
b.marks4 = [Link]();
[Link]();
}
}
TestCases:
[Link] Inputs Outputs
1 95 85 75 85 77 92 93 85.00 86.75
2 95 94 98 85 86 95 98 95.67 91.00
3 99 98 97 88 87 86 85 98.00 86.50
[Link] Inputs Outputs
4 95 86 74 75 65 84 50 85.00 68.50
5 90 80 60 55 66 77 88 76.67 71.50
6 95 85 75 85 77 92 93 85.00 86.75
Question 5:
Write a program to find the sum of divisors using the concept of abstract classes.
Create an abstract class "AbstractClass" which contains abstract methods getValue() and
divisorSum(int n). Then write a class called Calculator which extends the abstract class.
getValue(): Method need's to get input from the user.
divisorSum(int n): Method get's"n" as parameter and returns the sum of the numbers divisor.
Input Format
The input consists of a number.
Output Format
The output prints the sum of its proper divisors.
Constraints
0 <= n <= 9999
Title for Question 5: The sum of the number divisor
Solution:
import [Link];
abstract class AbstractClass {
int val;
abstract int getValue();
abstract int divisorSum(int n);
}
class Calculator extends AbstractClass {
public int getValue() {
Scanner in = new Scanner([Link]);
val = [Link]();
[Link]();
return val;
}
public int divisorSum(int n) {
int sum = 0;
for(int i=1; i<=n; i++) {
if(n%i == 0) {
sum += i;
}
}
return sum;
}
}
public class Main {
public static void main(String []args) {
Calculator calObj = new Calculator();
int value = [Link]();
int a = [Link](value);
[Link](a);
}
TestCases:
[Link] Inputs Outputs
1 4 7
2 13 14
3 0 0
4 9999 15912
5 123 168
6 25 31