Skip to content

Commit 1bda227

Browse files
Added Files via Upload
Practiced Java Programs
1 parent 97199d3 commit 1bda227

57 files changed

Lines changed: 1859 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎ActionListenerExample.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.awt.*;
2+
import java.awt.event.*;
3+
//1st step
4+
public class ActionListenerExample implements ActionListener{
5+
public static void main(String[] args) {
6+
Frame f=new Frame("ActionListener Example");
7+
final TextField tf=new TextField();
8+
tf.setBounds(50,50, 150,20);
9+
Button b=new Button("Click Here");
10+
b.setBounds(50,100,60,30);
11+
//2nd step
12+
b.addActionListener(new ActionListenerExample());
13+
f.add(b);f.add(tf);
14+
f.setSize(400,400);
15+
f.setLayout(null);
16+
f.setVisible(true);
17+
}
18+
//3rd step
19+
public void actionPerformed(ActionEvent e){
20+
tf.setText("Welcome to Javatpoint.");
21+
}
22+
}

‎AppletOval.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import java.awt.*;
2+
import java.applet.*;
3+
/*<applet code=AppletOval width=500 height=500></applet>*/
4+
public class AppletOval extends Applet
5+
{
6+
public void paint(Graphics g)
7+
{
8+
showStatus("applet oval program");
9+
g.drawOval(20,20,50,50);
10+
g.fillOval(20,20,50,50);
11+
g.drawOval(60,60,30,40);
12+
g.fillOval(250,250,30,40);
13+
}
14+
}

‎AppletRect.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import java.awt.*;
2+
import java.applet.*;
3+
/*<applet code=AppletRect width=500 height=500></applet>*/
4+
public class AppletRect extends Applet
5+
{
6+
public void paint(Graphics g)
7+
{
8+
g.drawString("Types of rectangles ",10,10);
9+
g.drawRect(20,20,100,60);
10+
g.fillRect(20,20,100,60);
11+
g.drawRoundRect(200,20,70,60,20,20);
12+
g.fillRoundRect(200,20,70,60,20,20);
13+
showStatus("This is Rectangles applet program");
14+
}
15+
}

‎Arithmetic.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.lang.*;
2+
3+
class Arithmetic
4+
{
5+
public static void main(String args[])
6+
{
7+
int a=20, b=5;
8+
int sum,sub,mul;
9+
float div;
10+
sum=a+b;
11+
sub=a-b;
12+
mul=a*b;
13+
div=a/b;
14+
System.out.println("The sum of two number is " + sum);
15+
System.out.println("The sub of two number is " + sub);
16+
System.out.println("The mul of two number is " + mul);
17+
System.out.println("The div of two number is " + div);
18+
}
19+
}

‎Armstrong.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.lang.*;
2+
3+
class Armstrong
4+
{
5+
public static void main(String args[])
6+
{
7+
int n= Integer.parseInt(args[0]);
8+
int rem=0,rev=0;
9+
int temp=n;
10+
while(temp>0)
11+
{
12+
rem=temp%10;
13+
rev=rev+(rem*rem*rem);
14+
temp=temp/10;
15+
}
16+
if(rev==n)
17+
System.out.println("The number is Armstrong");
18+
else
19+
System.out.println("The number is Not Armstrong");
20+
}
21+
}

‎BinarySearch.java‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import java.lang.*;
2+
import java.util.Scanner;
3+
4+
class BinarySearch
5+
{
6+
int binaryRecursive (int a[] , int low, int high, int key)
7+
{
8+
int mid;
9+
if(low == high)
10+
{
11+
if(key == a[high])
12+
{
13+
return low;
14+
}
15+
else
16+
return 0;
17+
}
18+
else{
19+
mid = low + (high - low)/2;
20+
if(key == a[mid])
21+
{
22+
return mid;
23+
}
24+
else if (key < a[mid])
25+
{
26+
return binaryRecursive(a,low,mid-1,key);
27+
}
28+
else
29+
{
30+
return binaryRecursive(a,mid+1,high,key);
31+
}
32+
}
33+
}
34+
public static void main(String[] args)
35+
{
36+
System.out.println("Binary Search Recursive");
37+
Scanner sc = new Scanner(System.in);
38+
System.out.println("Enter the size of the array");
39+
int size = sc.nextInt();
40+
int[] arr = new int[size];
41+
System.out.println("Enter the array elements");
42+
for(int i=0; i<arr.length; i++)
43+
{
44+
arr[i] = sc.nextInt();
45+
}
46+
System.out.println("Enter the element to be searched ");
47+
int key = sc.nextInt();
48+
// sorting the array in ascending order
49+
for(int i=0; i<arr.length; i++)
50+
{
51+
for(int j=i+1; j<arr.length; j++)
52+
{
53+
if(arr[i] > arr[j])
54+
{
55+
int temp = arr[i];
56+
arr[i] = arr[j];
57+
arr[j] = temp;
58+
}
59+
}
60+
}
61+
BinarySearch rb = new BinarySearch();
62+
int k = rb.binaryRecursive(arr, 0, size, key);
63+
if(k==0)
64+
{
65+
System.out.println("Element is not found");
66+
System.out.println(k);
67+
}
68+
else
69+
{
70+
System.out.println("Element is found at the index " + (k+1));
71+
}
72+
}
73+
}

