Java Lab Mannual
Java Lab Mannual
Code:
Class prc1
{
public static void main(String args[])
{
System.out.println(“Name:shahid”);
System.out.println(“Enrolment No.:214520307025”);
System.out.println(“Hello World”);
}
}
Output:
Practical-2
Write a program in Java to find maximum of three numbers using conditional
operator.
Code:
class prc2{
public static void main(String ar[]){
int a=10,b=20,c=30;
int max;
max=(a>b &&a>c)?a:(b>c)?b:c;
System.out.println("Maximum number is:"+max);
}
}
Output:
Practical-3
Write a program in Java to reverse the digits of a number using while loop.
Code:
class prc3
{
public static void main(String[] args)
{
int val=123;
System.out.println("Name: shahid ");
System.out.println("Enrolment No.:214520307025");
System.out.println("THE VALUE BEFORE REVERSE:"+val);
int rev=0;
while(val!=0)
{
rev=(rev*10)+(val%10);
val=val/10;
}
System.out.println("THE VALUE AFTER
REVERSE:"+rev);
}
}
Output:
Practical-4
Write a program in Java to add two 3*3 matrices.
Code:
class prc4
{
public static void main(String[] args)
{
System.out.println("Name:shahid Enrolment
NO.:214520307025");
int arr1[][]={{1,2,3},{1,3,4},{1,1,1}};
int arr2[][]={{3,2,1},{4,3,1},{2,2,2}};
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
arr3[i][j]=arr1[i][j]+arr2[i][j];
System.out.print(arr3[i][j]+" ");
}
System.out.println("");
}
}
}
Output:
Practical-5
Write a program in Java to generate first n prime numbers.
Code:
import java.util.Scanner;
class prc5
{
public static void main(String args[])
{
System.out.println("Name:shahid Enrolment No.:214520307025");
for(int num=2;;num++)
{ flag=0;
for(int div=2;div<num;div++)
{
if(num%2==0)
{
flag=1;
break;
}
}
if(flag==0)
{
System.out.println(num);
counter++;
if(counter==n)
{
break;
}
}
}
}
}
Output:
Practical-6
Write a program in Java which has a class Student having two instance variables
enrollmentNo and name. Create 3 objects of Student class in main method and
display student’s name.
Code:
class student{
int enrolmentno;
String name;
s1.getdata(29,"Nihal");
s2.getdata(42,"Mayank");
s3.getdata(40,"Vatsal");
}
}
Output:
Practical-7
Write a program in Java which has a class Rectangle having two instance
variables height and weight. Initialize the class using constructor.
Code:
import java.util.Scanner;
class Rectangle
{
float height;
float width;
float area;
Rectangle(float h,float w)
{
height=h;
width=w;
area=w*h;
}
}
class Prc7
{
public static void main(String args[])
{
//int a,b;
System.out.println("Enter Height:");
float a=s1.nextFloat();
System.out.println("Enter width:");
float b=s1.nextFloat();
System.out.println("Height:"+r1.height);
System.out.println("Width:"+r1.width);
System.out.println("Area of rectangle:"+r1.area);
}
}
Output:
Practical-8
Write a program in Java demonstrate the use of “this” keyword.
Code:
class prc8
{
int a;
prc8(int a)
{
this.a=a;
}
void show()
{
System.out.print("Name=shahid\n Enrolment
No:214520307025\n");
System.out.println(a);
}
public static void main(String args[])
{
prc8 ob=new prc8(10);
ob.show();
}
}
Output:
Practical-9
Write a program in Java to demonstrate the use of “static” keyword.
Code:
public class prc9
{
class x{
static int a=10;
}
static void fun()
{
System.out.println("This is static method");
}
public static void main(String args[])
{
System.out.println("Name:shahid\nEnrolment
NO:214520307025\n");
System.out.println(x.a);
fun();
}
}
Output:
Practical-10
Write a program in Java to demonstrate the use of "final" keyword.
Code:
final class demo{
final void disp(){
final int a=10;
System.out.println(a);
a=10;/*we will get error as we are re-assigning a final variable*/
}
}
class prc10 extends demo/* we will get error as we extends final class*/
{
void disp(){ /* we will get error as we override final method.*/
System.out.print("Hello");
}
public static void main(String ar[]);{
prc10 ob=new prc10();
prc10.disp();
}
}
Output:
Practical-11
Write a program in Java which has a class Shape having 2 overloaded methods
area(float radius) and area(float length, float width). Display the area of circle
and rectangle using overloaded methods.
Code:
class Shape{
float area(float radius) {
float ac;
ac=3.14f*(radius*radius);
return ac;
}
float area(float length,float width){
float rc;
rc=width*length;
return rc;
}
public static void main(String ar[]){
Shape s=new Shape();
System.out.println("Area of cirlce:"+s.area(10.5f));
System.out.println("Area of rextangle:"+s.area(5.5f,5.6f));
}
}
Output:
Practical-12
Write a program in Java to demonstrate the constructor overloading.
Code:
class prc12{
prc12()
{
System.out.println("This is default constructor");
}
prc12(int a)
{
System.out.println("This is parametarized constructor and cube of
"+a+" is:"+(a*a*a));
}
public static void main(String ar[])
{
prc12 ob1=new prc12();
prc12 ob2=new prc12(3);
}
}
Output:
Practical-13
Write a java program to demonstrate use of “String” class methods : chatAt(),
contains(), format(), length(), split().
Code:
import java.lang.String;
class prc13
{
public static void main(String ar[])
{
String str="Hello";
String[] arr=str.split(",",2);
System.out.println("CharAt() function:"+str.charAt(0));
System.out.println("Contains() function:"+str.contains("He"));
String.format("STR=%S",str);
System.out.println("length() function:"+str.length());
System.out.println("Split() function:"+str.charAt(0));
for(String s : arr)
{
System.out.println(s);
}
}
}
Output:
Practical-14
Write a program in Java to demonstrate single inheritance.
Code:
class a
{
void disp()
{
System.out.println("This is single inheritance.");
}
}
class prc14 extends a
{
public static void main(String ar[])
{
prc14 ob=new prc14();
ob.disp();
}
}
Output:
Practical-15
Write a program in Java to demonstrate multilevel inheritance.
Code:
class x{
void disp()
{
System.out.println("This is parent class");
}
}
class y extends x
{
void show(){
System.out.println("THis is intermediate derived class");
}
}
class prc15 extends y
{
void display()
{
System.out.println("THis is base class");
}
public static void main(String ar[])
{
prc15 ob=new prc15();
ob.disp();
ob.show();
ob.display();
}
}
Output:
Practical-16
Write a program in Java to demonstrate hierarchical inheritance.
Code:
class parent
{
void disp()
{
System.out.println("This is parent class");
}
}
class child extends parent{
void show()
{
System.out.println("This is Child class");
}
}
class prc16 extends parent{
void display()
{
System.out.println("This is another childclass");
}
public static void main(String ar[])
{
prc16 ob=new prc16();
ob.display();
ob.disp();
Practical-18
Write a program in Java which has a class Car having two instance variables
topSpeed and name. Override toString() method in Car class. Create 5 instances
of Car class and print the instances.
Practical-19
Write a program in Java to implement multiple inheritance using interfaces.
Code:
interface a{
public void show();
}
interface b{
public void disp();
}
class prc19 implements a,b{
public void show()
{
System.out.println("This is method of interface a");
}
public void disp()
{
System.out.println("THis is method of interface b");
}
public static void main(String ar[])
{
prc19 ob=new prc19();
ob.show();
ob.disp();
}
}
Output:
Practical-20
Write a program in Java which has an abstract class Shape having three
subclasses: Triangle, Rectangle, and Circle. Define method area() in the abstract
class Shape and override area() method to calculate the area.
Code:
import java.io.*;
abstract class Shape
{
float dim1,dim2,radius;
abstract float area();
}
class Triangle extends Shape
{
Triangle(float d1, float d2)
{
dim1=d1;
dim2=d2;
}
float area()
{
System.out.println("Area of Triangle is ");
return (dim1*dim2)/2;
}
}
class Rectangle extends Shape
{
Rectangle(float d1, float d2)
{
dim1=d1;
dim2=d2;
}
float area()
{
System.out.println("Area of Rectangle is ");
return dim1*dim2;
}
}
class Circle extends Shape
{
Circle(float d1)
{
radius=d1;
}
float area()
{
System.out.println("Area of Circle is ");
return 3.14f*radius*radius;
}
}
class prc20
{
public static void main(String arg[])
{
Triangle t=new Triangle(4.3f,5.3f);
Rectangle r=new Rectangle(2.4f,4.2f);
Circle c=new Circle(10.5f);
System.out.println(t.area());
System.out.println(r.area());
System.out.println(c.area());
}
}
Output:
Practical-21
Write a program in Java to demonstrate use of final class.
Code:
final class demo{
final void disp(){
final int a=10;
System.out.println(a);
a=10;/*we will get error as we are re-assigning a final variable*/
}
}
class prc10 extends demo/* we will get error as we extends final class*/
{
void disp(){ /* we will get error as we override final method.*/
System.out.print("Hello");
}
public static void main(String ar[]);{
prc10 ob=new prc10();
prc10.disp();
}
}
Output:
Practical-22
Write a program in Java to demonstrate use of package.
Code:
First.java:
package mypackage;
public class First
{
void dis()
{
System.out.println("This is first method");
}
}
Compiling package:
Prc22.java:
import mypackage;
public class Def22 extends First
{
void display()
{
System.out.println("This is second class method.");
}
public static void main(String args[])
{
Second s=new Second();
s.dis();
s.display();
}
}
Practical-23
Write a program in Java to develop user defined exception for 'Divide by Zero'
error.
Code:
class UserException extends Exception
{
private int ex;
UserException(int b)
{
ex=b;
}
public String toString()
{
return "My Exception : Number is not divided by "+ex;
}
}
class Def23
{
static void divide(int a,int b) throws UserException
{
if(b<=0)
{
throw new UserException(b);
}
else
{
System.out.println("Division : "+a/b);
}
}
public static void main(String arg[])
{
try
{
divide(10,0);
}
catch(UserException me)
{
System.out.println(me);
}
}
}
Output:
Practical-24
*Write a program in Java to develop Banking Application in which user
deposits the amount Rs 25000 and then start withdrawing of Rs 20000, Rs 4000
and it throws exception "Not Sufficient Fund" when user withdraws Rs. 2000
thereafter.
Code:
import java.util.Scanner;
class userexp extends Exception
{
public String toString()
{
return "Not Sufficient Fund";
}
}
class prc24
{
static int bal=0;
static int w=0;
prc24()
{
bal=25000;
}
static void withdrawal(int a)throws userexp
{
if(a>bal)
{
throw new userexp();
}
else
{
bal=bal-a;
}
}
public static void main(String args[])
{
prc24 d=new prc24();
Scanner sc=new Scanner(System.in);
try
{
System.out.println("Enter first withdrawal amount:");
w=sc.nextInt();
withdrawal(w);
Practical-27
Write a program in Java to perform read and write operations on a Text file.
Code:
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
w.write(System.getProperty("line.separator").getBytes());
}while(sc.hasNextLine());
w.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
void fileread()
{
System.out.println("\n\nDisplay Content of the FIle\n");
try
{
FileInputStream fin=new
FileInputStream("D:\\Java\\java_practicals\\java_practical\\lab_man\\demo.txt
");
int i=0;
while((i=fin.read())!=-1)
{
System.out.print((char)i);
}
fin.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String ar[])
{
prc27 d=new prc27();
d.filewrite();
d.fileread();
}
}
Output:
Practical-28
Write a program in Java to demonstrate use of List.
1) Create ArrayList and add weekdays (in string form)
2) Create LinkedList and add months (in string form) Display both List.
Code:
import java.util.*;
class prc28
{
public static void main(String ar[])
{
ArrayList<String> als=new ArrayList<String>();
als.add("Monday");
als.add("Tuesday");
als.add("Wednesday");
als.add("Thursday");
als.add("Friday");
als.add("Saturday");
als.add("Sunday");
System.out.println("List from ArrayList:");
for(String s: als)
{
System.out.println(s);
}
LinkedList<String> al=new LinkedList<String>();
al.add("January");
al.add("February");
al.add("March");
al.add("April");
al.add("May");
al.add("June");
al.add("July");
al.add("August");
al.add("September");
al.add("October");
al.add("November");
al.add("December");
Practical-29
Write a program in Java to create a new HashSet, add colors(in string form) and
iterate through all elements using for-each loop to display the collection.
Code:
import java.util.*;
public class prc29
{
public static void main(String[] args) {
HashSet<String> set=new HashSet<String>();
set.add("PINK");
set.add("RED");
set.add("BLUE");
set.add("GREEN");
for(int i=1;i<=4;i++)
{
System.out.println(al.get(i));
}
}
}
Output: