1 今天写了以圆类设计球类
题目要求:
程序源代码:
package Plane;
public class Point {
double x;
double y;
Point()//无参构造
{
x=0;
y=0;
System.out.println("Point Constructor run");
}
Point(double xv,double yv)//有参构造
{
x=xv;
y=yv;
System.out.println("Point Constructor run");
}
Point(Point p) //拷贝构造
{
x=p.x;
y=p.y;
System.out.println("Point CopyConstructor run");
}
void show() //显示Point信息
{
System.out.print("("+x+","+y+")");
}
void setX(double xv){x=xv;} //设置X坐标
void setY(double yv){y=yv;} //设置Y坐标
double getX() {return x;} //获取X坐标
double getY() {return y;} //获取Y坐标
}
double x;
double y;
Point()//无参构造
{
x=0;
y=0;
System.out.println("Point Constructor run");
}
Point(double xv,double yv)//有参构造
{
x=xv;
y=yv;
System.out.println("Point Constructor run");
}
Point(Point p) //拷贝构造
{
x=p.x;
y=p.y;
System.out.println("Point CopyConstructor run");
}
void show() //显示Point信息
{
System.out.print("("+x+","+y+")");
}
void setX(double xv){x=xv;} //设置X坐标
void setY(double yv){y=yv;} //设置Y坐标
double getX() {return x;} //获取X坐标
double getY() {return y;} //获取Y坐标
}
package Plane;
public class Circle extends Plane{
static double PI=3.14159;
double radius;
Circle()
{
super();
radius=0;
System.out.println("Circle Constructor run");
}
Circle(double xx,double yy,double rr)
{
super(xx,yy);
radius=rr;
System.out.println("Circle Constructor run");
}
Circle(Circle cir)
{
super(cir);
radius=cir.radius;
System.out.println("Circle CopyConstructor run");
}
void setR(double r){radius=r;}
double getR() {return radius;}
double length() {return 2*PI*radius;} //用于计算圆的周长
double area() {return PI*radius*radius;} //用于计算圆的面积
void show() {System.out.print("Circle(Point(X="+x+",Y="+y+"),Radius="+getR()+")");}
}
package Plane;
public class Sphere extends Circle{
Sphere(){super();}
Sphere(double xx,double yy,double rr)
{
super(xx,yy,rr);
System.out.println("Sphere Constructor run");
}
Sphere(Sphere v)
{
super(v);
System.out.println("Sphere CopyConstructor run");
}
void show() //用于显示球的信息
{
System.out.print("Sphere(");
super.show();
System.out.print(")");
}
double sArea() //用于计算球的面积
{
double s;
double r=getR();
s=4*PI*r*r;
return s;
}
double volume() //用于计算球的体积
{
double v;
double r=getR();
v=4*PI*r*r*r/3;
return v;
}
}
Sphere(){super();}
Sphere(double xx,double yy,double rr)
{
super(xx,yy,rr);
System.out.println("Sphere Constructor run");
}
Sphere(Sphere v)
{
super(v);
System.out.println("Sphere CopyConstructor run");
}
void show() //用于显示球的信息
{
System.out.print("Sphere(");
super.show();
System.out.print(")");
}
double sArea() //用于计算球的面积
{
double s;
double r=getR();
s=4*PI*r*r;
return s;
}
double volume() //用于计算球的体积
{
double v;
double r=getR();
v=4*PI*r*r*r/3;
return v;
}
}
package Plane;
import java.util.Scanner;
public class SolidMa extends Sphere{
void show(Point p){/*点基类的显示函数*/
p.show();
}
void length(Plane p){/*平面图形的周长函数*/
System.out.print("Length="+p.length());
}
void area(Plane p){/*平面图形的面积函数*/
System.out.print("Area="+p.area());
}
void show(Point p){/*点基类的显示函数*/
p.show();
}
void length(Plane p){/*平面图形的周长函数*/
System.out.print("Length="+p.length());
}
void area(Plane p){/*平面图形的面积函数*/
System.out.print("Area="+p.area());
}
void volumn( Sphere s){/*立体图形的体积函数*/
System.out.println("Volumn="+s.volume());
}
void s_Area(Sphere s){/*立体图形的表面积函数*/
System.out.print("S_Area=");
System.out.println(s.sArea());
}
public static void main(String[] args)
{
double r;
Scanner in=new Scanner(System.in);
r=in.nextDouble();
Sphere s1=new Sphere(1,2,3);
Sphere s2=new Sphere(s1);
SolidMa m=new SolidMa();
m.show(s1);
System.out.println();
m.area(s1);
m.length(s1);
m.s_Area(s1);
m.volumn(s1);
s2.setR(r);
m.show(s2);
System.out.println();
m.area(s2);
m.length(s2);
m.s_Area(s2);
m.volumn(s2);
in.close();
}
}
System.out.println("Volumn="+s.volume());
}
void s_Area(Sphere s){/*立体图形的表面积函数*/
System.out.print("S_Area=");
System.out.println(s.sArea());
}
public static void main(String[] args)
{
double r;
Scanner in=new Scanner(System.in);
r=in.nextDouble();
Sphere s1=new Sphere(1,2,3);
Sphere s2=new Sphere(s1);
SolidMa m=new SolidMa();
m.show(s1);
System.out.println();
m.area(s1);
m.length(s1);
m.s_Area(s1);
m.volumn(s1);
s2.setR(r);
m.show(s2);
System.out.println();
m.area(s2);
m.length(s2);
m.s_Area(s2);
m.volumn(s2);
in.close();
}
}
运行截图:
2 今天犯了一个低级的错误,局部变量没赋值
3 明天继续写题