Sunteți pe pagina 1din 16

JAVA PROGRAMMING LAB MANUAL By A.

JANARDHANA RAO
e-mailID: janardhan182@gmail.com
1. Write a JAVA program to display default value of all primitive data types of JAVA
class DefaultValues
{
static byte b;
static short s;
static int i;
static long l;
static float f;
static double d;
static char c;
static boolean bl;
public static void main(String[] args)
{
System.out.println("Byte :"+b);
System.out.println("Short :"+s);
System.out.println("Int :"+i);
System.out.println("Long :"+l);
System.out.println("Float :"+f);
System.out.println("Double :"+d);
System.out.println("Char :"+c);
System.out.println("Boolean :"+bl);
}
}
2. Write a JAVA program that displays the roots of a quadratic equation ax2+bx+c=0. Calculate the
discriminent D and basing on the value of D, describe the nature of roots
import java.io.*;
class Quadratic
{
public static void main(String args[])throws IOException
{
double x1,x2,disc,a,b,c;
InputStreamReader obj=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(obj);
System.out.println("enter a,b,c values");
a=Double.parseDouble(br.readLine());
b=Double.parseDouble(br.readLine());
c=Double.parseDouble(br.readLine());

disc=(b*b)-(4*a*c);
if(disc==0)
{
System.out.println("roots are real and equal ");
x1=x2=-b/(2*a);
System.out.println("roots are "+x1+","+x2);
}
else if(disc>0)
{
System.out.println("roots are real and unequal");
x1=(-b+Math.sqrt(disc))/(2*a);
x2=(-b+Math.sqrt(disc))/(2*a);
System.out.println("roots are "+x1+","+x2);
}
else
{
System.out.println("roots are imaginary");
}
}
}
3. Write a JAVA program to display the Fibonacci Sequence
class Fibonacci
{
public static void main(String args[])
{
int num = Integer.parseInt(args[0]);
System.out.println("*****Fibonacci Series*****");
int f1, f2=0, f3=1;
for(int i=1;i<=num;i++)
{
System.out.print(" "+f3+" ");
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
}
}
4. Write a JAVA program give example for command line arguments
public class Sum1
{
public static void main(String[] args)
{
int sum;

int i;
sum = 0;
for ( i = 0; i < args.length; i++ )
{
sum = sum + args[i];
}
System.out.println( sum );
}
}
5. Write a JAVA program to sort given list of numb
import java.util.Scanner;
class BubbleSort
{
public static void main(String []args)
{
int n, i, j, swap;
Scanner in = new Scanner (System.in);
System.out.println ("Input number of integers to sort");
n = in.nextInt();
int array[] = new int[n];
System.out.println("Enter " + n + " integers");
for (i = 0; i < n; i++)
array[i] = in.nextInt();
for (i = 0; i < ( n - 1 ); i++) {
for (j = 0; j < n - c - 1; j++) {
if (array[j] > array[j+1]) /* For descending order use < */
{
swap
= array[j];
array[j]
= array[j+1];
array[j+1] = swap;
}
}
}
System.out.println("Sorted list of numbers");
for (i = 0; i < n; i++)
System.out.println(array[i]);
}
}

6. Write a JAVA program to search for an element in a given list of elements (linear search)
import java.util.Scanner;
class LinearSearch
{
public static void main(String args[])
{

int c, n, search, array[];


Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
System.out.println("Enter value to find");
search = in.nextInt();
for (c = 0; c < n; c++)
{
if (array[c] == search)
{
System.out.println(search + " is present at location " + (c + 1) + ".");
break;
}
}
if (c == n)
System.out.println(search + " is not present in array.");
}
}
7. Write a JAVA program to search for an element in a given list of elements using binary search
mechanism
import java.util.Scanner;
class BinarySearch
{
public static void main(String args[])
{
int c, first, last, middle, n, search, array[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
System.out.println("Enter value to find");
search = in.nextInt();
first = 0;
last = n - 1;
middle = (first + last)/2;

while( first <= last )


{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
System.out.println(search + " found at location " + (middle + 1) + ".");
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
System.out.println(search + " is not present in the list.\n");
}
}
8. Write a JAVA program to determine the addition of two matrices
import java.util.Scanner;
class AddTwoMatrix
{
public static void main(String args[])
{
int m, n, c, d;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of matrix");
m = in.nextInt();
n = in.nextInt();
int first[][] = new int[m][n];
int second[][] = new int[m][n];
int sum[][] = 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 elements of second matrix");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )

second[c][d] = in.nextInt();
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d]; //replace '+' with '-' to subtract matrices
System.out.println("Sum of entered matrices:-");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
System.out.print(sum[c][d]+"\t");
System.out.println();
}
}
}
9.

Write a JAVA program to determine the multiplication of two matrices


import java.util.Scanner;
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");
}
}
}
}
10. Write a JAVA program to sort array of strings
import java.lang.*;
import java.io.*;
import java.util.*;
class Sort
{
public static void main(String arg[ ]) throws IOException
{
DataInputStream dis=new DataInputStream(System.in);
System.out.println("enter the size");
int n=Integer.parseInt(dis.readLine());
String array[ ]=new String[n];
System.out.println("enter names");
for(int i=0;i<n;i++)
{
array[i]=dis.readLine();
}

for(int i=0;i<n;i++)
{
for(int j=0;j<n-1;j++)
{
if(array[j].compareTo(array[j+i])>0)
{
String temp=array[j];
array [j]=array[j+1];
array[j+1]=temp;
}
}
}
System.out.println("the sorted strings are");
for(int i=0;i<n;i++)
System.out.println(array[i]);
}
}
11. Write a JAVA program to check whether given string is palindrome or not
import java.util.*;
class Palindrome
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to check if it is a palindrome");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");
}
}
12. Write a JAVA program for the following
a. Example for call by value
b. Example for call by reference

