Java Lab

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

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

System.out.println("Enter the number of elements:");


n = s.nextInt();

System.out.println("Enter elements in sorted order:");


for (int i = 0; i < n; i++) { // Correct loop variable scope
a[i] = s.nextInt();
}

System.out.println("Enter the element to search:");


num = s.nextInt();

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

System.out.println("Element not found");


}
}

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

System.out.println("Enter the number of elements:");


n = s.nextInt();

System.out.println("Enter the elements:");


for (i = 0; i < n; i++) {
a[i] = s.nextInt(); // Input array elements
}

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

// Print sorted array


System.out.println("Sorted elements:");
for (i = 0; i < n; i++) {
System.out.print(a[i] + "\t");
}
}
}

2(C). STRing BUFFEr


class Main {
public static void main(String[] args) {
StringBuffer sb1=new StringBuffer("hello world");
sb1.deleteCharAt(0);
sb1.length();
sb1.delete(0,6);
System.out.println(sb1);
}
}

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

void display(int num) {


System.out.println("Display with one parameter: " + num);
}
}

public class MethodOverloadingDemo {


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

obj.display(); // Calls method with no parameters


obj.display(10); // Calls method with one parameter
}
}
3.c Constructor
Here are concise examples of both a default constructor and a parameterized
constructor:

1. Default Constructor
java
Copy code
class DefaultConstructor {
int number;

// Default constructor
DefaultConstructor() {
number = 5;
}

void display() {
System.out.println("Number: " + number);
}

public static void main(String[] args) {


DefaultConstructor obj = new DefaultConstructor();
obj.display();
}
}
2. Parameterized Constructor
java
Copy code
class ParameterizedConstructor {
int number;

// Parameterized constructor
ParameterizedConstructor(int num) {
number = num;
}

void display() {
System.out.println("Number: " + number);
}

public static void main(String[] args) {


ParameterizedConstructor obj = new ParameterizedConstructor(10);
obj.display();
}
}
3(d).Constructor OL
class Main{
Main(){
System.out.println("default constructor");
}
Main(int l,int b){
int area=l*b;
System.out.println("area is:"+area);
}

public static void main(String[] args){


Main m1=new Main();
Main m2=new Main(10,20);
}
}
4.a single inheritance
class A {
public void disp() {
System.out.println("A class");
}
}

class B extends A {
public void disp() {
System.out.println("B class");
}
}

public class Main {


public static void main(String[] args) {
A a1 = new A(); // Object of class A
B b1 = new B(); // Object of class B
a1.disp(); // Calls disp() method from class A
b1.disp(); // Calls disp() method from class B (overridden)
}
}
4.b Multi level
class A {
public void disp() {
System.out.println("A class");
}
}

class B extends A {

public void disp() {


System.out.println("B class");
}
}
class C extends B {

public void disp() {


System.out.println("c class");
}
}
public class Main {
public static void main(String[] args) {
A a1 = new A(); // Object of class A
B b1 = new B();
C c1=new C();
a1.disp();
b1.disp();
c1.disp();
}
}

4.c abstract

abstract class shape{


abstract double area();
}
class rect extends shape{
double l=12,b=20;
double area(){
return l*b;
}
}
class sq extends shape{
double s=40;
double area(){
return 4*s;
}
}
public class Main{
public static void main(String args[]){
rect r=new rect();
sq s=new sq();
System.out.println("area of rectangle is"+r.area());
System.out.println("area of square is"+s.area());
}

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

}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy