0% found this document useful (0 votes)
2 views

Lab

The document contains multiple Java programming exercises covering various concepts such as primitive data types, quadratic equations, binary search, sorting algorithms, string manipulation, class mechanisms, constructors, inheritance, abstract classes, the 'super' keyword, interfaces, and runtime polymorphism. Each exercise includes a Java program, its output, and explanations of the concepts being demonstrated. The document serves as a comprehensive guide for learning and implementing fundamental Java programming techniques.

Uploaded by

Hemanth Atla
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab

The document contains multiple Java programming exercises covering various concepts such as primitive data types, quadratic equations, binary search, sorting algorithms, string manipulation, class mechanisms, constructors, inheritance, abstract classes, the 'super' keyword, interfaces, and runtime polymorphism. Each exercise includes a Java program, its output, and explanations of the concepts being demonstrated. The document serves as a comprehensive guide for learning and implementing fundamental Java programming techniques.

Uploaded by

Hemanth Atla
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Exercise - 1

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();

2. noreturn type and with parameter-list:


class A
{
void display(int l,int b)
{
System.out.println(l);
System.out.println(b);
}
}
class methoddemo
{
public static void main(String args[])
{
A a1=new A();
a1.display(10,20);
}
}
Output:
10
20

3.return type and without parameter-list


class A
{
int l=10,b=20;
int area()
{
return l*b;
}
}
class methoddemo
{
public static void main(String args[])
{
A a1=new
A(); int
r=a1.area();
System.out.println("The area is: "+r);
}
}
Output:
The area is:200

4. return type and with parameter-list:


class A
{
int area(int l,int b)
{
return l*b;
}
}
class methoddemo
{
public static void main(String args[])
{
A a1=new A();
int r=a1.area(10,20);
System.out.println(“The area is:”+r);
}
}
Output:
The area is:20
Aim: b)To write a JAVA program to implement constructor
Programs:
(i) A constructor with no parameters:
class A
{
int
l,b;
A()
{ l=10;
b=20
;
}
int area()
{
return l*b;
}
}
class constructordemo
{
public static void main(String args[])
{
A a1=new
A(); int
r=a1.area();
System.out.println("The area is: "+r);

}
}
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

b) Write a JAVA program to implement multi level 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 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:

abstract class shape


{
abstract double area();
}
class rectangle extends shape
{
double
l=12.5,b=2.5;
double area()
{
return l*b;
}
}
class triangle extends shape
{
double b=4.2,h=6.5;
double area()
{
return 0.5*b*h;
}
}
class square extends shape
{
double s=6.5;
double area()
{
return 4*s;
}
}
class shapedemo
{
public static void main(String[] args)
{
rectangle r1=new
rectangle(); triangle t1=new
triangle(); square s1=new
square();
System.out.println("The area of rectangle is:
"+r1.area()); System.out.println("The area of triangle
is: "+t1.area()); System.out.println("The area of
square is: "+s1.area());
}
}

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

(ii) Using super to call super class constructor (With parameters)


class A
{
int l,b;
A(int u,int v)
{
l=u;
b=v
;
}
}
class B extends A
{
int h;
B(int u,int v,int w)
{
super(u,v)
; h=w;
}
int volume()
{
return l*b*h;
}
}
class superdemo
{
public static void main(String args[])
{
B b1=new B(30,20,30);
int r=b1.volume();
System.out.println("The vol. is: "+r);
}
}

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.");
}
}

// Implement the interface in a class


class Car implements Vehicle {
// Implement the start method
@Override
public void start() {
System.out.println("The car starts.");
}

// Implement the stop method


@Override
public void stop() {
System.out.println("The car stops.");
}

// Optionally override the default method


@Override
public void honk() {
System.out.println("The car honks: Beep beep!");
}
}

// Another class implementing the same interface


class Bike implements Vehicle {
// Implement the start method
@Override
public void start() {
System.out.println("The bike starts.");
}

// Implement the stop method


@Override
public void stop() {
System.out.println("The bike stops.");
}

// Using the default method from the interface


}

// Main class to test the interface and its implementations


public class Main {
public static void main(String[] args) {
Vehicle myCar = new Car();
myCar.start(); // Outputs: The car starts.
myCar.honk(); // Outputs: The car honks: Beep beep!
myCar.stop(); // Outputs: The car stops.

Vehicle myBike = new Bike();


myBike.start(); // Outputs: The bike starts.
myBike.honk(); // Outputs: The vehicle honks.
myBike.stop(); // Outputs: The bike stops.
}
}
Output:
The car starts.
The car honks: Beep beep!
The car stops.
The bike starts.
The vehicle honks.
The bike stops.

c) Write a JAVA program that implements Runtime polymorphism


program:
// Superclass
class Animal {
// Method that will be overridden
void sound() {
System.out.println("Animal makes a sound");
}
}

// 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");
}
}

// Main class to test runtime polymorphism


public class Main {
public static void main(String[] args) {
// Create an Animal reference but point to a Dog object
Animal myAnimal = new Dog();
myAnimal.sound(); // Outputs: Dog barks

// Change reference to Cat object


myAnimal = new Cat();
myAnimal.sound(); // Outputs: Cat meows

// You can also declare an array of Animals


Animal[] animals = {new Dog(), new Cat()};

// Loop through the array and call sound method


for (Animal animal : animals) {
animal.sound(); // Outputs: Dog barks and Cat meows
}
}
}

Output:
Dog barks
Cat meows
Dog barks
Cat meows

You might also like