Wednesday, January 24, 2018

Solution for the assignment array merger

Note : Save this class inside ArrayMerger.java file under java project in Eclipse IDE

import java.util.Scanner;

public class ArrayMerger
{
public static void main(String[] args)
{
// create object of scanner to get data from keyboard
Scanner s = new Scanner(System.in);

System.out.print("Enter size for firstArray:");
int size1 = s.nextInt();

// create an array of given size
int[] firstArray = new int[size1];

// loop to store data inside array using for loop
for (int i = 0; i < firstArray.length; i++)
{
System.out.print("Input value for index "+i+" of firstArray : ");
// get a value from keyboard and store it inside array
firstArray[i] = s.nextInt();
}

// show a message
System.out.println("** firstArray initialized **");

System.out.print("Enter size for secondArray:");
int size2 = s.nextInt();

// create an array of given size
int[] secondArray = new int[size2];

// loop to store data inside secondArray using for loop
for (int i = 0; i < secondArray.length; i++)
{
System.out.print("Input value for index "+i+" of secondArray : ");
// get a value from keyboard and store it inside array
secondArray[i] = s.nextInt();
}

// show a message
System.out.println("** secondArray initialized **");

// show a menu
System.out.println("## Menu ##");
System.out.println("Type 1 to merge secondArray after firstArray");
System.out.print("Type 2 to merge firstArray after secondArray =>");

// get a value from keyboard and store it as a choice
int choice = s.nextInt();

// create a new array, its size is equal to the size of firstArray plus size of secondArray
int[] thirdArray = new int[firstArray.length + secondArray.length];

// check choice
if(choice == 1)
{
// this variable will be used as an index for thisrArray
int index = 0;

// fetch data from firstArray and store it inside thirdArray
for (int i = 0; i < firstArray.length; i++)
{
thirdArray[index] = firstArray[i];

// increase the index of thirdArray
index++;
}

// fetch data from secondArray and store it inside thirdArray
for (int i = 0; i < secondArray.length; i++)
{
thirdArray[index] = secondArray[i];

// increase the index of thirdArray
index++;
}
}
else
if(choice == 2)
{
// this variable will be used as an index for thirdArray
int index = 0;

// fetch data from firstArray and store it inside thirdArray
for (int i = 0; i < secondArray.length; i++)
{
thirdArray[index] = secondArray[i];

// increase the index of thirdArray
index++;
}

// fetch data from secondArray and store it inside thirdArray
for (int i = 0; i < firstArray.length; i++)
{
thirdArray[index] = firstArray[i];

// increase the index of thirdArray
index++;
}
}
else
{
System.out.println("invalid choice");
}

// show thirdArray using for loop
for (int i = 0; i < thirdArray.length; i++)
{
System.out.println("index :"+i+", element :"+thirdArray[i]);
}

}
}

Solution for the assignments find number in array

Note : Save this class inside FindInArray.java file under java project in Eclipse IDE

import java.util.Scanner;

public class FindInArray
{
public static void main(String[] args)
{
// create object of scanner to get data from keyboard
Scanner s = new Scanner(System.in);

System.out.print("Enter size for array:");
int size = s.nextInt();

// create an array of given size
int[] array = new int[size];

// loop to store data inside array using for loop
for (int i = 0; i < array.length; i++)
{
System.out.print("Input value for index " + i + " of array : ");
// get a value from keyboard and store it inside array
array[i] = s.nextInt();
}

// show a message
System.out.println("** array initialized **");

// show array using for loop
System.out.println("\nArray is given below ==>");
for (int i = 0; i < array.length; i++)
{
System.out.print("array[" + i + "] = " + array[i] + ", ");
}

// show a message to input a number
System.out.print("\n\nInput a number to search inside array :");

// input int value from keyboard and store it
int number = s.nextInt();

// show a menu
System.out.println("\n\n## Menu ##");
System.out.println("1 = to find first index of "+number+" inside array");
System.out.println("2 = to find last index of "+number+" inside array");
System.out.print("3 = to find all indexes of "+number+
                " inside array also its frequency =>");

// input int value from keyboard and store it
int choice = s.nextInt();

// this variable will keep track if a number is found in array or not
// i will store 1 inside this variable if number is found else it will store 0
int found = 0;

System.out.println("\n\n## Result ## \n");

if (choice == 1)
{
// fetch data from array using for loop
for (int i = 0; i < array.length; i++)
{
// check if number is equal to the i'th element of array
if (number == array[i])
{
// show the index at which number was found
System.out.println("First index of " + number + " inside array is " + i);

// store 1 in found since number is found in array
found = 1;

// terminate the loop using break keyword
break;
}
}

if(found == 0)
{
System.out.println(number+" not found in array");
}

}
else
if (choice == 2)
{
// fetch data from array using for loop from the last index
for (int i = array.length - 1; i >= 0; i--)
{
// check if number is equal to the i'th element of array
if (number == array[i])
{
// show the index at which number was found
System.out.println("Last index of " + number + " inside array is " + i);

// store 1 in found since number is found in array
found = 1;

// terminate the loop using break keyword
break;
}
}

if(found == 0)
{
System.out.println(number+" not found in array");
}

}
else if (choice == 3)
{
// this variable will keep frequency of number inside array
int counter = 0;

// fetch data from array using for loop
for (int i = 0; i < array.length; i++)
{
// check if number is equal to the i'th element of array
if (number == array[i])
{
// show the index at which number was found
System.out.println("Index of " + number + " inside array is " + i);

// store 1 in found since number is found in array
found = 1;

// increase the counter
counter++;
}
}

if(found == 1)
{
System.out.println("frequency of " + number + " in array is " + counter);
}
else
{
System.out.println(number+" not found in array");
}

}
else
{
System.out.println("##### invalid choice");
}

}
}

Monday, January 22, 2018

Solutions for the problems of 22-Jan-2018

Note : Save this class inside Rectangle.java file

import java.util.Scanner;

public class Rectangle
{
// non static data members
int width;
int height;

// define a no argument constructor
public Rectangle()
{
System.out.println("object of rectangle created by no arg cnst");
}

// define a parameterized constructor
public Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}

// define a non static method to show the data of rectangle
public void showRectangleInfo()
{
System.out.println("Rectangle info =>");
System.out.println("Width : "+width);
System.out.println("Height : "+height);

// get area of rectangle and store it
int area = width * height;
System.out.println("Area : "+area);

System.out.println(); // print a new line
}

// define main method
public static void main(String[] args)
{
/// create object of scanner to get data from keyboard
Scanner s = new Scanner(System.in);

System.out.print("Input width of rectangle :");
// get input from keyboard and store it inside local variables
int wi = s.nextInt();

System.out.print("Input width of rectangle :");
// get input from keyboard and store it inside local variables
int hi = s.nextInt();

// create object of rectangle by using parameterized constructor
// inside this constructor pass the values those were taken from the keyboard
Rectangle r = new Rectangle(wi, hi);

// show the information of rectangle using the method
r.showRectangleInfo();

}
}

===================================================

Note : Save this class inside Circle.java file

import java.util.Scanner;

public class Circle
{
// non static data member
double radius;

// define a no argument constructor
public Circle()
{
System.out.println("object of Circle created by no arg cnst");
}

// define a parameterized constructor
public Circle(double radius)
{
this.radius = radius;
}

// define a non static method to show the data of Circle
public void showCircleInfo()
{
System.out.println("## Circle info ##");
System.out.println("Radius : "+radius);
// get circumference of circle and store it
double cir = 2.0 * 3.14 * radius;
System.out.println("Circumference : "+cir);
// get diameter of circle and store it
double dia = 2.0 * radius;
System.out.println("Diameter : "+dia);
// get area of circle and store it
double area = 3.14 * radius * radius;
System.out.println("Area : "+area);
System.out.println();
}

// define main method
public static void main(String[] args)
{
/// create object of scanner to get data from keyboard
Scanner s = new Scanner(System.in);

System.out.print("Input radius of Circle :");
// get input from keyboard and store it inside local variables
double ra = s.nextDouble();

// create object of Circle by using parameterized constructor
// inside this constructor pass the value which is taken from the keyboard
Circle r = new Circle(ra);

// show the information of Circle using the method
r.showCircleInfo();
}
}

===================================================

Note : Save this class inside Square.java file

import java.util.Scanner;

public class Square
{
// non static data member
long side;

// define a no argument constructor
public Square()
{
System.out.println("object of Square created by no arg cnst");
}

// define a parameterized constructor
public Square(long side)
{
this.side = side;
}

// define a non static method to show the data of Square
public void showSquareInfo()
{
System.out.println("## Square info ##");
System.out.println("Side : "+side);
// get area of Square and store it
long area = side * side;
System.out.println("Area : "+area);
System.out.println();
}

// define main method
public static void main(String[] args)
{
/// create object of scanner to get data from keyboard
Scanner s = new Scanner(System.in);

System.out.print("Input side of Square :");
// get input from keyboard and store it inside local variables
long si = s.nextLong();

// create object of Square by using parameterized constructor
// inside this constructor pass the value which is taken from the keyboard
Square r = new Square(si);

// show the information of Square using the method
r.showSquareInfo();
}
}

Friday, January 19, 2018

Very important information asked frequently in interviews

Facts related to the switch statement in Java programming language.

1. We can not pass float, double, boolean, or long type value or variable inside switch
Only byte, short, int, char, and String is permissible 

Example :

long x = 1L;

float y = 2.0f;

int z = 3;

String str = "hello";

switch(x) <== this is wrong

switch(y) <== this is also wrong

switch(z) <== but, this is correct

switch(str) <== this is also correct

2. We can pass Object name of Integer or Character class inside switch (but the JDK must be 1.5 or above).
     Reason : The Integer will be translated to int (primitive) before using inside switch

 Example :

  Integer ref = 1;
 
  switch(ref) <== this is correct

 3. Only constant can be used along with case keyword.

 Example :

  int number = 1;
  final int choiceOne = 1; <== choiceOne is a constant
  int choiceTwo = 2; <== choiceTwo is a variable
 
switch(number)
{
case choiceOne :  <== this is correct, since we can use a constant 
                along with case
case choiceTwo :  <== this is not correct, since we can not use 
                a variable along with case, it is compiler error
}

4. Order of case of default can be anything, means the default can be written before the case

switch (key)
{
default:
break;

case value:
break;
}

Note : Create a java project in Eclipse IDE and save this class in TheSwitch.java file

public class TheSwitch
{
public static void main(String[] args)
{
int number = 2;
final int choiceOne = 1;
final int choiceTwo = 2;

switch (number)
{
case choiceOne:
System.out.println("case one executes");
break;

case choiceTwo:
System.out.println("case two executes");
break;

default :
System.out.println("default segment executes");
}
}
}

Thursday, January 18, 2018

Print diamond using two for loops only

This code is implemented and provided by Ajit Srivastava from Gniet college, Greater Noida.

Note : Create a Java project in Eclipse IDE and save this class inside PrintDiamondUsingTwoLoops.java file.

import java.util.Scanner;

public class PrintDiamondUsingTwoLoops
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter the number of terms for the Diamond pattern :");
int term = s.nextInt(); // fetch int value from the keyboard and store

int copyOfTerm = term;

for (int i = 1; i < 2 * term; i++)
{
for (int j = 1; j <= (term + 1); j++)
{
if (j <= copyOfTerm)
{
System.out.print(" ");
}
else
{
System.out.print("* ");
}

} // end  of inner for loop

System.out.println();

if (i < term)
{
copyOfTerm--;
}
else
{
copyOfTerm++;
}

} // end of outer for loop

} // end of main method

} // end of class

Tuesday, January 9, 2018

Solution assignment 002

public class SolutionLoop2
{
public static void main(String[] args)
{
int start = 10;
int end = 20;
int number = 1;
int choice = 0;
int flag = 0;
int counter = 0;

System.out.println("## Numbers from "+start+" to "+end+" are =>");
for(int i = start; i <= end; i++)
{
for(int j = i; j != 0; j = j / 10)
{
int temp = j % 10;
if(temp == number)
{
flag = 1;
}
}
if(flag == choice)
{
System.out.println(i);
counter++;
}
flag = 0;
}
System.out.println("## From "+start+" to "+end+" total numbers are "+counter);
}
}

Solution for the Assignment 001

Define a class inside java project under eclipse and save this code

public class Solution1
{
public static void main(String[] args)
{

long number = 54321L;

// find no of digits in this number
int digits = 0;
for(long i = number; i != 0; i = i / 10)
{
digits++;
}
System.out.println("## No of digits in "+number+" are "+digits);


// find max value and its index
long max = number % 10;
int in = 1;
int maxIndex = 1;

for(long i = number; i != 0; i = i / 10)
{
long temp = i % 10;
if(max < temp)
{
max = temp;
maxIndex = in;
}
in++;
}
int index = digits - maxIndex;
System.out.println("## Maximum value in "+number+" is "+max+" found at "+index);

// find min value and its index
long min = number % 10;
int in2 = 1;
int minIndex = 1;

for(long i = number; i != 0; i = i / 10)
{
long temp = i % 10;
if(min > temp)
{
min = temp;
minIndex = in2;
}
in2++;
}
int index2 = digits - minIndex;
System.out.println("## Minimum value in "+number+" is "+min+" found at "+index2);

// find sum of all digits
long sum = 0L;
for(long i = number; i != 0; i = i / 10)
{
long temp = i % 10;
sum = sum + temp;
}
System.out.println("## Sum of all digits are "+sum);


// reverse number and store inside a variable
long rev = 0L;
for(long i = number; i != 0; i = i / 10)
{
long temp = i % 10;
rev = rev * 10 + temp;
}
System.out.println("## Reverse of "+number+" is "+rev);
}
}