Skip to content

Commit 3b5ff17

Browse files
committed
Func. Overriding & Matrices
1 parent 73048c5 commit 3b5ff17

File tree

2 files changed

+177
-0
lines changed

2 files changed

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

java/matrices/Matrix2.java

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import java.util.Scanner;
2+
3+
public class Matrix2 {
4+
public void input(int[][] mat, int rows, int cols) {
5+
Scanner sc = new Scanner(System.in);
6+
int i, j;
7+
8+
for (i = 0; i < rows; i++) {
9+
for (j = 0; j < cols; j++) {
10+
mat[i][j] = sc.nextInt();
11+
}
12+
}
13+
}
14+
15+
public void display(int[][] mat, int rows, int cols) {
16+
int i, j;
17+
18+
for (i = 0; i < rows; i++) {
19+
for (j = 0; j < cols; j++) {
20+
System.out.print(mat[i][j] + " ");
21+
}
22+
System.out.println();
23+
}
24+
System.out.println();
25+
}
26+
27+
public void multiply(int[][] mat1, int[][] mat2, int[][] res, int r1, int c2, int c1) {
28+
int i, j, k;
29+
for (i = 0; i < r1; i++) {
30+
for (j = 0; j < c2; j++) {
31+
for (k = 0; k < c1; k++) {
32+
res[i][j] += mat1[i][k] * mat2[k][j];
33+
}
34+
}
35+
}
36+
}
37+
38+
public static void main(String[] args) {
39+
Matrix2 ob = new Matrix2();
40+
Scanner sc = new Scanner(System.in);
41+
int r1, c1, r2, c2;
42+
System.out.println("Enter the dimensions of Matrix 1:");
43+
r1 = sc.nextInt();
44+
c1 = sc.nextInt();
45+
System.out.println("Enter the dimensions of Matrix 2:");
46+
r2 = sc.nextInt();
47+
c2 = sc.nextInt();
48+
49+
if (c1 != r2) {
50+
System.out.println("Multiplication Not Possible!");
51+
System.exit(0);
52+
}
53+
54+
int[][] mat1 = new int[r1][c1];
55+
int[][] mat2 = new int[r2][c2];
56+
57+
System.out.println("Enter the elements of Matrix 1:");
58+
ob.input(mat1, r1, c1);
59+
System.out.println("Enter the elements of Matrix 2:");
60+
ob.input(mat2, r2, c2);
61+
62+
System.out.println("Matrix 1:");
63+
ob.display(mat1, r1, c1);
64+
System.out.println("Matrix 2:");
65+
ob.display(mat2, r2, c2);
66+
67+
int[][] res = new int[r1][c2];
68+
ob.multiply(mat1, mat2, res, r1, c2, c1);
69+
System.out.println("After Multiplication, Resultant:");
70+
ob.display(res, r1, c2);
71+
72+
sc.close();
73+
}
74+
}

0 commit comments

Comments
 (0)