0% found this document useful (0 votes)
16 views9 pages

Chapter 10 Aditya Satapathy

The document contains homework assignments for a B.Tech (CSE) student, Aditya Kumar Sataoathy, focusing on Java programming concepts. It includes tasks such as creating classes with encapsulation, demonstrating IS-A and HAS-A relationships, calculating areas of rectangles, using packages, and implementing inheritance and interfaces. Each task is accompanied by example code illustrating the required concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views9 pages

Chapter 10 Aditya Satapathy

The document contains homework assignments for a B.Tech (CSE) student, Aditya Kumar Sataoathy, focusing on Java programming concepts. It includes tasks such as creating classes with encapsulation, demonstrating IS-A and HAS-A relationships, calculating areas of rectangles, using packages, and implementing inheritance and interfaces. Each task is accompanied by example code illustrating the required concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Chapter-10 Homework

Name-Aditya kumar Sataoathy


Regd no-2341019349
Branch-B.Tech(CSE)
Q1. (a) Write a Java program to create a class Employee with private fields name, id, and salary.
(b) Add a method displayDetails() in the Employee class to display the employee's details.

(c) Demonstrate the concept of encapsulation by accessing private fields through the methods.

Program-

package chap10;

class Employee {

private String name;

private static int id;

private int salary;

public Employee(String n, int i, int s) {

this.name = n;

this.id = i;

this.salary = s;

public String getName() {

return name;

public void setName(String name) {

this.name = name;

public static int getId() {

return id;

}
public void setId(int id) {

this.id=id;

public int getSalary() {

return salary;

public void setSalary(int salary) {

this.salary = salary;

public void displayDetails() {

System.out.println("Name: " + name);

System.out.println("ID: " + id);

System.out.println("Salary: " + salary);

public static void main(String[] args) {

Employee emp1 = new Employee("Aditya", 101, 1000);

System.out.println("Initial Employee Details");

emp1.displayDetails();

emp1.setName("Rabi");

emp1.setId(102);

emp1.setSalary(1500);

System.out.println("\nUpdateed Employee Details (Access Through Getters)");

System.out.println("Name:"+emp1.getName());

System.out.println("Id:"+getId());

System.out.println("Salary $:"+emp1.getSalary());

System.out.println("\nUpdated Employee Details (Using Method);");

emp1.displayDetails();

}
Output-

Q2. (a) Write a Java program to demonstrate the IS-A relationship by creating a Vehicle class and a
subclass Car with additional properties. Add a display() method to show polymorphic behavior.

(b) Implement a HAS-A relationship by adding an Engine class and including it as a property in the
Car class. Add a method startEngine() in the Engine class and call it from the Car class.

(c) Demonstrate polymorphism by creating a list of Vehicle objects (both Vehicle and Car) and
calling the display() method on each object.

Program-

package chap10;

class Vehicle {

String Name;

Engine engine;

Vehicle(String nm) {

this.Name = nm;

this.engine = new Engine();

void display() {

System.out.println("Car Company: " + this.Name);

void startEngine() {

engine.startEngine();

}
class Car extends Vehicle {

Car(String nm) {

super(nm);

public static void main(String[] args) {

Vehicle C1 = new Vehicle("Toyota");

Vehicle C2 = new Vehicle("Tata");

C1.display();

C1.startEngine();

C2.display();

C2.startEngine();

class Engine {

void startEngine() {

System.out.println("Engine started.");

Output-
Q3. (a) Write a Java program to create a class Rectangle with attributes length and breadth.

(b) Use a constructor to initialize these values.

(c) Add a method calculateArea() to compute and return the area of the rectangle.

Program-

package chap10;

public class Rectangle {

static int length;

static int breath;

public Rectangle(int length,int breath) {

this.length=length;

this.breath=breath;

int calculateArea() {

return length*breath;

public static void main(String[] args) {

Rectangle rect1=new Rectangle(20,12);

System.out.println("Area:"+rect1.calculateArea());

}
Output-

Q4. (a) Create a package mypackage containing a class Student with attributes name and
rollNumber.

(b) Use the this keyword in a constructor to resolve variable shadowing.

(c) Add a method displayDetails() in the Student class to print the student's details.

Program-

package mypackage;

public class Student {

String Name;

int rollNumber;

public Student(String nm,int rn) {

Name=nm;

rollNumber=rn;

void displayDetrails(){

System.out.println("Name:"+this.Name);

System.out.println("Roll Number:"+this.rollNumber);

public static void main(String[] args) {

Student s1=new Student("Aditya",101);

s1.displayDetrails();

Output-
Q5. (a) Write a program that demonstrates the use of super to call the parent class constructor and
methods.

(b) Create a parent class Animal and a subclass Dog with additional behavior.

(c) Create an interface Speak with a method makeSound(). Implement it in the Animal and Dog
classes to demonstrate polymorphism.

Program-

package chap10;

interface Speak {

void makeSound();

class Animal implements Speak {

String name;

Animal(String name) {

this.name = name;

System.out.println("Animal constructor called: " + name);

public void makeSound() {

System.out.println("Generic animal sound");

class Dog extends Animal {

String breed;
Dog(String name, String breed) {

super(name);

this.breed = breed;

System.out.println("Dog constructor called: " + name + ", " + breed);

public void makeSound() {

super.makeSound();

System.out.println("Woof! Woof!");

public void fetch() {

System.out.println("Fetching...");

public class Main {

public static void main(String[] args) {

Dog myDog = new Dog("Buddy", "Golden Retriever");

myDog.makeSound();

myDog.fetch();

Output-

You might also like