[Go to site: main page, start]

0% found this document useful (0 votes)
30 views21 pages

Java Programming Sample Files

Uploaded by

ansh parashar
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)
30 views21 pages

Java Programming Sample Files

Uploaded by

ansh parashar
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

4/26/24, 10:10 PM <h2>Selected files</h2>

Selected files
34 printable files

java\[Link]
java\[Link]
java\[Link]
java\[Link]
java\[Link]
java\[Link]
java\[Link]
java\[Link]
java\[Link]
java\[Link]
java\[Link]
java\[Link]
java\[Link]
java\[Link]
java\[Link]
java\[Link]
java\[Link]
java\[Link]
java\[Link]
java\[Link]
java\[Link]
java\[Link]
java\[Link]
java\[Link]
java\[Link]
java\Inheritance\[Link]
java\[Link]
java\[Link]
java\ExceptionHandling\[Link]
java\DataStructs\[Link]
java\ExceptionHandling\[Link]
java\DataStructs\[Link]
java\Multithreading\[Link]
java\[Link]

java\[Link]

1 class HelloWorld {
2 public static void main(String[] args) {
3 [Link]("Hello World");
4 }
5 }

java\[Link]

1 class Concatenate {
2 public static void main(String[] args) {
3 String s = "folks";
4 [Link]("Hello" + " " + s);
5 }
6 }

java\[Link]
localhost:51815/04d07b4e-6e7b-4361-8bb3-74b4eb8fb525/ 1/21
4/26/24, 10:10 PM <h2>Selected files</h2>

1 import [Link];
2 class Swap {
3 public static void main(String[] args) {
4 int a, b, temp;
5 Scanner cin = new Scanner([Link]);
6 [Link]("Enter first number: ");
7 a = [Link]();
8 [Link]("Enter second number: ");
9 b = [Link]();
10 [Link]("a = " + a + " and b = " + b);
11 temp = a;
12 a = b;
13 b = temp;
14 [Link]("Value Swapped!\na = " + a + " and b = " + b);
15 }
16 }

java\[Link]

1 import [Link];
2 class UserInput {
3 public static void main(String[] args) {
4 int num;
5 Scanner cin = new Scanner([Link]);
6 [Link]("Enter a number: ");
7 num = [Link]();
8 [Link](num);
9 }
10 }

java\[Link]

1 class Reverse {
2 public static void main(String[] args) {
3 int x = 1234;
4 int[] rem = new int[4];
5 [Link]("Number: " + x);
6 int i = 0;
7 while(x > 0) {
8 rem[i] = x % 10;
9 x = x/10;
10 i++;
11 }
12 [Link]("Reverse of the number: ");
13 for(int j = 0; j < 4; j++) {
14 [Link](rem[j]);
15 }
16 }
17 }

java\[Link]

localhost:51815/04d07b4e-6e7b-4361-8bb3-74b4eb8fb525/ 2/21
4/26/24, 10:10 PM <h2>Selected files</h2>

1 import [Link];
2 class Smallest {
3 public static void main(String[] args) {
4 int a, b, c;
5 Scanner cin = new Scanner([Link]);
6 [Link]("Enter first number: ");
7 a = [Link]();
8 [Link]("Enter second number: ");
9 b = [Link]();
10 [Link]("Enter third number: ");
11 c = [Link]();
12 if(a <= b && a <= c)
13 [Link](a + " is the smallest number");
14 else if(b <= a && b <= c)
15 [Link](b + " is the smallest number");
16 else
17 [Link](c + " is the smallest number");
18 }
19 }

java\[Link]

1 import [Link];
2 class Largest {
3 public static void main(String[] args) {
4 int a, b, c;
5 Scanner cin = new Scanner([Link]);
6 [Link]("Enter first number: ");
7 a = [Link]();
8 [Link]("Enter second number: ");
9 b = [Link]();
10 [Link]("Enter third number: ");
11 c = [Link]();
12 if(a >= b && a >= c)
13 [Link](a + " is the largest number");
14 else if(b >= a && b >= c)
15 [Link](b + " is the largest number");
16 else
17 [Link](c + " is the largest number");
18 }
19 }

java\[Link]

1 class SecondLargest {
2 public static void main(String[] args) {
3 int[] a = {1, -2, 27, 99, 5};
4 int largest = 0, secondLargest = 0;
5 for(int i = 0; i < [Link]; i++) {
6 if(largest < a[i])
7 largest = a[i];
8 }

localhost:51815/04d07b4e-6e7b-4361-8bb3-74b4eb8fb525/ 3/21
4/26/24, 10:10 PM <h2>Selected files</h2>
9 for(int i = 0; i < [Link]; i++) {
10 if(secondLargest < a[i] && a[i] != largest) {
11 secondLargest = a[i];
12 }
13 }
14 [Link](largest);
15 [Link](secondLargest);
16 }
17 }

java\[Link]

1 class Lcm {
2 public static void main(String[] args) {
3 int x = 12, y = 36, lcm, max;
4 max = (x > y) ? x : y; //36
5 lcm = max;
6 while(true) {
7 if(max % x == 0 && max % y == 0) {
8 lcm = max;
9 break;
10 }
11 max++;
12 }
13 [Link]("LCM(" + x + ", " + y + ") = " + lcm);
14 }
15 }

java\[Link]

1 import [Link];
2 class Hcf {
3 public static void main(String[] args) {
4 int a, b, max, hcf = 1;
5 Scanner cin = new Scanner([Link]);
6 [Link]("Enter first number: ");
7 a = [Link]();
8 [Link]("Enter second number: ");
9 b = [Link]();
10 max = (a > b) ? a : b;
11 for(int i = 1; i < max; i++) {
12 if(max % i == 0) {
13 hcf = i;
14 }
15 }
16 [Link]("hcf(" + a + ", " + b + ") = " + hcf);
17 }
18 }

java\[Link]

1 import [Link];

localhost:51815/04d07b4e-6e7b-4361-8bb3-74b4eb8fb525/ 4/21
4/26/24, 10:10 PM <h2>Selected files</h2>
2 class Fibonacci {
3 public static void main(String[] args) {
4 Scanner sc = new Scanner([Link]);
5 int a = 0, b = 1, n;
6 [Link]("Enter n: ");
7 n = [Link]();
8 for(int i = 1; i <= n; i++) {
9 [Link](a + " ");
10 int temp = a + b;
11 a = b;
12 b = temp;
13 }
14 }
15 }

java\[Link]

1 class Factorial {
2 static int fac(int n) {
3 if(n == 0) { return 1; }
4 return n * fac(n-1);
5 }
6 public static void main(String[] args) {
7 int result;
8 result = fac(5);
9 [Link](result);
10 }
11 }

java\[Link]

1 import [Link];
2 class Pattern1 {
3 public static void main(String[] args) {
4 Scanner sc = new Scanner([Link]);
5 int n;
6 [Link]("Enter the number of rows: ");
7 n = [Link]();
8 for(int i = 1; i <= n; i++) {
9 for(int j = 1; j <= 2*i-1; j++)
10 [Link]("*");
11 [Link]("");
12 }
13 }
14 }

java\[Link]

1 import [Link];
2 class Pattern2 {
3 public static void main(String[] args) {
4 Scanner sc = new Scanner([Link]);

localhost:51815/04d07b4e-6e7b-4361-8bb3-74b4eb8fb525/ 5/21
4/26/24, 10:10 PM <h2>Selected files</h2>
5 int n;
6 [Link]("Enter the number of rows: ");
7 n = [Link]();
8 for(int i = n; i >= 1; i--) {
9 for(int j = 1; j <= 2*i-1; j++)
10 [Link]("*");
11 [Link]("");
12 }
13 }
14 }

java\[Link]

1 import [Link];
2 class Pattern3 {
3 public static void main(String[] args) {
4 int n;
5 Scanner sc = new Scanner([Link]);
6 [Link]("Enter the number of rows: ");
7 n = [Link]();
8 for(int i = 1; i <= n; i++) {
9 for(int j = 1; j <= 2*i-1; j++)
10 [Link](" ");
11 for(int k = 1; k <= 2*i-1; k++)
12 [Link]("*");
13 [Link]("");
14 }
15 }
16 }
17 /*
18 *8
19 ***6
20 *****4
21 *******2
22 *********0
23 */

java\[Link]

1 import [Link];
2 class Triangle {
3 public static void main(String[] args) {
4 int b, h, a;
5 Scanner sc = new Scanner([Link]);
6 [Link]("Enter height: ");
7 h = [Link]();
8 [Link]("Enter base: ");
9 b = [Link]();
10 a = (b*h)/2;
11 [Link]("The area of triangle of height " + h + " and base " + b + " is " +
a + " sq. units");
12 }
13 }

localhost:51815/04d07b4e-6e7b-4361-8bb3-74b4eb8fb525/ 6/21
4/26/24, 10:10 PM <h2>Selected files</h2>
java\[Link]

1 import [Link];
2
3 class OneDArray {
4 public static void main(String[] args) {
5 int n;
6 Scanner sc = new Scanner([Link]);
7 [Link]("Enter the size for array: ");
8 n = [Link]();
9 int[] a;
10 a = new int[n];
11 for(int i = 0; i < n; i++) {
12 [Link]("Enter element for index " + i + ": ");
13 a[i] = [Link]();
14 }
15 for(int i = 0; i < n; i++) {
16 [Link](a[i] + " ");
17 }
18 }
19 }

java\[Link]

1 import [Link];
2 class TwoDArray {
3 public static void main(String[] args) {
4 int m, n;
5 Scanner sc = new Scanner([Link]);
6 [Link]("Enter the dimensions for array");
7 [Link]("Enter row: ");
8 m = [Link]();
9 [Link]("Enter column: ");
10 n = [Link]();
11 int[][] a = new int[m][n];
12 for(int i = 0; i < m; i++) {
13 for(int j = 0; j < n; j++) {
14 [Link]("Enter element for index (" + i + ", " + j + "): ");
15 a[i][j] = [Link]();
16 }
17 }
18 for(int i = 0; i < n; i++) {
19 for(int j = 0; j < n; j++) {
20 [Link](a[i][j] + " ");
21 }
22 [Link]("");
23 }
24 }
25 }

java\[Link]

localhost:51815/04d07b4e-6e7b-4361-8bb3-74b4eb8fb525/ 7/21
4/26/24, 10:10 PM <h2>Selected files</h2>

1 import [Link];
2 class ThreeDArray {
3 public static void main(String[] args) {
4 Scanner sc = new Scanner([Link]);
5 int[][][] a = new int[4][4][3];
6 [Link]("Enter elements for array of dimension ");
7 [Link]([Link] + " x " + a[0].length + " x " + a[0][0].length);
8 [Link](">> ");
9 for(int i = 0; i < [Link]; i++) {
10 for(int j = 0; j < a[i].length; j++) {
11 for(int k = 0; k < a[i][j].length; k++) {
12 a[i][j][k] = [Link]();
13 }
14 }
15 }
16 for(int i = 0; i < [Link]; i++) {
17 for(int j = 0; j < a[i].length; j++) {
18 for(int k = 0; k < a[i][j].length; k++) {
19 [Link](a[i][j][k] + " ");
20 }
21 [Link]("");
22 }
23 [Link]("");
24 }
25 }
26 }

java\[Link]

