Lab Report 1
Lab Report 1
Students Details
Name ID
Md. Tasnimur Rahman Shakir 221902285
[For teachers use only: Don’t write anything inside this box]
Marks: Signature:
Comments: Date:
Contents
1 Introduction 2
1.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3.1 Code & Output . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Design Goals/Objectives . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.5 Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1
Chapter 1
Introduction
1.1 Overview
Inheritance is a fundamental concept in object-oriented programming (OOP) that al-
lows classes to inherit properties and behavior from other classes. In Java, inheritance
forms the basis of code reuse and abstraction, enabling developers to create hierarchical
relationships between classes and build upon existing code to create new classes with
additional features.
1.2 Motivation
The motivation behind using inheritance in Java is to achieve several key benefits:
1. Code Reusablity.
3. Polymorphism.
4. Extensibility.
1.3 Implementation
Shape Hierarchy: Create a shape hierarchy with a base class "Shape" and derived
classes such as "Circle," "Rectangle," and "Triangle." Implement methods for calculat-
ing the area and perimeter of each shape.
package LabReportOne;
2
import java.util.Scanner;
class Shape {
double width, height;
public Shape(double width, double height) {
this.width = width;
this.height = height;
}
public double getArea(){
System.out.println("");
return 0;
}
public double getPerimeter(){
System.out.println("");
return 0;
}
}
class Circle extends Shape {
@Override
public double getArea() {
return Math.PI * width * width;
}
@Override
public double getPerimeter() {
return 2 * Math.PI * width;
}
}
class Rectangle extends Shape {
@Override
public double getArea() {
return width * height;
}
@Override
public double getPerimeter() {
return 2 * (width + height);
3
}
}
class Triangle extends Shape {
@Override
public double getArea() {
return (width * height) / 2;
}
@Override
public double getPerimeter() {
return width + height + Math.sqrt(width * width + height * height);
}
}
System.out.println();
System.out.println();
4
System.out.println("Area Of Circle(" + radius + "): " + circle.getArea(
System.out.println("Perimeter Of Circle(" + radius + "): " + circle.get
}
}
}
Output 1
Problem 2
import java.util.Scanner;
class Employee {
final String name;
String role;
double experience;
double basicSalary;
5
if (experience >= 10) {
this.experience = 0.5;
} else if (experience >= 5) {
this.experience = 0.4;
} else {
this.experience = 0.25;
}
}
6
scanner.nextLine(); // Consume newline character
manager = new Manager(managerName, managerExperience, managerBasicSalar
// Get Developer details
System.out.print("Enter Developer’s name: ");
String developerName = scanner.nextLine();
System.out.print("Enter Developer’s experience (in years): ");
int developerExperience = scanner.nextInt();
System.out.print("Enter Developer’s basic salary: ");
double developerBasicSalary = scanner.nextDouble();
scanner.nextLine(); // Consume newline character
developer = new Developer(developerName, developerExperience, developer
// Get Salesperson details
System.out.print("Enter Salesperson’s name: ");
String salespersonName = scanner.nextLine();
System.out.print("Enter Salesperson’s experience (in years): ");
int salespersonExperience = scanner.nextInt();
System.out.print("Enter Salesperson’s basic salary: ");
double salespersonBasicSalary = scanner.nextDouble();
scanner.nextLine(); // Consume newline character
salesperson = new Salesperson(salespersonName, salespersonExperience, s
}
Output 2
7
Problem 3
Vehicle Inheritance: Create a vehicle hierarchy with a base class "Vehicle" and derived
classes such as "Car," "Motorcycle," and "Truck." Implement methods to calculate the
fuel consumption and display the details of each vehicle.
import java.util.Scanner;
class Vehicle {
String make;
String model;
int year;
double fuelEfficiency;
public Car(String make, String model, int year, double fuelEfficiency, int numb
super(make, model, year, fuelEfficiency);
this.numberOfSeats = numberOfSeats;
}
8
public Motorcycle(String make, String model, int year, double fuelEfficiency, b
super(make, model, year, fuelEfficiency);
this.hasSidecar = hasSidecar;
}
public Truck(String make, String model, int year, double fuelEfficiency, String
super(make, model, year, fuelEfficiency);
this.cargoCapacity = cargoCapacity;
}
9
car = new Car(carMake, carModel, carYear, carFuelEfficiency, carNumberOfSea
scanner.close();
10
Output 1
Problem 4
School System: Create a school system with a base class "Person" and derived classes
such as "Student" and "Teacher." Implement methods to calculate the average grade of
a student and display the subjects taught by a teacher.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
class Person {
final String name;
11
final int age;
12
System.out.print("Enter student’s age: ");
int studentAge = scanner.nextInt();
scanner.nextLine(); // Consume newline character
System.out.print("Enter student’s grades (separated by spaces): ");
String[] gradeTokens = scanner.nextLine().split(" ");
List<Integer> studentGrades = new ArrayList<>();
for (String gradeToken : gradeTokens) {
studentGrades.add(Integer.valueOf(gradeToken));
} student = new Student(studentName, studentAge, studentGrades);
// Get teacher details
System.out.print("Enter teacher’s name: ");
String teacherName = scanner.nextLine();
System.out.print("Enter teacher’s age: ");
int teacherAge = scanner.nextInt();
scanner.nextLine(); // Consume newline character
System.out.print("Enter teacher’s subjects (separated by commas): ");
String subjectsInput = scanner.nextLine();
List<String> teacherSubjects = Arrays.asList(subjectsInput.split("\\s*,
teacher = new Teacher(teacherName, teacherAge, teacherSubjects);
}
13
Output 1
1.5 Application
1. Creating class hierarchies: Inheritance allows the creation of parent and child
classes, enabling the reuse of code and promoting a hierarchical structure for
related classes.
2. Code reusability: Inheritance enables the child classes to inherit attributes and
methods from parent classes, reducing code duplication and promoting efficient
development.
Overall, inheritance in Java is applied for creating class hierarchies, promoting code
reusability, facilitating polymorphism, and in the development of frameworks and li-
braries. [1]
14
References
[1] Omid C Farokhzad and Robert Langer. Impact of nanotechnology on drug delivery.
ACS nano, 3(1):16–20, 2009.
15