Tuesday, 30 August 2016

JUnit test cases

----------------------------------------------------------------------------------------

public class junitTest {

private String msg;
public junitTest(String msg)
{
this.msg=msg;
}

    public String printmsg()
    {
    System.out.println(msg);
    return msg;
    }
 
}
-----------------------------------------------------------------------------------


import static org.junit.Assert.*;

import org.junit.Test;




public class junitTestTest {
    
String msg="sree";
junitTest obj = new junitTest(msg);
@Test
public void testing()
{
assertEquals(msg,obj.printmsg());
}
}

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


public class MyClass {

  public int multiply(int x ,int y)

  {
  if(x>10)
  throw new IllegalArgumentException("x should be less than 10");
 
  return x*y;
 
  }
}


-------------------------------------------------------------------------------

import static org.junit.Assert.*;
import org.junit.Test;


public class MyClassTest {


@Test
  public void TestMultiply()
{
 
  MyClass test = new MyClass();
     System.out.println(test.multiply(2,3));
     assertEquals(45,test.multiply(5,9));
   
 
}  

}

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





Monday, 29 August 2016

Java.util Package Classes

Java.util.ArrayList



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

import java.util.ArrayList;



public class arrayList {


public static void main(String args[])
{
ArrayList<String> mylist= new ArrayList<String>();
mylist.add("sree");
mylist.add("krishna");
for(String str:mylist)
System.out.println(str);
}
}

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

import java.util.ArrayList;


class arrayList{
public static void main(String args[])
{
        ArrayList<Integer> mylist= new ArrayList<Integer>();
        mylist.add(9);
        mylist.add(6);
        mylist.add(1);
        
            for(int x:mylist)
        System.out.println(x);
}
}

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

import java.util.ArrayList;

class arrayList{
public static void main(String args[])
{
ArrayList<Integer> mylist = new ArrayList<Integer>(5);
mylist.add(1);
mylist.add(2);
mylist.add(4);
mylist.add(5);
for(int x:mylist)
System.out.println(x);
System.out.println("===========");
mylist.add(2,3);  //index starts from zer0
for(int x: mylist)
System.out.println(x);
}
 
}

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




import java.util.ArrayList;

class arrayList{
public static void main(String args[])
{
ArrayList<Integer> list1= new ArrayList<Integer>(5);
list1.add(1);
list1.add(2);
list1.add(3);
for(Integer x: list1)
System.out.println(x);
      System.out.println("===============");
      
      
      
   
      
   ArrayList<Integer> list2 =new ArrayList<Integer>(5);
   list2.add(4);
   list2.add(5);
   list2.add(6);
   
   
      
   
   for(Integer y:list2)
    System.out.println(y);
   System.out.println("===============");
   list1.addAll(list2);
   for(Integer x: list1)
System.out.println(x);
      System.out.println("===============");
      for(Integer y:list2)
    System.out.println(y);
   System.out.println("===============");
   
}

}




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



import java.util.ArrayList;

class arrayList{
public static void main(String args[])
{
ArrayList<Integer> list1= new ArrayList<Integer>(5);
list1.add(1);
list1.add(5);
list1.add(6);
for(Integer x: list1)
System.out.println(x);
      System.out.println("===============");
      
      
      
   
      
   ArrayList<Integer> list2 =new ArrayList<Integer>(5);
   list2.add(2);
   list2.add(3);
   list2.add(4);
   
   
      
   
   for(Integer y:list2)
    System.out.println(y);
   System.out.println("===============");
   list1.addAll(1,list2);
   
   for(Integer y:list1)
    System.out.println(y);
   System.out.println("===============");
   
}

}


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

import java.util.ArrayList;

public class arrayList{
public static void main(String args[])
{
ArrayList<Integer> mylist = new ArrayList<Integer>();
 
mylist.add(2);
mylist.add(5);
mylist.add(6);
System.out.println("size of the list: "+mylist.size());
mylist.clear();
System.out.println("size of the list: "+mylist.size());

}
}

======================================================================
import java.util.ArrayList;

public class arrayList{
public static void main(String args[])
{
ArrayList<Integer> mylist =new ArrayList<Integer>(5);
mylist.add(5);
mylist.add(9);
mylist.add(21);
boolean isThere;
isThere=mylist.contains(9);
System.out.println(isThere);
}
}



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

import java.util.ArrayList;

public class arrayList{
public static void main(String args[])
{
ArrayList<Integer> mylist =new ArrayList<Integer>(5);
mylist.add(5);
mylist.add(9);
mylist.add(21);
mylist.add(34);
boolean isThere;
isThere=mylist.contains(9);
System.out.println(isThere);
System.out.println(mylist.size());
  int res=mylist.get(2);
  System.out.println("res: "+res);
}
}


======================================================================
import java.util.ArrayList;

public class arrayList{
public static void main(String args[])
{
ArrayList<String> mylist = new ArrayList<String>(5);
mylist.add("a");
mylist.add("b");
mylist.add("c");
mylist.add("d");
int res= mylist.indexOf("b");
System.out.println(res);
}

}



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

import java.util.ArrayList;

public class arrayList{
public static void main(String args[])
{
ArrayList<String> mylist = new ArrayList<String>(5);
mylist.add("a");
mylist.add("b");
mylist.add("c");
mylist.add("d");
mylist.add("c");
mylist.add("b");
mylist.add("c");
mylist.add("d");
int res= mylist.indexOf("b");
System.out.println(res);
boolean isEmpty;
isEmpty = mylist.isEmpty();
System.out.println(isEmpty);
for(String x:mylist)
System.out.println(x);
        int lastIndex;
lastIndex=mylist.lastIndexOf("c");
System.out.println(lastIndex);
}

}


======================================================================
import java.util.ArrayList;

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

ArrayList<Integer> mylist =new ArrayList<Integer>(5);
mylist.add(5);
mylist.add(9);
mylist.add(4);
System.out.println("size of the list: "+mylist.size());
for(Integer x: mylist)
{ System.out.print(x);System.out.print(",");
}    
mylist.remove(1);
System.out.println("size of the list: "+mylist.size());
for(Integer x: mylist)
{ System.out.print(x);System.out.print(",");
}    

}

}


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

java.util.HashMap



import java.util.HashMap;

public class hashMap {


public static void main(String args[])
{
HashMap mymap = new HashMap();

mymap.put(1, "apple");
mymap.put(2, "samsung");
mymap.put(3,"nokia");

System.out.println(mymap);

mymap.clear();

System.out.println(mymap);

}


}


======================================================================
import java.util.HashMap;

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

    HashMap mymap = new HashMap();
    HashMap secnd = new HashMap();
 
     mymap.put(1, "english");
     mymap.put(2, "hindi");
     mymap.put(3, "telugu");
   
     System.out.println(mymap);
 
       secnd=(HashMap)mymap.clone();
   
      System.out.println(secnd);

}

}


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

import java.util.HashMap;

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

    HashMap mymap = new HashMap();

 
     mymap.put(1, "english");
     mymap.put(2, "hindi");
     mymap.put(3, "telugu");
     mymap.put(5, "");
   
      System.out.println(mymap.containsKey(2));  //checks if key present
      System.out.println(mymap.containsValue("hindi"));  // checks if value present
   
   
   
   
   
 

}

}


======================================================================
import java.util.HashMap;
import java.util.Set;


public class hashMap{


public static void main(String args[])
{

HashMap test =new HashMap();

test.put(1, "C");
test.put(2, "c++");
test.put(3,"Java");

  Set myset=test.entrySet();

  System.out.println(myset);   //entry set
   
  System.out.println(test);    // printing normal map


}



}



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



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




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


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




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












Java.util Package Classes

Java.util.ArrayList



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

import java.util.ArrayList;



public class arrayList {


public static void main(String args[])
{
ArrayList<String> mylist= new ArrayList<String>();
mylist.add("sree");
mylist.add("krishna");
for(String str:mylist)
System.out.println(str);
}
}

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

import java.util.ArrayList;


class arrayList{
public static void main(String args[])
{
        ArrayList<Integer> mylist= new ArrayList<Integer>();
        mylist.add(9);
        mylist.add(6);
        mylist.add(1);
        
            for(int x:mylist)
        System.out.println(x);
}
}

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

import java.util.ArrayList;

class arrayList{
public static void main(String args[])
{
ArrayList<Integer> mylist = new ArrayList<Integer>(5);
mylist.add(1);
mylist.add(2);
mylist.add(4);
mylist.add(5);
for(int x:mylist)
System.out.println(x);
System.out.println("===========");
mylist.add(2,3);  //index starts from zer0
for(int x: mylist)
System.out.println(x);
}
 
}

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




import java.util.ArrayList;

class arrayList{
public static void main(String args[])
{
ArrayList<Integer> list1= new ArrayList<Integer>(5);
list1.add(1);
list1.add(2);
list1.add(3);
for(Integer x: list1)
System.out.println(x);
      System.out.println("===============");
      
      
      
   
      
   ArrayList<Integer> list2 =new ArrayList<Integer>(5);
   list2.add(4);
   list2.add(5);
   list2.add(6);
   
   
      
   
   for(Integer y:list2)
    System.out.println(y);
   System.out.println("===============");
   list1.addAll(list2);
   for(Integer x: list1)
System.out.println(x);
      System.out.println("===============");
      for(Integer y:list2)
    System.out.println(y);
   System.out.println("===============");
   
}

}




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



