Programs

1. Addition of two values passing by command line argument.

class Demo1
{
public static void main(String args[])
{
int a,b,c;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);

c=a+b;
System.out.println("sum="+c);
}
}

2. Write a program to find the result of following expression.
1) a << 2 + b >> 2
2) (a<<2) + (b>>2)
3) a & b
4) a | 4 + a >> b & 7
class ass1-3
{
public static void main(String args[])
{
int a=8;
int b=4;
System.out.println(a<<2);
System.out.println(b>>2);
System.out.println(a<<2+b>>2);
System.out.println((a<<2)+(b>>2));
System.out.println(a&b);
System.out.println(a|4+a>>b&7);
}
}
4. Write a program in java to explain the use of break and continue statements.

class BreakContinue

  public static void main(String args[])  
   
for(int i=0; i<3; i++)  
             
                Inner :  for(int j=0; j<5; j++)  
                  {                      
if(j == 2) 
                    break Inner;  
                
                      System.out.print(j+" "); 
                  }  
                  System.out.print(i+" "); 
             
              System.out.println("Loops complete."); 
  

outer : for(int i=0; i<3; i++)  
             
                  for(int j=0; j<5; j++)  
                  {                   
  if(j == 2) 
                        continue outer;  
                
                     System.out.print(j+" "); 
                 
 
                  System.out.print("This statement does not exit."); 
             
 
               System.out.println("Loops complete."); 
   

 5. Create Box class with data members width, height and depth. Objects of this class initialize with zero value or by passing single value. Create a method to calculate volume and display it.

class Box
{
float width;
float height;
float depth;

Box(float a,float b,float c)
{
width=a;
height=b;
depth=c;
}
void calculate()
{
double ans=0;
ans=width*height*depth;
System.out.println("volume="+ans);
}
}
class Box1
{
public static void main(String args[])
{
Box b=new Box(10,20,30);
b.calculate();
}
}

6. Calculate area of square, rectangle and triangle using the three same name methods 'area';
class Calculate
{
void area(int l)
{
int ans=0;
ans=l*l;
System.out.println("Squre="+ans);
}
void area(int w,int  h)
{
float ans=0;
ans=w*h;
System.out.println("Rectangle="+ans);
}
void area(double b,double h)
{
double ans=0;
ans=b*h*(1/2);
System.out.println("Triangle="+ans);
}
}
class DemoCal
{
public static void main(String args[])
{
Calculate C=new Calculate();
C.area(10);
C.area(10,20);
C.area(30.15,40.12);
}
}

7. Make CALC class with two data members. Perform addition, subtraction, multiplication and division operation of given two values by user. Initialize both data members with zero. (use menu-driven)
import java.io.*;
class DemoCalc
{
int a;
int b;
void input()
{
try
{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
System.out.print("a=");
a=Integer.parseInt(br.readLine());
System.out.print("b=");
b=Integer.parseInt(br.readLine());
}
catch(IOException e)
{
}
}
}
class Calc
{
public static void main(String args[])
{
DemoCalc C=new DemoCalc();
int ans=0;
int ch;
C.input();
try
{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.println("0 : Exit");
System.out.println("1 : addition");
System.out.println("2 : subtraction");
System.out.println("3 : multiplication");
System.out.println("4 : division");

System.out.println("enter the choise");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 0:
System.out.println("Exiting...");
break;

case 1:
ans=C.a+C.b;
System.out.println("addition="+ans);
break;
case 2:
ans=C.a-C.b;
System.out.println("subtracation="+ans);
break;
case 3:
ans=C.a*C.b;
System.out.println("multiplication="+ans);
break;
case 4:
ans=C.a/C.b;
System.out.println("division="+ans);
break;
}
}while(ch!=0);
}
catch(IOException e)
{
}
}
}

8. Create array with values {8,7,6,5,4} and display it.
class Array
{
public static void main(String args[])
{
int arr[]={8,7,6,5,4};
int i;
for(i=0;i<5;i++)
{
System.out.print(arr[i]);
}
}
}
9. Write a program which display following pattern.
1
1 2
1 2 3
1 2 3 4
class Pattern1
{
public static void main(String args[])
{
int i,j;
for(i=0;i<=5;i++)
{
for(j=1;j<i;j++)
{
System.out.print(j);
}
System.out.println();
}
}
}

