Saturday, September 30, 2017

A very important conceptual question frequently asked in MNC's.

Note: Before using this code please create a Java Project in Eclipse IDE and save this code in TestRTP.java file

Please see the output and understand the concept by reading the explanation. Given below this code

import java.util.*;
class Father 
{
public String name = "Satnam";

public static void eating()
{
System.out.println(" is eating fruits");
}

public void drinking()
{
System.out.println(" is drinking coconut water");
}
}

class Son extends Father
{
public String name = "Gurnam";

public static void eating()
{
System.out.println(" is eating pasta");
}

public void drinking()
{
System.out.print(" is drinking mango juice\n");
}
}

public class TestRTP
{
public static void main(String[] args)
{
Father f = new Son(); // assume it is line no-1

System.out.print(f.name); // assume it is line no-2
f.eating(); // assume it is line no-3

System.out.print(f.name);
f.drinking(); // assume it is line no-4
}
}

Output of code =>

Satnam is eating fruits
Satnam is drinking mango juice

Explanation of code =>

Father is a super-class and Son is a subclass. In subclass we are re-defining the static-method and non-static-method of super-class. When a subclass redefine the same non-static-method of super-class, this process is known as method-overriding.

At line-1 we are taking a reference-variable of super-class(ie. father) and assigning the object of subclass (ie. son) into it (this process is known as polymorphic assignment).

At line-2 when we access the variable 'name', the 'name' of father will be printed since the data members (i mean variables) are mainly bound to the type of object rather than the object itself.

The same process will be followed at line-3 when we call a static-method (static-method of super-class will be invoked).

But when we come to line-4 the whole scenario will be changed.
At line-4 the non-static-method of sub-class will be invoked.
Reason, the overridden non-static-method are bound to object rather than the type of object.
So even the reference-variable belongs to super-class the object is of subclass, so in this case the method of subclass (ie. son) will be invoked.

Friday, September 22, 2017

Code by Vasu, Minimum Distance between two words in a paragraph

This code is implemented and provided by Vasu. 

About code : Using this program we can find out the minimum distance between two words inside a paragraph.

Note: Before using this code please create a Java project in Eclipse and save this class inside MinimumDistance.java file.

import java.util.Scanner;

public class MinimumDistance {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in) ;

String para = "We have the BST search and insert operations. "
                         + "We also In the search post and delete operation is discussed. "
                                 + "When we delete a node, there possibilities arise." ;

para = para.toLowerCase();

System.out.println("Enter The two words : ");

String word1 = sc.next() , word2 = sc.next() ;

int index1 = -1 , index2 = -1 , min = Integer.MAX_VALUE;

for (int i = 0; i < para.length() ; i++) {

int count1 = 0 , count2 = 0 ;

for (int j = 0; j < word1.length(); j++) {

if(word1.charAt(j) != para.charAt(i+count1)) {
break ;
}
                                else {
if(count1 == word1.length()-1) {
index1 = i ;
count1 = 0 ;
break ;
}
                                        else {
count1++ ;
}
}
}

for (int j = 0; j < word2.length(); j++) {
if(word2.charAt(j) != para.charAt(i+count2)) {
break ;
}
                                else {
if(count2 == word2.length()-1) {
index2 = i ;
count2 = 0 ;
break ;
}
                                        else {
count2++ ;
}
}
}

if(index2 > index1) {
if(index2-index1 < min) {
min = index2-index1 ;
}
}


}

System.out.println("The Minimum distance between the two words "
                                        +"in the para is : " + min);

}
}

Output of this program =>
Enter The two words :
We
is
The Minimum distance between the two words in the para is : 95

Wednesday, September 20, 2017

Code by Vasu, Tic Tac Toe in Java

This code is implemented and provided by Vasu.

It is a code to play the famous Tic Tac Toe game. When we run this code a GUI will be shown in order to play the game.

Note. Before using this code please create a "Java Project" in Eclipse IDE and save this class inside 
Game.java file

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import java.awt.GridLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Game {

JFrame frame;
JButton[] jButtons = new JButton[9] ;
String[] board = new String[9] ;
int player = 1 ;

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Game window = new Game();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});

}

public Game() {
for (int i = 0; i < board.length; i++) {
board[i] = String.valueOf(i) ;
}
initialize();
}

private void initialize() {
frame = new JFrame();
frame.setTitle("Player " + 1 + " turn");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout(3, 3, 0, 0));


ActionListener listener = new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e) {
           if (e.getSource() instanceof JButton) {
            JButton button = (JButton) e.getSource();
            update(button);
           }
       }
   };

