Skip to content

Commit 3a176e3

Browse files
committed
miscallenous: 2
1 parent f8d2d82 commit 3a176e3

File tree

7 files changed

+502
-0
lines changed

7 files changed

+502
-0
lines changed

java/threads/Prime.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class TestRange extends Thread {
2+
static long possPrime;
3+
long from, to;
4+
5+
TestRange(int argFrom, long argPossPrime) {
6+
possPrime = argPossPrime;
7+
if (argFrom == 0)
8+
from = 2;
9+
else
10+
from = argFrom;
11+
12+
to = argFrom + 99;
13+
}
14+
15+
public void run() {
16+
for (long i = from; i <= to && i < possPrime; i++) {
17+
if (possPrime % i == 0) {
18+
System.out.println("factor " + i + " found by thread " + getName());
19+
return;
20+
}
21+
Thread.yield();
22+
}
23+
}
24+
}
25+
26+
public class Prime {
27+
public static void main(String[] s) {
28+
long possPrime = Long.parseLong(s[0]);
29+
int centuries = (int) (possPrime / 100) + 1;
30+
for (int i = 0; i < centuries; i++) {
31+
new TestRange(i * 100, possPrime).start();
32+
}
33+
}
34+
}

test/Matrix.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
// package test;
3+
import java.util.Scanner;
4+
5+
public class Matrix {
6+
int rows, cols;
7+
int data[][];
8+
9+
Matrix(int r, int c) {
10+
this.rows = r;
11+
this.cols = c;
12+
this.data = new int[this.rows][this.cols];
13+
}
14+
15+
public void input() {
16+
Scanner sc = new Scanner(System.in);
17+
int i, j;
18+
for (i = 0; i < this.rows; i++) {
19+
for (j = 0; j < this.cols; j++) {
20+
this.data[i][j] = sc.nextInt();
21+
}
22+
}
23+
}
24+
25+
public void display() {
26+
int i, j;
27+
for (i = 0; i < this.rows; i++) {
28+
for (j = 0; j < this.cols; j++) {
29+
System.out.print(this.data[i][j] + " ");
30+
}
31+
System.out.println();
32+
}
33+
34+
System.out.println();
35+
}
36+
37+
public Matrix multiply(Matrix A, Matrix B) {
38+
if (A.cols != B.rows) {
39+
System.out.println("Multiplication NOT POSSIBLE!");
40+
return null;
41+
}
42+
43+
Matrix res = new Matrix(A.rows, B.cols);
44+
int i, j, k;
45+
46+
for (i = 0; i < A.rows; i++) {
47+
for (j = 0; j < B.cols; j++) {
48+
for (k = 0; k < A.cols; k++) {
49+
res.data[i][j] += A.data[i][k] + B.data[k][j];
50+
}
51+
}
52+
}
53+
54+
return res;
55+
}
56+
57+
public static void main(String[] args) {
58+
Matrix A = new Matrix(3, 2);
59+
Matrix B = new Matrix(2, 3);
60+
61+
System.out.println("Enter elements of Matrix 1:");
62+
A.input();
63+
System.out.println("Enter elements of Matrix 2:");
64+
B.input();
65+
66+
System.out.println("Matrix 1:");
67+
A.display();
68+
System.out.println("Matrix 2:");
69+
B.display();
70+
71+
Matrix res = new Matrix(0, 0);
72+
res = res.multiply(A, B);
73+
74+
if (res != null) {
75+
System.out.println("Resultant:");
76+
res.display();
77+
}
78+
}
79+
}

test/Overriding.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// package test;
2+
3+
class Shape {
4+
public double getArea() {
5+
return 0.0;
6+
}
7+
8+
public double getPerimeter() {
9+
return 0.0;
10+
}
11+
12+
public double getExpenses() {
13+
return getArea() * 30.0;
14+
}
15+
}
16+
17+
class Circle extends Shape {
18+
double radius;
19+
20+
Circle(double r) {
21+
radius = r;
22+
}
23+
24+
@Override
25+
public double getArea() {
26+
return (22.0/7.0) * radius * radius;
27+
}
28+
29+
@Override
30+
public double getPerimeter() {
31+
return 2 * (22.0/7.0) * radius;
32+
}
33+
}
34+
35+
class Rectangle extends Shape {
36+
double length, breadth;
37+
38+
Rectangle(double l, double b) {
39+
length = l;
40+
breadth = b;
41+
}
42+
43+
@Override
44+
public double getArea()
45+
{
46+
return length * breadth;
47+
}
48+
49+
@Override
50+
public double getPerimeter()
51+
{
52+
return 2 * (length + breadth);
53+
}
54+
}
55+
56+
class Triangle extends Shape {
57+
int a, b, c;
58+
59+
Triangle(int x, int y, int z) {
60+
a = x;
61+
b = y;
62+
c = z;
63+
}
64+
65+
@Override
66+
public double getArea()
67+
{
68+
double s = (a + b + c) / 2.0;
69+
70+
double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
71+
return area;
72+
}
73+
74+
@Override
75+
public double getPerimeter()
76+
{
77+
return a + b + c;
78+
}
79+
}
80+
81+
public class Overriding {
82+
public static void main(String[] args) {
83+
Circle c = new Circle(5.3);
84+
Rectangle r = new Rectangle(5.1, 6.2);
85+
Triangle t = new Triangle(4, 3, 5);
86+
87+
System.out.println("For Circle:");
88+
System.out.println("Area = " + c.getArea());
89+
System.out.println("Perimeter = " + c.getPerimeter());
90+
System.out.println("Expenses = Rs. " + c.getExpenses());
91+
92+
System.out.println("For Rectangle:");
93+
System.out.println("Area = " + r.getArea());
94+
System.out.println("Perimeter = " + r.getPerimeter());
95+
System.out.println("Expenses = Rs. " + r.getExpenses());
96+
97+
System.out.println("For Triangle:");
98+
System.out.println("Area = " + t.getArea());
99+
System.out.println("Perimeter = " + t.getPerimeter());
100+
System.out.println("Expenses = Rs. " + t.getExpenses());
101+
}
102+
}

test/inheritance.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Base
5+
{
6+
public:
7+
int x;
8+
9+
Base(int a) : x(a)
10+
{
11+
cout << "Base Class Constructor: x = " << x << endl;
12+
}
13+
14+
~Base()
15+
{
16+
cout << "Base class Destructor" << endl;
17+
}
18+
};
19+
20+
class Derived : public Base
21+
{
22+
public:
23+
int y;
24+
25+
Derived(int a, int b) : Base(a)
26+
{
27+
y = b;
28+
cout << "Derived Class Constructor: y = " << y << endl;
29+
}
30+
31+
~Derived()
32+
{
33+
cout << "Derived class Destructor" << endl;
34+
}
35+
};
36+
37+
38+
int main() {
39+
Derived ob(5, 6);
40+
return 0;
41+
}

test/inheritance.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// package test;
2+
3+
class Base {
4+
int x;
5+
6+
Base(int a) {
7+
x = a;
8+
System.out.println("Base Class Constructor: x = " + x);
9+
}
10+
}
11+
12+
class Derived extends Base {
13+
int y;
14+
15+
Derived(int a, int b) {
16+
super(a);
17+
y = b;
18+
System.out.println("Derived Class Constructor: y = " + y);
19+
}
20+
}
21+
22+
public class inheritance {
23+
public static void main(String[] args) {
24+
Derived ob = new Derived(10, 20);
25+
System.out.println(ob.getClass());
26+
}
27+
}

0 commit comments

Comments
 (0)