import java.util.ArrayList;

class arrayList{
public static void main(String args[])
{
ArrayList<Integer> list1= new ArrayList<Integer>(5);
list1.add(1);
list1.add(5);
list1.add(6);
for(Integer x: list1)
System.out.println(x);
      System.out.println("===============");
      
      
      
   
      
   ArrayList<Integer> list2 =new ArrayList<Integer>(5);
   list2.add(2);
   list2.add(3);
   list2.add(4);
   
   
      
   
   for(Integer y:list2)
    System.out.println(y);
   System.out.println("===============");
   list1.addAll(1,list2);
   
   for(Integer y:list1)
    System.out.println(y);
   System.out.println("===============");
   
}

}


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

import java.util.ArrayList;

public class arrayList{
public static void main(String args[])
{
ArrayList<Integer> mylist = new ArrayList<Integer>();
 
mylist.add(2);
mylist.add(5);
mylist.add(6);
System.out.println("size of the list: "+mylist.size());
mylist.clear();
System.out.println("size of the list: "+mylist.size());

}
}

======================================================================
import java.util.ArrayList;

public class arrayList{
public static void main(String args[])
{
ArrayList<Integer> mylist =new ArrayList<Integer>(5);
mylist.add(5);
mylist.add(9);
mylist.add(21);
boolean isThere;
isThere=mylist.contains(9);
System.out.println(isThere);
}
}



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

import java.util.ArrayList;

public class arrayList{
public static void main(String args[])
{
ArrayList<Integer> mylist =new ArrayList<Integer>(5);
mylist.add(5);
mylist.add(9);
mylist.add(21);
mylist.add(34);
boolean isThere;
isThere=mylist.contains(9);
System.out.println(isThere);
System.out.println(mylist.size());
  int res=mylist.get(2);
  System.out.println("res: "+res);
}
}


======================================================================
import java.util.ArrayList;

public class arrayList{
public static void main(String args[])
{
ArrayList<String> mylist = new ArrayList<String>(5);
mylist.add("a");
mylist.add("b");
mylist.add("c");
mylist.add("d");
int res= mylist.indexOf("b");
System.out.println(res);
}

}



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

import java.util.ArrayList;

public class arrayList{
public static void main(String args[])
{
ArrayList<String> mylist = new ArrayList<String>(5);
mylist.add("a");
mylist.add("b");
mylist.add("c");
mylist.add("d");
mylist.add("c");
mylist.add("b");
mylist.add("c");
mylist.add("d");
int res= mylist.indexOf("b");
System.out.println(res);
boolean isEmpty;
isEmpty = mylist.isEmpty();
System.out.println(isEmpty);
for(String x:mylist)
System.out.println(x);
        int lastIndex;
lastIndex=mylist.lastIndexOf("c");
System.out.println(lastIndex);
}

}


======================================================================
import java.util.ArrayList;

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

ArrayList<Integer> mylist =new ArrayList<Integer>(5);
mylist.add(5);
mylist.add(9);
mylist.add(4);
System.out.println("size of the list: "+mylist.size());
for(Integer x: mylist)
{ System.out.print(x);System.out.print(",");
}    
mylist.remove(1);
System.out.println("size of the list: "+mylist.size());
for(Integer x: mylist)
{ System.out.print(x);System.out.print(",");
}    

}

}


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

java.util.HashMap



import java.util.HashMap;

public class hashMap {


public static void main(String args[])
{
HashMap mymap = new HashMap();

mymap.put(1, "apple");
mymap.put(2, "samsung");
mymap.put(3,"nokia");

System.out.println(mymap);

mymap.clear();

System.out.println(mymap);

}


}


======================================================================
import java.util.HashMap;

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

    HashMap mymap = new HashMap();
    HashMap secnd = new HashMap();
   
     mymap.put(1, "english");
     mymap.put(2, "hindi");
     mymap.put(3, "telugu");
   
     System.out.println(mymap);
 
       secnd=(HashMap)mymap.clone();
     
      System.out.println(secnd);

}

}


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

import java.util.HashMap;

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

    HashMap mymap = new HashMap();

   
     mymap.put(1, "english");
     mymap.put(2, "hindi");
     mymap.put(3, "telugu");
     mymap.put(5, "");
   
      System.out.println(mymap.containsKey(2));  //checks if key present
      System.out.println(mymap.containsValue("hindi"));  // checks if value present
     
     
   
   
   
   

}

}


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




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













Wednesday, 24 August 2016

Java methods

1)


-----------------------------------------------------------------


public class Employee {

String name;
int age;
String desg;
double sal;

   
public Employee(String name)
{
this.name=name;

}
public void empAge(int age)
{
this.age=age;
}

public void empDesg(String desg)
{
this.desg=desg;

}

  public void empSal(double sal)
  {
  this.sal=sal;

  }


void printEmployee()
{
System.out.println("Name : "+name);
System.out.println("Age : "+age);
System.out.println("Desg : "+desg);
System.out.println("Salary : "+sal);

}




}

------------------------------------------------------------



public class EmployeeTest{

public static void main(String args[])
{

Employee empOne=new Employee("sree");

empOne.empAge(24);
empOne.empDesg("developer");
empOne.empSal(11280.50);
empOne.printEmployee();

Employee empTwo=new Employee("krishna");

empTwo.empAge(21);
empTwo.empDesg("soft developer");
empTwo.empSal(11580);
empTwo.printEmployee();

}



}



----------------------------------------------------------------------------------------------------------------------

2)


public class FindMin {

public static void main(String args[])
{
int a,b,c;
a=5;
b=9;
c=minfn(a,b);
System.out.println("in main:"+c);

}

public static int minfn(int x, int y)
{
int min;
if (x>y)
min =y;
else min=x;
System.out.println(min);
return min;

}


}


--------------------------------------------------------------------------------------------------------------


3)



public class methodOver {

public static void main(String args[])
{
int a=3,b=4;

double c=1.3,d=3.5;


int res=minfn(a,b);

double resy=minfn(c,d);

System.out.println("int:"+res);
System.out.println("double: "+resy);
}
 public static int minfn(int x,int y)
  {
 int min;
 if (x>y)
 min=y;
 else min=x;
 
 return min;
 
  }

public static double minfn(double a,double b)
{
double min;
  //a>b?min=b:min=a;
if(a>b)
min=b;
else min=a;
return min;
}
}


-------------------------------------------------------------------------------------------

4)







Tuesday, 9 August 2016

Stirng builder


public class HelloWorld {

    public static void main(String args[])
    {

        // String Builders

        StringBuilder sb= new StringBuilder();
        sb.append("Hello");
        System.out.println(sb);

        //string object can't modified        //string builder modifies original string
          StringBuilder s1=new StringBuilder("hello again!");
         System.out.println(s1);


        // specify intial capacity 5 chars in string build
         StringBuilder s2=new StringBuilder(5);
        System.out.println(s2);
        s2.append("hey back to java programming");
        System.out.println(s2);

        //capacity is flexible
        //find length        System.out.println(s2.length());


        //concatination          StringBuilder s3=new StringBuilder("Hello");
          s3.append("world");
        System.out.println(s3);

        s3.append("!");
        System.out.println(s3);



        // methods
        StringBuilder s4=new StringBuilder("Hello again");
        System.out.println(s4);


       //insert
        s4.insert(5,"2");
        System.out.println(s4);


        //replace
        s4.replace(5,s4.length()," world");
        System.out.println(s4);

        //delete        s4.delete(5,s4.length());
        System.out.println(s4);


        s4.append("abcdef ghijkl");
        System.out.println(s4);

        s4.delete(8,14);        /// Helloabcdef ghijkl        System.out.println(s4); // Helloabcijkl
        // delete char at
        System.out.println(s4.length());
          //s4.deleteCharAt(s4.lenght());

        //java.lang.StringBuilder.deleteCharAt         // here length is 12: indexes(0->11)        /*        s4.deleteCharAt(12);        System.out.println(s4);          */
        s4.deleteCharAt(11);
        System.out.println(s4);


           String s5="Hello world";
          for(int i=0;i<s5.length();i++)
          {
              System.out.println(s5.charAt(i));

          }




        String s6= "Hello from ";

        /// it won't modify the existing string by concatination        //we need to store the info in other string.
          String s8=s6.concat("sree");
        StringBuilder s7 = new StringBuilder("hey sree ");
        s7.append("wzzup");

        System.out.println(s6);
        System.out.println(s7);
        System.out.println(s8);


        s7.deleteCharAt(0);
        System.out.println(s7);

        //insert        s7.insert(0,"hie h");
        System.out.println(s7);

        // delete
         s7.delete(0,3);
        System.out.println(s7);


        /// repalce
          s7.replace(0,3,"hieee");
        System.out.println(s7);




        //converting to string
        int no=1024;
        String text=String.valueOf(no);
        System.out.println(text);

         System.out.println(text.substring(0,1));


          String flt=String.valueOf(24.56f);
          String dbl=String.valueOf(24.56);
          String bool=String.valueOf(true);

        System.out.println(flt);
        System.out.println(dbl);
        System.out.println(bool);


         String num="12345";
        System.out.println(num);
         int nob=Integer.parseInt(num);
        System.out.println(nob);

         //doble.parsedouble        //float.parse        //Boolean.parse

    }
}