for (int i = 0; i < 9 ; i++) {
jButtons[i] = new JButton(String.valueOf(i));
jButtons[i].setFont(new Font("Arial", Font.PLAIN, 40));
jButtons[i].setForeground(new Color(255, 255, 255, 0));
jButtons[i].setFocusPainted(false);
jButtons[i].addActionListener(listener);
frame.getContentPane().add(jButtons[i]);
}

}


public void update(JButton button) {

int i , player_number = 0 ;

if(player%2 != 0) {
player_number = 1 ;
board[Integer.parseInt(button.getText())] = "O" ;
button.setText("O") ;
}else {
player_number = 2 ;
board[Integer.parseInt(button.getText())] = "X" ;
button.setText("X") ;
}


button.setEnabled(false);

i=checkwin();

player++;

if(i == 1) {
 JOptionPane.showMessageDialog(null,  "Player " + player_number +" win" , "YEAH!!",JOptionPane.INFORMATION_MESSAGE);
}else if(i == 0) {
JOptionPane.showMessageDialog(null,  "Match Draw");
}

if(player%2 != 0) {
player_number = 1 ;
}else {
player_number = 2 ;
}

frame.setTitle("Player " + player_number + " turn");

}

public int checkwin() {
if (board[0].equals(board[1]) && board[1].equals(board[2])) {
jButtons[0].setBackground(Color.CYAN);
jButtons[1].setBackground(Color.CYAN);
jButtons[2].setBackground(Color.CYAN);
return 1;
}
else if (board[3].equals(board[4]) && board[4].equals(board[5])) {
jButtons[3].setBackground(Color.CYAN);
jButtons[4].setBackground(Color.CYAN);
jButtons[5].setBackground(Color.CYAN);
return 1;
}
else if (board[6].equals(board[7]) && board[7].equals(board[8])) {
jButtons[6].setBackground(Color.CYAN);
jButtons[7].setBackground(Color.CYAN);
jButtons[8].setBackground(Color.CYAN);
return 1;
}
else if (board[0].equals(board[3]) && board[3].equals(board[6])) {
jButtons[0].setBackground(Color.CYAN);
jButtons[3].setBackground(Color.CYAN);
jButtons[6].setBackground(Color.CYAN);
return 1;
}
else if (board[1].equals(board[4]) && board[4].equals(board[7])) {
jButtons[1].setBackground(Color.CYAN);
jButtons[4].setBackground(Color.CYAN);
jButtons[7].setBackground(Color.CYAN);
return 1;
}
else if (board[2].equals(board[5]) && board[5].equals(board[8])) {
jButtons[2].setBackground(Color.CYAN);
jButtons[5].setBackground(Color.CYAN);
jButtons[8].setBackground(Color.CYAN);
return 1;
}
else if (board[0].equals(board[4]) && board[4].equals(board[8])) {
jButtons[0].setBackground(Color.CYAN);
jButtons[4].setBackground(Color.CYAN);
jButtons[8].setBackground(Color.CYAN);
return 1;
}
else if (board[2].equals(board[4]) && board[4].equals(board[6])) {
jButtons[2].setBackground(Color.CYAN);
jButtons[4].setBackground(Color.CYAN);
jButtons[6].setBackground(Color.CYAN);
return 1;
}
else if (!board[0].equals("0") && !board[1].equals("1")
                 && !board[2].equals("2") && !board[3].equals("3")
                && !board[4].equals("4") && !board[5].equals("5")
                && !board[6].equals("6") && !board[7].equals("7")
               && !board[8].equals("8") )

return 0;
else
return -1;
}


}

Friday, September 15, 2017

Code by Vasu, How to find Derivative of a polynomial



This code is implemented and provided by Vasu

A code to find Derivative of a polynomial

Note: Before using this code please create a java project and save this class inside DerivativePolynomial.java file 


import java.util.Scanner;

public class DerivativePolynomial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the degree : ");
int degree = sc.nextInt();
System.out.println("Enter the " + (degree+1) + " coefficients " );
int[] coeff = new int[degree+1];
for (int i = 0; i < coeff.length; i++) {
coeff[i] = sc.nextInt();
}
int[] derivativeCoeff = new int[degree] ;
for (int i = 0; i < derivativeCoeff.length;  i++) {
derivativeCoeff[i] = coeff[i]*(degree - i);
}
for (int i = 0; i < derivativeCoeff.length; i++) {
System.out.print(derivativeCoeff[i] + "*x^" + (degree - i) );
if(i != derivativeCoeff.length-1 && derivativeCoeff[i+1] > 0  ) {
System.out.print("+");
}
}
}

}

Output of Code =>

Enter the degree : 3
Enter the 4 coefficients 
20
12
10
30
60*x^3+24*x^2+10*x^1