// The base class
class Numb { // I have used Numb instead of Number because Number is a part of Java.lang
double r;
Numb() {r=0;}
Numb(double r) {
this.r = r;
}
Numb(double r,
double i) {}
void add(Numb n) {
r = adj(r+n.r);
}
void sub(Numb n) {
r = adj(r-n.r);
}
void show() {
System.out.print(r);
}
double adj(double
a) {
return (a >
999999) ? 999999:(a<-999999)?-999999:a;
}
}
class Complex extends Numb {
double i;
Complex(double r,
double i) {
super.r=r;
this.i=i;
}
void add(Complex
n) {
super.r =
adj(r+n.r);
this.i =
adj(i+n.i);
}
void sub(Complex
n) {
super.r =
adj(r-n.r);
this.i =
adj(i-n.i);
}
public void show()
{
super.show();
System.out.print((i>=0)?"+":"-");
System.out.println("i"+Math.abs(i));
}
}
class Matrix extends Numb {
final int ORDER =
2;
double ij[][]=new
double[ORDER][ORDER];
Matrix(double a,
double b, double c, double d) {
ij[0][0]=a;
ij[0][1]=b;
ij[1][0]=c;
ij[1][1]=d;
}
void add(Matrix a)
{
for (int x=0;
x<ORDER; x++) {
for (int y=0;
y<ORDER; y++) {
ij[x][y]=adj(ij[x][y]+a.ij[x][y]);
}
}
}
void sub(Matrix a)
{
for (int x=0;
x<ORDER; x++) {
for (int y=0;
y<ORDER; y++) {
ij[x][y]=adj(ij[x][y]-a.ij[x][y]);
}
}
}
void show() {
for (int x=0;
x<ORDER; x++) {
for (int y=0;
y<ORDER; y++) {
System.out.print(ij[x][y]+"
");
}
System.out.println();
}
}
}
public class Mix {
public static void
main(String args[]) {
Numb n = new
Numb(5);
Numb n2 = new
Numb(10);
Complex c1 = new
Complex(5, 6);
Complex c2 = new
Complex(6, 12);
Matrix m1 = new
Matrix(1, 2, 3, 4);
Matrix m2 = new
Matrix(10, 33, 22, 11);
System.out.println("\nTest of Number\n--------------");
n2.add(n);
System.out.print("n+n2=");
n2.show(); System.out.println();
System.out.println("\nTest of Complex\n---------------");
c2.sub(c1);
System.out.print("c1+c2="); c2.show(); System.out.println();
System.out.println("\nTest of Matrix\n--------------");
m2.sub(m1);
System.out.println("m2-m1=");
m2.show();
}
}