10. Write a program which display following pattern.
1
2 3
4 5 6
7 8 9 10
class Pattern1
{
public static void main(String args[])
{
int i,j,k=8,n=1;

for(i=0;i<5;i++)
{
for(j=0;j<k;j++)
{
System.out.print(" ");
}
k--;
for(j=0;j<i;j++)
{
System.out.print(n++);
}
System.out.println();
}
}
}

11. Write a program which display following pattern.
      1
    2 2
  3 3 3
4 4 4  4

class Pattern
{
public static void main(String args[])
{
int i,j,k=8,n=1;

for(i=0;i<5;i++)
{
for(j=0;j<k;j++)
{
System.out.print(" ");
}
k--;
for(j=0;j<i;j++)
{
System.out.print(" "+(n++));
}
System.out.println();
}
}
}

12. Create integer array of size 5. Insert 5 values through user and sort the array and display it. Also  calculate total of all 5 values.
import java.io.*;
class Sort
{
public static void main(String args[])
{
int a[]=new int[5];
int i,sum=0,temp,j;
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
for(i=0;i<5;i++)
{
a[i]=Integer.parseInt(br.readLine());
}
}
catch(IOException e)
{
}
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("Sort array");
for(i=0;i<5;i++)
{
sum=sum+a[i];
System.out.println(a[i]);
}
System.out.println("sum="+sum);
}
}

13. Write a program which have a class AVERAGE contain a single dimensional array MARKS (size 5) as data member. Take input from parameterized constructor. Take methods to calculate average marks and display it. Create only one object of class.
class Average
{
int arr[]=new int[5];
Average(int a,int b,int c,int d,int e)
{
arr[0]=a;
arr[1]=b;
arr[2]=c;
arr[3]=d;
arr[4]=e;
}

}
class DemoAvg
{
public static void main(String args[])
{
Average A = new Average(10,20,30,40,50);
int i,sum=0;
float avg=0;
for(i=0;i<5;i++)
{
sum=sum+A.arr[i];
}
System.out.println("sum="+sum);
avg=(float)sum/5;
System.out.println("avg="+avg);
}
}

14. Create STACK class with push, pop fun.
import java.io.*;
class Stack
{
int size=5;
int s[] = new int[size]; 
int tos=-1;
void push(int value) 
{
  if(tos==size-1)
  System.out.println("Stack is full."); 
else 
{
tos=tos+1;
s[tos] = value; 
}
}
void pop() 

int value;
if(tos==-1) 

System.out.println("Stack underflow."); 

else 
{
value=s[tos]; 
tos=tos-1;
System.out.println("value="+value);
}
}
}
class DemoStack
{
public static void main(String args[])
{
Stack S1=new Stack();
int i,val;
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("push the value in the stack");
for(i=0;i<5;i++)
{
val=Integer.parseInt(br.readLine());
S1.push(val);
}
}
catch(IOException e)
{
}
System.out.println("pop the value from the stack");
for(i=0;i<5;i++)
{
S1.pop();
}
}
}

15. Write a program in java to find A x B where A is a matrix of 3 x 3 and B is a matrix of 3 x 4. Take the values in matrixes A and B form the user.
import java.io.*;
class Matrix
{
public static void main(String args[])
{
long towD[][]=new long[3][3];
long towD1[][]=new long[3][4];
long towD2[][]=new long[3][3];
int i,j,k;
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("insert the first matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
towD[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("insert the second matrix");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
towD1[i][j]=Integer.parseInt(br.readLine());
}
}
}
catch(IOException e)
{
}
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
towD2[i][j]=0;
for(k=0;k<3;k++)
{
towD2[i][j]=towD2[i][j]+(towD[i][k]*towD[k][j]);
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(towD2[i][j]);
}
System.out.println();
}
}
}

16. Create one string array of size 5. Insert 5 names through user and sort the array name wise and display it.
import java.io.*;
class String1
{
public static void main(String args[])
{
String name[]=new String[5];
String temp;
int i,j;
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the name");
for(i=0;i<5;i++)
{
name[i]=br.readLine();
}
}
catch(IOException e)
{
}
System.out.println("name wise sorting");
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if((name[i].compareToIgnoreCase(name[j]))>0)
{
temp=name[i];
name[i]=name[j];
name[j]=temp;
}
}
}
for(i=0;i<5;i++)
{
System.out.println(name[i]);
}
}
}

Comments