1 import [Link];
2 class Mat {
3 Scanner sc = new Scanner([Link]);
4 int [][]a;
5 Mat() { a = new int[2][2]; }
6 Mat(int n) { a = new int[n][n]; }
7 Mat(int m, int n) {
8 a = new int[m][n];
9 }
10 Mat(Mat m) {
11 this.a = new int[[Link]][m.a[0].length];
12 for(int i = 0; i < [Link]; i++)
13 for(int j = 0; j < a[0].length; j++)
14 this.a[i][j] = m.a[i][j];
15 }
16 void set() {
17 [Link]("Enter elements for matrix of dimension " + [Link] + " x " +
a[0].length);
18 for(int i = 0; i < [Link]; i++)
19 for(int j = 0; j < a[0].length; j++)
20 a[i][j] = [Link]();
21 [Link]("");
22 }
23 void resize(int m, int n) {
24 a = new int[m][n];
25 }
localhost:51815/04d07b4e-6e7b-4361-8bb3-74b4eb8fb525/ 8/21
4/26/24, 10:10 PM <h2>Selected files</h2>
26 void disp() {
27 for(int i = 0; i < [Link]; i++) {
28 for(int j = 0; j < a[0].length; j++)
29 [Link](a[i][j] + " ");
30 [Link]("");
31 }
32 [Link]("");
33 }
34 Mat add(Mat m) {
35 if([Link] == [Link] && this.a[0].length == m.a[0].length) {
36 Mat temp = new Mat([Link], this.a[0].length);
37 for(int i = 0; i < [Link]; i++)
38 for(int j = 0; j < a[0].length; j++)
39 temp.a[i][j] = this.a[i][j] + m.a[i][j];
40 return temp;
41 }
42 else return new Mat(1);
43 }
44 Mat sub(Mat m) {
45 if([Link] == [Link] && this.a[0].length == m.a[0].length) {
46 Mat temp = new Mat([Link], this.a[0].length);
47 for(int i = 0; i < [Link]; i++)
48 for(int j = 0; j < a[0].length; j++)
49 temp.a[i][j] = this.a[i][j] - m.a[i][j];
50 return temp;
51 }
52 else return new Mat(1);
53 }
54 Mat mul(Mat m) {
55 if(this.a[0].length == [Link]) {
56 Mat temp = new Mat([Link], m.a[0].length);
57 for(int i = 0; i < [Link]; i++)
58 for(int j = 0; j < temp.a[0].length; j++)
59 for(int k = 0; k < [Link]; k++)
60 temp.a[i][j] += this.a[i][k] * m.a[k][j];
61 return temp;
62 }
63 else return new Mat(1);
64 }
65 Mat trans() {
66 Mat temp = new Mat(this.a[0].length, [Link]);
67 for(int i = 0; i < a[0].length; i++)
68 for(int j = 0; j < [Link]; j++)
69 temp.a[i][j] = this.a[j][i];
70 return temp;
71 }
72 Mat diag() {
73 if([Link] == this.a[0].length) {
74 Mat temp = new Mat(1, [Link]);
75 for(int i = 0; i < [Link]; i++)
76 temp.a[0][i] = this.a[i][i];
77 return temp;
78 }
79 else return new Mat(1);
80 }
81 }

localhost:51815/04d07b4e-6e7b-4361-8bb3-74b4eb8fb525/ 9/21
4/26/24, 10:10 PM <h2>Selected files</h2>
82 class RunMat {
83 public static void main(String[] args) {
84 Mat m1 = new Mat(2, 3);
85 Mat m2 = new Mat(3, 2);
86 [Link]();
87 [Link]();
88 [Link]();
89 [Link]();
90 [Link](m2).disp();
91 [Link](m2).disp();
92 [Link](m2).disp();
93 [Link]().disp();
94 [Link]().disp();
95 [Link]().disp();
96 [Link]().disp();
97
98 Mat m3 = new Mat(m2);
99 [Link]();
100 [Link](3, 3);
101 [Link]();
102 [Link]();
103 }
104 }

java\[Link]

1 class Cplx {
2 private int a;
3 private int b;
4 Cplx() {
5 a = b = 0;
6 }
7 Cplx(int x) {
8 a = x;
9 b = x;
10 }
11 Cplx(int x, int y) {
12 a = x;
13 b = y;
14 }
15 Cplx(Cplx n) {
16 a = n.a;
17 b = n.b;
18 }
19 Cplx add(Cplx n) {
20 Cplx temp = new Cplx(); // data members of temp depend upon default constructor
21 temp.a = this.a + n.a;
22 temp.b = this.b + n.b;
23 return temp;
24 }
25 Cplx sub(Cplx n) {
26 Cplx temp = new Cplx();
27 temp.a = this.a - n.a;
28 temp.b = this.b - n.b;
29 return temp;

localhost:51815/04d07b4e-6e7b-4361-8bb3-74b4eb8fb525/ 10/21
4/26/24, 10:10 PM <h2>Selected files</h2>
30 }
31 Cplx mul(Cplx n) {
32 Cplx temp = new Cplx();
33 temp.a = (this.a * n.a - this.b * n.b);
34 temp.b = (this.a * n.b + n.a * this.b);
35 return temp;
36 }
37 void disp() {
38 [Link](this.a + " + " + this.b + "i");
39 }
40 }
41 class RunCplx {
42 public static void main(String[] args) {
43 Cplx n1 = new Cplx(12, 24);
44 Cplx n2 = new Cplx(3, 18);
45 [Link]();
46 [Link]();
47 [Link](n2).disp();
48 [Link](n2).disp();
49 [Link](n2).disp();
50 }
51 }

java\[Link]

1 import [Link];
2 class Point {
3 private double x;
4 private double y;
5 void set(int m, int n) {
6 this.x = m;
7 this.y = n;
8 }
9 void disp() {
10 [Link]("(" + this.x + ", " + this.y + ")");
11 }
12 Point midPoint(Point n) {
13 Point temp = new Point();
14 temp.x = (this.x + n.x)/2;
15 temp.y = (this.y + n.y)/2;
16 return temp;
17 }
18 double dist(Point n) {
19 return [Link](((this.x-n.x)*(this.x-n.x)) + ((this.y-n.y)*(this.y-n.y)));
20 }
21 Point inc() {
22 Point temp = new Point();
23 temp.x = ++this.x;
24 temp.y = ++this.y;
25 return temp;
26 }
27 Point dec() {
28 Point temp = new Point();
29 temp.x = --this.x;
30 temp.y = --this.y;

localhost:51815/04d07b4e-6e7b-4361-8bb3-74b4eb8fb525/ 11/21
4/26/24, 10:10 PM <h2>Selected files</h2>
31 return temp;
32 }
33 }
34 class RunPoint {
35 public static void main(String[] args) {
36 Point p1 = new Point();
37 Point p2 = new Point();
38 Point m = new Point();
39 double d;
40 [Link]("P");
41 [Link](2, 4);
42 [Link]();
43
44 [Link]("Q");
45 [Link](-1, 3);
46 [Link]();
47
48 [Link]("M = ");
49 m = [Link](p2);
50 [Link]();
51
52 d = [Link](p2);
53 [Link]("d = " + d + " units");
54
55 [Link]("Increment by 1");
56 [Link]();
57 [Link]();
58
59 [Link]();
60 [Link]();
61
62 [Link]("Decrement by 1");
63 [Link]();
64 [Link]();
65
66 [Link]();
67 [Link]();
68 }
69 }

java\[Link]

1 class Rat {
2 private int p;
3 private int q;
4
5 Rat() {
6 p = q = 1;
7 }
8 Rat(int n) {
9 if(q != 0) p = q = n;
10 else p = q = 1;
11 }
12 Rat(int m, int n) {
13 p = m; q = n;

localhost:51815/04d07b4e-6e7b-4361-8bb3-74b4eb8fb525/ 12/21
4/26/24, 10:10 PM <h2>Selected files</h2>
14 }
15 Rat(Rat r) {
16 p = r.p;
17 q = r.q;
18 }
19 void set(int x, int y) {
20 p = x;
21 q = y;
22 }
23 void stdForm() {
24 if(q == 0) q = 1;
25 else { int h = hcf(p, q); if(h > 1) { p /= h; q /= h; } }
26 }
27 int hcf(int m, int n) {
28 if(n == 0) return m;
29 return hcf(n, m % n);
30 }
31 void disp() {
32 [Link]("(" + p + ", " + q + ")");
33 }
34 float toDec() {
35 return p/q;
36 }
37 Rat add(Rat n) {
38 Rat temp = new Rat();
39 temp.p = this.p * n.q + n.p * this.q;
40 temp.q = this.q * n.q;
41 return temp;
42 }
43 Rat sub(Rat n) {
44 Rat temp = new Rat();
45 temp.p = this.p * n.q - n.p * this.q;
46 temp.q = this.q * n.q;
47 return temp;
48 }
49 Rat mul(Rat n) {
50 Rat temp = new Rat();
51 temp.p = this.p * n.p;
52 temp.q = this.q * n.q;
53 return temp;
54 }
55 }
56 class RunRat {
57 public static void main(String[] args) {
58 Rat r1 = new Rat();
59 Rat r2 = new Rat();
60 [Link](6, 4);
61 [Link](-1, 2);
62 [Link]();
63 [Link]();
64 [Link]();
65 /*
66 [Link](r2).disp();
67 [Link](r2).disp();
68 [Link](r2).disp();
69 */

localhost:51815/04d07b4e-6e7b-4361-8bb3-74b4eb8fb525/ 13/21
4/26/24, 10:10 PM <h2>Selected files</h2>
70 }
71 }

java\[Link]

1 import [Link];
2
3 class List {
4 Scanner sc = new Scanner([Link]);
5 int[] a;
6 List() {
7 a = new int[1];
8 }
9 List(int x) {
10 a = new int[x];
11 }
12 List(List l) {
13 a = new int[[Link]];
14 }
15 void disp() {
16 [Link]("{");
17 for(int i = 0; i < [Link]; i++) {
18 [Link](a[i]);
19 if(i != [Link]-1) {
20 [Link](", ");
21 }
22 }
23 [Link]("}");
24 }
25 void add() {
26 [Link]("Enter elements for a list of size " + [Link]);
27 for(int i = 0; i < [Link]; i++) {
28 a[i] = [Link]();
29 }
30 [Link]("Elements added to the list");
31 }
32 void remove() {
33 [Link]("Enter the index of the element to be deleted: ");
34 int i = [Link]();
35 a[i] = 0;
36 [Link](a[i] + " removed from the list");
37 }
38 void remove(int i) {
39 [Link](a[i] + " removed from the list");
40 a[i] = 0;
41 }
42 List merge(List l) {
43 List temp = new List([Link] + [Link]);
44 for(int i = 0; i < [Link]; i++) {
45 temp.a[i] = this.a[i];
46 }
47 int lengthOfa = [Link];
48 for(int i = 0; i < [Link]; i++) {
49 temp.a[lengthOfa] = l.a[i];
50 lengthOfa++;

localhost:51815/04d07b4e-6e7b-4361-8bb3-74b4eb8fb525/ 14/21
4/26/24, 10:10 PM <h2>Selected files</h2>
51 }
52 return temp;
53 }
54 int search(int x) {
55 for(int i = 0; i < [Link]; i++) {
56 if(a[i] == x)
57 return i;
58 }
59 return -1;
60 }
61 }
62 class RunList {
63 public static void main(String[] args) {
64 List l1 = new List(6);
65 List l2 = new List(4);
66 [Link]();
67 [Link]();
68 [Link]();
69 [Link]();
70 int result = [Link](5);
71 if(result == -1)
72 [Link]("Element not found");
73 else
74 [Link]("Element found at index " + result);
75 [Link](l2).disp();
76 }
77 }

java\[Link]

1 import [Link];
2 class Calc {
3 static Scanner sc = new Scanner([Link]);
4 static double x, y;
5 static char opr;
6 static double add() {
7 return x + y;
8 }
9 static double sub() {
10 return x - y;
11 }
12 static double mul() {
13 return x * y;
14 }
15 static double div() {
16 return x / y;
17 }
18 public static void main(String[] args) {
19 [Link]("[CALCULATOR]");
20 [Link]("x: ");
21 x = [Link]();
22 [Link]("Enter operator (+) (-) (*) (/): ");
23 opr = [Link]().charAt(0);
24 [Link]("y: ");
25 y = [Link]();

localhost:51815/04d07b4e-6e7b-4361-8bb3-74b4eb8fb525/ 15/21
4/26/24, 10:10 PM <h2>Selected files</h2>
26 switch(opr) {
27 case '+': [Link](x + " + " + y + " = " + add()); break;
28 case '-': [Link](x + " - " + y + " = " + sub()); break;
29 case '*': [Link](x + " * " + y + " = " + mul()); break;
30 case '/': [Link](x + " / " + y + " = " + div()); break;
31 default: [Link]("Invalid input!"); break;
32 }
33 }
34 }

java\Inheritance\[Link]

