[Link].
U4AIE24046
Samarth C. Gowda
CONSTRUCTOR OVERLOAD
Q1:
import [Link];
class studentNameAge {
private String name;
private int age;
public studentNameAge() {
[Link] = "Unknown";
[Link] = 0;
public studentNameAge(String name) {
[Link] = name;
[Link] = 0;
public studentNameAge(String name, int age) {
[Link] = name;
[Link] = age;
public void display() {
[Link]("Name: " + name + ", Age: " + age);
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
studentNameAge student = null;
[Link]("Choose an option to create a student:");
[Link]("1. Default Constructor");
[Link]("2. Constructor with Name");
[Link]("3. Constructor with Name and Age");
int choice = [Link]();
[Link]();
switch (choice) {
case 1:
student = new studentNameAge();
break;
case 2:
[Link]("Enter name: ");
String name = [Link]();
student = new studentNameAge(name);
break;
case 3:
[Link]("Enter name: ");
String nameWithAge = [Link]();
[Link]("Enter age: ");
int age = [Link]();
student = new studentNameAge(nameWithAge, age);
break;
default:
[Link]("Invalid choice! Creating a default student.");
student = new studentNameAge();
[Link]("\nStudent Details:");
[Link]();
[Link]();
Q2:
import [Link];
public class BankAccDet {
private String accountNumber;
private double balance;
public BankAccDet() {
[Link] = "Not Assigned";
[Link] = 0.0;
public BankAccDet(String accountNumber) {
[Link] = accountNumber;
[Link] = 0.0;
public BankAccDet(String accountNumber, double balance) {
[Link] = accountNumber;
[Link] = balance;
public void display() {
[Link]("Account Number: " + accountNumber + ", Balance: $" + balance);
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
BankAccDet account = null;
[Link]("Choose an option to create a bank account:");
[Link]("1. Default Account (Balance: 0.0)");
[Link]("2. Account with Account Number");
[Link]("3. Account with Account Number and Initial Balance");
int choice = [Link]();
[Link]();
switch (choice) {
case 1:
account = new BankAccDet();
break;
case 2:
[Link]("Enter account number: ");
String accNumber = [Link]();
account = new BankAccDet(accNumber);
break;
case 3:
[Link]("Enter account number: ");
String accNumberWithBalance = [Link]();
[Link]("Enter initial balance: ");
double initialBalance = [Link]();
account = new BankAccDet(accNumberWithBalance, initialBalance);
break;
default:
[Link]("Invalid choice! Creating a default account.");
account = new BankAccDet();
}
[Link]("\nBank Account Details:");
[Link]();
[Link]();
}
Q3:
import [Link];
public class Car {
private String brand;
private String model;
private double price;
public Car() {
[Link] = "Unknown";
[Link] = "Unknown";
[Link] = 0.0;
public Car(String brand, String model) {
[Link] = brand;
[Link] = model;
[Link] = 0.0;
public Car(String brand, String model, double price) {
[Link] = brand;
[Link] = model;
[Link] = price;
public void displayDetails() {
[Link]("Car Brand: " + brand);
[Link]("Car Model: " + model);
[Link]("Car Price: $" + price);
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
Car car = null;
[Link]("Choose an option to create a Car:");
[Link]("1. Default Car (Brand: Unknown, Model: Unknown, Price: $0.0)");
[Link]("2. Car with Brand and Model");
[Link]("3. Car with Brand, Model, and Price");
int choice = [Link]();
[Link]();
switch (choice) {
case 1:
car = new Car();
break;
case 2:
[Link]("Enter Car Brand: ");
String brand = [Link]();
[Link]("Enter Car Model: ");
String model = [Link]();
car = new Car(brand, model);
break;
case 3:
[Link]("Enter Car Brand: ");
String brandWithPrice = [Link]();
[Link]("Enter Car Model: ");
String modelWithPrice = [Link]();
[Link]("Enter Car Price: ");
double price = [Link]();
car = new Car(brandWithPrice, modelWithPrice, price);
break;
default:
[Link]("Invalid choice! Creating a default car.");
car = new Car();
[Link]("\nCar Details:");
[Link]();
[Link]();
}
Q4:
import [Link];
public class Employee {
private int id;
private String name;
private double salary;
public Employee() {
[Link] = 0;
[Link] = "Unknown";
[Link] = 0.0;
public Employee(int id, String name) {
[Link] = id;
[Link] = name;
[Link] = 0.0;
public Employee(int id, String name, double salary) {
[Link] = id;
[Link] = name;
[Link] = salary;
public void displayDetails() {
[Link]("Employee ID: " + id);
[Link]("Employee Name: " + name);
[Link]("Employee Salary: " + salary);
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
Employee employee = null;
[Link]("Choose an option to create an Employee:");
[Link]("1. Default Employee (ID: 0, Name: Unknown, Salary: 0.0)");
[Link]("2. Employee with ID and Name");
[Link]("3. Employee with ID, Name, and Salary");
int choice = [Link]();
[Link]();
switch (choice) {
case 1:
employee = new Employee();
break;
case 2:
[Link]("Enter Employee ID: ");
int empId = [Link]();
[Link]();
[Link]("Enter Employee Name: ");
String empName = [Link]();
employee = new Employee(empId, empName);
break;
case 3:
[Link]("Enter Employee ID: ");
int empIdWithSalary = [Link]();
[Link]();
[Link]("Enter Employee Name: ");
String empNameWithSalary = [Link]();
[Link]("Enter Employee Salary: ");
double empSalary = [Link]();
employee = new Employee(empIdWithSalary, empNameWithSalary, empSalary);
break;
default:
[Link]("Invalid choice! Creating a default employee.");
employee = new Employee();
[Link]("\nEmployee Details:");
[Link]();
[Link]();
}
Method Overloading
Q1:
import [Link];
public class volume {
public double compute(double radius) {
return (4.0 / 3.0) * [Link] * [Link](radius, 3);
public double compute(int side) {
return [Link](side, 3);
public double compute(double length, double width, double height) {
return length * width * height;
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
volume volumeCalculator = new volume();
[Link]("Choose a shape to calculate volume:");
[Link]("1. Sphere");
[Link]("2. Cube");
[Link]("3. Cuboid");
int choice = [Link]();
switch (choice) {
case 1:
[Link]("Enter the radius of the sphere: ");
double radius = [Link]();
double sphereVolume = [Link](radius);
[Link]("Volume of the Sphere: " + sphereVolume);
break;
case 2:
[Link]("Enter the side length of the cube: ");
int side = [Link]();
double cubeVolume = [Link](side);
[Link]("Volume of the Cube: " + cubeVolume);
break;
case 3:
[Link]("Enter the length of the cuboid: ");
double length = [Link]();
[Link]("Enter the width of the cuboid: ");
double width = [Link]();
[Link]("Enter the height of the cuboid: ");
double height = [Link]();
double cuboidVolume = [Link](length, width, height);
[Link]("Volume of the Cuboid: " + cuboidVolume);
break;
default:
[Link]("Invalid choice. Please select a valid option.");
[Link]();
}
Q2:
import [Link];
class Area{
double Area(double radius){
return 3.14*radius*radius;
int Area(int length, int breadth){
return length*breadth;
}
int Area(int side){
return side*side;
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the Shape (1=Circle, 2=square, 3=rectangle): ");
int n = [Link]();
Area area = new Area();
switch (n) {
case 1:
[Link]("Enter the radius: ");
double radius = [Link]();
[Link]("Area of Circle: " + [Link](radius));
break;
case 2:
[Link]("Enter the side length: ");
int side = [Link]();
[Link]("Area of Square: " + [Link](side));
break;
case 3:
[Link]("Enter the length and breadth: ");
int length = [Link]();
int breadth = [Link]();
[Link]("Area of Rectangle: " + [Link](length, breadth));
break;
default:
[Link]("Invalid Shape");
break;
[Link]();
Q3:
import [Link];
public class salary {
private double baseSalary;
public salary(double baseSalary) {
[Link] = baseSalary;
public double calculateSalary() {
return baseSalary;
public double calculateSalary(double bonus) {
return baseSalary + bonus;
public double calculateSalary(double bonus, double tax) {
return baseSalary + bonus - tax;
public double calculateSalary(double bonus, double tax, int workingHours) {
return baseSalary + bonus - tax + (baseSalary / 160) * workingHours;
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter base salary: ");
double baseSalary = [Link]();
salary emp = new salary(baseSalary);
[Link]("\nSelect calculation method:");
[Link]("1. Base salary");
[Link]("2. Base salary + Bonus");
[Link]("3. Base salary + Bonus - Tax");
[Link]("4. Base salary + Bonus - Tax + Working Hours");
int choice = [Link]();
switch (choice) {
case 1:
[Link]("Salary: " + [Link]());
break;
case 2:
[Link]("Enter bonus: ");
double bonus = [Link]();
[Link]("Salary: " + [Link](bonus));
break;
case 3:
[Link]("Enter bonus: ");
bonus = [Link]();
[Link]("Enter tax: ");
double tax = [Link]();
[Link]("Salary: " + [Link](bonus, tax));
break;
case 4:
[Link]("Enter bonus: ");
bonus = [Link]();
[Link]("Enter tax: ");
tax = [Link]();
[Link]("Enter working hours: ");
int workingHours = [Link]();
[Link]("Salary: " + [Link](bonus, tax, workingHours));
break;
default:
[Link]("Invalid choice.");
[Link]();