Lab
Lab
Aim:a) To write a JAVA program to display default value of all primitive data type of JAVA
Program:
class defaultdemo
{
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("The default values of primitive data types are:");
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);
}
}
Output:
The default values of primitive data types are:
Byte :0
Short :0
Int :0
Long :0
Float :0.0
Double :0.0
Char :
Boolean :fals
e
Aim: b)To write a java program that display the roots of a quadratic equation ax2+bx=0.
Calculate the discriminate D and basing on value of D, describe the nature of root.
Program:
import java.util.*;
class
quadraticdemo
{
public static void main(String[] args)
{
int a, b, c;
double r1, r2,
D;
Scanner s = new Scanner(System.in);
System.out.println("Given quadratic equation:ax^2 + bx +
c"); System.out.print("Enter a:");
a = s.nextInt();
System.out.print("Enter
b:"); b = s.nextInt();
System.out.print("Enter
c:"); c = s.nextInt();
D=b*b-4*a*
c; if(D > 0)
{
System.out.println("Roots are real and
unequal"); r1 = ( - b + Math.sqrt(D))/(2*a);
r2 = (-b - Math.sqrt(D))/(2*a);
System.out.println("First root is:"+r1);
System.out.println("Second root is:"+r2);
}
else if(D == 0)
{
System.out.println("Roots are real and
equal"); r1 = (-b+Math.sqrt(D))/(2*a);
System.out.println("Root:"+r1);
}
else
{
System.out.println("Roots are imaginary");
}
}
}
Output:
Given quadratic equation:ax^2 + bx +
c Enter a:2
Enter b:3
Enter c:1
Roots are real and unequal
First root is:-0.5
Second root is:-1.0
Exercise - 2
a) Write a JAVA program to search for an element in a given list of elements using
binarysearch mechanism.
Program:
import java.util.Scanner;
class binarysearchdemo
{
public static void main(String args[])
{
int n, i, num,first, last,
middle; int a[ ]=new
int[20];
Scanner s = new Scanner(System.in);
System.out.println("Enter total number of
elements:"); n = s.nextInt();
System.out.println("Enter elements in sorted
order:"); for (i = 0; i < n; i++)
a[i] = s.nextInt();
System.out.println("Enter the search
value:"); num = s.nextInt();
first = 0;
last = n -
1;
middle = (first + last)/2;
while( first <= last )
{
if ( a[middle] < num
) first = middle + 1;
else if ( a[middle] == num )
{
System.out.println("number
} found"); break;
else
{
} last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
System.out.println( " Number is not found");
}
}
Output:
Enter total number of
elements: 5
Enter elements:
24689
Enter the search
value: 8
number found
b) Write a JAVA program to sort for an element in a given list of elements using bubble
sort
Program:
Importjava.util.Scanner;
class bubbledemo
{
public static void main(String args[])
{
int n, i,j, temp;
int a[ ]=new int[20];
Scanner s = new Scanner(System.in);
System.out.println("Enter total number of
elements:"); n = s.nextInt();
System.out.println("Enter
elements:"); for (i = 0; i < n; i++)
a[i] = s.nextInt();
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("The sorted elements are:");
for(i=0;i<n;i++)
System.out.print("\t"+a[i]);
}
}
Output:
Enter total number of elements:
10
Enter elements:
3257689140
The sorted elements are:
0 12 3 4 5 6 7 8 9
c)Write a JAVA program using String Buffer to delete, remove character.
Program:
class stringbufferdemo
{
public static void main(String[] args)
{
StringBuffer sb1 = new StringBuffer("Hello
World"); sb1.delete(0,6);
System.out.println(sb1);
StringBuffer sb2 = new StringBuffer("Some
Content"); System.out.println(sb2);
sb2.delete(0, sb2.length());
System.out.println(sb2);
StringBuffer sb3 = new StringBuffer("Hello
World"); sb3.deleteCharAt(0);
System.out.println(sb3);
}
}
Output:
World
Some Content
ello World
Exercise – 3:
Aim: a)To write a JAVA program to implement class mechanism. – Create a class, methods and
invoke them inside main method
Programs:
1. no return type and without parameter-list:
class A
{
int l=10,b=20;
void display()
{
System.out.println(l);
System.out.println(b);
}
}
class methoddemo
{
public static void main(String args[])
{
Output:
10
} 20
}
A a1=new A();
a1.display();
}
}
Output:
The area is:200
(ii) A constructor with parameters
class A
{
int l,b;
A(int u,int v)
{
l=u;
b=v;
}
int area()
{
return l*b;
}
}
class constructordemo
{
public static void main(String args[])
{
A a1=new A(10,20);
int r=a1.area();
System.out.println("The area is:
"+r);
}
}
Output:
The area is:200
Aim:c) To write a JAVA program to implement constructor overloading
Program:
class A
{
int l,b;
A()
{
l=10;
b=20
;
}
A(int u,int v)
{
l=u;
b=v;
}
int area()
{
return l*b;
}
}
class overconstructdemo
{
public static void main(String args[])
{
A a1=new A();
int
r1=a1.area();
System.out.println("The area is:
"+r1); A a2=new A(30,40);
int r2=a2.area();
System.out.println("The area is:
"+r2);
}
}
Output:
The area is: 200
The area is:
1200
Aim: d)To write a JAVA program implement method overloading
Program:
ClassA
{
int
l=10,b=20;
int area()
{
return l*b;
}
int area(int l,int b)
{
return l*b;
}
}
class overmethoddemo
{
public static void main(String args[])
{
A a1=new A();
int
r1=a1.area();
System.out.println("The area is:
"+r1); int r2=a1.area(5,20);
System.out.println("The area is:
"+r2);
}
}
Output:
The area is: 200
The area is: 100
Exercise – 4
a) Write a JAVA program to implement Single Inheritance
Program:
class A
{
A( }
)
{
}
System.out.println("Inside A's Constructor");
class B extends A
{
B(
)
{ System.out.println("Inside B's Constructor");
}
}
class singledemo
{
public static void main(String args[])
{
B b1=new B();
}
}
Output:
Inside A's Constructor
Inside B's Constructor
Program:
class A
{
A(
)
{ System.out.println("Inside A's Constructor");
}
}
class B extends A
{
B(
)
{ System.out.println("Inside B's Constructor");
}
}
class C extends B
{
C()
{
System.out.println("Inside C's Constructor");
}
}
class multidemo
{
public static void main(String args[])
{
C c1=new C();
}
}
Output:
Inside A's Constructor
Inside B's Constructor
Inside C's Constructor
c) Write a JAVA program for abstract class to find areas of different shapes
program:
Output:
The area of rectangle is: 31.25
The area of triangle is: 13.65
The area of square is: 26.0
Exercise - 5
a) Write a JAVA program give example for “super” keyword.
Programs:
(i) Using super to call super class constructor (Without parameters)
class A
{
int l,b;
A()
{
l=10;
b=20
;
}
}
class B extends A
{
int h;
B()
{
super()
; h=30;
}
int volume()
{
return l*b*h;
}
}
class superdemo
{
public static void main(String args[])
{
B b1=new B();
int r=b1.volume();
System.out.println("The vol. is: "+r);
}
}
Output:
The vol. is:6000
Output:
The vol. is:18000
b) Write a JAVA program to implement Interface. What kind of Inheritance can be achieved?
Program:
// Define the interface
interface Vehicle {
// Abstract method
void start();
// Abstract method
void stop();
// Default method
default void honk() {
System.out.println("The vehicle honks.");
}
}
// Subclass 1
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
// Subclass 2
class Cat extends Animal {
@Override
void sound() {
System.out.println("Cat meows");
}
}
Output:
Dog barks
Cat meows
Dog barks
Cat meows