Wednesday, January 27, 2016

Calculate Cube And Square Program in Java

This is a simple program to print a square or cube in java .
First take the input from the user using Scanner class object then check the user input is valid or not using if condition if input is valid than the code which is written inside the if condition will be work otherwise it will print the statement (invalid input) from else.
equalsIgnoreCase() is present in string class it ignore the uppercase or lowercase input it will treat same (a or A) .
After getting a valid input from the user it will again check either input is s or q if input is s than  first if condition will work and if input is c than second .
Than it will ask to type a no. when user will type the no. it will go to the method and return the square or cube than using  System.out.println("square of "+n+" is =  "+square(n)); or  System.out.println("cube of "+n+" is =  "+cube(n));  line it will print the output .





public class SquareAndCube
 {
public static void main(String[] args)
       {
Scanner scn =new Scanner(System.in);
System.out.println("Press s for square OR q for cube");
String s=scn.next();
if(s.equalsIgnoreCase("s") ||s.equalsIgnoreCase("q"))
{
if(s.equalsIgnoreCase("s"))
{
System.out.println(" Type no. to find square");
int n=scn.nextInt();
System.out.println("square of "+n+" is =  "+square(n));
}
else if(s.equalsIgnoreCase("q"))
{
System.out.println("Type no. to find cube");
int n=scn.nextInt();
System.out.println("cube of "+n+" is =  "+cube(n));
}

}
else
                   {
System.out.println("invalid input .. Please enter either s or q");
}

}
public static int square(int n)
  {
return n*n;
  }
  public static int cube(int n)
  {
return n*n*n;
  }

}

Print A Table

Its a simple table printing program in java . in this program using Scanner class to get the input from user at runtime.
n is the number which will provided by user.
now for loop will run from 1 to 10 and inside the for loop we are multiplying n with i . i which will run from 1 to 10 and when i<=10 loop will stop .




public class PrintTable

{
public static void main(String[] args)

      {
Scanner scn=new Scanner(System.in);
System.out.println(" type a no. to get Table");
int n=scn.nextInt();
for(int i=1;i<=10;i++)
{
System.out.println(n+"*"+i+"="+i*n);
}
}

}

Thursday, January 14, 2016

Store Elements From ArrayList To Array

Cwonvert an ArrayList To an Array is a most common question and somtimes we need to store an arraylist elements in an array . to convert an ArrayList to an array a method present in Collection fraimwork  toArray() .
In this program first  i am creating an arraylist than printing that ArrayList .al is the refrance of ArrayList.
than taking on Object[] to store the elements of ArrayList in the array than using toArray() method converting ArrayList to Array .

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ArrayListToArray {

public static void main(String[] args) {

List al=new ArrayList();

al.add(5);
al.add("abc");
al.add(65);
al.add('x');

Iterator itr=al.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
System.out.println(" after changing to array");
Object[] ar=al.toArray();
for(Object a:ar)
{
System.out.println(a);
}
}

}

Wednesday, January 13, 2016

Custome Sorting Using Compare Method Of Comparator interface in Java

Comparator Interface has two methods compare(Obj o1, Obj o2) and equals(). equals method is also available in Object Class . so we override only compare() for custome shorting of multiple objects. in compare() we can pass multiple objects for comparision .

In this example i am sorting base on name, age and salary . so creating seprate class for every sorting criteria.


import java.util.*;


class Empl
{
String name;
int age;
double sal;
Empl(String name, int age, double sal)
{
this.name=name;
this.age=age;
this.sal=sal;
}
public String toString()
{
return "{"+name+", "+age+", "+sal+"}";
}

  }
class AgeSorter implements Comparator
{
public int compare(Object o1, Object o2)
{
return  ((Empl)o1).age-((Empl)o2).age;
}
}

class SalSorter implements Comparator
{
public int compare( Object o1, Object o2)
{
double s1=((Empl)o1).sal;
double s2=((Empl)o2).sal;
if(s1>s2)
{
   return 1;
}
else if (s1<s2)
{
return -1;
}
 
            else
            {
  return 0;
             }
}
}

class NameSorter implements Comparator
{
public  int compare(Object o1, Object o2)
{
return ((Empl)o1).name.compareTo(((Empl)o2).name);
}
}

public class TwoObjCompare
{
public static void main(String args[])
{
Empl[] e=new Empl[4];
e[0]=new Empl("Ajay", 32,234232);
e[1]=new Empl("Vijay", 53, 34324);
e[2]=new Empl("Pawan",34,35532);
e[3]=new Empl("Ram",43,45355.32);
for(Empl i:e)
{
System.out.println(i);
}
Scanner scn=new Scanner(System.in);
System.out.println("type sorting option (name/age/sal)");
String s=scn.next();
if(s.equals("age"))
{
Arrays.sort(e, new AgeSorter());
}
else if(s.equals("sal"))
{
Arrays.sort(e, new SalSorter());
}
else
{
Arrays.sort(e, new NameSorter());
}
System.out.println("after sorting "+s);
for(Empl i:e)
{
System.out.println(i);
}
}

}

Monday, January 11, 2016

Reverse The Words From String In JAVA

This is the most common Question from string to reverse the word from a string for exmp: "Hello World" will be like "olleh dlrow".


import java.util.Scanner;

public class ReverseWordsOfString {

public static void main(String[] args) {
System.out.println("Enter your String :");
Scanner scanobj = new Scanner(System.in);

char ch;
char c ;

for(;;)
{
String s1 = scanobj.next();

for (int i = 0; i < s1.length(); i++)
{
c = s1.charAt(i);
}
   System.out.println();

for (int x = s1.length()-1; x >= 0; x--)
{
ch = s1.charAt(x);
System.out.print(ch);

}


}
}
}

Enter your String :
hello world

olleh
dlrow