Java_Lab_Programs_Solutions
Java_Lab_Programs_Solutions
OUTPUT
Program No: 02
Write a Java Program to demonstrate Arrays.
class Array
{
public static void main(String[] args)
{
int number[];
number=new int[5];
for(int i=0;i<args.length;i++)
{
number[i]=Integer.parseInt(args[i]);
}
for(int i=0;i<args.length;i++)
{
for(int j=0;j<args.length;j++)
{
if(number[i]<number[j])
{
int temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
}
System.out.println("Sorted array");
for(int i=0;i<args.length;i++)
{
System.out.println(number[i]);
}
}
}
OUTPUT
Program No: 03
Write a Java Program to demonstrate the concept of Class and Object (Display Student
Information).
class Student
{
String Student_Name;
int Student_Rollno;
int Age;
float Marks;
void StudentInfo()
{
System.out.println("Student_Name : "+Student_Name);
System.out.println("Student_Rollno : "+Student_Rollno);
System.out.println("Age : "+Age);
System.out.println("Marks : "+Marks);
}
OUTPUT
Program No: 04
Write a Java Program to demonstrate the concept of Constructor and Constructor
Overloading.
class Box
{
double width, height, depth;
Box()
{
width = height = depth = 0;
}
Box(double len)
{
width = height = depth = len;
}
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
double volume()
{
return width * height * depth;
}
}
class Test
{
public static void main(String args[])
{
Box mybox1 = new Box();
Box mycube = new Box(7);
Box mybox2 = new Box(10, 20, 15);
double vol;
vol = mybox1.volume();
System.out.println(" Volume of mybox1 is " + vol);
vol = mycube.volume();
System.out.println(" Volume of mycube is " + vol);
vol = mybox2.volume();
System.out.println(" Volume of mybox2 is " + vol);
}
}
OUTPUT
Program No: 05
Write a Java Program to demonstrate the concept of Method Overloading.
class Transaction
{
void Deposit( double amt,String name)
{
System.out.println("Wel-Come "+name+" to your savings account");
if(amt>=500 && amt<=50000)
{
void increment()
{
System.out.println("The Employee incremented salary is :" +(salary + (salary * 0.2)) );
}
}
class PermanentEmp extends Employee
{
double hike = 0.5;
void increment()
{
System.out.println("The Permanent Employee incremented salary is :" +(salary +
(salary * hike)) );
}
}
class TemporaryEmp extends Employee
{
double hike = 0.35;
void increment()
{
System.out.println("The Temporary Employee incremented salary is :" +(salary +
(salary * hike)) );
}
}
public class EmpSalHike
{
public static void main(String args[])
{
Employee e = new Employee( );
PermanentEmp p = new PermanentEmp();
TemporaryEmp t = new TemporaryEmp();
e.increment();
p.increment();
t.increment();
}
}
OUTPUT
Program No: 07
Write a Java Program to implement Inner class and demonstrate its Access
Protections.
public class Car
{
private String model;
protected int year;
private Engine engine;
Engine eng = new Engine();
OUTPUT
Program No: 08
Write a Java Program to demonstrate the working of this and super keyword.
class Person
{
private String name;
private int age;
System.out.println();
OUTPUT
Program No: 10
Write a Java Program to demonstrate Abstract Classes and Abstract Methods.
abstract class Shape
{
protected String color;
public Shape(String color)
{
this.color = color;
}
OUTPUT
Program No: 11
interface Withdrawable
{
void withdraw(double amount);
}
OUTPUT
Program No: 13
Write a Java program to demonstrate the working of try, catch and finally
block in exception handling.
import java.util.Scanner;
OUTPUT
Program No: 14
Write Java Program to demonstrate the Packages.
Department.java
package department;
public class Department
{
private String departmentName;
Contact.java
package contacts;
import department.Department;
Main.java
import contacts.Contact;
import department.Department;
public class Main
{
public static void main(String[] args)
{
Department department = new Department("Computer Science");
System.out.println("Contact Details:");
contact.display();
}
}
OUTPUT
Program No: 15