public class Complex {
private double x,y;
public Complex(double real,double imaginary){
x=real;
y=imaginary;
}
public String toString(){
return "("+x+","+y+"i)";
}
public Complex add(Complex a){
return new Complex(x+a.x,y+a.y);
}
public Complex add(double x,double y){
return new Complex(this.x+x,this.y+y);
}
public static Complex add(Complex a1,Complex a2){
return new Complex(a1.x+a2.x,a1.y+a2.y);
}
public Complex multiply(Complex a){
double X1=this.x*a.x-this.y*a.y;
Double Y1=this.x*a.y+this.y*a.x;
return new Complex(X1,Y1);
}
public Complex multiply(double x,double y){
double X1=this.x*x-this.y*y;
Double Y1=this.x*y+this.y*x;
return new Complex(X1,Y1);
}
public static Complex multiply(Complex a,Complex b){
double X1=a.x*b.x-a.y*b.y;
Double Y1=a.x*b.y+a.y*b.x;
return new Complex(X1,Y1);
}
public double ABC(){
return Math.sqrt(this.x*this.x+this.y+this.y);
}
public static void main(String[] args) {
Complex x,y,z;
x=new Complex(4,5);
y=new Complex(3.4,2.8);
System.out.println("加法:");
z=add(x,y);
System.out.println("resulu1="+z);
z=x.add(y);
System.out.println("resulu2="+z);
z=x.add(6,8);
System.out.println("resulu3="+z);
System.out.println("乘法:");
Complex y1=new Complex(1,2);
System.out.println("multiply(Complex a):"+x.multiply(y1));
System.out.println("multiply(double x,double y):"+x.multiply(1.0,2.0));
System.out.println("static Complex multiply(Complex a,Complex b):"+multiply(x,y1));
System.out.printf("x的模长为:%.4f",x.ABC());
}
}
public class Person {
String name;
String sex;
int age;
public Person(String name,String sex,int age){
this.name=name;
this.sex=sex;
this.age=age;
}
public String toString(){
return "姓名:"+name+" 性别:"+sex+" 年龄:"+age;
}
public static void main(String[] args) {
Person P=new Person("张三","男",18);
System.out.println(P);
Teacher PT=new Teacher("张三","男",18,"教授","计算机");
System.out.println(PT);
Student PS=new Student("张三","男",18,4845148,"2020/9/30","计算机");
System.out.println(PS);
}
}
class Teacher extends Person{
String Zhicheng;
String department;
public Teacher(String name,String sex,int age, String Zhicheng,String department){
super(name,sex,age);
this.Zhicheng=Zhicheng;
this.department=department;
}
public String toString(){
return "姓名:"+name+" 性别:"+sex+" 年龄:"+age+" 职称:"+Zhicheng+" 部门:"+department;
}
}
class Student extends Person{
int Num;
String ctime;
String specialized;
public Student(String name,String sex,int age,int Num,String ctime, String specialized){
super(name,sex,age);
this.Num=Num;
this.ctime=ctime;
this.specialized=specialized;
}
public String toString(){
return "姓名:"+name+" 性别:"+sex+" 年龄:"+age+" 学号:"+Num+" 入学时间:"+ctime+" 专业:"+specialized;
}
}
public class Point {
public int x;
public int y;
public Point(int a,int b){
this.x=a;
this.y=b;
} //构造函数
public double distance(Point P){
return Math.sqrt(((this.x-P.x)*(this.x-P.x)+(this.y-P.y)*(this.y-P.y)));
}
public double distance(int X1,int Y1){
double d=(this.x-X1)*(this.x-X1)+(this.y-Y1)*(this.y-Y1);
//System.out.println(d);
d=Math.sqrt(d);
return d;
}
public static double distance(Point X,Point Y){
double d=(double)(X.x-Y.x)*(X.x-Y.x)+(X.y-Y.y)*(X.y-Y.y);
d=Math.sqrt(d);
//System.out.println(X.x);
return d;
}
public static void main(String[] args) {
Point P1=new Point(4,5);
Point P2=new Point(9,10);
System.out.printf("distance(Point P):d=%.4f\n",P1.distance(P2));
System.out.printf("distance(int x,int y):d=%.4f\n",P1.distance(9,10));
System.out.printf("distance(Point X,Point Y):d=%.4f\n",distance(P1,P2));
}
}