You are on page 1of 27

Program No:

Date:

AIM:Write a program to convert a decimal number into binary equalent.

Program:
import java.io.*;
class Decimal
{
void binary(int n)
{
int b[] = new int[40];
int index = 0;
while(n > 0)
{
b[index++] = n%2;
n = n/2;
}
for(int i = index­1;i >= 0;i­­)
{
System.out.print(b[i]);
}
System.out.print("\n");
}
}
class Binary
{
public static void main(String args[])throws IOException
{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    int n;
    System.out.print("Enter a decimal number:");
    n=Integer.parseInt(br.readLine());
    Decimal obj =new Decimal();
    System.out.print("\nBinary equalent of "+n+" : ");
    obj.binary(n);
}
}
Output:
Program No:
Date:

AIM:Write a programe to find the transpose of a matrix.

Program:
import java.util.Scanner;
class Tra
{
int i,j,a[][];
void disp(int r,int c,int a[][])
{
System.out.println("Matrix is:");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print(a[i][j] +" ");
}
System.out.println();
}
}
void transpose(int r,int c,int a[][])
{
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
System.out.print(a[j][i] +" ");
}
System.out.println();
}
}
}
public class Transpose
{
public static void main(String arg[])
{
int i,j;
Tra s1=new Tra();
System.out.println("Enter total rows and columns:");
Scanner s=new Scanner(System.in);
int r=s.nextInt();
int c=s.nextInt();
int a[][]=new int[r][c];
System.out.println("Enter matrix:");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
a[i][j]=s.nextInt();
}
s1.disp(r,c,a);
System.out.println("Transpose of matrix:");
s1.transpose(r,c,a);
}
}

Output:
Program No:
Date:

AIM:Write a program to add and multiply two matrices.

Program:
import java.io.*;
import java.util.*;
class Mat
{
 int i,j;
 Scanner sc=new Scanner(System.in);
void read(int a[][],int k,int l)
 {
  for(i=0;i<k;i++)
  {
   for(j=0;j<l;j++)
    {
      a[i][j]=sc.nextInt();
    }
  }
 }
 void disp(int a[][],int k,int l)
 {
   for(i=0;i<k;i++)
    {
     System.out.print("\n");
     for(j=0;j<l;j++)
     {
      System.out.print(a[i][j]);
      System.out.print("\t");
     }
    }
 }
}
class Matrix
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
char ch;
int a[][]=new int [5][5];
int b[][]=new int [5][5];
int c[][]=new int [5][5];
int k,l,m,n,op,i,j;
Mat ob=new Mat();
System.out.println("Enter the order of First matrix:");
k=sc.nextInt();
l=sc.nextInt();
System.out.println("Enter the elements of first matrix");
ob.read(a,k,l);
System.out.println("Enter the order of Second matrix:");
m=sc.nextInt();
n=sc.nextInt();
System.out.println("Enter the Elements of Second matrix");
ob.read(b,m,n);
System.out.println("First Matrix:");
ob.disp(a,k,l);
System.out.println("\nSecond Matrix:");
ob.disp(b,m,n);
do
{
System.out.print("\n\n\n\t1.Matrix Addition\n\t2.Matrix Multiplication\n\tEnter 
Your Choice : ");
op=sc.nextInt();
switch(op)
{
case 1:

if ((k==m) &&(l==n))
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}

System.out.println("Sum of Matrices:");
ob.disp(c,m,n);
}
else
{
System.out.println("Cannot Add Matrices With Different Order");
}
break;

case 2:

if (l==m)
{
for(i=0;i<k;i++)
{
for(j=0;j<l;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}

System.out.println("Product of Matrices:");
for(i=0;i<m;i++)
{
System.out.print("\n");
for(j=0;j<n;j++)
{
System.out.print(c[i][j]);
System.out.print("\t");
}
}
System.out.print("\n");
}
else
{
System.out.println("Cannot Multiply Matrices With Different Order");
}
break;

default:
System.out.println("Invalid Choice");
break;
}
System.out.print("\nDo you want to continue (Y/N)?:");
ch=sc.next().charAt(0);
}
while(ch=='Y' || ch=='y');
}
}
Output:
Program No:
Date:

AIM:Write a program to sort a mark list and print the first run mark.

Program:
import java.io.*;
import java.util.*;
class Firstrun
{
void firstmark()
{
int i, j, temp;

 Scanner sc=new Scanner(System.in);   
System.out.println("Enter Array Size : ");
int n=sc.nextInt();
int arr[] = new int[n];
System.out.println("Enter the marks : ");
for(i=0; i<n; i++)
{
 arr[i] = sc.nextInt();
}
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println("after Sorting is :");
for(i=0; i<n; i++)
{
System.out.println(arr[i]+ "  ");
}
System.out.println("The First Run mark is\t"+arr[n­1]);
}
}
class Mark
{
public static void main(String args[]) throws IOException
{
Firstrun ob=new Firstrun();
ob.firstmark();
}
}

Output:
Program No:
Date:

AIM:Write a program to convert temperature in Celcius to Farenheit and vice versa.

Program:

import java.io.*;
class FahrenheittoCelsius
{

double celsius(double f)
{
return  (f­32)*5/9;
}

double fahrenheit(double c)
{
return (c*9/5)+32;
}

class Faren
{
public static void main(String arg[])throws IOException
{
    double a,c;
                 BufferedReader br=new BufferedReader(new 
InputStreamReader(System.in));
                 FahrenheittoCelsius ob=new  FahrenheittoCelsius();
                  
           System.out.print("Enter  Fahrenheit temperature:");
                   a=Double.parseDouble(br.readLine());
           System.out.println("Celsius temperature is = "+ob.celsius(a));
           
           System.out.print("\nEnter  celsius temperature:");
                   c=Double.parseDouble(br.readLine());
           System.out.print("\nFahrenheit temperature is = "+ob.fahrenheit(c));

           
                    

}
Output:
Program No:
Date:

AIM:Write a program to print the largest and smallest values in a 2 dimensional
matrix.

Program:
import java.util.*;
class Mat
{
 int r,c;
 int a[][]=new int[10][10];
 void read()
 {
      Scanner sc=new Scanner(System.in);
      System.out.println("Enter the order of the matrix :");
      r=sc.nextInt();
      c=sc.nextInt();
      System.out.println("Enter the matrix :");
      for(int i=0;i<r;i++)
      {
      for(int j=0;j<c;j++)
      {
      a[i][j]=sc.nextInt();
      }
      }
 }
 void show()
 {
    for(int i=0;i<r;i++)
    {
    for(int j=0;j<c;j++)
    {
    System.out.print("  "+a[i][j]);
    }
    System.out.println();
    }
 }
void largesmall()
{
  int l=a[0][0];
  int s=a[0][0];
  for(int i=0;i<r;i++)
  {
  for(int j=0;j<c;j++)
  {
     if(a[i][j]>l)
     l=a[i][j];
     if(a[i][j]<s)
     s=a[i][j];
  }
}
System.out.println("largest is:"+l);
System.out.println("smallest is:"+s);
}
}
class Largest
 {
    public static void main(String arg[])
    {
      Mat ob1=new Mat();
      ob1.read();
      System.out.println(" matrix is:");
      ob1.show();
      ob1.largesmall();
    }
}

Output:
Program No:
Date:

AIM:Write a java program that uses an overloaded method volume() that returns the 
volume of a cube,cylinder and of a rectangular box.

Program:

import java.io.*;
class Overload
{
 void volume(float e)
 {
  System.out.println("Volume of Cube="+(e*e*e));
 }
 int volume(int w,int h,int l)
 {
  return w*h*l;
 }
 void volume(float r,float h)
 {
  double t=3.14*r*r*h;
  System.out.println("Volume of Rectangular Box="+t);
 }
 }
 class Volume
 {
  public static void main(String args[])throws IOException
  {
   int l,w,h,m,rect,ch;
   float e,r,b;
   Overload ob=new Overload();
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   do
   {
   System.out.print("\n1.Cube\n2.Cylinder\n3.Rectangular Box\nEnter Your 
Choice:");
   ch=Integer.parseInt(br.readLine());
   switch(ch)
   {
   case 1:
    System.out.print("Enter the edge:");
    e=Float.parseFloat(br.readLine());
    ob.volume(e);
    break;
  case 2:
    System.out.println("Enter the length,width & height:");
   l=Integer.parseInt(br.readLine());
    w=Integer.parseInt(br.readLine());
    h=Integer.parseInt(br.readLine());
    rect=ob.volume(l,w,h);
    System.out.println("Volume of Rectangular Box="+ rect);
    break;
   case 3:
    System.out.println("Enter the radius & height of cylinder:");
    r=Float.parseFloat(br.readLine());
   b=Float.parseFloat(br.readLine());
    ob.volume(r,b);
  }
  }while(ch<4);
  }
 }
Output:
Program No:
Date:

AIM:Write a program that available with String & StringBuffer class methods.

Program:
import java.io.*;
class Sample
{
void read()

 StringBuffer s= new StringBuffer("Anamika");
System.out.print("\nString: "+s);
       
