maurice Questions of answers
maurice 3 months ago Answer question Write a program to calculate the difference between the sum of the odd level and even level nodes of a Binary Tree
import java.util.LinkedList;  
import java.util.Queue;  
   
public class DiffOddEven {  
    
    //Represent a node of binary tree  
    public static class Node{  
        int data;  
        Node left;  
        Node right;  
        
        public Node(int data){  
            //Assign data to the new node, set left and right children to null  
            this.data = data;  
            this.left = null;  
            this.right = null;  
            }  
        }  
    
    //Represent the root of binary tree  
    public Node root;  
  
    public DiffOddEven(){  
        root = null;  
    }  
   
    //difference() will calculate the difference between sum of odd and even levels of binary tree  
    public int difference() {  
          int oddLevel = 0, evenLevel = 0, diffOddEven = 0;  
          
          //Variable nodesInLevel keep tracks of number of nodes in each level  
          int nodesInLevel = 0;  
          
          //Variable currentLevel keep track of level in binary tree  
          int currentLevel = 0;  
          
          //Queue will be used to keep track of nodes of tree level-wise  
          Queue<Node> queue = new LinkedList<Node>();  
          
          //Check if root is null  
          if(root == null) {  
              System.out.println("Tree is empty");  
              return 0;  
          }  
          else {  
              //Add root node to queue as it represents the first level  
              queue.add(root);  
              currentLevel++;  
              
              while(queue.size() != 0) {  
                  
                  //Variable nodesInLevel will hold the size of queue i.e. number of elements in queue  
                  nodesInLevel = queue.size();  
                  
                  while(nodesInLevel > 0) {  
                      Node current = queue.remove();  
                      
                    //Checks if currentLevel is even or not.  
                      if(currentLevel % 2 == 0)  
                          //If level is even, add nodes's to variable evenLevel  
                          evenLevel += current.data;  
                      else  
                          //If level is odd, add nodes's to variable oddLevel  
                          oddLevel += current.data;  
                      
                      //Adds left child to queue  
                      if(current.left != null)  
                          queue.add(current.left);  
                      //Adds right child to queue  
                      if(current.right != null)   
                          queue.add(current.right);  
                     nodesInLevel--;  
                  }  
                  currentLevel++;  
              }  
              //Calculates difference between oddLevel and evenLevel  
              diffOddEven = Math.abs(oddLevel - evenLevel);  
          }  
          return diffOddEven;  
      }  
  
    public static void main (String[] args) {  
        
        DiffOddEven bt = new DiffOddEven();  
        //Add nodes to the binary tree  
        bt.root = new Node(1);  
        bt.root.left = new Node(2);  
        bt.root.right = new Node(3);  
        bt.root.left.left = new Node(4);  
        bt.root.right.left = new Node(5);  
        bt.root.right.right = new Node(6);  
    
        //Display the difference between sum of odd level and even level nodes  
        System.out.println("Difference between sum of odd level and even level nodes: " + bt.difference());  
}  
} 

Output:

Difference between sum of odd level and even level nodes: 11
maurice 3 months ago Answer question Write a program in Java to find the maximum and minimum value node from a circular linked list.
public class MinMax {  
    //Represents the node of list.  
    public class Node{  
        int data;  
        Node next;  
        public Node(int data) {  
            this.data = data;  
        }  
    }  
    
    //Declaring head and tail pointer as null.  
    public Node head = null;  
    public Node tail = null;  
    
    //This function will add the new node at the end of the list.  
    public void add(int data){  
        //Create new node  
        Node newNode = new Node(data);  
        //Checks if the list is empty.  
        if(head == null) {  
             //If list is empty, both head and tail would point to new node.  
            head = newNode;  
            tail = newNode;  
            newNode.next = head;  
        }  
        else {  
            //tail will point to new node.  
            tail.next = newNode;  
            //New node will become new tail.  
            tail = newNode;  
            //Since, it is circular linked list tail will points to head.  
            tail.next = head;  
        }  
    }  
    
    //Finds out the minimum value node in the list  
    public void minNode() {  
        Node current = head;  
        //Initializing min to initial node data  
        int min = head.data;  
        if(head == null) {  
            System.out.println("List is empty");  
        }  
        else {  
             do{  
                 //If current node's data is smaller than min  
                 //Then replace value of min with current node's data  
                 if(min > current.data) {  
                     min = current.data;  
                 }  
                 current= current.next;  
            }while(current != head);  
        
            System.out.println("Minimum value node in the list: "+ min);  
        }  
    }  
        
    //Finds out the maximum value node in the list  
    public void maxNode() {  
        Node current = head;  
        //Initializing max to initial node data  
        int max = head.data;  
        if(head == null) {  
            System.out.println("List is empty");  
        }  
        else {  
             do{  
                 //If current node's data is greater than max  
                 //Then replace value of max with current node's data  
                 if(max < current.data) {  
                     max = current.data;  
                 }  
                 current= current.next;  
                }while(current != head);  
             
            System.out.println("Maximum value node in the list: "+ max);  
        }  
    }  
                
    public static void main(String[] args) {  
        MinMax cl = new MinMax();  
        //Adds data to the list  
        cl.add(5);  
        cl.add(20);  
        cl.add(10);  
        cl.add(1);  
        //Prints the minimum value node in the list  
        cl.minNode();  
        //Prints the maximum value node in the list  
        cl.maxNode();  
    }  
}  

Output:

Minimum value node in the list: 1
Maximum value node in the list: 20
maurice 3 months ago Answer question Write a program in Java to create a doubly linked list containing n nodes
 public class CountList {  
    
    //Represent a node of the doubly linked list  
  
    class Node{  
        int data;  
        Node previous;  
        Node next;  
        
        public Node(int data) {  
            this.data = data;  
        }  
    }  
    
    //Represent the head and tail of the doubly linked list  
    Node head, tail = null;  
    
    //addNode() will add a node to the list  
    public void addNode(int data) {  
        //Create a new node  
        Node newNode = new Node(data);  
        
        //If list is empty  
        if(head == null) {  
            //Both head and tail will point to newNode  
            head = tail = newNode;  
            //head's previous will point to null  
            head.previous = null;  
            //tail's next will point to null, as it is the last node of the list  
            tail.next = null;  
        }  
        else {  
            //newNode will be added after tail such that tail's next will point to newNode  
            tail.next = newNode;  
            //newNode's previous will point to tail  
            newNode.previous = tail;  
            //newNode will become new tail  
            tail = newNode;  
            //As it is last node, tail's next will point to null  
            tail.next = null;  
        }  
    }  
    
    //countNodes() will count the nodes present in the list  
    public int countNodes() {  
        int counter = 0;  
        //Node current will point to head  
        Node current = head;  
        
        while(current != null) {  
            //Increment the counter by 1 for each node  
            counter++;  
            current = current.next;  
        }  
        return counter;  
    }  
    
    //display() will print out the elements of the list  
    public void display() {  
        //Node current will point to head  
        Node current = head;  
        if(head == null) {  
            System.out.println("List is empty");  
            return;  
        }  
        System.out.println("Nodes of doubly linked list: ");  
        while(current != null) {  
            //Prints each node by incrementing the pointer.  
  
            System.out.print(current.data + " ");  
            current = current.next;  
        }  
    }  
    
    public static void main(String[] args) {  
        
        CountList dList = new CountList();  
        //Add nodes to the list  
        dList.addNode(1);  
        dList.addNode(2);  
        dList.addNode(3);  
        dList.addNode(4);  
        dList.addNode(5);  
        
        //Displays the nodes present in the list  
        dList.display();  
        
        //Counts the nodes present in the given list  
        System.out.println("\nCount of nodes present in the list: " + dList.countNodes());  
    }  
}  

Output:

Nodes of doubly linked list: 
1 2 3 4 5 
Count of nodes present in the list: 5
maurice 3 months ago Answer question How to perform quicksort in Java?
public class QuickSort {  
public static void main(String[] args) {  
    int i;  
    int[] arr={90,23,101,45,65,23,67,89,34,23};  
    quickSort(arr, 0, 9);  
    System.out.println("\n The sorted array is: \n");  
    for(i=0;i<10;i++)  
    System.out.println(arr[i]);  
  }  
  public static int partition(int a[], int beg, int end)  
  {  
    
    int left, right, temp, loc, flag;   
    loc = left = beg;  
    right = end;  
    flag = 0;  
    while(flag != 1)  
    {  
      while((a[loc] <= a[right]) && (loc!=right))  
      right--;  
      if(loc==right)  
      flag =1;  
      elseif(a[loc]>a[right])  
      {  
        temp = a[loc];  
        a[loc] = a[right];  
        a[right] = temp;  
        loc = right;  
      }  
      if(flag!=1)  
      {  
        while((a[loc] >= a[left]) && (loc!=left))  
        left++;  
        if(loc==left)  
        flag =1;  
        elseif(a[loc] <a[left])  
        {  
          temp = a[loc];  
          a[loc] = a[left];  
          a[left] = temp;  
          loc = left;  
        }  
      }  
    }  
    returnloc;  
  }  
  static void quickSort(int a[], int beg, int end)  
  {  
    
    int loc;  
    if(beg<end)  
    {  
      loc = partition(a, beg, end);  
      quickSort(a, beg, loc-1);  
      quickSort(a, loc+1, end);  
    }  
  }  
}  

Output:

The sorted array is: 
23
23
23
34
45
65
67
89
90
101
maurice 3 months ago Answer question How to perform merge sort in Java?
public class MyMergeSort  
{  
void merge(int arr[], int beg, int mid, int end)  
{  
  
int l = mid - beg + 1;  
int r = end - mid;  
  
intLeftArray[] = new int [l];  
intRightArray[] = new int [r];  
  
for (int i=0; i<l; ++i)  
LeftArray[i] = arr[beg + i];  
  
for (int j=0; j<r; ++j)  
RightArray[j] = arr[mid + 1+ j];  
  
  
int i = 0, j = 0;  
int k = beg;  
while (i<l&&j<r)  
{  
if (LeftArray[i] <= RightArray[j])  
{  
arr[k] = LeftArray[i];  
i++;  
}  
else  
{  
arr[k] = RightArray[j];  
j++;  
}  
k++;  
}  
while (i<l)  
{  
arr[k] = LeftArray[i];  
i++;  
k++;  
}  
  
while (j<r)  
{  
arr[k] = RightArray[j];  
j++;  
k++;  
}  
}  
  
void sort(int arr[], int beg, int end)  
{  
if (beg<end)  
{  
int mid = (beg+end)/2;  
sort(arr, beg, mid);  
sort(arr , mid+1, end);  
merge(arr, beg, mid, end);  
}  
}  
public static void main(String args[])  
{  
intarr[] = {90,23,101,45,65,23,67,89,34,23};  
MyMergeSort ob = new MyMergeSort();  
ob.sort(arr, 0, arr.length-1);  
  
System.out.println("\nSorted array");  
for(int i =0; i<arr.length;i++)  
{  
  System.out.println(arr[i]+"");  
}  
}  
}  

Output:

Sorted array 
23
23
23
34
45
65
67
89
90
101
maurice 3 months ago Answer question How to perform Linear Search in Java?
import java.util.Scanner;  
  
public class Leniear_Search {  
public static void main(String[] args) {  
  int[] arr = {10, 23, 15, 8, 4, 3, 25, 30, 34, 2, 19};  
  int item,flag=0;   
  Scanner sc = new Scanner(System.in);  
  System.out.println("Enter Item ?");  
  item = sc.nextInt();  
  for(int i = 0; i<10; i++)  
  {  
    if(arr[i]==item)  
    {  
      flag = i+1;  
      break;  
    }  
    else   
      flag = 0;   
  }  
  if(flag != 0)  
  {  
    System.out.println("Item found at location" + flag);  
  }  
  else   
    System.out.println("Item not found");  
  
}  
}  

Output:

Enter Item ?
23
Item found at location 2
Enter Item ?
22
Item not found
maurice 3 months ago Answer question How to perform Selection Sort in Java?

Consider the following program to perform selection sort in Java.

public class SelectionSort {  
public static void main(String[] args) {  
  int[] a = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};  
  int i,j,k,pos,temp;  
  for(i=0;i<10;i++)  
  {  
    pos = smallest(a,10,i);  
    temp = a[i];  
    a[i]=a[pos];  
    a[pos] = temp;  
  }  
  System.out.println("\nprinting sorted elements...\n");  
  for(i=0;i<10;i++)  
  {  
    System.out.println(a[i]);  
  }  
}  
public static int smallest(int a[], int n, int i)  
{  
  int small,pos,j;  
  small = a[i];  
  pos = i;  
  for(j=i+1;j<10;j++)  
  {  
    if(a[j]<small)  
    {  
      small = a[j];  
      pos=j;  
    }  
  }  
  return pos;  
}  
}  

Output:

printing sorted elements...
7
9
10
12
23
23
34
44
78
101
maurice 3 months ago Answer question How to perform Binary Search in Java?
import java.util.*;  
public class BinarySearch {  
public static void main(String[] args) {  
  int[] arr = {16, 19, 20, 23, 45, 56, 78, 90, 96, 100};  
  int item, location = -1;  
  System.out.println("Enter the item which you want to search");  
  Scanner sc = new Scanner(System.in);  
  item = sc.nextInt();  
  location = binarySearch(arr,0,9,item);  
  if(location != -1)  
  System.out.println("the location of the item is "+location);  
  else   
    System.out.println("Item not found");  
  }  
public static int binarySearch(int[] a, int beg, int end, int item)  
{  
  int mid;  
  if(end >= beg)   
  {   
    mid = (beg + end)/2;  
    if(a[mid] == item)  
    {  
      return mid+1;  
    }  
    else if(a[mid] < item)   
    {  
      return binarySearch(a,mid+1,end,item);  
    }  
    else   
    {  
      return binarySearch(a,beg,mid-1,item);  
    }  
  }  
  return -1;   
}  
}  

Output:

Enter the item which you want to search 
45 
the location of the item is 5
maurice 3 months ago Answer question How to perform Bubble Sort in Java?
public class BubbleSort {  
  public static void main(String[] args) {  
  int[] a = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};  
  for(int i=0;i<10;i++)  
  {  
    for (int j=0;j<10;j++)  
    {  
      if(a[i]<a[j])  
      {  
        int temp = a[i];  
        a[i]=a[j];  
        a[j] = temp;   
      }  
    }  
  }  
  System.out.println("Printing Sorted List ...");  
  for(int i=0;i<10;i++)  
  {  
    System.out.println(a[i]);  
  }  
}  
}  

Output:

Printing Sorted List . . . 
7
9
10
12
23
34
34
44
78 
101 
maurice 3 months ago Answer question Can RMI and CORBA based applications interact?

Yes, they can. RMI is available with IIOP as the transport protocol instead of JRMP.

maurice 3 months ago Answer question What is JRMP?

JRMP (Java Remote Method Protocol) can be defined as the Java-specific, stream-based protocol which looks up and refers to the remote objects. It requires both client and server to use Java objects. It is wire level protocol which runs under RMI and over TCP/IP.

maurice 3 months ago Answer question What is the use of HTTP-tunneling in RMI?

HTTP tunneling can be defined as the method which doesn't need any setup to work within the firewall environment. It handles the HTTP connections through the proxy servers. However, it does not allow outbound TCP connections.

maurice 3 months ago Answer question What are the steps involved to write RMI based programs?

There are 6 steps which are performed to write RMI based programs.

  • Create the remote interface.
  • Provide the implementation of the remote interface.
  • Compile the implementation class and create the stub and skeleton objects using the rmic tool.
  • Start the registry service by the rmiregistry tool.
  • Create and start the remote application.
  • Create and start the client application.
maurice 3 months ago Answer question What is the purpose of stub and skeleton?

Stub

The stub is an object, acts as a gateway for the client side. All the outgoing requests are routed through it. It resides at the client side and represents the remote object. When the caller invokes the method on the stub object, it does the following tasks:

  • It initiates a connection with remote Virtual Machine (JVM).
  • It writes and transmits (marshals) the parameters to the remote Virtual Machine (JVM).
  • It waits for the result.
  • It reads (unmarshals) the return value or exception.
  • It finally, returns the value to the caller.

Skeleton

The skeleton is an object, acts as a gateway for the server side object. All the incoming requests are routed through it. When the skeleton receives the incoming request, it does the following tasks:

  • It reads the parameter for the remote method.
  • It invokes the method on the actual remote object.
  • It writes and transmits (marshals) the result to the caller.
maurice 3 months ago Answer question What do you understand by the bean persistent property?

The persistence property of Java bean comes into the act when the properties, fields, and state information are saved to or retrieve from the storage

maurice 3 months ago Answer question What is the purpose of using the Java bean?

According to Java white paper, it is a reusable software component. A bean encapsulates many objects into one object so that we can access this object from multiple places. Moreover, it provides the easy maintenance.

maurice 3 months ago Answer question What is a JavaBean?

JavaBean is a reusable software component written in the Java programming language, designed to be manipulated visually by a software development environment, like JBuilder or VisualAge for Java. t. A JavaBean encapsulates many objects into one object so that we can access this object from multiple places. Moreover, it provides the easy maintenance. Consider the following example to create a JavaBean class.

//Employee.java  
package mypack;  
public class Employee implements java.io.Serializable{  
private int id;  
private String name;  
public Employee(){}  
public void setId(int id){this.id=id;}  
public int getId(){return id;}  
public void setName(String name){this.name=name;}  
public String getName(){return name;}  
}
maurice 3 months ago Answer question How will you load a specific locale?

By ResourceBundle.getBundle(?) method.

maurice 3 months ago Answer question What is Locale?

A Locale object represents a specific geographical, political, or cultural region. This object can be used to get the locale-specific information such as country name, language, variant, etc.

import java.util.*;  
public class LocaleExample {  
public static void main(String[] args) {  
Locale locale=Locale.getDefault();  
//Locale locale=new Locale("fr","fr");//for the specific locale  
  
System.out.println(locale.getDisplayCountry());  
System.out.println(locale.getDisplayLanguage());  
System.out.println(locale.getDisplayName());  
System.out.println(locale.getISO3Country());  
System.out.println(locale.getISO3Language());  
System.out.println(locale.getLanguage());  
System.out.println(locale.getCountry());  
  
}  
}  

Output:

United States
English
English (United States)
USA
eng
en
US
maurice 3 months ago Answer question Can you write a Java class that could be used both as an applet as well as an application?

Yes. Add a main() method to the applet.