1) AIM:-
Write a JAVA program to display default values of all primitive data types of
JAVA?
Program:
import java.lang.*;
class DefaultValues
{
static byte num1;
static short num2;
static int num3;
static long num4;
static float num5;
static double num6;
static char num7;
static boolean num8;
public static void main(String args[])
{
System.out.println(num1);
System.out.println(num2);
System.out.println(num3);
System.out.println(num4);
System.out.println(num5);
System.out.println(num6);
System.out.println(num7);
System.out.println(num8);
}
}
Output:-
2) Aim:
Write a JAVA program that display the roots of a quadratic equation ax2+bx+c=0.
Program:
import java.lang.*;
class QuadraticRoots
{
public static void main(String args[])
{
int a = 2;
int b = 6;
int c = 4;
//Finding out the roots
double temp1 = Math.sqrt(b * b - 4 * a * c);
double root1 = (-b + temp1) /
(2*a) ;
double root2 = (-b - temp1) /
(2*a) ;
System.out.println("The roots of the Quadratic Equation \"2x2
+ 6x + 4 = 0\" are "+root1+" and "+root2);
}
}
Output:
3) Aim:
Write a JAVA program to display the Fibonacci sequence.
Program:
import java.lang.*;
class FibonacciNum
{
public static void main(String args[])
{
int a = 0;
int b = 1;
for (int i = 0;i<10;i++)
{
System.out.println(a);
a = a+b;
b = a-b;
}
}
}
Output:-
4) Write
a JAVA program give example for command line arguments.
Program:
import java.lang.*;
class CommandLineArg
{
public static void
main(String args[])
{
int num1;
float num2;
byte num3;
num1=Integer.parseInt(args[0]);
num2=Float.parseFloat(args[1]);
num3=Byte.parseByte(args[2]);
System.out.println("The
int value is:"+num1);
System.out.println("The
float value is:"+num2);
System.out.println("The
Byte value is:"+num3);
System.out.println("The
String value is:"+args[3]);
}
}
Output:-
5) Aim:-
Write a Java program to sort given list of numbers.
Program:-
import java.lang.*;
import java.util.Arrays;
class SortList
{
public static void main(String args[])
{
int num[]=new int[6];
num[0]=13;
num[1]=67;
num[2]=54;
num[3]=3;
num[4]=43;
num[5]=17;
System.out.println("Before
Sorting");
for(int i:num)
System.out.println(i);
Arrays.sort(num);
System.out.println("After Sorting");
for(int j:num)
System.out.println(j);
}
}
Output:-
6 ) Aim:-Write
a JAVA program to search for an element in a given list o elements(linear
search).
Program:-
import java.util.Scanner;
import java.lang.*;
class LinearSearchExample
{
public static void main(String args[])
{
int counter, num, item, array[];
Scanner input = new Scanner(System.in);
System.out.println("Enter number of elements:");
num = input.nextInt();
array = new int[num];
System.out.println("Enter " + num + " integers");
for (counter = 0; counter < num; counter++)
array[counter] = input.nextInt();
System.out.println("Enter the search value:");
item = input.nextInt();
for (counter = 0; counter < num; counter++)
{
if (array[counter] == item)
{
System.out.println(item+" is
present at location "+(counter+1));
break;
}
}
if (counter == num)
System.out.println(item + " doesn't exist in array.");
}
}
Output:-
7) Aim:-
Write a JAVA program to search for an element in a given list of elements using
binary search mechanism .
Program:
import
java.util.Scanner;
import java.lang.*;
import java.lang.*;
class
BinarySearchExample
{
public static void main(String args[])
{
int counter, num, item, array[], first,
last, middle;
//To capture user input
Scanner input = new Scanner(System.in);
System.out.println("Enter number of
elements:");
num = input.nextInt();
//Creating array to store the all the
numbers
array = new int[num];
System.out.println("Enter " +
num + " integers");
//Loop to store each numbers in array
for (counter = 0; counter < num; counter++)
array[counter] = input.nextInt();
System.out.println("Enter the search
value:");
item = input.nextInt();
first = 0;
last = num - 1;
middle = (first + last)/2;
while( first <= last )
{
if ( array[middle] < item )
first = middle + 1;
else if ( array[middle] == item )
{
System.out.println(item + "
found at location " + (middle + 1) + ".");
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if ( first > last )
System.out.println(item + " is
not found.\n");
}
}
Output:-
8) Aim:-
Write a JAVA program to determine the addition of two matrices.
Program:-
import java.lang.*;
class AdditionMatrix
{
int A[][];
int B[][];
int C[][];
int row;
int coloum;
public AdditionMatrix(int n, int m)
{
this.row = n;
this.coloum = m;
A = new int[n][m];
B = new int[n][m];
C = new int[n][m];
//assign random value for A,B
for (int r = 0; r < n; r++)
{
for (int c = 0; c < m; c++)
{
A[r][c] = (int)
Math.round(Math.random() * 98 + 10);
B[r][c] = (int)
Math.round(Math.random() * 12);
}
}
}
public void addition()
{
for (int r = 0; r < row; r++)
{
for (int c = 0; c < coloum;
c++)
{
C[r][c] = A[r][c] + B[r][c];
}
}
}
public void printA()
{
for (int r = 0; r < row; r++)
{
for (int c = 0; c < coloum; c++)
{
System.out.print(A[r][c] +
"\t");
}
System.out.println();
}
}
public void printB()
{
for (int r = 0; r < row; r++)
{
for (int c = 0; c < coloum; c++)
{
System.out.print(B[r][c] +
"\t");
}
System.out.println();
}
}
public void printC()
{
for (int r = 0; r < row; r++)
{
for (int c = 0; c < coloum; c++)
{
System.out.print(C[r][c] +
"\t");
}
System.out.println();
}
}
public static void main(String args[])
{
AdditionMatrix S = new AdditionMatrix(4, 5);
System.out.println("Matrix A
:");
S.printA();
System.out.println("=======+==========+============+=========");
System.out.println("Matrix of B :");
S.printB();
System.out.println("=======+==========+============+=========");
S.addition();
System.out.println("Matrix of C, sum A & B :");
S.printC();
System.out.println("=======+==========+============+=========");
}
}
Output:-
9) Aim:- Write a JAVA program to determine the Multiplication of two matrices.
Program:
import
java.util.Scanner;
import java.lang.*;
class MatrixMultiplication
{
public static void main(String args[])
{
int m, n, p, q, sum = 0, c, d, k;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number
of rows and columns of first matrix");
m = in.nextInt();
n = in.nextInt();
int first[][] = new int[m][n];
System.out.println("Enter the
elements of first matrix");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
first[c][d] = in.nextInt();
System.out.println("Enter the number
of rows and columns of second matrix");
p = in.nextInt();
q = in.nextInt();
if ( n != p )
System.out.println("Matrices with
entered orders can't be multiplied with each other.");
else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];
System.out.println("Enter the
elements of second matrix");
for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
second[c][d] = in.nextInt();
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum +
first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
System.out.println("Product of
entered matrices:-");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
System.out.print(multiply[c][d]+"\t");
System.out.print("\n");
}
}
}
}
Output:-
\
10) Aim:- Write a JAVA program to sort
an of strings.
Program:-
import java.lang.*;
import java.io.*;
import java.util.Arrays;
class SortStringList
{
public static void main(String
args[])throws IOException
{
int k;
String names[]=new String[6];
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter 6
Names:");
for(k=0;k<6;k++)
names[k]=br.readLine();
System.out.println("Before
Sorting");
for(String i:names)
System.out.println(i);
Arrays.sort(names);
System.out.println("After Sorting");
for(String j:names)
System.out.println(j);
}
}
Output:-










No comments:
Post a Comment