class CallByValue {
public static void main ( String[] args ) {
int x =3;
System.out.println ( "Value of x before calling increment() is "+x);
increment(x);
System.out.println ( "Value of x after calling increment() is "+x);
}
public static void increment ( int a ) {
System.out.println ( "Value of a before incrementing is "+a);
a= a+1;
System.out.println ( "Value of a after incrementing is "+a);
}
}
class Number {
int x;
}
class CallByReference {
public static void main ( String[] args ) {
Number a = new Number();
a.x=3;
System.out.println("Value of a.x before calling increment() is "+a.x);
increment(a);
System.out.println("Value of a.x after calling increment() is "+a.x);
}
public static void increment(Number n) {
System.out.println("Value of n.x before incrementing x is "+n.x);
n.x=n.x+1;
System.out.println("Value of n.x after incrementing x is "+n.x);
}
}
13
Write a JAVA program to give the example for this operator. And also use the this
keyword as return statement
class Student11{
int id;
String name;
Student11(int id,String name){
this.id = id;

this.name = name;
}
void display(){System.out.println(id+" "+name);
}
public static void main(String args[]){
Student11 s1 = new Student11(111,"Karan");
Student11 s2 = new Student11(222,"Aryan");
s1.display();
s2.display();
}
}
14. Write a JAVA program to demonstrate static variables, methods, and blocks
class UseStatic {
static int a = 3;
static int b;
static void meth(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static {
System.out.println("Static block initialized.");
b = a * 4;
}
public static void main(String args[]) {
meth(42);
}
}
15. Write a JAVA program to give the example for super keyword
class Vehicle{
int speed=50;
}
class Bike4 extends Vehicle{
int speed=100;
void display(){
System.out.println(super.speed);//will print speed of Vehicle now
}
public static void main(String args[]){
Bike4 b=new Bike4();
b.display();
}
}

16. Write a JAVA program that illustrate simple inheritance


public class Inherit_Single {
protected String str;
Inherit_Single()
{
str = "Java ";
}
}
class SubClass extends Inherit_Single {
SubClass() {
str = str.concat("World !!!");
}
void display()
{
System.out.println(str);
}
}
class MainClass {
public static void main (String args[]){
SubClass obj = new SubClass();
obj.display();
}
}

17. Write a JAVA program that illustrate multi-level inheritance


public class Inherit_Multilevel {

protected String str;


Inherit_Multilevel() {
str = "J";
}
}
class SubClass1 extends Inherit_Multilevel {
SubClass1() {
str = str.concat("A");
}
}
class SubClass2 extends SubClass1 {
SubClass2() {
str = str.concat("V");
}
}
class SubClass3 extends SubClass2 {
SubClass3() {
str = str.concat("A");
}
void display() {
System.out.println(str);
}
}
class MainClass {
public static void main(String args[]) {
SubClass3 obj = new SubClass3();
obj.display();
}
}

18.Write a JAVA program demonstrating the difference between method


overloading and method overriding

The following application demonstrates method overloading:


class OverloadDemo
{
void triangleArea(float base, float height)
{
float area;
area = base * height / 2.0f;
System.out.println(Area = + Area);
}
void triangleArea(float side1, float side2, float side3)
{
float area,s;
s = (side1 + side2 + side3) / 2.0;
area = Math.sqrt(s*(s-side1) * (s-side2) * (s-side3) );
System.out.println(Area = + area);
}
}
class MainOverloadDemo
{
public static void main(String args[])
{
OverloadDemo ovrldDemo = new OverloadDemo();
ovrldDemo.triangleArea(20.12,58.36);
ovrldDemo triangleArea(63.12,54.26,95.24);
}
}

code example for method overriding:


class Super
{
int sum;
A(int num1, int num2)
{
sum = a+b;
}
void add()
{

System.out.println("Sum : " + sum);


}
}
class Sub extends Sub
{
int subSum;
Sub(int num1, int num2, int num3)
{
super(num1, num2);
subSum = num1+num2+num3;
}
void add()
{
super.add();
System.out.println("Sum of 3 nos : " +subSum);
}
}
19. Write a JAVA program demonstrating the difference between method
overloading and constructor overloading

class OverloadDemo
{
void triangleArea(float base, float height)
{
float area;
area = base * height / 2.0f;
System.out.println(Area = + Area);
}
void triangleArea(float side1, float side2, float side3)
{
float area,s;
s = (side1 + side2 + side3) / 2.0;
area = Math.sqrt(s*(s-side1) * (s-side2) * (s-side3) );
System.out.println(Area = + area);
}
}
class MainOverloadDemo
{
public static void main(String args[])
{
OverloadDemo ovrldDemo = new OverloadDemo();

ovrldDemo.triangleArea(20.12,58.36);
ovrldDemo triangleArea(63.12,54.26,95.24);
}
}
constructor Overloading:
class OverLoad
{
int age, num1,num2,num3,sum,e,b,c;
String name;
OverLoad(String n,int a) //constructor
{
name=n;
age=a;
}
void disp()
{
System.out.println("Name of the person :"+name);
System.out.println("age of the person :"+age);
}
OverLoad(int e,int b,int c)
{
num1=e;
num2=b;
num3=c;
}
void show()
{
int sum=num1+num2+num3;
System.out.println("sum =:"+sum);
}
public static void main(String args[])
{
OverLoad ob=new OverLoad("kapil" ,19);
ob.disp();
OverLoad ab=new OverLoad(4,5,6);
ab.show();
}
}

21.Write a JAVA program that describes exception handling mechanism


22.Write a JAVA program for example of try and catch block. In this check
whether the given array size is negative or not

class NegTest
{
public static void main(String a[])
{
try
{
int a1[] = new int[-2];
System.out.println(first element : +a1[0]);
}
catch(NegativeArraySizeException n)
{
System.out.println( generated exception : + n);
}
System.out.println( After the try block);
}
}

S-ar putea să vă placă și