0% found this document useful (0 votes)
19 views30 pages

Lab 1 - 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views30 pages

Lab 1 - 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

OUTPUT :

Default value of byte : 0


Default value of short : 0
Default value of int : 0
Default value of long : 0
Default value of float : 0.0
Default value of double : 0.0
Default value of char :
Default value of boolean : false
EX NO 1.a Write a Java program to display default value
DATE of all primitive data type of Java

public class Main


{
static byte a;
static short b;
static int c;
static long d;
static float e;
static double f;
static char g;
static boolean h;
public static void main(String[] args)
{
System.out.println("Default value of byte : " + a);
System.out.println("Default value of short : " + b);
System.out.println("Default value of int : " + c);
System.out.println("Default value of long : " + d);
System.out.println("Default value of float : " + e);
System.out.println("Default value of double : " + f);
System.out.println("Default value of char : " + g);
System.out.println("Default value of boolean : " + h);
}
}
OUTPUT :
ax^2 + bx = 0
Enter coe icient a :
3
Enter coe icient b :
36
The discriminant is: 1296.0
The equation has one real root: -6.0
EX NO 1.b Write a Java program that display the roots of a quadratic
equation ax2+bx=0. Calculate the discriminate D and
DATE basing on value of D, describe the nature of root

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("ax^2 + bx = 0");
System.out.println("Enter coe icient a : ");
double a = scanner.nextDouble();
System.out.println("Enter coe icient b : ");
double b = scanner.nextDouble();
double D = b * b;
System.out.println("The discriminant is: " + D);
if (D > 0)
{
double root1 = -b / (2 * a);
System.out.println("The equation has one real root: " + root1);
}
else if (D == 0)
{
double root = -b / (2 * a);
System.out.println("The equation has a double real root: " + root);
}
else
{
System.out.println("The equation has no real roots.");
}

scanner.close();
}
}
OUTPUT:
Element found at the index 8
EX NO 2.a Write a Java program to search for an element in a given list
DATE of elements using Binary Search mechanism

public class Main


{
public static void main(String[] args) {
int[] arr = {11,22,33,44,55,66,77,88,99};
int target = 99;
int result = -1;
int left = 0;
int right = arr.length - 1;
while (left <= right)
{
int mid = left + (right-left)/2;
if(arr[mid] == target)
{
result = mid ;
break;
}
if(arr[mid] < target)
{
left = mid + 1 ;
}
else
{
right = mid -1 ;
}
}
if(result == -1)
{
System.out.println("Element not found");
}
else
{
System.out.println("Element found at the index " +result);
}
}}
OUTPUT :
Original Array:
[5, 3, 6, 1, 2, 4, 9, 8, 7]
Sorted Array:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
EX NO 2.b Write a Java program to sort for an element in a given list of
DATE elements using Bubble Sort

