Explain the concept of inheritance and the substitution principle using an examp
ID: 3694081 • Letter: E
Question
Explain the concept of inheritance and the substitution principle using an example (Java code with result screen shot). Add comments in the code explaining the steps otherwise you lose points. Using Merge Sort technique show how two sorted arrays can be sorted into one. Show using a Java program and the results screen shot (you can come up with any two sorted arrays). Add comments in the code explaining the steps otherwise you lose points. Write the Java code for a method of the Binary Search Tree class - Comparable smallest() that returns the smallest element of a tree. Execute the code and show the result screen shot.Explanation / Answer
2)class add1//parent class
{
//parent class sum method doing addition
public int sum(int a,int b)
{
return a+b;
}
}
class add2 extends add1//child class
{
//child class sum method doing multiplication
public int sum(int x,int y)
{
return x*y;
}
public static add1 addnum()//reference to child class
{
return new add2();
}
public static void main(String args[])
{
add1 obj = addnum();//creating reference
int res = obj.sum(4,5);//calling method
System.out.println("result: "+res);//printing result
}
}
output
result : 20
3)
class MergeArrays
{
public static void main(String[] args) {
int array1[]={3,5, 9, 30, 56};
int array2[]={1, 6, 15, 50, 70};
int[] result=merge(array1, array2);
for (int i=0; i<result.length;i++)
System.out.print(result[i]+ " ");
}
static int[] merge(int[] array1, int[] array2)
{
int l1=array1.length;
int l2=array2.length;
int[] result = new int[l1+l2];
int i=0, j = 0;
for(int k = 0 ; k< (l1+l2);k++)
{
//If array1 is empty, copy the elements of the array array2 to the result array .
//When i equals the length of array arr1 , copy the remaining elements in the array array2 to result
if ( i >= l1 )
{
result[k] = array2[j];
j ++;
}
//If array2 is empty, copy the elements of the array arr1 to the result array .
//When j equals the length of array arr2 , copy the remaining elements in the array arr1 to result
else if ( j >= l2)
{
result[k] = array1[i];
i ++;
}
else
{
if ( array1[i] < array2[j])
{
result[k] = array1[i];
i ++;
}
else
{
result[k] = array2[j];
j ++;
}
}
}
return result;
}
}
output:
1 3 5 6 9 15 30 50 56 70
4)
// A binary tree node
class Node {
int data;
Node left, right;
Node(int d) {
data = d;
left = right = null;
}
}
class BinaryTree {
static Node head;
//method to insert node
Node insert(Node node, int data) {
//if empty tree return new node
if (node == null) {
return (new Node(data));
} else {
if (data <= node.data) {
node.left = insert(node.left, data);
} else {
node.right = insert(node.right, data);
}
return node;
}
}
//method to search min value
int minvalue(Node node) {
Node current = node;
while (current.left != null) {
current = current.left;
}
return (current.data);
}
public static void main(String[] args) {
BinaryTree t = new BinaryTree();
Node root = null;
root = t.insert(root, 5);
t.insert(root, 2);
t.insert(root, 3);
t.insert(root, 7);
t.insert(root, 4);
System.out.println("The minimum value is " + t.minvalue(root));
}
}
output:
The minimum value is 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.