Java Lab Manual
Java Lab Manual
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[])
{
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.
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();
}
}
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();
}
}
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);
}
}