import java.util.Arrays;
public class Main
{
public static void main(String[] args)
{
int[] arr = {5, 3, 6, 1, 2, 4, 9, 8, 7};
System.out.println("Original Array:");
System.out.println(Arrays.toString(arr));

for (int i = 0; i < arr.length - 1; i++)


{
for (int j = 0; j < arr.length - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

System.out.println("Sorted Array:");
System.out.println(Arrays.toString(arr));
}
}
OUTPUT :
After deleting character at index 5: HelloWorld!
After removing substring 'World': Hello!
After clearing the bu er:
EX NO 2.c Write a JAVA program using String Buffer to delete, remove
DATE character

public class Main


{
public static void main(String[] args)
{
StringBu er sb = new StringBu er("Hello World!");

sb.deleteCharAt(5);
System.out.println("After deleting character at index 5: " + sb);

sb.delete(5, 10);
System.out.println("After removing substring 'World': " + sb);

sb.delete(0, sb.length());
System.out.println("After clearing the bu er: " + sb);
}
}
OUTPUT :
Sum is : 15
Di erence is : 5
EX NO 3.a Write a Java program to implement class mechanism.
Create a class, methods and invoke them inside main
DATE method

class Calculator
{
int a = 10 ;
int b = 5 ;
public void add()
{
System.out.println("Sum is : " +(a+b));
}
public void subtract()
{
System.out.println("Di erence is : " +(a-b));
}
}

public class Main


{
public static void main(String[] args)
{
Calculator obj = new Calculator();
obj.add();
obj.subtract();
}
}
OUTPUT :
Sum of two integers: 30
Sum of three integers: 60
Sum of two doubles: 30.8
EX NO 3.b Write a Java program implement Method Overloading
DATE

class Calculator
{
public int add(int a, int b)
{
return a + b;
}
public int add(int a, int b, int c)
{
return a + b + c;
}
public double add(double a, double b) {
return a + b;
}
}

public class Main


{
public static void main(String[] args)
{
Calculator obj = new Calculator();

System.out.println("Sum of two integers: " + obj.add(10, 20));


System.out.println("Sum of three integers: " + obj.add(10, 20, 30));
System.out.println("Sum of two doubles: " + obj.add(10.5, 20.3));

}
}
OUTPUT :
Name is : James
Age is : 34
EX NO 3.c Write a Java program to implement Constructor
DATE

class Person
{
String name;
int age;

public Person()
{
name = "James";
age = 34;
}

public void display()


{
System.out.println("Name is : " + name);
System.out.println("Age is : " + age);
}
}

public class Main


{
public static void main(String[] args)
{
Person obj = new Person();
obj.display();
}
}
OUTPUT :
Name is : James
Age is : 34
Name is : Stephen
Age is : 34
Name is : David
Age is : 34
EX NO 3.d Write a Java program to implement
DATE Constructor Overloading

class Person
{
String name;
int age;
public Person() {
name = "James";
age = 34;
}
public Person(String x) {
name = x;
age = 34;
}
public Person(String x , int y) {
name = x;
age = y;
}
public void display()
{
System.out.println("Name is : " + name);
System.out.println("Age is : " + age);
}
}
public class Main {
public static void main(String[] args) {
Person obj = new Person();
Person obj2 = new Person("Stephen");
Person obj3 = new Person("David" , 34);
obj.display();
obj2.display();
obj3.display();
}
}
OUTPUT :
This animal eats food.
The dog barks.
EX NO 4.a Write a Java program to implement
DATE Single Inheritance

class Animal
{
public void eat()
{
System.out.println("This animal eats food.");
}
}

class Dog extends Animal


{
public void bark()
{
System.out.println("The dog barks.");
}
}

public class Main


{
public static void main(String[] args)
{
Dog obj = new Dog();

obj.eat();
obj.bark();
}
}
OUTPUT :
Eating...
Barking...
Weeping...
EX NO 4.b Write a Java program to implement
DATE Multi Level Inheritance

class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}
class Puppy extends Dog {
void weep() {
System.out.println("Weeping...");
}
}
public class Main
{
public static void main(String[] args)
{
Puppy obj = new Puppy();

obj.eat();
obj.bark();
obj.weep();
}
}
OUTPUT :
Area of Circle: 314.1592653589793
Area of Rectangle: 50.0
EX NO 4.c Write a Java program for abstract class to
DATE find areas of different shapes

abstract class Shape {


abstract void area();
}

class Circle extends Shape {


double radius = 10 ;
void area() {
double x = Math.PI * radius * radius ;
System.out.println("Area of Circle: " + x);
}
}

class Rectangle extends Shape {


double length = 10 ;
double breadth = 5 ;
void area() {
double x = length * breadth ;
System.out.println("Area of Rectangle: " + x);
}
}
public class Main {
public static void main(String[] args) {
Circle obj1 = new Circle();
obj1.area();
Rectangle obj2 = new Rectangle();
obj2.area();
}
}
OUTPUT :
Animal makes a sound.
Animal Name
EX NO 5.a Write a Java program give example for
DATE “super” keyword

class Animal {
String name = "Animal Name";
public void sound() {
System.out.println("Animal makes a sound.");
}
}

class Dog extends Animal {


public void sound() {
super.sound();
System.out.println(super.name);
}
}

public class Main {


public static void main(String[] args) {
Dog obj = new Dog();
obj.sound();
}
}
OUTPUT :
Car is accelerating
Car is braking
EX NO 5.b
DATE Write a Java program to implement Interface

interface Drive {
void accelerate();
}

interface Stop {
void brake();
}

class Car implements Drive , Stop {

public void accelerate() {


System.out.println("Car is accelerating");
}

public void brake() {


System.out.println("Car is braking");
}
}

public class Main {


public static void main(String[] args) {
Car obj = new Car();
obj.accelerate();
obj.brake();
}
}
OUTPUT :
Executing child class method
Dog is barking.
EX NO 5.c Write a Java program that implements
DATE Runtime Polymorphism

class Animal {
public void sound() {
System.out.println("Executing parent class method");
System.out.println("Animal makes a sound.");
}
}

class Dog extends Animal {


public void sound() {
System.out.println("Executing child class method");
System.out.println("Dog is barking. ");
}
}

public class Main {


public static void main(String[] args) {
Dog obj = new Dog();
obj.sound();
}
}

You might also like