0% found this document useful (0 votes)
5 views37 pages

Java Journal 2023

The document contains multiple Java programming examples demonstrating various concepts such as conditional statements, loops, classes, method overloading, inheritance, exception handling, and GUI creation. Each example includes code snippets with corresponding outputs, covering topics like calculating factorials, area and circumference of circles, and creating threads. Additionally, it showcases mouse event handling and creating graphical user interfaces using Swing and AWT libraries.

Uploaded by

keertanadatti49
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views37 pages

Java Journal 2023

The document contains multiple Java programming examples demonstrating various concepts such as conditional statements, loops, classes, method overloading, inheritance, exception handling, and GUI creation. Each example includes code snippets with corresponding outputs, covering topics like calculating factorials, area and circumference of circles, and creating threads. Additionally, it showcases mouse event handling and creating graphical user interfaces using Swing and AWT libraries.

Uploaded by

keertanadatti49
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1

01. Program to assign two integer values to X and Y. Using the „if‟ statement the
output of the program should display a message whether X is greater than Y.

public class Test


{

public static void main(String[] args)


{
int x = 50, y = 20;
if (x > y)
{
[Link]("x is greater than y");

[Link]("This statement is outside the body of if");


}
}

OUTPUT:

x is greater than y
2

02. Program to list the factorial of the numbers 1 to 10. To calculate the factorial
value, use while loop. (Hint: Fact of 4 = 4*3*2*1)

import [Link];

public class FactorialUsingWhileLoop


{
public static void main(String[] args)
{
int fact = 1;
int i = 1;
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
if(num<=10)
{
while(i<= num)
{
fact = fact * i;
i++; //increment i by 1

}
[Link]("\nFactorial of " + num + " is: " + fact);
}
else
{
[Link]("Please,Enter the number in between 1 to 10");
}
}
}

OUTPUT:

Enter a number:

Factorial of 3 is: 6
3

03. Program to find the area and circumference of the circle by accepting the radius
from the user.

import [Link].*;

public class CircleArea {

public static void main(String[] args)

double radius, area, circumference;

Scanner in = new Scanner([Link]);

[Link]("Enter Radius of Circle:");

radius = [Link]();

// Calculate area and circumference of circle

area = [Link] * radius * radius;

circumference = 2 * [Link] * radius;

[Link]("Area of Circle : " + area);

[Link]("Circumference of Circle : " + circumference);

OUTPUT:

Enter Radius of Circle:

Area of Circle : 28.274333882308138

Circumference of Circle : 18.84955592153876


4

04. Program to add two integers and two float numbers. When no arguments are
supplied, give a default value to calculate the sum. Use function overloading.

class Overloading

static int add(int a, int b)//Both integer variables

return a+b;//Returning the sum

static float add(float a, float b)//Both double variables

return a+b;//Returning the sum

class TestOverloading

public static void main(String[] args)

[Link]([Link](1,10));

[Link]([Link](10.5f,20.7f));

OUTPUT:

11

31.2
5

05. Program to perform mathematical operations. Create a class called AddSub with
methods to add and subtract. Create another class called MulDiv that extends from
AddSub class to use the member data of the super class. MulDiv should have methods
to multiply and divide A main function should access the methods and perform the
mathematical operations.

class addsub

int num1;

int num2;

addsub(int n1, int n2)

num1 = n1;

num2 = n2;

int add()

return num1+num2;

int sub()

return num1-num2;

class multdiv extends addsub

{
6

public multdiv(int n1, int n2)

super(n1, n2);

int mul()

return num1*num2;

float div()

return num2/num1;

public void display()

[Link]("Number 1 :" + num1);

[Link]("Number 2 :" + num2);

public class adsb

public static void main(String arg[])

addsub r1=new addsub(50,20);

int ad = [Link]();

int sb = [Link]();
7

[Link]("Addition =" +ad);

[Link]("Subtraction =" +sb);

multdiv r2 =new multdiv(4,20);

int ml = [Link]();

float dv =[Link]();

[Link]("Multiply =" +ml);

[Link]("Division =" +dv);

OUTPUT:

Addition =70

Subtraction =30

Multiply =80

Division =5.0
8

06. Program with class variable that is available for all instances of a class. Use
static variable declaration. Observe the changes that occur in the object‟s
member variable values.

class Demo6
{
static int count=0;
Demo6()
{
count++;
}
void disp()
{
[Link](count);
}
public static void main(String a[])
{
Demo6 d[]=new Demo6[5];
for(int i=0;i<5;i++)
{
d[i]= new Demo6();
d[i].disp();
}
}
}

OUTPUT:

1
2
3
4
5
9

07. Program to create a student class with following attributes; Enrollment No:
Name, Mark of sub1, Mark of sub2, mark of sub3, Total Marks. Total of the three
marks must be calculated only when the student passes in all three subjects. The
passing mark for each subject is 50. If a candidate fails in any one of the subjects
his total mark must be declared as zero. Using this condition write a constructor
for this class. Write separate functions for accepting and displaying student
details. In the main method create an array of three student objects and display
the details.

import [Link].*;
class Student
{
Scanner sc=new Scanner([Link]);
int RegNo,Total=0,subjects,i;
String name;
int marks[];
Student()
{
[Link]("Enter Registration No.: ");
RegNo=[Link]();
[Link]("Enter Student Name: ");
name=[Link]();
int total=getMarks();
[Link]("Total Marks of student "+name+": " +Total);
}

public intgetMarks(){
marks=new int[3];
[Link]("Enter three subject marks: ");
for(i=0;i<3;i++)
{
marks[i]=[Link]();
Total+=marks[i];
}
for(i=0;i<3;i++)
{
if(marks[i]<50)
{
Total=0;
}}
10

return Total;
}}

class StudDemo
{
public static void main(String args[])
{
Student s[]=new Student[3];
for(int i=0;i<3;i++)
{
s[i]=new Student();
}
}
}

OUTPUT:
11

08. Write a program to demonstrate multiple inheritance and use of


Implementing Interfaces
interface CollegeInfo
{
public void collegeDetail();
public void studentData();
}
interface HostelInfo
{
public void hostelDetail();
public void studentRecord();
}
public class Student implements CollegeInfo, HostelInfo
{
public void hostelDetail()
{
[Link]("Hostel Name : Vivekananda");
[Link]("Hostel location : College Campus");
}
public void studentRecord()
{
[Link]("Student selected based on : Percentage, Financial condition");
}
public void collegeDetail()
{
[Link]("College Name : BLDE");
[Link]("College Grade : A+");
[Link]("University of College : RCUB");
}
public void studentData()
{
[Link]("Courses of Student : BCA, BBA, BCom, BSc, BA");
}
public static void main (String[] args){
Student obj = new Student();
[Link]();
[Link]();
[Link]();
[Link]();
}}
12

OUTPUT:
13

09. Illustrate creation of thread by a) Extending Thread class. b) Implementing


Runnable Interfaces.

a) Extending Thread class.

class A extends Thread

public void run()

for(inti=1;i<=5;i++)

[Link]("Thread A"+i);

[Link]("Exit from A");

class B extends Thread

public void run()

for(int i=1;i<=5;i++)

[Link]("Thread B"+i);

[Link]("Exit from B");

}
14

class C extends Thread

public void run()

for(inti=1;i<=5;i++)

[Link]("Thread C"+i);

[Link]("Exit from C");

class ThreadTest

public static void main(String args[])

A obja=new A();

B objb=new B();

[Link]();

[Link]();

[Link]();

}
15

OUTPUT:

thread a1

thread c1

thread b1

thread b2

thread b3

thread b4

thread b5

Exit from b

thread a2

thread c2

thread c3

thread c4

thread c5

Exit from c

thread a3

thread b4

thread b5

exit from a
16

b) Implementing Runnable interface.

class callme

void call(String msg)

[Link]("["+msg);

try

[Link](1000);

catch(InterruptedException e)

[Link]("Interrupted");

[Link]("]");

class caller implements Runnable

String msg;

callme target;

Thread t;

public caller(callmetarg,String s)

target=targ;
17

msg=s;

t=new Thread(this);

[Link]();

public void run()

synchronized(target)

[Link](msg);

} }}

class Synch1 {

public static void main(String args[])

callme target=new callme();


caller ob1=new caller(target,"Hello");

caller ob2=new caller(target,"Synchronssized");

caller ob3=new caller(target,"World");

try{

[Link](); [Link]();[Link]();

catch(InterruptedException e)

[Link]("Interrupted");

}
}
18

