Computer (5)
Computer (5)
class Perimeter
Perimeter(double a, double b)
this.a = a;
this.b = b;
double Calculate()
return 2 * (a + b);
void show()
class Area extends Perimeter // Subclass to compute the area of the parallelogram
double h; // Height
double area; // Area
void doarea()
area = b * h;
@Override
void show()
// Main class
class Main
void main()
}
}
OUTPUT
Q.Write a program to insert a node in binary tree in
java?
Algorithm
Step1:Start
void main()
new BinaryTreeInsert().run();
Node left;
Node right;
int value;
this.value=value;
System.out.println("Buildingtreewithrootvalue" +
rootnode.value);
System.out.println("=================================");
insert(rootnode, 11);
insert(rootnode, 15);
insert(rootnode, 16);
insert(rootnode, 23);
insert(rootnode, 79);
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
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:
publicclassLinkedList3
void main()
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);
ll2.add("Ravi");
ll2.add("Hanumat");
System.out.println("Updatedlist:"+ll);
System.out.println("AfterinvokingremoveAll()method:"+ll);
//Removing first element from the list ll.removeFirst();
System.out.println("AfterinvokingremoveFirst()method:"+ll);
System.out.println("AfterinvokingremoveLast()method:"+ll);
//Removinglastoccurrenceofelementfromthelist
ll.removeLastOccurrence("Harsh");
System.out.println("After invoking remove LastOccurrence() 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 Detail
// Instance variables
// Parameterized constructor
this.name = name;
this.address = address;
this.telno = telno;
this.rent = rent;
void show()
class Bill extends Detail // Subclass to calculate and display the bill
// Instance variables
// Parameterized constructor
if (n <= 100)
amt = rent;
else
{
void show()
// Example usage
class Main
void main()
Bill obj = new Bill("Alice", "123 Maple Street", "9876543210", 150.0, 250);
obj.cal();
obj.show();
OUTPUT