1、创建一个复数类Complex,并实现复数的常用算数运算。Complex包含两个私有属性real和img,令 j 代表复数单位(j2=-1),具体要求如下:
① real和img都是双精度浮点数类型,并提供两个构造函数,一个是无参数构造函数(real和img都初始化为0.0),另外一个是有参数(参数为double real和double img)构造函数;
② 分别为real和img提供getter和setter方法;
③ 提供五个公有实例方法,实现复数的加、减、乘、除、求模功能;
④ 重写toString方法,将复数转换为 real+img*j 的形式;
⑤ 重写equals方法,两个复数只有在实部和虚部均相等时,这两个复数才相等;
⑥ 编写main函数,测试上述复数的各种功能。
public class Complex {
double real, img;
public Complex()
{
this.real=0;
this.img=0;
}
public Complex(double real,double img)
{
this.real=real;
this.img=img;
}
public double getReal()
{
return this.real;
}
public double getimg()
{
return this.img;
}
public void setReal(double real)
{
this.real=real;
}
public void setImg(double img)
{
this.img=img;
}
public Complex add(Complex a,Complex b)
{
Complex temp = new Complex();
temp.real = a.real+b.real;
temp.img = a.img+b.img;
return temp;
}
public Complex dec(Complex a,Complex b)
{
Complex temp = new Complex();
temp.real = a.real-b.real;
temp.img = a.img-b.img;
return temp;
}
public Complex mul(Complex a,Complex b)
{
Complex temp = new Complex();
temp.real = a.real*b.real-a.img*b.img;
temp.img = a.real*b.img+a.img*b.real;
return temp;
}
public Complex div(Complex a,Complex b)
{
Complex temp = new Complex();
temp.real=(a.real*b.real+a.img*b.img)/(b.real*b.real+b.img*b.img);
temp.img =(a.img*b.real-a.real*b.img)/(b.real*b.real+b.img*b.img);
return temp;
}
public void Modulo()
{
System.out.println("模长为:"+Math.pow((Math.pow(this.real,2)+Math.pow(this.img,2)),0.5));
}
public void Print()
{
System.out.println(this.real+"+"+this.img+"i");
}
@Override
public String toString(){
return this.real+"+"+this.img+"j";
}
public boolean equals(Object tmp){
if(tmp == null)
return false;
else{
if(tmp instanceof Complex){
Complex c =(Complex) tmp;
return this.real == c.real && this.img == c.img;
}
else
return false;
}
}
public static void main(String[] args){
Complex a = new Complex(2.0,1.0);
Complex b = new Complex(4.0,2.0);
Complex temp = new Complex(3,4);
Complex add = new Complex();
Complex dec = new Complex();
Complex mul = new Complex();
Complex div = new Complex();
add = add.add(a,b);
add.Print();
dec = dec.dec(a,b);
dec.Print();
mul = mul.mul(a,b);
mul.Print();
div = div.div(a,b);
div.Print();
temp.Modulo();
System.out.println(temp.toString());
System.out.println("相等情况:"+a.equals(b));
}
}
2、编写程序,求三角形、矩形、正方形和圆形的周长和面积。具体要求如下:
① 定义一个Point类,它包含两个属性:横坐标x和纵坐标y,和一个构造函数Point(double x, double y);
② 定义一个接口Shape,它包含两个抽象方法:计算面积的方法double computeArea()和计算周长的方法double computeDiameter();
③ 定义一个三角形Triangle,它实现了接口Shape,并包含三个Point类型的私有属性A、B、C。分别为三个属性定义getter和setter方法。定义Triangle的一个构造函数Triangle(Point A, Point B, Point C),并要验证三个点A、B、C是否能构成三角形(任意两边之和大于第三边),如果不能构成三角形,为用户输出提示信息;
④ 定义一个矩形Rectangle,它实现了接口Shape,并包含四个Point类型的私有属性A、B、C、D。分别为这四个属性定义getter和setter方法。定义Rectangle的一个构造函数Rectangle(Point A, Point B, Point C, Point D), 并验证这四个点是否能构成矩形(AB=CD, AD=BC, AB垂直于BC),如果不能构成三角形,为用户输出提示信息。另外,Rectangle中包含一个公有实例方法draw(),它负责在控制台打印信息“这是一个矩形”;
⑤ 定义一个圆形Circle,它实现了接口Shape,并包含两个私有属性:一个是Point类型的属性A,另外一个是double类型的属性radius。分别为这两个属性定义getter和setter方法。定义Circle的一个构造函数Circle(Point A, double radius);
⑥ 定义一个正方形Square,它继承自Rectangle类,并包含四个Point类型的私有属性A、B、C、D。定义Square的构造函数,并判断这四个点是否能构成正方形。Square重写Rectangle中的draw()方法,并在控制台打印“这是一个正方形”;
⑦ 定义一个公有类型ShapeTest,包含main方法。在main方法中,分别初始化一个三角形、矩形、正方形和圆形,并输出它们的面积和周长。另外,在main函数中声明一个Rectangle,并将其绑定到一个Square对象,测试Java中的多态性。
public class Point {
double x,y;
public Point(){
this.x=0;
this.y=0;
}
public Point(double x,double y){
this.x=x;
this.y=y;
}
interface Shape{
public double computeArea();
public double computeDiameter();
}
static class Triangle implements Shape
{
static double AB;
static double AC;
static double BC;
public Triangle(Point A, Point B, Point C){
double AB_sqrt = Math.pow((B.y-A.y),2)+Math.pow((B.x-A.x),2);
double AC_sqrt = Math.pow((C.y-A.y),2)+Math.pow((C.x-A.x),2);
double BC_sqrt = Math.pow((B.y-C.y),2)+Math.pow((B.x-C.x),2);
AB = Math.pow(AB_sqrt,0.5);
AC = Math.pow(AC_sqrt,0.5);
BC = Math.pow(BC_sqrt,0.5);
if(Math.pow(AB_sqrt,0.5)+Math.pow(AC_sqrt,0.5)>Math.pow(BC_sqrt,0.5)&&Math.pow(AB_sqrt,0.5)+Math.pow(AC_sqrt,0.5)>Math.pow(AC_sqrt,0.5)&&Math.pow(BC_sqrt,0.5)+Math.pow(AC_sqrt,0.5)>Math.pow(AB_sqrt,0.5))
{
System.out.println("这三点能构成三角形");
}
else
{
System.out.println("这三点不能构成三角形");
}
}
@Override
public double computeArea() {
double p;
p=(AB+BC+AC)/2;
return Math.pow((p*(p-AB)*(p-AC)*(p-BC)),0.5);
}
@Override
public double computeDiameter() {
return AB+AC+BC;
}
Point temp = new Point();
public double getter_x()
{
return this.temp.x;
}
public double getter_y()
{
return this.temp.y;
}
public double setter_x()
{
return this.temp.x;
}
public double setter_y()
{
return this.temp.y;
}
}
static class Rectangle implements Shape
{
static double AB;
static double AC;
static double BD;
static double DC;
static double AD;
static double BC;
Point temp = new Point();
public Rectangle(Point A, Point B, Point C,Point D){
double AB_sqrt = Math.pow((B.y-A.y),2)+Math.pow((B.x-A.x),2);
double AC_sqrt = Math.pow((C.y-A.y),2)+Math.pow((C.x-A.x),2);
double BD_sqrt = Math.pow((B.y-D.y),2)+Math.pow((B.x-D.x),2);
double DC_sqrt = Math.pow((D.y-C.y),2)+Math.pow((D.x-C.x),2);
double AD_sqrt = Math.pow((D.y-A.y),2)+Math.pow((D.x-A.x),2);
double BC_sqrt = Math.pow((B.y-C.y),2)+Math.pow((B.x-C.x),2);
AB = Math.pow(AB_sqrt,0.5);
AC = Math.pow(AC_sqrt,0.5);
BD = Math.pow(BD_sqrt,0.5);
DC = Math.pow(DC_sqrt,0.5);
AD = Math.pow(AD_sqrt,0.5);
BC = Math.pow(BC_sqrt,0.5);
if(AB!=DC||AD!=BC||AB_sqrt+AC_sqrt!=BC_sqrt)
{
System.out.println("这四点不能构成矩形");
}
}
public double getter_x()
{
return this.temp.x;
}
public double getter_y()
{
return this.temp.y;
}
public double setter_x()
{
return this.temp.x;
}
public double setter_y()
{
return this.temp.y;
}
@Override
public double computeDiameter() {
return AB*AC;
}
@Override
public double computeArea() {
return 2*(AB+AC);
}
public void draw()
{
System.out.println("这是一个矩形");
}
}
static class Circle implements Shape
{
static double r;
Point temp;
public Circle(Point A,double radius)
{
temp=A;
r=radius;
}
public double getter_x()
{
return this.temp.x;
}
public double getter_y()
{
return this.temp.y;
}
public double setter_x()
{
return this.temp.x;
}
public double setter_y()
{
return this.temp.y;
}
@Override
public double computeArea() {
return 0.5*Math.PI*Math.pow(r,2);
}
@Override
public double computeDiameter() {
return 2*Math.PI*r;
}
}
static class Square extends Rectangle
{
public Square(Point A, Point B, Point C, Point D)
{
super(A, B, C, D);
if(AB==AC&&AB==BD&&AB==DC)
{
System.out.println("这是正方形");
}
else
{
System.out.println("这不是正方形");
}
}
public void draw(){
System.out.println("这是一个正方形");
}
}
public static void main(String[] args)
{
Point A = new Point(0,0);
Point B = new Point(3,0);
Point C = new Point(0,3);
Point D = new Point(3,3);
Triangle sjx = new Triangle(A, B, C);
Rectangle jx = new Rectangle(A, B, C, D);
Circle cirle = new Circle(A, 2);
Square zfx = new Square(A, B, C, D);
Rectangle many_test = new Square(A, B, C, D);
System.out.println("三角形面积:"+sjx.computeDiameter());
System.out.println("矩形面积:"+jx.computeDiameter());
System.out.println("正方形面积:"+zfx.computeDiameter());
System.out.println("圆形面积:"+cirle.computeDiameter());
System.out.println("三角形周长:"+sjx.computeArea());
System.out.println("矩形周长:"+jx.computeArea());
System.out.println("正方形周长:"+zfx.computeArea());
System.out.println("圆形周长:"+cirle.computeArea());
}
}