OUTPUT:

[Hello

[World

[Synchronssized

]
19

[Link] a package „BCA‟ in your current working directory.

a. Create a class student in the above package with the following attributes: Name,
age, gender. Include appropriate constructor and a method for displaying the details.

b. Import above package and access the member variables and function contained in
a package

package BCA;

public class Student12

String sname, gen;

int age;

public Student12(String sn,String g, int a)

sname=sn;

gen=g;

age=a;

public void disp()

[Link]("Student Name: "+sname);

[Link]("Gender: "+gen);

[Link]("Age: "+age);

}
20

import BCA.Student12;

class Test12

public static void main(String a[])

Student12 s=new Student12("Chanakya","Male",8);

[Link]();

OUTPUT:
21

Part-B
22

01. Program to catch Negative Array Size Exception. This exception is


caused whenthe array size is initialized to negative values.

import [Link];

class Lab2B

public static void main(String ar[])

try

Scanner sc= new Scanner([Link]);

[Link]("Enter the size of array");

int n= [Link]();

int[] a=new int[n];

[Link]("Enter the array elements");


for(int i=0;i<n;i++)

{ a[i]=[Link]();

[Link]("Entered elements are");

for(int i=0;i<n;i++)
{

[Link](a[i]);

}}

catch(NegativeArraySizeException e)

[Link]("Size of the array should be greater than 0"); }}}


23

OUTPUT:
24

02. Program to demonstrate exception handling with try, catch and finally.

import [Link];

class Demo2

public static void main(String ar[])

try

Scanner sc= new Scanner([Link]);

[Link]("Enter two values");

int a=[Link]();

int b=[Link]();

int c=a/b;

[Link]("Division of two Numbers :"+c);

catch(Exception e)

[Link](e);

finally

[Link]("Program executed ... ");

}
25

OUTPUT:
26

03. PROGRAM TO CREATE AND DISPLAY MESSAGE ON WINDOW.

import [Link].*;

import [Link].*;

class DisplayMessageOnWindow

public static void main(String args[])

JFrame frame=new JFrame("DISPLAYING MESSAGE ON WINDOW");

[Link](500,500);

[Link]([Link]);

[Link](JFrame.EXIT_ON_CLOSE);

[Link](null);

JLabel label=new JLabel();

[Link](0,100,500,50);

[Link](label);

String str="WELCOME TO JAVA WORLD";

[Link](str);

[Link](true);

}
27

OUTPUT:
28

04. PROGRAM TO DRAW DIFFERENT SHAPES IN A CREATED WINDOW.

import [Link].*;

import [Link];

public class DrawShapes extends Canvas

public void paint(Graphics g)

[Link](25,25,100,25);

[Link](25,40,100,50);

[Link](145,40,100,50);

[Link](265,40,50,50);

[Link](25,125,100,50,15,15);
[Link](145,125,100,50,15,15);
[Link](25,205,100,50);
[Link](145,205,100,50);
[Link](265,205,50,50);
g.draw3DRect(25,280,100,50,true);
g.draw3DRect(145,280,100,50,false);
[Link](25,345,100,50,25,75);
[Link](145,345,100,50,125,75);
}
public static void main(String[] args)
{
DrawShapes m=new DrawShapes();
JFrame f=new JFrame();
[Link](m);
[Link](400,400);
[Link](true);
}
}
29

OUTPUT:
30

05. JAVA PROGRAM TO DRAW A GRID LINES

import [Link].*;
import [Link].*;
public class MyGridLayout{
JFrame f;
MyGridLayout(){
f=new JFrame();
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
// adding buttons to the frame
[Link](b1); [Link](b2); [Link](b3);
[Link](b4); [Link](b5); [Link](b6);
[Link](b7); [Link](b8); [Link](b9);

// setting grid layout of 3 rows and 3 columns


[Link](new GridLayout(3,3));
[Link](300,300);
[Link](true);
}
public static void main(String[] args) {
new MyGridLayout();
}
}
31

6. Demonstrate the various mouse handling events using suitable example.

import [Link].*;

import [Link].*;

public class MouseListnerExample extends Frame implements MouseListener

Label l;

MouseListnerExample()

addMouseListener(this);

l=new Label();

[Link](20,50,100,20);

add(l);

setSize(300,300);

setLayout(null);

setVisible(true);

public void mouseClicked(MouseEvent e)

[Link]("Mouse Clicked");

public void mouseEntered(MouseEvent e)

[Link]("Mouse Enterd");

}
32

public void mouseExited(MouseEvent e)

[Link]("Mouse exited");

public void mousePressed(MouseEvent e)

[Link]("Mouse pressed");

public void mouseReleased(MouseEvent e)

[Link]("Mouse released");

public static void main(String[] args)

new MouseListnerExample();

OUTPUT:
33

7. Program which creates a frame with two buttons father and mother. Whenwe
click the father button the name of the father, his age and designation mustappear.
When we click mother similar details of mother also appear.
import [Link].*;
import [Link].*;
import [Link].*;
public class CreateFrameExample extends JFrame
{
JButton father,mother,close; JLabel name,age,des;
CreateFrameExample()
{
father = new JButton("Father");
mother = new JButton ("Mother");
close = new JButton ("Exit");
name = new JLabel ("");
age = new JLabel ("");
des = new JLabel ("");
setLayout (new GridLayout(6,1));
setSize (400,200);
add(father);
add(mother);
add(close);
add(name);
add(age);
add(des);
setVisible(true);

//setDefaultCloseOperation(JFrame.EXIT_NO_CLOSE);
ButtonHandler bh = new ButtonHandler();
[Link](bh);
[Link](bh);
//[Link](bh);
[Link](bh);
}
class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent ae)
34

{
if ([Link]()==father)
{

[Link]("Name: ABC");
[Link]("Age: 48");
[Link]("Principal");

}
if ([Link]()==mother)
{
[Link]("Name: xyz");
[Link]("Age: 44"); [Link]("Teacher");
}
if ([Link]()==close)
{
[Link](0);
}
}
}
public static void main(String args[])
{
new CreateFrameExample();
}
}
35

OUTPUT
36

08. Program to create a window with text fields and buttons.


the “add” button adds the two integers and display the result.
import [Link].*;
import [Link].*;
import [Link].*;
public class LearnAWT12 extends Frame
{
TextField tf1;
TextField tf2;
Label l1;
Button b;
LearnAWT12()
{
setTitle("Adder");
tf1=new TextField();
[Link](100,50,85,20);
tf2=new TextField();
[Link](100,100,85,20);
b=new Button("Add");
[Link](110,220,60,40);
l1=new Label("");
[Link](100,120,85,20);
add(b);
add(tf1);
add(tf2);
add(l1);
setSize(300,300);
setVisible(true);
[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int a=[Link]([Link]());
int b=[Link]([Link]());
int c=a+b;
[Link]("Their sum is="+[Link](c));
}
});
}
public static void main(String []args)
{
new LearnAWT12();
}
}
37

OUTPUT

You might also like