1 class Animal {
2 void eat() {
3 [Link]("Eating");
4 }
5 void sleep() {
6 [Link]("Sleeping");
7 }
8 }
9 class Dog extends Animal {
10 Dog() {
11 [Link]("Woof");
12 }
13 }
14 class Cow extends Animal {
15 Cow() {
16 [Link]("Moo");
17 }
18 }
19 class RunAnimal {
20 public static void main(String[] args) {
21 Dog d1 = new Dog();
22 [Link]();
23 [Link]();
24 Cow c1 = new Cow();
25 [Link]();
26 [Link]();
27 }
28 }

java\[Link]

1 abstract class A {
2 abstract void callme();
3 void callmetoo() {
4 [Link]("This is concrete method");
5 }
6 }
7 class B extends A {
8 void callme() {
9 [Link]("callme implemented");
10 }

localhost:51815/04d07b4e-6e7b-4361-8bb3-74b4eb8fb525/ 16/21
4/26/24, 10:10 PM <h2>Selected files</h2>
11 /*
12 void callmetoo() {
13 [Link]("callmetoo redefined");
14 }
15 */
16 }
17 class AbstractDemo {
18 public static void main(String args[]) {
19 B b = new B();
20 [Link]();
21 [Link]();
22 }
23 }

java\[Link]

1 interface Callback {
2 void callback(String s);
3 }
4 class Server {
5 void call(String s) {
6 [Link]("Calling " + s);
7 }
8 }
9 class Client extends Server implements Callback {
10 public void callback(String s) {
11 [Link]("Callback " + s);
12 }
13 }
14 class InterfaceDemo {
15 public static void main(String args[]) {
16 Client c = new Client();
17 [Link]("hello");
18 [Link]("copy");
19 }
20 }

java\ExceptionHandling\[Link]

1 class Exc {
2 public static void main(String []args) {
3 try {
4 int []a = new int[5];
5 [Link](a[12]);
6 [Link]("This statement comes after exception");
7 } catch (Exception e) {
8 [Link](e);
9 }
10 finally {
11 [Link]("This statement will execute anyway");
12 }
13 }
14 }

localhost:51815/04d07b4e-6e7b-4361-8bb3-74b4eb8fb525/ 17/21
4/26/24, 10:10 PM <h2>Selected files</h2>
java\DataStructs\[Link]

1 import [Link];
2 class Stack {
3 Scanner sc = new Scanner([Link]);
4 private int []s = new int[5];
5 private int top;
6 Stack() {
7 top = -1;
8 }
9 void push() {
10 if(top == [Link]-1) {
11 [Link]("Stack overflow!");
12 }
13 else {
14 [Link]("Enter a number: ");
15 s[++top] = [Link]();
16 }
17 }
18 void pop() {
19 if(top == -1) {
20 [Link]("Stack underflow!");
21 }
22 else {
23 [Link](s[top--] + " popped");
24 }
25 }
26 void peek() {
27 if(top == -1) {
28 [Link]("Stack is empty!");
29 }
30 else {
31 for(int i = 0; i < [Link]; i++)
32 [Link](s[i] + " ");
33 [Link]("");
34 }
35 }
36 }
37 class RunStack {
38 public static void main(String []args) {
39 Scanner sc = new Scanner([Link]);
40 Stack s = new Stack();
41 [Link]("[STACK MENU]");
42 [Link]("1. Push\n2. Pop\n3. Peek\n4. Exit");
43 int ch;
44 do {
45 [Link]("Choose an operation: ");
46 ch = [Link]();
47 switch(ch) {
48 case 1: [Link](); break;
49 case 2: [Link](); break;
50 case 3: [Link](); break;
51 case 4: [Link]("Exiting..."); [Link](0);
52 default: [Link]("Invalid input!"); break;
53 }

localhost:51815/04d07b4e-6e7b-4361-8bb3-74b4eb8fb525/ 18/21
4/26/24, 10:10 PM <h2>Selected files</h2>
54 } while(ch != 4);
55 }
56 }

java\ExceptionHandling\[Link]

1 // Stack class using exception


2
3 import [Link];
4 class Stack {
5 Scanner sc = new Scanner([Link]);
6 private int []s = new int[5];
7 private int top;
8 Stack() {
9 top = -1;
10 }
11 void push() {
12 [Link]("Enter a number: ");
13 s[++top] = [Link]();
14 }
15 void pop() {
16 [Link](s[top--] + " popped");
17 }
18 void peek() {
19 for(int i = 0; i < [Link]; i++)
20 [Link](s[i] + " ");
21 [Link]("");
22 }
23 }
24 class RunStack {
25 public static void main(String []args) {
26 Scanner sc = new Scanner([Link]);
27 Stack s = new Stack();
28 [Link]("[STACK MENU]");
29 [Link]("1. Push\n2. Pop\n3. Peek\n4. Exit");
30 int ch;
31 do {
32 [Link]("Choose an operation: ");
33 ch = [Link]();
34 try {
35 switch(ch) {
36 case 1: [Link](); break;
37 case 2: [Link](); break;
38 case 3: [Link](); break;
39 case 4: [Link]("Exiting..."); [Link](0);
40 default: [Link]("Invalid input!"); break;
41 }
42 } catch (ArrayIndexOutOfBoundsException e) {
43 [Link](e);
44 }
45 } while(ch != 4);
46 }
47 }

java\DataStructs\[Link]
localhost:51815/04d07b4e-6e7b-4361-8bb3-74b4eb8fb525/ 19/21
4/26/24, 10:10 PM <h2>Selected files</h2>

1 import [Link];
2 class Queue {
3 Scanner sc = new Scanner([Link]);
4 private int []q = new int[5];
5 private int front, rear;
6 Queue() {
7 front = rear = -1;
8 }
9 void insert() {
10 if(rear == [Link]-1) {
11 [Link]("Queue overflow!");
12 }
13 else {
14 [Link]("Enter a number: ");
15 q[++rear] = [Link]();
16 if(rear == 0) front = rear;
17 }
18 }
19 void delete() {
20 if(rear == -1) {
21 [Link]("Queue underflow!");
22 }
23 else if (front == rear) {
24 [Link](q[front] + " deleted");
25 front = rear = -1;
26 [Link]("Queue is empty");
27 }
28 else {
29 [Link](q[front++] + " deleted");
30 }
31 }
32 void display() {
33 if(rear == -1) {
34 [Link]("Queue is empty!");
35 }
36 else {
37 for(int i = 0; i < [Link]; i++)
38 [Link](q[i] + " ");
39 [Link]();
40 }
41 }
42 }
43 class RunQueue {
44 public static void main(String []args) {
45 Scanner sc = new Scanner([Link]);
46 Queue q = new Queue();
47 [Link]("[QUEUE MENU]");
48 [Link]("1. Insert\n2. Delete\n3. Display\n4. Exit");
49 int ch;
50 do {
51 [Link]("Choose an operation: ");
52 ch = [Link]();
53 switch(ch) {
54 case 1: [Link](); break;
55 case 2: [Link](); break;
localhost:51815/04d07b4e-6e7b-4361-8bb3-74b4eb8fb525/ 20/21
4/26/24, 10:10 PM <h2>Selected files</h2>
56 case 3: [Link](); break;
57 case 4: [Link]("Exiting..."); [Link](0);
58 default: [Link]("Invalid input!"); break;
59 }
60 } while (ch != 4);
61 }
62 }

java\Multithreading\[Link]

1 class NewThread extends Thread {


2 public static void main(String[] args) {
3 NewThread thread = new NewThread();
4 [Link]();
5 [Link]("This statement is outside of the thread");
6 }
7 public void run() {
8 [Link]("This statement is running in a thread");
9 }
10 }

java\[Link]

1 import [Link].*;
2 class ShowSourceCode {
3 public static void main(String[] args) {
4 try {
5 FileInputStream fin = new FileInputStream("[Link]");
6 int r = [Link]();
7 while(r != -1) {
8 [Link](10);
9 [Link]((char)r);
10 r = [Link]();
11 }
12 }
13 catch(Exception e) {
14 [Link]();
15 }
16 }
17 }

localhost:51815/04d07b4e-6e7b-4361-8bb3-74b4eb8fb525/ 21/21

You might also like