Lab 1 - 5
Lab 1 - 5
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
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));
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
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));
}
}
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;
}
}
}
}
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;
}
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.");
}
}
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
class Animal {
String name = "Animal Name";
public void sound() {
System.out.println("Animal makes a sound.");
}
}
interface Drive {
void accelerate();
}
interface Stop {
void brake();
}
class Animal {
public void sound() {
System.out.println("Executing parent class method");
System.out.println("Animal makes a sound.");
}
}