  int p = s.length();
        int q = s.capacity();
        System.out.println("\nLength of string =" + p);
        System.out.println("\nCapacity of string =" + q);
        s.append("John");
        System.out.println("\n Add Surname: "+s);
        s.append(1);
        System.out.println("\n Append an Integer: "+s); 
s.replace(2, 6, "Francis");
        System.out.println("\n Replace with another name: "+s);
s.delete(0, 6);
        System.out.println("\n Delete the first name: "+s); 
        s.deleteCharAt(5);
        System.out.println("\n Delete last char: "+s);
s.reverse();
        System.out.println("\n Reverse the string: "+s);
}
}

class Strbuff
 {
    public static void main(String[] args)
    {
Sample s1= new Sample();
s1.read(); 
       
    }
}
Output:
Program No:
Date:

AIM:Write a program to perform string sorting

Program:

import java.util.Scanner;
class Order
{
void sortt(int n,  String names[])
{
int  i, j;
String temp;
for (i = 0; i < n; i++) 
{
for (j = i + 1; j < n; j++) 
{
if (names[i].compareTo(names[j])>0) 
{
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
}
}
public class Strsort
{
public static void main(String[] args) 
{
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of names :");
n = s.nextInt();
String names[] = new String[n];
Scanner s1 = new Scanner(System.in);
System.out.println("Enter the names:");
for(int i = 0; i < n; i++)
{
names[i] = s1.nextLine();
}
Order obj=new Order();
obj.sortt(n,names);
System.out.print("Names in Sorted Order:\n");
for (int i = 0; i < n ­ 1; i++) 
{
System.out.println(names[i] );
}
System.out.println(names[n ­ 1]);
}
}

Output:
Program No:
Date:

AIM:Write a java program to find the number of objects created in a class.

Program:
import java.io.*;
class Sub
{
static int j=1;
void check()
{
j++;
System.out.println(j);
}
}
class Objcount
{
public static void main(String args[])
{
System.out.println("Number of objects created:");
Sub s1=new Sub();
s1.check();
Sub s2=new Sub();
s2.check();
Sub s3=new Sub();
s3.check();
}
}

Output:
Program No:
Date:

AIM:Write a program to find the factorial of a number using the concept recursion.

Program:

import java.io.*;
    class Fact
    {  
     static int find(int n)
     {    
      if (n == 0)    
        return 1;    
      else    
        return(n * find(n­1));    
     }    
     }
     class Factorial
     {
     public static void main(String args[])throws IOException
     {  
      int f=1;  
      int number; 
      Fact obj=new Fact();
      System.out.println("Enter the number:");
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      number=Integer.parseInt(br.readLine());
      f= obj.find(number);   
      System.out.println("Factorial of "+number+" is: "+f);    
     }  
    }  

Output:
Program No:
Date:

AIM:Create a class Matrix in which a method is used to read two matrices and 
another method is used to display the sum of the above two matrices.

Program:

import java.util.*;
import java.io.*;
class Mat
{
  int i,j;
  Scanner sc=new Scanner(System.in);
 void read(int a[][],int r,int c)
 {
  for(i=0;i<r;i++)
  {
   for(j=0;j<c;j++)
   {
    a[i][j]=sc.nextInt();
   }
  }
 }
 void disp(int a[][],int r,int c)
 {
  for(i=0;i<r;i++)
    {
     System.out.print("\n");
     for(j=0;j<c;j++)
     {
      System.out.print(a[i][j]);
      System.out.print("\t");
     }
    }
      System.out.print("\n");
 }

 void add(int a[][],int b[][],int r1,int c1,int r2,int c2)
 {
 int s[][]=new int [5][5];
  s[i][j]=0;
  for(i=0;i<r1;i++)
  {
   for(j=0;j<c1;j++)
   {
    s[i][j]+=a[i][j]+b[i][j];
   }
  }
  if((r1==r2) && (c1==c2))
 {
  disp(s,r1,c1);
 }
 else
 System.out.println("Matrix addition not possible");
 }
}

class Matrix1
{
public static void main(String args[])
{
 int r1,c1,r2,c2;
  Scanner sc=new Scanner(System.in);
 Mat m=new Mat();

 System.out.println("Enter the order of first matrix:");
 r1=sc.nextInt();
 c1=sc.nextInt();
 int a[][]=new int [5][5];
int b[][]=new int [5][5];
int c[][]=new int [5][5];
 System.out.println("Enter the first matrix:");
 m.read(a,r1,c1);
 System.out.println("Enter the order of second matrix:");
 r2=sc.nextInt();
 c2=sc.nextInt();
 System.out.println("Enter the second matrix:");
 m.read(b,r2,c2);
 System.out.println("First matrix:");
 m.disp(a,r1,c1);
  System.out.println("\nSecond matrix:");
 m.disp(b,r2,c2);
 System.out.println("\n\nSum of matrices");
  m.add(a,b,r1,c1,r2,c2);
 }
}
Output:
Program No:
Date:

AIM:Create an abstract class named Shape with methods calcArea and calcPeri 
.Create a classes to find  the area and perimenter  of cylinder using the above methods
(calcArea and calcperi of Shape)

Program:

import java.io.*;
import java.util.*;

 abstract class Shape
{
abstract void calcArea(double r, double h);
abstract void calcPeri(double d, double hg);
}

class FindArea extends Shape
 {

void calcArea(double r, double h)
{
double area = 2*3.14*r*h+2*3.14*r*r;
System.out.println("Area of cylinder: "+area);
}

void calcPeri(double d, double hg)
{
double peri = 2*d+2*hg;
System.out.println("Perimeter of cylinder: "+peri);
}
 
}
class Abstra
 {
public static void main(String args[]) throws IOException
{
double  h, r,d;
FindArea a = new FindArea();
Scanner get = new Scanner(System.in);

System.out.print("\nEnter Radius &  Height of cylinder: \n");
r = get.nextDouble();
h = get.nextDouble();
a.calcArea(r, h);

System.out.print("\nEnter Diameter & Height of cylinder: \n");
d= get.nextDouble();
h= get.nextDouble();
a.calcPeri(d, h);

}
}

Output:

You might also like