Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Java Lab File

Uploaded by

Tech Army
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Java Lab File

Uploaded by

Tech Army
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Lab File

of

Object Oriented Programming in Java

In partial fulfillment of the requirements of the award of the

Degree of Bachelor of Technology

in

Computer Science Engineering

by

Yash Manav
System ID: 2023286700
Under the mentorship of

Mr. Sushant Jhingran

Department of CSE
School of Engineering & Technology
Sharda University
Greater Noida
[2024-25]
INDEX
S.No Experiment Title Page No. Signature
Experiment 1
Inheritance in java

Code:

public class Inheritance {


int roll_no;
String name;

public void initial(int r,String n) {


roll_no = r;
name = n;
}

}
public class Inherit_class extends Inheritance{
public static void main(String[] args) {

Inheritance obj = new Inheritance();


obj.initial(1, "Ravi");
System.out.println(obj.name);

Inherit_class obj2 = new Inherit_class();


obj2.name = "Yash";
System.out.println(obj2.name);
}
}

OUTPUT
Experiment 2
Polymorphism in java
(i) overloading

Code:
public class Overloading {
public static void main(String[] args) {
sum(10);
sum(5,10);
}

public static void sum(int a) {


System.out.println(a+10);
}

public static void sum(int a,int b) {


System.out.println(a+b);
}

OUTPUT
(ii) Overriding

Code:

class Parent {
public void m1()
{
System.out.println("From parent m1()");
}

public void m2()


{
System.out.println("From parent m2()");
}
}

class Child extends Parent {


public void m1()
{
System.out.println("From child m1()");
}

public void m2()


{
System.out.println("From child m2()");
}
}

class Main {
public static void main(String[] args)
{
Parent obj1 = new Parent();
obj1.m2();
Parent obj2 = new Child();
obj2.m2();
}
}
Experiment 3
WAP in java to check if a number is even or not.

Code:

import java.util.Scanner;
public class EvenorOdd {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
System.out.println("Enter a number: ");
n = sc.nextInt();
if(n%2 == 0)
System.out.println("Number is even");
else
System.out.println("Number is odd");
sc.close();
}
}

OUTPUT:
Experiment 4
WAP in java to search a number in an array

Code:

import java.util.Scanner;
public class LinearSearch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int a[] = {2,5,6,34,23,5,-2,-4,0,11};


System.out.println("Enter the number to be searched: ");
int n = sc.nextInt();
int f = -1;

for(int i = 0;i<a.length;i++) {
if(a[i] == n) {
f = i;
break;
}
}
if(f!=-1)
System.out.println("Number is at "+f+" index");
else
System.out.println("Number is not found");
}
}

OUTPUT:
Experiment 5
WAP in java to reverse the given string

Code:

import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string: ");
String s = sc.nextLine();
String rs = "";
for(int i = s.length()-1;i>=0;i--) {
rs = rs+s.charAt(i);
}
System.out.println("Reverse String: "+rs);
}
}

OUTPUT:

You might also like