Professional Documents
Culture Documents
PRACTICAL FILE
02 s/9/21
Hand Ling dada yf 2 Is//1
19/4/21
ID/LD Avvay 31 22/7/21
Varjou dslov G/lol
Kecusio -9 14 1|21
Method ovtr loding 7-8 1 |1M
Construcor overloncing 9 lo
Vurio o AL Condrell
A-vSay
hd 4n ie a 1 2
Date. /5/2/2 L
Pho gran
CHAR
ChaDataTste
ps Stli vold main ( Sting ar L1)
Toacher's Sign.
ProblemsJavadoc Declaration Console Git Staging
<terminated> DataTypes [Java Application] D:teclipse-jee-2021-06-R-win32-x86_64\e
123322
-42937.9
19.98
false
vivek
Date.
Page No. 2
Expt.
doulle numl-42137,1d
System.od.pintln num)
boadean b =-
Syslarm ost pintln CA2;
Char ch Vivek *_
Sysam opiniln(Ch)j
Teacher's Sign.
ProblemsJavadoc Declaration Console 2 Git Staging
Aim teCastng
Cla Test
public S t i vehd main Sting arj L )
Iautomal he_Lonyeuion
Syatim out pinl 2n Trd Vlut. " iD
Syaim o nnto " Leng Va e);
Systany ok þnnta ( pat ek t)
d loo oY
itn int ) ,
Siyslin ou_pit. C DoA Mahs+d);
I_rtiond povt s
Teacher's Sign.
ProblemsJavadocDeclaration Console Git Staging
D:\eclipse-jee-2021-06-R-win32-x86_64\eclipse
terminated> Test1 [Java Application]
Conversion of int to byte
i-257b=1
Split ghConveion
mLAnd domui n isdd th pyoqYimmu
Imilicit1 Conveysiym
miams Jhat as donu b th Compi
dos to by )
MrKumar
MrsKumar
Date. 2z//
Expt. 2 Page No.
Aim ray D 4 2p
On dimunsionsN
Tn dim r
Clas Iubdimmaions
public Shut vaid mdin ( Shinyarg C3)
SSthng ) IM Sluton
Mrs Ms
Toacher's Sign.
Console DRecursionjava
<terminated> Recursion [Java Application) C:\Program Files Javajdk-16
hello 1
hello 2
hello 3
hello 4
hello 5
Date 3ot 9
Expt. Page No
Reaniipn
bublir as Rtclem
Stu Unt Counl
3tute weidp
Qnt
CCaunt<: s )
Sysem o p n t en
oll +COnt)
3
3
ult Sti vid main (Ating L ang )
poj
3
achor's Sign
Console Recursionjava
22
33
Date.
Addu
Aat ist dd int a,int b ) on_at
ShticLnt add Cint a
ini b, iade)
AAuzn atbtc
las TtOnalsacurg
DublicSttir_vid man 31 nmg wug L)
pnt nCAAdee, add l ll, 1D)
Syalimout println Addu add [/1, 1/, 1))
Teacher's Sign.
OConsole Studentjava
terminated> Student [lava Application) C\Program Files Javaljdk-16.0.21binljavav
this a default constructor
Student Id : 0
Student Name null
Parameterized Constructor values:
Student Id 10
Student Name David
Date.
i clas Stclent
Stnng name
Studert C)
Syam pant e the dault congirucho)
Stucdent(mt i Stin )
Nam
Teacher's Sign.
Console 3Studentjava
terminated» Student [Java Application] CAProgram Files Javaljdk-16.0.2
111 VIVEK Gne College
222 GOOGLE Gne College
Date.
Expt Page No.
Clas 9tudent
String nem
Stalpo S1ing Colisgt bne Catiey
Stuknt int h
Stiy h )1
n
td
dshle ) Sgaltm ou pitlu ellone "
Celigcoltcyc)j
pubicSthluc vod main tede
S1nng )
ary)
Shudtnt S1 n ew Stedent "Viek ),_
Shdent S
S dis pla,
S play
Teachor iqn
clejava
Java Application] C\Program FilesJava
jdk-16.0.2\binijavaw.exe (16-Nov-2021, 3:59:27 pm -3:59:32 pm
thread "main" java.lang.Error: Unresolved
compilation
final field Vehicle. speedlimit cannot be assigned
OConsole demojava
<terminated» demo Java Application] C\Program
Files\Javaljdk-16.0.2\bin\javaw.exe (16-Nov-2021, 4
inside demo's finalize ())
Calling finalize method of the Object class
Date.
Expt. Page No.
publue a s hicu
hina imt ptzd nut KO
Veid LonthadSpuul) 1
Lptud aiii 1So
1tmahn Sting o )
VehrrleO
_cb Cont rd Spd C)
inaliz (
Jnpart
pabirClas dne
peli
palkclicld nali zu ) hias Thenble
t S Cam cui pirtinInde dimes nalizu")
Th Comman as a7gumat
Jhi bragremmA the rgu mn
eXculuon proqysm T
COn t h Orauminlu dusing
mmird d lis
ei by pa
Argums
Command
1, 4
bulie
Ora
Suan d pd O Argamint_Dra + b L:] )
Sttm.ot prialln Aryume tuo t bl/)
Teacher's Sign.
PROGRAMMING WITH JAVA LAB
Subject Code: LPCIT-109
PRACTICAL FILE
SINGLE INHERITANCE
package programs;
class Animal{
void eat(){
System.out.println("eating...\n");
}
}
class Dog extends Animal{
void bark(){
System.out.println("barking....\n");
}
}
public class SingleInheritance {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.eat();
d1.bark();
}
}
OUTPUT:
MULTILEVEL INHERITANCE
package programs;
package programs;
class Student {
final String clg = "GNDEC";
int year;
void showDetail() {
System.out.println("clg: " + clg + " year: " + year);
}
void showDetail() {
System.out.println("clg: " + clg + " year: " + year +" RollNO. : "+rollNo);
}
}
s2.showElective();
s1.showDetail();
}
}
OUTPUT:
HIERARCHICAL INHERITANCE
package programs;
class Cycle{
protected String companyName = "Hero";
void NoOfWheels(){
System.out.println("Bicycle have 2 wheels\n");
}
}
class CycleWithGear extends Cycle{
void NoOfGears(){
System.out.println("This Bicycle have 3 gears\nCompanyName: "+ companyName);
}
}
class CycleWithoutGear extends Cycle{
void typeOfBrake(){
System.out.println("This Bicycle is fitted with Disk brake\n");
}
}
package programs;
class CollegeStudent{
void degreeCourse(){
System.out.println("\n\nEnrolled in a degree course");
}
}
class BtechStudent extends CollegeStudent{
@Override
void degreeCourse(){
System.out.println("\n\nEnrolled in B.tech degree course");
}
}
}
}
OUTPUT:
PRACTICAL 13
Abstract class
package programs;
abstract class TwoWheelerVehicle{
TwoWheelerVehicle(){
System.out.println("It is a Two Wheeler vehicle (Parent Constructor Called)");
}
void haveGear(){
System.out.println("It has gears");
}
}
void type(){
System.out.println("This is a sports Bike");
}
}
v1 = new Bike();
v1.haveGear();
v1.type();
}
}
OUTPUT:
PRACTICAL 14
Nested class
package programs;
class OuterClass {
private static int p = 10;
static int s = 12;
int x = 11;
void fun() {
System.out.println("This is method inside outer class");
}
}
}
class InnerClass {
void display() {
System.out.println("private static member of OuterClass: " + p);
System.out.println("static member of OuterClass: " + s);
System.out.println("non-static member of OuterClass: " + x);
}
}
void getValue() {
int sum = 10;
// Local Inner class
class LocalInnerClass {
int divisor;
int remainder;
LocalInnerClass(int divisor) {
this.divisor = divisor;
}
int division() {
remainder = sum % divisor;
return remainder;
}
}
LocalInnerClass localInnerClass = new LocalInnerClass(4);
System.out.println("Division Remainder: " + localInnerClass.division());
}
// Anonymous Class
OuterClass outerClass = new OuterClass(){
@Override
void fun(){
System.out.println("This is fun method overridden in anonymous class");
}
};
OuterClass.InnerClass innerClass = outerClass.new InnerClass();
// innerClass.fun(); // can't access
innerClass.display();
outerClass.getValue();
System.out.println("___________________");
outerClass.fun();
}
}
OUTPUT:
PRACTICAL 15
Constructor chaining
package programs;
class Shape{
String shapeName;
int sides;
Shape(){
System.out.println("This is a Shape class");
}
Shape(String shapeName){
this();
this.shapeName = shapeName;
System.out.println("Shape: "+shapeName);
}
Shape(int sides){
this("rectangle");
this.sides = sides;
System.out.println("sides: "+sides);
}
Rectangle(){
super(4);
System.out.println("This is Rectangle Class");
}
void area(){
System.out.println("Area: "+ length*breadth);
}
}
r1.area();
}
}
OUTPUT:
PRACTICAL 16
Importing classes from user defined package and creating packages using
access protection
//Saved as UserDefinedPackage.java
package com.mypack;
public class UserDefinedPackage{
private int x = 78;
protected String name = "Parvesh";
public int y = 19;
protected UserDefinedPackage(){
System.out.println("This is A class in com.mypack");
}
}
}
OUTPUT:
PRACTICAL 17
Interfaces, nested interfaces and use of extending interfaces
INTERFACE
package programs;
interface Book{
void showTitle();
void showPrice();
}
@Override
public void showTitle(){
System.out.println("Book Name: "+name);
}
@Override
public void showPrice(){
System.out.println("Price: "+price);
}
}
public class InterfacesInJava {
public static void main(String[] args) {
JavaBook jb1 = new JavaBook("JavaProgramming", 300);
jb1.showTitle();
jb1.showPrice();
}
}
OUTPUT:
OUTPUT:
PRACTICAL 18
To Demonstrate Exception Handling - using predefined exception
package com.mypack;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
class Division{
int a, b;
Division(int a, int b){
this.a = a;
this.b = b;
if(b==0){
throw new ArithmeticException("Can not divide by zero!!");
}
}
void showQuotient(){
System.out.println("Quotient: "+a/b);
}
}
class CheckedException{
void readFile() throws FileNotFoundException{
File f1 = new File("D:\\FileOne.txt");
OUTPUT:
PRACTICAL 19
To Demonstrate Exception Handling - creating user defined exceptions
package com.mypack;
class Vote{
int age;
Vote(){
}
void checkAgeForVoting(int age) throws NotFitForVote{
if(age<18){
throw new NotFitForVote("You can not vote now, but after "+(18-age)+" year/years
you can vote");
}else{
System.out.println("You can Vote :)");
}
}
}
public class UserDefinedException {
public static void main(String[] args) {
try {
Vote v1 = new Vote();
v1.checkAgeForVoting(17);
} catch (Exception e) {
e.printStackTrace();
}
OUTPUT:
PRACTICAL 20
TO DEMONSTRATE MULTITHREADING BY EXTENDING THREAD
CLASS
package com.mypack;
package com.mypack;
th.start();
OUTPUT:
PRACTICAL 22
To demonstrate Thread life cycle
package com.mypack;
try
Thread.sleep(1500);
catch (InterruptedException e)
e.printStackTrace();
ThreadLifeCycle.thread1.getState());
try
Thread.sleep(200);
catch (InterruptedException e)
e.printStackTrace();
}
thread1.start();
thread1.getState());
thread2.start();
// thread2 moved to Runnable state
thread2.getState());
try
Thread.sleep(200);
catch (InterruptedException e)
e.printStackTrace();
thread2.getState() );
try
thread2.join();
catch (InterruptedException e)
e.printStackTrace();
thread2.getState());
}
}
OUTPUT:
PRACTICAL 23
To demonstrate Applet life cycle
package com.mypack;
import java.applet.*;
import java.awt.*;
@SuppressWarnings("serial")
/*
</applet>
*/
System.out.println("1.I am init()");
System.out.println("2.I am start()");
System.out.println("3.I am paint()");
{
System.out.println("4.I am stop()");
System.out.println("5.I am destroy()");
OUTPUT:
PRACTICAL 24
To demonstrate Applet for configuring Applets by passing parameters
package com.mypack;
import java.awt.*;
import java.applet.*;
/*
</applet>
*/
String name;
String age;
String sport;
String food;
String fruit;
String destination;
public void init(){
name = getParameter("Name");
age = getParameter("Age");
food = getParameter("Food");
fruit = getParameter("Fruit");
destination = getParameter("Destination");
sport = getParameter("Sport");
OUTPUT:
PRACTICAL 25
To demonstrate Event Handling
package com.mypack;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
JButton b;
TextField textField;
EventHandling(){
f.add(b);
f.add(textField);
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.addActionListener(this);
b.setText("Hi! clicked..");
textField.setText("Welcome to EventHandling");
new EventHandling();
OUTPUT:
PRACTICAL 26
To demonstrate Reading and writing from a particular file
package com.mypack;
import java.io.*;
public class ReadingAFile {
public static void main(String[] args) {
File file = new File("D:\\5th
Semester\\ProgrammingWithJava\\ParveshBhatt_1905375\\readFile.txt");
try{
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String str;
while((str = br.readLine()) != null){
System.out.println(str);
}
br.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
}
OUTPUT:
Writing to file
package com.mypack;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
bfr.write(text);
bfr.close();
sc.close();
}
OUTPUT:
PRACTICAL 27
To demonstrate Database connectivity for various DDL and DML
operations
package com.mypack;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
Connection connection;
Statement statement;
DatabaseConnectivity(){
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (Exception e) {
try {
} catch (Exception e) {
try {
statement = connection.createStatement();
statement.executeUpdate(sql);
} catch (Exception e) {
}
}
try {
connection.close();
} catch (Exception e) {
dbc.createConnection();
dbc.createAndExecuteSQL();
dbc.closeConnection();
OUTPUT:
PRACTICAL 28
To demonstrate String class and its methods
package com.mypack;
import java.lang.String;
System.out.println(s1);
String s2=str2.toUpperCase();
System.out.println(s2);
if(s1.equals(s2)){
} else{
String str3="CompuTEr";
String str4="COMputer";
if(str3.equalsIgnoreCase(str4)){
} else{
System.out.println("Is it true"+str5==str6);
int a=str5.indexOf('b') ;
System.out.println(b);
int c=str6.indexOf("oY");
System.out.println(c);
System.out.println(d);
System.out.println(sc);
OUTPUT:
PRACTICAL 29
To demonstrate StringBuffer class and its methods
package com.mypack;
import java.lang.StringBuffer;
public class StringBufferClass {
public static void main(String[] args) {
StringBuffer str=new StringBuffer("This is JAVA practical 29 ");
str.append("to demonstrate StringBuffer class and its method");
System.out.println(str);
StringBuffer str1= new StringBuffer("This is laptop");
str1.insert(8, "gaming ");
System.out.println(str1);
StringBuffer str2=new StringBuffer("code in Python");
str2.replace(8, 14, "Java");
System.out.println(str2);
str2.delete(5, 7);
System.out.println(str2);
StringBuffer str3=new StringBuffer("Java programming");
str3.reverse();
System.out.println(str3);
System.out.println(str2.capacity());
str2.ensureCapacity(45);
System.out.println(str2.capacity());
}
}
OUTPUT: