Java Lab
Java Lab
Java Lab
1.b)
import java.util.*;
class Main {
public static void main(String[] args) {
int a, b, c;
Double d, r1, r2;
Scanner s = new Scanner(System.in);
System.out.println("enter a value");
a = s.nextInt();
System.out.println("enter b value");
b = s.nextInt();
System.out.println("enter c value");
c = s.nextInt();
d = b * b - 4.0 * a * c;
if (d > 0) {
System.out.println("roots are real and unequal");
r1 = (-b + Math.sqrt(d)) / (2 * a); // Fix precedence issue
r2 = (-b - Math.sqrt(d)) / (2 * a); // Fix precedence issue
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 / (2.0 * a); // Fix equal root calculation
System.out.println("first root is " + r1);
} else {
System.out.println("they are imaginary");
}
}
}
2.a)BINARY SRCH
import java.util.Scanner;
class Main {
public static void main(String[] args) {
int n, num, f, l, mid;
int[] a = new int[20];
Scanner s = new Scanner(System.in);
f = 0;
l = n - 1;
while (f <= l) {
mid = (f + l) / 2; // Calculate mid-point
if (a[mid] < num) {
f = mid + 1; // Adjust lower bound
} else if (a[mid] == num) {
System.out.println("Element found at index: " + mid);
return; // Exit the program once the element is found
} else {
l = mid - 1; // Adjust upper bound
}
}
2(b).BUBBLE SORT
import java.util.Scanner;
class Main {
public static void main(String[] args) {
int n, i, j, temp;
int[] a = new int[20];
Scanner s = new Scanner(System.in);
// Bubble sort
for (i = 0; i < n; i++) {
for (j = 0; j < n - i - 1; j++) { // Adjust inner loop to exclude sorted elements
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp; // Swap elements
}
}
}
3.aClass Mechanism
class A {
int l=10,b=20;
void display() {
System.out.println(l);
System.out.println(b);
}
}
class Main {
public static void main(String args[]) {
A a1=new A();
a1.display();
}
}
2.
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);
}
}
3(b).Method ol
class Main {
void display() {
System.out.println("Display with no parameters.");
}
1. Default Constructor
java
Copy code
class DefaultConstructor {
int number;
// Default constructor
DefaultConstructor() {
number = 5;
}
void display() {
System.out.println("Number: " + number);
}
// Parameterized constructor
ParameterizedConstructor(int num) {
number = num;
}
void display() {
System.out.println("Number: " + number);
}
class B extends A {
public void disp() {
System.out.println("B class");
}
}
class B extends A {
4.c abstract
5.a super
class animal{
void sound(){
System.out.println("animals makes sounds");
}
}
class dog extends animal{
void sound(){
super.sound();
System.out.println("dog barks");
}
}
public class Main{
public static void main(String args[]){
dog d=new dog();
d.sound();
}
}
5.b INTERFACE
interface A{
public void disp();
}
class B implements A{
public void disp(){
System.out.println("class b");
}
}
class c extends B{
public void disp2(){
System.out.println("class c");
}
}
public class Main{
public static void main(String args[]){
c c1=new c();
c1.disp();
c1.disp2();
}
}
5c.Runtime Polymorphism
class A
{
void display()
{
System.out.println("Inside A class");
}
}
class B extends A
{
void display()
{
System.out.println("Inside B class");
}
}
class C extends A
{
void display()
{
System.out.println("Inside C class");
}
}
class Main
{
public static void main(String args[])
{
A a1=new A();
B b1=new B();
C c1=new C();
A ref;
ref=c1;
ref.display();
ref=b1;
ref.display();
ref=a1; ref.display();
}
}