Q)A superclass Perimeter has been defined to calculate the perimeter of a
parallelogram. Define a subclass Area to compute the area of the parallelogram by
using the required data members of the superclass. The details are given below:
Class name: Perimeter
Data members/instance variables:
a: to store the length in decimal
b: to store the breadth in decimal
Member functions:
Perimeter (…): parameterized constructor to assign values to data members
double Calculate(): calculate and return the perimeter of a parallelogram is 2*
(length + breadth)
void show(): to display the data members along with the perimeter of the
parallelogram
Class name: Area
Data members/instance variables:
h: to store the height in decimal
area: to store the area of the parallelogram
Member functions:
Area(…): parameterized constructor to assign values to data members of both the
classes
void doarea(): compute the area as (breadth * height)
void show(): display the data members of both classes along with the area and
perimeter of the parallelogram.
Specify the class Perimeter giving details of the constructor (…), double Calculate
and void show (). Using the concept of inheritance, specify the class Area giving
details of the constructor (…), void doarea () and void show (). The main function
and algorithm need not be written.
OUTPUT
// Superclass to calculate the perimeter of a parallelogram
class Perimeter
protected double a; // Length
protected double b; // Breadth
Perimeter(double a, double b)
this.a = a;
this.b = b;
double Calculate()
return 2 * (a + b);
void show()
System.out.println("Length: " + a);
System.out.println("Breadth: " + b);
System.out.println("Perimeter: " + Calculate());
class Area extends Perimeter // Subclass to compute the area of the parallelogram
double h; // Height
double area; // Area
Area(double a, double b, double h)
super(a, b); // Call superclass constructor
this.h=h; // Initialize height
void doarea()
area = b * h;
@Override
void show()
super.show(); // Call superclass's show method
System.out.println("Height: " + h);
System.out.println("Area: " + area);
// Main class
class Main
void main()
Area obj = new Area(5.0, 3.0, 4.0); // Initialize dimensions
obj.doarea(); // Calculate area
obj.show(); // Display all details
}
}
OUTPUT
Q.Write a program to insert a node in binary tree in
java?
Algorithm
Step1:Start
Step2: Define the Node Class: Step3: Define
the main Method: Step4: Define the run
Method: Step5:DefinetheinsertMethod:
Step6: Execution:
Step7: Stop
Code
public class Binary_Tree_Insert
void main()
new BinaryTreeInsert().run();
static class Node
Node left;
Node right;
int value;
public Node(int value)
this.value=value;
public void run()
Node rootnode = new Node(25);
System.out.println("Buildingtreewithrootvalue" +
rootnode.value);
System.out.println("=================================");
insert(rootnode, 11);
insert(rootnode, 15);
insert(rootnode, 16);
insert(rootnode, 23);
insert(rootnode, 79);
public void insert(Node node, int value)
if(value<node.value)
if (node.left!=null)
insert(node.left,value);
} else
System.out.println("Inserted"+value+"toleftofNode"+node.value);
node.left=newNode(value);
else if(value>node.value)
if (node.right!= null)
insert(node.right,value);
else
System.out.println("Inserted" + value + "torightofNode" + node.value);
node.right=newNode(value);
}
Output
Q.Write a program to remove an element in different
ways using LinkList?
Algorithm
Step1:Start
Step2:Create a LinkedList:
Step3:Remove a Specific Element:
Step4:Remove an Element by Index:
Step5:Add a Collection of Elements:
Step6:Remove All Elements from a Collection: Step7 :Remove
the First Element:
Step8:Remove the Last Element:
Step9 :Remove the First Occurrence of a Specific Element: Step10:Remove the
Last Occurrence of a Specific Element: Step11 :Clear the List:
Step12:Stop
Code
import java.util.*;
publicclassLinkedList3
void main()
LinkedList<String> ll=new LinkedList<String>(); ll.add("Ravi");
ll.add("Vijay");
ll.add("Ajay");
ll.add("Anuj");
ll.add("Gaurav");
ll.add("Harsh");
ll.add("Virat");
ll.add("Gaurav");
ll.add("Harsh");
ll.add("Amit");
System.out.println("Initiallistofelements:"+ll);
ll.remove("Vijay"); //Removingspecificelementfromarraylist
System.out.println("Afterinvokingremove(object)method:"+ll);
//Removingelementonthebasisofspecificposition ll.remove(0);
System.out.println("After invoking remove(index) method:"+ll);
LinkedList<String> ll2=new LinkedList<String>();
ll2.add("Ravi");
ll2.add("Hanumat");
// Adding new elements to arraylist ll.addAll(ll2);
System.out.println("Updatedlist:"+ll);
//Removing all the new elements from array list ll.removeAll(ll2);
System.out.println("AfterinvokingremoveAll()method:"+ll);
//Removing first element from the list ll.removeFirst();
System.out.println("AfterinvokingremoveFirst()method:"+ll);
//Removing first element from thel ist ll.removeLast();
System.out.println("AfterinvokingremoveLast()method:"+ll);
//Removing first occurrence of element from the list ll.removeFirstOccurrence("Gaurav");
System.out.println("After invoking removeFirstOccurrence() method: "+ll);
//Removinglastoccurrenceofelementfromthelist
ll.removeLastOccurrence("Harsh");
System.out.println("After invoking remove LastOccurrence() method:" ll );
//Removing all the elements available in the list ll.clear();
System.out.println("After invoking clear()method:"+ll);
Output
Q) A superclass Detail has been defined to store the details of a customer. Define a
subclass Bill to compute the monthly telephone charge of the customer as per the chart is
given below:
Number of calls: Rate
1 – 100: Only rental charge
101 – 200: 60 paise per call + rental charge
201 – 300: 80 paise per call + rental charge
Above 300: 1 rupee per call + rental charge
The details of both the classes are given below:
Class name: Detail
Data members/instance variables:
name: to store the name of the customer
address: to store the address of the customer
telno: to store the phone number of the customer
rent: to store the monthly rental charge
Member functions:
Detail (…): parameterized constructor to assign values to data members
void show (): to display the details of the customer
Class name: Bill
Data members/instance variables:
n: to store the number of calls
amt: to store the amount to be paid by the customer
Member functions:
Bill (…): parameterized constructor to assign values to data members of both classes and
to initialize amt = 0.0void cal(): calculate the monthly telephone charge as per the chart is
given above
void show(): displays the details of the customer and amount to be paid.
Specify the class Detail giving details of the constructor, and void show(). Using the
concept of inheritance, specify the class Bill giving details of the constructor(), void cal()
and void show().
THE MAIN ( ) FUNCTION AND ALGORITHM NEED NOT BE WRITTEN.
CODE
// Superclass to store customer details
class Detail
// Instance variables
protected String name; // Name of the customer
protected String address; // Address of the customer
protected String telno; // Phone number of the customer
protected double rent; // Monthly rental charge
// Parameterized constructor
Detail(String name, String address, String telno, double rent)
this.name = name;
this.address = address;
this.telno = telno;
this.rent = rent;
// Method to display customer details
void show()
System.out.println("Customer Name: " + name);
System.out.println("Address: " + address);
System.out.println("Telephone Number: " + telno);
System.out.println("Monthly Rental Charge: " + rent);
}
class Bill extends Detail // Subclass to calculate and display the bill
// Instance variables
private int n; // Number of calls
private double amt; // Amount to be paid
// Parameterized constructor
Bill(String name, String address, String telno, double rent, int n)
// Call the constructor of the superclass
super(name, address, telno, rent);
this.n = n; // Initialize number of calls
this.amt = 0.0; // Initialize amount to 0
void cal() // Method to calculate the amount to be paid
if (n <= 100)
amt = rent;
else if (n <= 200)
amt = rent + (n - 100) * 0.60;
else if (n <= 300)
amt = rent + (100 * 0.60) + (n - 200) * 0.80;
else
{
amt = rent + (100 * 0.60) + (100 * 0.80) + (n - 300) * 1.0;
// Method to display customer details and bill amount
void show()
super.show(); // Call the superclass show method
System.out.println("Number of Calls: " + n);
System.out.println("Amount to be Paid: " + amt);
// Example usage
class Main
void main()
// Create a Bill object
Bill obj = new Bill("Alice", "123 Maple Street", "9876543210", 150.0, 250);
// Calculate the bill
obj.cal();
// Display the customer details and bill amount
obj.show();
OUTPUT