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.]


2 comments: