0% found this document useful (0 votes)
11 views

OOP Lab

The document contains 8 code examples demonstrating various Java concepts like command line arguments, static functions, interfaces, method overloading, constructor overloading, inheritance, abstract classes, user input using Scanner class, and multithreading. For each example, the code and output are provided. The concepts covered include classes, objects, interfaces, inheritance, polymorphism, abstraction and multithreading.

Uploaded by

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

OOP Lab

The document contains 8 code examples demonstrating various Java concepts like command line arguments, static functions, interfaces, method overloading, constructor overloading, inheritance, abstract classes, user input using Scanner class, and multithreading. For each example, the code and output are provided. The concepts covered include classes, objects, interfaces, inheritance, polymorphism, abstraction and multithreading.

Uploaded by

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

Omdayal Group of Institutions

PCA1 Lab Copy

Name: Sandip Majhi

Roll No: 27500122058

Subject: OOP Lab

Semester: 5th

Year: 2023-2024
1. WAP to input salary from command line argument.

Code:

public class Salary {


public static void main(String args[])
{
String name = args[0];
int salary = Integer.parseInt(args[1]);
System.out.println("Name is: "+name);
System.out.println("Salary is: "+salary);
}
}

Output:
javac Salary.java
java Salary “Sandip” 5000

Name is: Sandip


Salary is: 5000

2. WAP to implement static function.

Code:

public class Static_func {


static String name = "Sandip";
String roll = "27500122058";

static void displayName()


{
System.out.println("Name is: "+name);
}
void displayRoll()
{
System.out.println("Roll is: "+roll);
}

public static void main(String args[])


{
Static_func x = new Static_func();
displayName();
x.displayRoll();

}
}

Output:
Name is: Sandip
Roll is: 27500122058

3. WAP to implement Interface.

Code:

interface A
{
static final int a=20;
abstract public void displayA();
}
interface B
{
static final int b=10;
abstract public void displayB();
}

public class Interface implements A, B {


public void displayA()
{
System.out.println("A is: "+a);
}
public void displayB()
{
System.out.println("B is: "+b);
}
public static void main(String args[])
{
Interface i = new Interface();
i.displayA();
i.displayB();
}
}

Output:
A is: 20
B is: 10

4. WAP to implement method overloading.

Code:

class Addition
{
public int sum(int a, int b)
{
return a+b;
}
public int sum(int a, int b, int c)
{
return a+b+c;
}
}

public class Overloading {


public static void main(String args[])
{
Addition a = new Addition();
int r1 = a.sum(2,3);
int r2 = a.sum(4,6,8);
System.out.println("Sum of two numbers is: "+r1);
System.out.println("Sum of three numbers is: "+r2);
}
}

Output:
Sum of two numbers is: 5
Sum of three numbers is: 18

5. WAP to implement constructor overloading.


Code:

class Rectangle
{
int length,breadth;
Rectangle()
{
length = 4;
breadth = 8;
}
Rectangle(int length, int breadth)
{
this.length = length;
this.breadth = breadth;
}

public void display()


{
System.out.println("length is: "+length);
System.out.println("breadth is: "+breadth);
}
}

public class Constructor {


public static void main(String args[])
{
Rectangle r1 = new Rectangle();
Rectangle r2 = new Rectangle(6,9);

r1.display();
r2.display();
}
}

Output:
length is: 4
breadth is: 8
length is: 6
breadth is: 9
6. WAP to implement Inheritance and abstract class in java.

Code:

abstract class Employee{


String name;
String id;
String address;
double salary;
Employee(String name, String id, String address, double salary)
{
this.name=name;
this.id=id;
this.address=address;
this.salary=salary;
}
abstract void Display();
}
class Another extends Employee{
String dept;
Another(String name, String id, String address, double salary,
String dept)
{
super(name, id, address, salary);
this.dept=dept;
}
void Display(){
System.out.println(name+"\t"+id+"\t"+address+"\
t"+salary+"\t"+dept);
}
}
class EmployeeAbstract{
public static void main(String args[]){
Another a[]=new Another[3];
a[0]=new Another("Sandip", "08A", "Baluhati", 6000.50,
"Coding");
a[1]=new Another("Harry", "04A", "Delhi ", 3000.00, "IT");
a[2]=new Another("Daniel", "18B", "Mumbai ", 1000.8, "Data
Entry");
System.out.println("Name\tID\tAddress\t\tSalary\
tDepartment");

System.out.println("----------------------------------------------------
");
for(int k=0;k<3;k++){
a[k].Display();
}
}
}

Output:

Name ID Address Salary Department


----------------------------------------------------
Sandip 08A Baluhati 6000.5 Coding
Harry 04A Delhi 3000.0 IT
Daniel 18B Mumbai 1000.8 Data Entry

7. WAP to take user input using scanner class.

Code:

import java.util.Scanner;
class Newvs
{
public static void main(String args[])
{
String a;
Scanner x=new Scanner(System.in);
System.out.println("Enter your name: ");
a = x.nextLine();
System.out.println("Hello "+a);
}
}

Output:
Enter your name:
Sandip
Hello Sandip
8. WAP to implement multithreading.

Code:
class A extends Thread{
public void run(){
for(int i = 0; i <= 10; i++){
System.out.println("Hello form A: i = "+i);
}
System.out.println("Exit from A");
}
}

class B extends Thread{


public void run(){
for(int j = 0; j <= 10; j++){
System.out.println("Hello form B: j = "+j);
}
System.out.println("Exit from B");
}
}

public class Multithreading {


public static void main(String args[])
{
A a = new A();
B b = new B();

a.start();
b.start();
}
}

Output:
Hello form A: i = 0
Hello form B: j = 0
Hello form A: i = 1
Hello form B: j = 1
Hello form A: i = 2
Hello form B: j = 2
Hello form B: j = 3
Hello form A: i = 3
Hello form A: i = 4
Hello form A: i = 5
Hello form B: j = 4
Hello form B: j = 5
Hello form B: j = 6
Hello form A: i = 6
Hello form B: j = 7
Hello form A: i = 7
Hello form A: i = 8
Hello form A: i = 9
Hello form B: j = 8
Hello form A: i = 10
Hello form B: j = 9
Exit from A
Hello form B: j = 10
Exit from B

You might also like