Java Lab File
Java Lab File
of
The term work of Java Programming Lab (PCS-408), being submitted by Himanshu Agrawal,
Roll no 2161177 to Graphic Era Hill University Bhimtal Campus for the award of bona fide work
carried out by him/her. He has worked under my guidance and supervision and fulfilled the
(…………………)
Ravindra Koranga
ACKNOWLEDGEMENT
I take immense pleasure in thanking Mr. Ravindra Koranga (Assistant Professor, CS,
GEHU Bhimtal Campus) for allowing us to carry out this project work under his excellent and
optimistic supervision. This has all been possible due to his novel inspiration, able guidance and
I want to extend thanks to our President “Prof. (Dr.) Kamal Ghanshala” for providing us all
infrastructure and facilities to work in need without which this work would not be possible.
HIMANSHU AGRAWAL
STUDENT’S DECLARATION
I, Himanshu Agrawal hereby declare the work, which is being presented in the report, entitled Term
work of Java Programming Lab (PCS-408) in partial fulfillment of the requirement for the award
of the degree Bachelor of Technology (Computer Science) in the session 2022-2023 for semester
IV, is an authentic record of my own work carried out under the supervision of Mr. Ravindra
The matter embodied in this project has not been submitted by me for the award of any other degree.
Index/List of Experiments
1) WAP to show the use of class, object and constructor in java. Create a box class with
variables length, width, height and functions set(), get(), volume().
2) WAP to show function overloading in java.
3) WAP to show the use of static keyword in java.
4) WAP to show inheritance in java.
5) WAP to show function overriding in java.
6) WAP to show constructor chaining in java.
7) WAP to show the use of super keyword in java.
8) WAP to show the use of super() method in java.
9) WAP to show the use of abstract class in java.
10) WAP to show the use of interface in java.
11) WAP to show the use of throw and throws keyword in java.
12) WAP to show to create a thread in java.
13) WAP to write some text to a file and then read this file and print it.
14) WAP to show the use of ArrayList in java Collections.
15) WAP to create a Swing GUI application. Take two input from user and print the
difference.
16) WAP to show the Database connectivity using jdbc driver in java. Connect to a
Database and print the table in the database.
1. WAP to show the use of class, object and constructor in java. Create a box class with variables length,
width, height and functions set(), get(), volume().
class box{
l=x;
w=y;
h=z;
l=x;
w=y;
h=z;
void get(){
System.out.println(l);
System.out.println(w);
System.out.println(h);
void vol(){
System.out.println(l*w*h);
class demobox{
public static void main(String ar[]){
obj1=new box(10,20,30);
obj1.get();
obj1.vol();
import java.util.Scanner;
class demooverloading{
System.out.println(x+y);
System.out.println(x+y+z);
int a,b,c;
a=scobj.nextInt();
b=scobj.nextInt();
c=scobj.nextInt();
add(a,b);
add(a,b,c);
class box{
static {
color="Red";
l=x;w=y;h=z;
System.out.println(l);
System.out.println(w);
System.out.println(h);
System.out.println(color);
}
}
class demo{
box.get();
obj1.get();
class box{
l=x;w=y;h=z;
System.out.println(l);
System.out.println(w);
System.out.println(h);
System.out.println(l*w*h);
}
class d1 extends box{
l=x;w=y;h=z;wt=u;
System.out.println(l);
System.out.println(w);
System.out.println(h);
System.out.println(wt);
System.out.println(wt/(l*w*h));
l=x;w=y;h=z;wt=u;col=s;
System.out.println(l);
System.out.println(w);
System.out.println(h);
System.out.println(wt);
System.out.println(col);
}
class demo{
d1 obj_d1=new d1();
d2 obj_d2=new d2();
obj_d2.set(1,2,3,4,"red");
obj_d2.get();
obj_d2.set(4,5,6);
obj_d2.get();
obj_d2.vol();
obj_d2.density();
class box{
l=x;w=y;h=z;
System.out.println(l);
System.out.println(w);
System.out.println(h);
l=x;w=y;h=z;wt=u;
System.out.println(l);
System.out.println(w);
System.out.println(h);
System.out.println(wt);
System.out.println(wt/(l*w*h));
l=x;w=y;h=z;wt=u;col=s;
System.out.println(l);
System.out.println(w);
System.out.println(h);
System.out.println(wt);
System.out.println(col);
class demo{
d1 obj_d1=new d1();
d2 obj_d2=new d2();
box ref;
ref=obj_d2;
ref.set(1,2,3);
ref.get();
class box {
protected int l, w, h;
box(){
System.out.println("box called");
}
public void set(int x, int y, int z) {
l=x;
w=y;
h=z;
System.out.println(l);
System.out.println(w);
System.out.println(h);
d1(){
System.out.println("d1 called");
l=x;
w=y;
h=z;
wt=u;
}
System.out.println(l);
System.out.println(w);
System.out.println(h);
System.out.println(wt);
//non parameterized in d2
d2(){
System.out.println("d2 called");
l=x;
w=y;
h=z;
wt=u;
col=s;
System.out.println(w);
System.out.println(h);
System.out.println(wt);
System.out.println(col);
class demo{
obj_d2.set(1,2,3,4,"red");
obj_d2.get();
//super keyword is used when base and derived class has variable or functions with same name.
class box {
protected int l, w, h;
l=x;
w=y;
h=z;
System.out.println(l);
System.out.println(w);
System.out.println(h);
//set in d1 class.
System.out.println(l);
System.out.println(w);
System.out.println(h);
System.out.println(wt);
//set in d2 class
System.out.println(l);
System.out.println(w);
System.out.println(h);
System.out.println(wt);
System.out.println(col);
}
class demo{
d2 obj_d2=new d2();
obj_d2.get();
//When base class has a parameterized constructor, derived class constructor must call super()
class box {
protected int l, w, h;
//parameterized Constructor
l=x;
w=y;
h=z;
}
l=x;
w=y;
h=z;
System.out.println(l);
System.out.println(w);
System.out.println(h);
//d1 must call super because base class box has a parameterized constructor
w=y;
h=z;
wt=u;
System.out.println(l);
System.out.println(w);
System.out.println(h);
System.out.println(wt);
//parameterized is present in d2
//d2() must call super(), because base class has a parameterized constructor
l=x;
w=y;
h=z;
wt=u;
col=s;
System.out.println(l);
System.out.println(w);
System.out.println(h);
System.out.println(wt);
System.out.println(col);
class demo{
obj_d2.get();
}
}
//abstract classes are used to provide abstraction. The derived class must define the abstract functions in abstract
class
//show the use of abstract, concrete, final, static, constructor in abstract class
abstract public void get(); // it will not be defined here. box class must define it
base(){
}
class box extends base{
l=x;w=y;h=z;
System.out.println(l);
System.out.println(w);
System.out.println(h);
class demo{
box obj=new box();//when obj of derived class is created base constructor is called because of constructor
chaining.
obj.set(1,2,3);
obj.get();
base.fun();
interface myinterface{
l=x;w=y;h=z;
System.out.println(l);
System.out.println(w);
System.out.println(h);
System.out.println(l*w*h);
}
class demo{
obj.set(1,2,3);
obj.get();
11. WAP to show the use of throw and throws keyword in java.
import java.util.Scanner;
class demo{
throws IllegalAccessException{
if(x==0){
if(x==1){
}
}
int a;
a=sc.nextInt();
try{
fun(a);
catch(ArithmeticException e){
catch(IllegalAccessException e){
class task{
for(int i=0;i<100;i++){
System.out.println("thread1:"+i+" ");
for(char c='a';c<'z';c++){
System.out.println("thread2 : "+c);
Thread obj_thread;
task obj_task;
thread1(task obj_task){
this.obj_task=obj_task;
obj_thread=new Thread(this);
obj_thread.start();
obj_task.run();
Thread obj_thread;
task obj_task;
thread2(task obj_task){
this.obj_task=obj_task;
obj_thread=new Thread(this);
obj_thread.start();
obj_task.run2();
class demo{
18. WAP to write some text to a file and then read this file and print it.
import java.io.*;
class demo {
for(int i=0;i<s.length();i++){
fout.write(s.charAt(i));
fout.close();
int c;
while( (c=fin.read())!=-1 ){
System.out.print((char)c);
import java.util.*;
class demo{
al.add(10);
al.add(20);
al.add(30);
al.add(40);
System.out.println(al);
al.remove(1);
al.remove(Integer.valueOf(10) );
System.out.println(al);
22. WAP to create a Swing GUI application. Take two input from user and print the difference.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class demo {
JLabel l;
JTextField a,b;
JButton bb;
demo(){
jf.setLayout(new FlowLayout() );
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(500,450);
jf.setVisible(true);
l=new JLabel("Label");
a=new JTextField(10);
b=new JTextField(10);
bb=new JButton("submit");
jf.add(l);
jf.add(a);
jf.add(b);
jf.add(bb);
bb.addActionListener(new ActionListener(){
if( ae.getActionCommand().equals("submit") ){
int x= Integer.parseInt(a.getText());
int y=Integer.parseInt(b.getText() );
l.setText("output is :"+String.valueOf(result));
);
}
public static void main(String args[]){
SwingUtilities.invokeLater(
new Runnable() {
new demo();
);
}
23. WAP to show the Database connectivity using jdbc driver in java. Connect to a Database and print
the table in the database.
import java.sql.*;
class JdbcSelectTest {
try (
"jdbc:mysql://localhost:3306/ebookshop",
){
System.out.println("The SQL statement is: " + strSelect + "\n"); // Echo For debugging
int rowCount = 0;
++rowCount;
}
System.out.println("Total number of records = " + rowCount);
} catch(SQLException ex) {
ex.printStackTrace();
23. WAP to show the Database connectivity using jdbc driver in java. Connect to a
Database and print the table in the database.
import java.sql.*;
class JdbcSelectTest {
try (
Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/ebookshop",
){
System.out.println("The SQL statement is: " + strSelect + "\n"); // Echo For debugging
int rowCount = 0;
++rowCount;
} catch(SQLException ex) {
ex.printStackTrace();
Output