Showing posts with label LAB PROGRAMS. Show all posts
Showing posts with label LAB PROGRAMS. Show all posts

Saturday, 16 July 2011

19.07.2011 and 21.07.2011 LAB PROGRAMS


UNIVERSITY SYLLABUS:
EX NO:2
1.Design a Date class similar to the one provided  in the java.util package.
(First you should finish that progam,then only you can go to next program):
(Search the Date class methods in Internet  and try.First define get method and set the method value into setmethod.Dvelope a code to check whether the given date is actual date or before or  after the date.Here the algorithm for DATE class program.)
ALGORITHIM:
step 1:Define the class MyDate with members date,month,year,hour,min,sec.
step 2:USing a default constructor,set the values using the predefined Dateclass.
step 3:use parameterised constructors to set the values for the members.
step 4:Define the methods getYear(),getMonth(),getDate(),getHours(),getMinutes(),getSeconds to return the year,month,date,hours,minutes and seconds.
step 5:Define the methods setYear(),setMonth(),setDate(),setHours(),setMinutes(),setSeconds() to set the values for year,month,date,hours,minutes and seconds.and also get the date,year,month,time,sec using Scanner.
step 6:Define the method after() with the return type boolean to check whether the given date is after the current date.
step 7:Define the method before() with the return type boolean to check whether the given date is before the current date.
step 8:Define the method compareTo() with the return type integer to compare two dates.it will return 0 if both are same,will return 1 if date is after currrent date else it returns -1.
step 9:terminate the class.                                                 
step 10:Define the datetest and define the main() in it.
step 11:Create many objects for the class MyDate and access the methods of the class using different objects.and compare the date to display the result.
step 12:Display the result.
step 13:terminate the main() and class.
OTHER LAB PROGRAMS:
1.Sting s1=”Hello”; Sring s2=”Hello”;Sring s3=”Good-Bye”;String s4=”HELLO”;
Apply all String comparision methods to the value of s.

2.String s=”Now is the time for all good men”+”to come to the aid of country.”;
Apply all String searching method to the value of s.

3.String s=”APPLY ALL STRING MODIFICATION METHOD:”;
Apply all String modification methods  to the value of s.

4.Write a java program to check whether the String  is palindrome or not.

5.Display the following output :
S1=hello
S2=Java world
S3=world
S4=hello
Input:
Char carry[]={‘J’,’a’,’v’,’a’,’w’,’o’,’r’,’l’,’d’};
Apply any string operations.

6.Display the following output using arraycopy method.
0:2001
1:2002
2:2003
3:5
4:7
5:11
6:13

Input:
Int[] first={2,3,5,7,11,13};
Int[] second={2001,2002,2003,2004,2005,2006,2007};

7.Develop a program to sorting the following array elements:
a){45,65,12,32,1,23,39,4,8,3}
b){46,78,34,43,23,22,11,55,23,111}

8.Write a java program  to convert  the value “Programmer” in the string variable convert to “Programming”.
9.Write a java program to search for a number in the given array using binary search.
10.Diaplay the output for the following program:
public class StringsDemo3 {
 
        public static void main(String[] args) {
               String str1 = "My name is bob";
               char str2[] = new char[str1.length()];
               String str3 = "bob";
               String str4 = "cob";
               String str5 = "BoB";
               String str6 = "bob";
               System.out.println("Length of the String str1 : " + str1.length());
               System.out.println("Character at position 3 is : "
                               + str1.charAt(3));
               str1.getChars(0, str1.length(), str2, 0);
               System.out.print("The String str2 is : ");
               for (int i = 0; i < str2.length; i++) {
                       System.out.print(str2[i]);
               }
               System.out.println();
               System.out.print("Comparision Test : ");
               if (str3.compareTo(str4) < 0) {
                       System.out.print(str3 + " < " + str4);
               } else if (str3.compareTo(str4) > 0) {
                       System.out.print(str3 + " > " + str4);
               } else {
                       System.out.print(str3 + " equals " + str4);
               }
               System.out.println();
               System.out.print("Equals Test");
               System.out.println("str3.equalsIgnoreCase(5) : "
                               + str3.equalsIgnoreCase(str5));
               System.out.println("str3.equals(6) : " + str3.equals(str6));
               System.out.println("str1.equals(3) : " + str1.equals(str3));
               str5.toUpperCase(); //Strings are immutable
               System.out.println("str5 : " + str5);
               String temp = str5.toUpperCase();
               System.out.println("str5 Uppercase: " + temp);
               temp = str1.toLowerCase();
               System.out.println("str1 Lowercase: " + str1);
               System.out.println("str1.concat(str4): " + str1.concat(str4));
               String str7temp = "  \t\n Now for some Search and Replace Examples    ";
               String str7 = str7temp.trim();
               System.out.println("str7 : " + str7);
               String newStr = str7.replace('s', 'T');
               System.out.println("newStr : " + newStr);
               System.out.println("indexof Operations on Strings");
               System.out.println("Index of p in " + str7 + " : "
                               + str7.indexOf('p'));
               System.out.println("Index of for in " + str7 + " : "
                               + str7.indexOf("for"));
               System.out.println("str7.indexOf(for, 30) : "
                               + str7.indexOf("for", 30));
               System.out.println("str7.indexOf('p', 30) : "
                               + str7.indexOf('p', 30));
               System.out.println("str7.lastIndexOf('p') : "
                               + str7.lastIndexOf('p'));
               System.out.println("str7.lastIndexOf('p', 4) : "
                               + str7.lastIndexOf('p', 4));
               System.out.print("SubString Operations on Strings");
               String str8 = "SubString Example";
               String sub5 = str8.substring(5); // "ring Example"
               String sub3_6 = str8.substring(3, 6); // "Str"
               System.out.println("str8 : " + str8);
               System.out.println("str8.substring(5) : " + sub5);
               System.out.println("str8.substring(3,6) : " + sub3_6);
        }
}

11.Try the programs for all array methods.(Try Example Program for every methods)
12.Try the programs for StrinBuilder class methods. _________________________________________________________________________________
Note:
1.Finish all bending lab programs.
2.Finish  all class work examples.
3.Study for VIVA.

Sunday, 10 July 2011

TOMORROW LAB PROGRAMS


ARRAYS EXAMPLE PROGRAMS:

1)Display the output for the following programs:
a)
class aa
{
public static void main(String args[])
{
int i;
int a[]=new int[10];
for(i=0;i<a.length;i++)
{
a[i]=i+1;
}
System.out.println(""+a[i]+"");
}}

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

int[][] a2 = new int[10][5];

 for (int i=0; i<a2.length; i++) {
     for (int j=0; j<a2[i].length; j++) {
a2[i][j]=i;
         System.out.print(" " + a2[i][j]);
     }
     System.out.println("");
 }

  }
}
c)
class anarry {
     public static void main(String[] args) {
          int[] anArray={900,1000};      // allocates memory for 10 integers
          anArray[0] = 900;
         anArray[1] = 1000;

          System.out.println("Element at index 1: " + anArray[0]);
         System.out.println("Element at index 2: " + anArray[1]);
     }
}
d) class arr
{
public static void main(String args[])
{

String months[] =
         {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
          "July", "Aug", "Sep", "Oct", "Nov", "Dec"};
      
          for (int i = 0; i < months.length; i++ ) {
          System.out.println("month: " +months[i]);

}
}
}

e)
class arry
{
int num[];
arry()
{
num=new int[10];
for(int i=0;i<num.length;i++)
for(int j=0;j<num.length;j++)
{

num[i]=i+5;
}}
void print()
{

System.out.println("Numbersare");
for(int i=0;i<num.length;i++)
{
System.out.println(num[i]);
}
}
public static void main(String args[])
{
arry s=new arry();
s.print();
}
}

f)
class arrys
{
int num[][];
arrys()
{
num=new int[2][2];
for(int i=0;i<num.length;i++)
for(int j=0;j<num.length;j++)

num[i][j]=(i+1)*(j+1);
}
void print()
{
System.out.println("Numbersare");
for(int i=0;i<num.length;i++)
{
for(int j=0;j<num.length;j++)


System.out.println(""+num[i][j]+"");
}
}
public static void main(String args[])
{
arrys s=new arrys();
s.print();
}
}

g)
import java.util.*;
class b
{
int a[]=new int[5];
int b[]=new int[5];
int sum;
Scanner ac=new Scanner(System.in);
void dis()
{
System.out.println("Enter the a values");
System.out.println("Enter the b values");

for(int i=0;i<=4;i++)
{

a[i]=ac.nextInt();
b[i]=ac.nextInt();
}
}

void pus()
{
for(int i=0;i<=4;i++)
{
sum=a[i]+b[i];
System.out.println("Numbers are"+sum);
}

}
public static void main(String args[])
{
b aaa=new b();
aaa.dis();
aaa.pus();
}
}

h)
public class copy{
  public static void main(String[] args){
 char[] copyfrom = {'a','b','c','d','e','f','g','h','i','j'};
  char[] copyTo = new char[6];
  System.arraycopy(copyfrom, 2, copyTo, 0, 6);
  System.out.println(new String (copyTo));
  }
}

i)
class multi {
    public static void main(String[] args) {
        String[][] names = {{"Mr. ", "Mrs. ", "Ms. "},
                            {"Smith", "Jones"}};
        System.out.println(names[0][0] + names[1][0]); //Mr. Smith
        System.out.println(names[0][2] + names[1][1]); //Ms. Jones
   
System.out.println(names.length);}
}

j)
class sam
{
int num[];
sam()
{
num=new int[5];
int n[]={1,2,3,4,5};
num=n;
}
void print()
{
System.out.println("NUMBERS ARE");
for(int i=0;i<num.length;i++)
System.out.println(num[i]);
}
public static void main(String args[])
{
sam s=new sam();
s.print();
}
}

k)
public class Sum
{
  public static void main(String[] args)
  {
  int[] x = new int [10];
  for (int i = 0; i<x.length; i++ )
  x[i] = i;
  int sum = 0;
  for(int i = 0; i<x.length; i++)
  sum += x[i];
  System.out.println(sum);
  }
}

l)
import java.util.*;
class name
{
int no[]=new int[2];
String names[]=new String[2];
Scanner ac=new Scanner(System.in);
void getdata()
{
System.out.println("Enter the Name and Rollno values");
for(int i=0;i<2;i++)
{
no[i]=ac.nextInt();
names[i]=ac.next();
}
}
void putdata()
{
System.out.println("Roll nO\t Name\n");
for(int i=0;i<2;i++)
{
System.out.println(no[i]+"\t"+names[i]+"\n");

}}
public static void main(String args[])
{
name n=new name();
n.getdata();
n.putdata();
}
}

m)
public class CopyArray {
  public static void main(String[] args) {
  int array1[]= {2,3,4,5,8,9};
  int array2[] = new int[6];
  System.out.println("array:");
  System.out.print("[");
  for (int i=0; i<array1.length; i++){
  System.out.print(" "+array1[i]);
  }
  System.out.print("]");
  System.out.println("\narray1:");
  System.out.print("[");
  for(int j=0; j<array1.length; j++){
  array2[j] = array1[j];
  System.out.print(" "+ array2[j]);
  }
  System.out.print("]");
  }
}


n)
public class ArrayAverage{
  public static void main(String[] args) {
  int nums[]={5,4,3,2,1};
  int result=0;
  int i=0;
  for(i=0; i < nums.length; i++){
  result=result + nums[i];
  }
  System.out.println("Average is =" + result/nums.length);
  }
}

o)
class MatrixExample{
  public static void main(String[] args)  {
  int array[][]= {{1,3,5},{2,4,6}};
  System.out.println("Row size= " + array.length);
  System.out.println("Column size = " + array[1].length);
  outputArray(array);
  }
  
 public static void outputArray(int[][] array) {
 int rowSize = array.length;
 int columnSize = array[0].length;
 for(int i = 0; i <= 1; i++) {
 System.out.print("[");
 for(int j = 0; j <= 2; j++) {
 System.out.print(" " + array[i][j]);
 }
 System.out.println(" ]");
 }
 System.out.println();
 }
}

2.Develop a java program to perform Matrix addition  using java arrays.
  Tips:
[we are going to calculate the sum of two matrix and containing its rows and columns. See below for better understanding to this.
In this program we are going to calculate the sum of two matrix. To make this program, we need to declare two dimensional array of type integer. Firstly it calculates the length of the both the arrays. Now we need to make a matrix out of it. To make the matrix we will use the for loop. By making use of the for loop the rows and column will get divide. This process will be performed again for creating the second matrix.
After getting both the matrix with us, we need to sum both the matrix. The both matrix will be added by using the for loop with array[i][j]+array1[i][j]. The output will be displayed by using the println() method.]

3.Develop a java program to perform Matrix Subtraction using java arrays.
4.Develop a java program to perform Matrix Multiplication  using java arrays.
5.Deveop a  java program for implementing the two dimensional array program and its square.
[Tips:
We are going to display the square of two matrix. Firstly, we have to define a class "SquareMatrix".  Then we take an integer type array that contains integer type values. After this, we use two 'for' loop that denotes rows and columns of a matrix. After getting both the matrix with us we need to square both matrix. When we go to square this array then we use "square[i][j] =square[i][j] * square[i][j]". So, the output will be displayed on the screen command prompt by using the println() method.]