Basic Java Lab File

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

Index

Sr. no. Experiment Name Date of Remarks


Experiment
01. Write a program to swap two values using object
reference. Your program should have a swap
function.
02. Write an application that accepts one command
line argument; Display the line of reporting if
number is even or odd.
03. Write a program that describes a class person. It
should have instance variables to record name,
age and salary. Create a person object. Set and
display its instance variables.
04. Write a program to show the concept of
constructors.
05. Write a program that shows passing object as
parameter.
06. Write a program that illustrates method
overriding.
07. Write a program to illustrate dynamic
polymorphism.
08. Write a program to show the concept of method
overloading.
09. Write a program to show the concept of
inheritance.
10. Write a program to illustrating a super class
variable a referencing as sub class object.
11. Write a program to illustrating all uses of super
keywords.
12. Write an application that shows the usage of try,
catch, throws and finally.
13. Write an application that shows how to create a
user-defined exception.
14. Create a customized exception and also make
use of all the 5 exception keywords.
15. Write a program to show the concept of
packages.
Experiment No. 1
class car
{
int no;
car(int no)
{
this.no=no;
}
public static void swap(car c1, car c2)
{
int temp;
temp = c1.no;
c1.no = c2.no;
c2.no = temp;
}
public static void main(String args[])
{
car c1 = new car(10);
car c2 = new car(20);
swap(c1,c2);

System.out.println("After swapping: ");


System.out.println("Car c1: "+c1.no);
System.out.println("Car c2: "+c2.no);
Experiment No. 2
class even
{
public static void main(String args[])
{
System.out.println(args[0]);
int x = Integer.parseInt(args[0]);

if(x%2==0)
{
System.out.println("You press even number.");
}
else
{
System.out.println("You press odd number.");
}
}
}

Output:
Experiment No. 03
// WAP to create class person with variables name, age and salary. Create a person object. Set
and display its instance variables.
import java.util.*;
class person
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name:");
String name = sc.nextLine();

System.out.print("Enter your age:");


int age = sc.nextInt();

System.out.print("Enter you salary:");


int salary = sc.nextInt();

System.out.println("Name:"+ name);
System.out.println("Age:"+ age);
System.out.println("Salary:"+ salary);
}
}
Output:
Experiment No. 04
class bank
{
int balance;
bank()
{
balance=10000;
}
bank(int balance)
{
this.balance=balance;
}
public void display()
{
System.out.println("Initial Balance:" + balance);
}
public static void main(String args[])
{
bank b = new bank();
bank b1 = new bank(50000);
b.display();
b1.display();
}
}

Output:
Experiment No. 05
class student
{
String branch;

student()
{
branch = "CSE";
}
student(student s1)
{
branch = s1.branch;
}
public void display()
{
System.out.println("Branch: "+branch);
}
public static void main(String args[])
{
student s = new student();
student s1 = new student(); //passing object as parameter.

s.display();
s1.display();
}
}

Output:

You might also like