Java Journal
Java Journal
BCA Semester – 4
Journal
Page 1 of 30
BCA Department
Laboratory Certificate
Programming with Java during the academic year 2022-23. Her/His enrollment
Date: _____________
Page 2 of 30
Index
Page Date of Date of
Sr. Name of Experiments Remarks
No. Experiment Supervision
Unit-1
1 Hello World Program 6
2 Java Variables 6
3 Leap Year 6
4 Find vowels 6
5 Passing an array to function 7
6 Class and Objects 7
7 Class with Method 8
8 Parameterized constructor 8
9 Constructor Overloading 8
10 Jagged Array 9
11 Copy constructor 9
12 Java Inheritance 9
13 Method Overloading 10
Unit-2
14 Constructor in Inheritance 12
15 Abstract Class 12
16 Final Class 13
17 Java Interface 13
18 Inner Class 14
19 util.Date class 14
20 Java Wrapper Classes 14
21 Creating user defined package 15
22 Java StringTokenizer 15
Unit-3
23 18
24 18
25 19
26 21
27 21
28 22
29 23
30
31
32
33
34
35
36
Unit-4
Page 3 of 30
Index
Page Date of Date of
Sr. Name of Experiments Remarks
No. Experiment Supervision
37
38
39
40
41
42
43
Unit-5
44
45
46
47
48
Page 4 of 30
Unit – 1
Page 5 of 30
1. Hello World Program
1 class HelloJava {
2 public static void main(String arg[]) {
3 System.out.println("Hello Java");
4 System.out.print("Java is an OOP");
5 }
6 }
2. Java Variables
1 //Java Variables
2 class VariableDemo {
3 public static void main(String[] arg) {
4 int i=10;
5 String n="Java";
6 float f=5.5f;
7 System.out.println("Value of i: "+i);
8 System.out.println("Value of n: "+n);
9 System.out.println("Value of f: "+f);
10 }
11 }
3. Leap Year
1 //Leap year example using if...else
2
3 public class LeapYearExample {
4 public static void main(String[] args) {
5 int year=2021;
6 if(((year % 4==0) && (year % 100!=0)) || (year % 400==0)){
7 System.out.println("LEAP YEAR");
8 }
9 else{
10 System.out.println("COMMON YEAR");
11 }
12 }
13 }
4. Find vowels
1 //Vowels using switch...case
2
3 public class SwitchExample {
4 public static void main(String[] args) {
5 char ch='L';
6 switch(ch)
7 {
8 case 'a':
9 System.out.println("Vowel");
10 break;
11 case 'e':
12 System.out.println("Vowel");
13 break;
14 case 'i':
15 System.out.println("Vowel");
16 break;
17 case 'o':
18 System.out.println("Vowel");
19 break;
20 case 'u':
Page 6 of 30
21 System.out.println("Vowel");
22 break;
23 case 'A':
24 System.out.println("Vowel");
25 break;
26 case 'E':
27 System.out.println("Vowel");
28 break;
29 case 'I':
30 System.out.println("Vowel");
31 break;
32 case 'O':
33 System.out.println("Vowel");
34 break;
35 case 'U':
36 System.out.println("Vowel");
37 break;
38 default:
39 System.out.println("Consonant");
40 }
41 }
42 }
Page 7 of 30
7. Class with Method
1 //Class with method
2
3 class Employee{
4 int id;
5 String name;
6 float salary;
7 void setData(int i, String n, float s) {
8 id=i;
9 name=n;
10 salary=s;
11 }
12 void getData() {
13 System.out.println(id+" "+name+" "+salary);
14 }
15 }
16 public class TestEmployee {
17 public static void main(String[] args) {
18 Employee e1=new Employee();
19 Employee e2=new Employee();
20 e1.setData(101,"Ravi",45000);
21 e2.setData(102,"Mohit",25000);
22 e1.getData();
23 e2.getData();
24 }
25 }
8. Parameterized constructor
1 //Java Program to demonstrate the use of the parameterized constructor.
2
3 class Student4{
4 int id;
5 String name;
6 //creating a parameterized constructor
7 Student4(int i,String n){
8 id = i;
9 name = n;
10 }
11 //method to display the values
12 void display(){
13 System.out.println(id+" "+name);
14 }
15 public static void main(String args[]){
16 //creating objects and passing values
17 Student4 s1 = new Student4(111,"Ritul");
18 Student4 s2 = new Student4(222,"Ravi");
19 //calling method to display the values of object
20 s1.display();
21 s2.display();
22 }
23 }
9. Constructor Overloading
1 //Java program to overload constructors
2 class Student5{
3 int id;
4 String name;
5 int age;
6 //creating two arg constructor
7 Student5(int i,String n){
8 id = i;
9 name = n;
Page 8 of 30
10 }
11 //creating three arg constructor
12 Student5(int i,String n,int a){
13 id = i;
14 name = n;
15 age=a;
16 }
17 void display(){System.out.println(id+" "+name+" "+age);}
18
19 public static void main(String args[]){
20 Student5 s1 = new Student5(111,"Mohit");
21 Student5 s2 = new Student5(222,"Priyanshu",25);
22 s1.display();
23 s2.display();
24 }
25 }
Page 10 of 30
Unit – 2
Inheritance, Java Packages
Page 11 of 30
14. Constructor in Inheritance
1 //Constructor in Inheritance
2 class Animal{
3 Animal() {
4 System.out.println("From animal constructor");
5 }
6 void eat(){
7 System.out.println("eating....");
8 }
9 protected void finalize() {
10 System.out.println("End of animal");
11 }
12 }
13 class Dog extends Animal{
14 Dog() {
15 System.out.println("From dog constructor");
16 }
17 void bark(){
18 System.out.println("barking...");
19 }
20 protected void finalize() {
21 System.out.println("End of dog");
22 }
23
24 }
25 class BabyDog extends Dog{
26 BabyDog() {
27 System.out.println("From babydog constructor");
28 }
29 void weep(){
30 System.out.println("weeping...");
31 }
32 protected void finalize() {
33 System.out.println("End of babydog");
34 }
35
36 }
37 class TestInheritance2{
38 public static void main(String args[]){
39 BabyDog d=new BabyDog();
40 d.weep();
41 d.bark();
42 d.eat();
43 d=null;
44 System.gc();
45 }
46 }
Page 12 of 30
11 void draw(){System.out.println("drawing circle");}
12 }
13
14 class TestAbstraction{
15 public static void main(String args[]){
16 Shape s1=new Circle();
17 Shape s2=new Rectangle();
18 s1.draw();
19 s2.draw();
20 }
21 }
Page 14 of 30
28 System.out.println("Integer object: "+intobj);
29 System.out.println("Long object: "+longobj);
30 System.out.println("Float object: "+floatobj);
31 System.out.println("Double object: "+doubleobj);
32 System.out.println("Character object: "+charobj);
33 System.out.println("Boolean object: "+boolobj);
34
35 //Unboxing: Converting Objects to Primitives
36 byte bytevalue=byteobj;
37 short shortvalue=shortobj;
38 int intvalue=intobj;
39 long longvalue=longobj;
40 float floatvalue=floatobj;
41 double doublevalue=doubleobj;
42 char charvalue=charobj;
43 boolean boolvalue=boolobj;
44
45 //Printing primitives
46 System.out.println("---Printing primitive values---");
47 System.out.println("byte value: "+bytevalue);
48 System.out.println("short value: "+shortvalue);
49 System.out.println("int value: "+intvalue);
50 System.out.println("long value: "+longvalue);
51 System.out.println("float value: "+floatvalue);
52 System.out.println("double value: "+doublevalue);
53 System.out.println("char value: "+charvalue);
54 System.out.println("boolean value: "+boolvalue);
55 }
56 }
Page 15 of 30
Unit – 3
Exception Handling, Threading
and Streams (Input and Output)
Page 16 of 30
23. Exception Handling
1 //Exception Handling Demonstration
2 public class Main
3 {
4 public static void main(String[] args) {
5 int a=10,b=0,c=0;
6 System.out.println("Start of main()");
7 try{
8 c=a/b;
9 }catch(ArithmeticException ae) {
10 System.out.println(ae);
11 }finally {
12 System.out.println("I am always there...");
13 }
14 System.out.println("Value of C:"+c);
15 System.out.println("End of main()");
16 }
17 }
Page 19 of 30
16 class MyThread implements Runnable {
17 public void run() {
18 for(int i = 1;i<=10;i++) {
19 System.out.println(Thread.currentThread().getName());
20 }
21 }
22 }
Page 20 of 30
Unit – 4
Applets, Layout Managers
Page 21 of 30
27. Database Connectivity with Book Table of Library Database.
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Threading.Tasks;
9 using System.Windows.Forms;
10 using System.Data.SqlClient;
11
12 namespace HelloWorldForm
13 {
14 public partial class Form8 : Form
15 {
16 SqlConnection con;
17 SqlCommand cmd;
18 public Form8()
19 {
20 InitializeComponent();
21 }
22
23 private void Form8_Load(object sender, EventArgs e)
24 {
25 con = new SqlConnection(@"Data
26 Source=(LocalDB)\v11.0;AttachDbFilename=D:\Demos\CSharp\HelloWorldForm\HelloWorldForm\Library.mdf;
27 Integrated Security=True");
28 con.Open();
29 ShowData();
30 }
31
32 private void button1_Click(object sender, EventArgs e)
33 {
34 string sql;
35 sql="insert into book values(" + textBox1.Text + ",'
36 "+textBox2.Text+"','"+textBox3.Text+"')";
37 cmd = new SqlCommand(sql,con);
38 cmd.ExecuteNonQuery();
39 ShowData();
40
41 }
42 private void ShowData()
43 {
44 string sql;
45 sql = "select * from book";
46 SqlCommand cmd = new SqlCommand(sql, con);
47 SqlDataAdapter da = new SqlDataAdapter(cmd);
48 DataTable dt = new DataTable();
49 da.Fill(dt);
50 dataGridView1.DataSource = dt;
51
52 }
53 private void Form8_Deactivate(object sender, EventArgs e)
54 {
55 con.Close();
56 }
57
58 private void button2_Click(object sender, EventArgs e)
59 {
60 string sql;
61 sql = "Delete from Book where ID=" + textBox1.Text;
62 cmd = new SqlCommand(sql, con);
63 cmd.ExecuteNonQuery();
64 ShowData();
65 }
66
67 private void button3_Click(object sender, EventArgs e)
68 {
69 string sql;
70 sql = "Update Book Set BookTitle=@title, Author=@author where ID=" + textBox1.Text;
71 cmd = new SqlCommand(sql, con);
72 cmd.CommandType = CommandType.Text;
73 cmd.Parameters.AddWithValue("@title", textBox2.Text);
74 cmd.Parameters.AddWithValue("@author", textBox3.Text);
Page 22 of 30
75 cmd.ExecuteNonQuery();
76 ShowData();
77 }
78 }
79 }
80
Page 23 of 30
Unit – 5
Page 24 of 30
28. Create custom Button Control.
1 //Class Name: ButtonZ
2 using System;
3 using System.Collections.Generic;
4 using System.Text;
5 using System.Drawing;
6 using System.Data;
7 using System.Windows.Forms;
8 using System.Drawing.Drawing2D;
9
10 namespace Custom_Controls_in_CS
11 {
12 public class ButtonZ : System.Windows.Forms.Button
13 {
14 Color clr1, clr2;
15 private Color color1 = Color.DodgerBlue;
16 private Color color2 = Color.MidnightBlue;
17 private Color m_hovercolor1 = Color.Turquoise;
18 private Color m_hovercolor2 = Color.DarkSlateGray;
19 private int color1Transparent = 250;
20 private int color2Transparent = 250;
21 private Color clickcolor1 = Color.Yellow;
22 private Color clickcolor2 = Color.Red;
23 private int angle = 90;
24 private int textX = 100;
25 private int textY = 25;
26 private String text = "";
27 public Color buttonborder_1 = Color.FromArgb(220, 220, 220);
28 public Color buttonborder_2 = Color.FromArgb(150, 150, 150);
29 public Boolean showButtonText = true;
30 public int borderWidth = 2;
31 public Color borderColor = Color.Transparent;
32 public enum ButtonsShapes
33 {
34 Rect,
35 RoundRect,
36 Circle
37 }
38 ButtonsShapes buttonShape;
39
40 public ButtonsShapes ButtonShape
41 {
42 get { return buttonShape; }
43 set
44 {
45 buttonShape = value; Invalidate();
46 }
47 }
48 public String ButtonText
49 {
50 get { return text; }
51 set { text = value; Invalidate(); }
52 }
53
54 public int BorderWidth
55 {
56 get { return borderWidth; }
57 set { borderWidth = value; Invalidate(); }
58 }
59 void SetBorderColor(Color bdrColor)
60 {
61 int red = bdrColor.R - 40;
62 int green = bdrColor.G - 40;
63 int blue = bdrColor.B - 40;
64 if (red < 0)
65 red = 0;
66 if (green < 0)
67 green = 0;
68 if (blue < 0)
69 blue = 0;
70 buttonborder_1 = Color.FromArgb(red, green, blue);
71 buttonborder_2 = bdrColor;
72 }
73 public Color BorderColor
74 {
Page 25 of 30
75 get { return borderColor; }
76 set
77 {
78 borderColor = value;
79 if (borderColor == Color.Transparent)
80 {
81 buttonborder_1 = Color.FromArgb(220, 220, 220);
82 buttonborder_2 = Color.FromArgb(150, 150, 150);
83 }
84 else
85 {
86 SetBorderColor(borderColor);
87 }
88
89 }
90 }
91 public Color StartColor
92 {
93 get { return color1; }
94 set { color1 = value; Invalidate(); }
95 }
96 public Color EndColor
97 {
98 get { return color2; }
100 set { color2 = value; Invalidate(); }
101 }
102 public Color MouseHoverColor1
103 {
104 get { return m_hovercolor1; }
105 set { m_hovercolor1 = value; Invalidate(); }
106 }
107 public Color MouseHoverColor2
108 {
109 get { return m_hovercolor2; }
110 set { m_hovercolor2 = value; Invalidate(); }
111 }
112 public Color MouseClickColor1
113 {
114 get { return clickcolor1; }
115 set { clickcolor1 = value; Invalidate(); }
116 }
117 public Color MouseClickColor2
118 {
119 get { return clickcolor2; }
120 set { clickcolor2 = value; Invalidate(); }
121 }
122
123 public int Transparent1
124 {
125 get { return color1Transparent; }
126 set
127 {
128 color1Transparent = value;
129 if (color1Transparent > 255)
130 {
131 color1Transparent = 255;
132 Invalidate();
133 }
134 else
135 Invalidate();
136 }
137 }
138
139 public int Transparent2
140 {
141 get { return color2Transparent; }
142 set
143 {
144 color2Transparent = value;
145 if (color2Transparent > 255)
146 {
147 color2Transparent = 255;
148 Invalidate();
149 }
150 else
151 Invalidate();
152 }
153 }
Page 26 of 30
154 public int GradientAngle
155 {
156 get { return angle; }
157 set { angle = value; Invalidate(); }
158 }
159 public int TextLocation_X
160 {
161 get { return textX; }
162 set { textX = value; Invalidate(); }
163 }
164 public int TextLocation_Y
165 {
166 get { return textY; }
167 set { textY = value; Invalidate(); }
168 }
170 public Boolean ShowButtontext
171 {
172 get { return showButtonText; }
173 set { showButtonText = value; Invalidate(); }
}
public ButtonZ()
{
this.Size = new Size(100, 40);
this.BackColor = Color.Transparent;
this.FlatStyle = FlatStyle.Flat;
this.FlatAppearance.BorderSize = 0;
this.FlatAppearance.MouseOverBackColor = Color.Transparent;
this.FlatAppearance.MouseDownBackColor = Color.Transparent;
text = this.Text;
}
Page 27 of 30
color2 = clr2;
SetBorderColor(borderColor);
this.Invalidate();
}
b.Dispose();
}
b.Dispose();
}
Page 28 of 30
//draw round rectangular button function
void DrawRoundRectangularButton(Graphics g)
{
Color c1 = Color.FromArgb(color1Transparent, color1);
Color c2 = Color.FromArgb(color2Transparent, color2);
Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2,
angle);
if (showButtonText)
{
Point p = new Point(textX, textY);
SolidBrush frcolor = new SolidBrush(this.ForeColor);
g.DrawString(text, this.Font, frcolor, p);
}
b.Dispose();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
switch (buttonShape)
{
case ButtonsShapes.Circle:
this.DrawCircularButton(e.Graphics);
break;
case ButtonsShapes.Rect:
this.DrawRectangularButton(e.Graphics);
break;
case ButtonsShapes.RoundRect:
this.DrawRoundRectangularButton(e.Graphics);
break;
}
}
}
}
Page 29 of 30
Page 30 of 30