‎BubbleSort.java‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.lang.*;
2+
import java.util.*;
3+
4+
class BubbleSort{
5+
public static void main(String args[])
6+
{
7+
Scanner s = new Scanner(System.in);
8+
System.out.println("Enter the size of the array");
9+
int size = s.nextInt();
10+
int arr[] = new int[size];
11+
for(int i=0; i<size; i++)
12+
{
13+
arr[i] = s.nextInt();
14+
}
15+
System.out.println("Array Before Sorting");
16+
for(int i=0; i<size; i++)
17+
{
18+
System.out.print(arr[i]+ " ");
19+
}
20+
int temp;
21+
for(int i=0; i<arr.length; i++)
22+
{
23+
for(int j=0; j<arr.length-1-i;j++ )
24+
{
25+
if(arr[j]>arr[j+1])
26+
{
27+
temp = arr[j];
28+
arr[j] = arr[j+1];
29+
arr[j+1] = temp;
30+
}
31+
}
32+
}
33+
System.out.println("\nAfter Sorting Array Elements ");
34+
for(int i=0; i<arr.length; i++)
35+
{
36+
System.out.print(arr[i] + " ");
37+
}
38+
}
39+
40+
}

‎ButtonDemo.java‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.applet.*;
2+
import java.awt.*;
3+
import java.awt.event.*;
4+
/*
5+
<applet code = "ButtonDemo" width = 500 height = 300>
6+
</applet>
7+
*/
8+
9+
public class ButtonDemo extends Applet implements ActionListener
10+
{
11+
Button b1,b2,b3;
12+
String msg = " ";
13+
public void init()
14+
{
15+
b1 = new Button("RED");
16+
b2 = new Button("GREEN");
17+
b3 = new Button("BLUE");
18+
add(b1);
19+
add(b2);
20+
add(b3);
21+
b1.addActionListener(this);
22+
b2.addActionListener(this);
23+
b3.addActionListener(this);
24+
25+
}
26+
public void actionPerformed(ActionEvent ae)
27+
{
28+
String s = ae.getActionCommand();
29+
if(s.equals("RED"))
30+
{
31+
setBackground(Color.red);
32+
msg = "I pressed on red color";
33+
}
34+
else if(s.equals("GREEN"))
35+
{
36+
setBackground(Color.green);
37+
msg = "I pressed on green color";
38+
}
39+
else
40+
{
41+
if(s.equals("BLUE"))
42+
{
43+
setBackground(Color.blue);
44+
msg = "I pressed on blue color";
45+
}
46+
}
47+
repaint();
48+
49+
}
50+
51+
public void paint (Graphics g)
52+
{
53+
54+
g.drawString(msg,0,100);
55+
56+
}
57+
}

‎ButtonDemo3.java‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.awt.*;
2+
import java.awt.event.*;
3+
import java.applet.*;
4+
/*<applet code="ButtonDemo3" width=500 height=500></applet>*/
5+
public class ButtonDemo3 extends Applet implements ActionListener {
6+
Button b1, b2, b3;
7+
String msg = " ";
8+
public void init() {
9+
b1 = new Button("red");
10+
b2 = new Button("green");
11+
b3 = new Button("blue");
12+
add(b1);
13+
add(b2);
14+
add(b3);
15+
b1.addActionListener(this);
16+
b2.addActionListener(this);
17+
b3.addActionListener(this);
18+
19+
}
20+
public void actionPerformed(ActionEvent ae)
21+
{
22+
String s = ae.getActionCommand();
23+
if(s.equals("red"))
24+
{
25+
setBackground(Color.red);
26+
showStatus("the red button is pressed");
27+
}
28+
else if(s.equals("green"))
29+
{
30+
setBackground(Color.green);
31+
showStatus("the green button is pressed");
32+
}
33+
else if(s.equals("blue"))
34+
{
35+
setBackground(Color.blue);
36+
showStatus("the blue button is pressed");
37+
}
38+
repaint();
39+
}
40+
public void paint(Graphics g) {
41+
g.drawString(msg, 0, 100);
42+
43+
}
44+
}

‎ButtonDemoLab.java‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.awt.*;
2+
import java.awt.event.*;
3+
import java.applet.*;
4+
/*<applet code = ButtonDemoLab width = 300 height = 300 > </applet>*/
5+
public class ButtonDemoLab extends Applet implements ActionListener
6+
{
7+
Button b1,b2,b3;
8+
String msg = " ";
9+
public void init()
10+
{
11+
b1 = new Button("RED");
12+
b2 = new Button("GREEN");
13+
b3 = new Button("BLUE");
14+
add(b1);
15+
add(b2);
16+
add(b3);
17+
b1.addActionListener(this);
18+
b2.addActionListener(this);
19+
b3.addActionListener(this);
20+
}
21+
public void actionPerformed(ActionEvent e)
22+
{
23+
String s = e.getActionCommand();
24+
if(s.equals("RED"))
25+
{
26+
setBackground(Color.red);
27+
showStatus("I pressed red button");
28+
}
29+
else if(s.equals("BLUE"))
30+
{
31+
setBackground(Color.blue);
32+
showStatus("I pressed blue button");
33+
}
34+
else if(s.equals("GREEN"))
35+
{
36+
setBackground(Color.green);
37+
showStatus("I pressed green button");
38+
}
39+
repaint();
40+
41+
}
42+
public void paint(Graphics g)
43+
{
44+
g.drawString(msg,0,100);
45+
}
46+
}

0 commit comments

Comments
 (0)