Java定义一个圆类circle_circle类「建议收藏」

Java定义一个圆类circle_circle类「建议收藏」学习内容:以圆类Circle及立体图形类Solid为基础设计球类Sphere代码示例:package实验三;importjava.util.Scanner;classPoint{ privatedoublex; privatedoubley; publicPoint

大家好,欢迎来到IT知识分享网。

学习内容:以圆类 Circle 及立体图形类 Solid 为基础设计球类 Sphere

代码示例:

package 实验三;

import java.util.Scanner;

class Point{
private double x;
private double y;
public Point(double x,double y) {
this.x=x;
this.y=y;
System.out.println(“Point Constructor run”);
}
public void setX(double x) {//设置x坐标
this.x=x;
}
public final double getX(){//返回x坐标
return x;
}
public void setY(double y) {//设置y坐标
this.y=y;
}
public final double getY(){//返回y坐标
return y;
}
public void show() {//显示点的坐标
System.out.print(“Point(X=”+x+”,Y=”+y+”)”);
}
}
abstract class Plane extends Point{
private double x;
private double y;
public Plane(double x, double y) {
super(x, y);
this.x=x;
this.y=y;
}
double length() {
return 0;
}
double area() {
return 0;
}
}
class Circle extends Plane{

private double x;
private double y;
final double PI=3.14159;
private double radius;
public Circle(double x,double y,double radius){
super(x,y);
this.x=x;
this.y=y;
this.radius=radius;
System.out.println(“Circle Constructorrun”);
}
public void setR(double r) {
radius=r;
}
public double getR() {
return radius;
}
public void show() {
System.out.println(“Circle(Point(“+x+”,”+y+”),Radius=”+radius+”)”);
}
public double area() {
double area=PI*radius*radius;
System.out.println(“Area=”+area);
return 0;
}
public double length() {
double length=2*PI*radius;
System.out.println(“Length=”+length);
return 0;
}
}
abstract class Solid extends Circle{
private double x;
private double y;
private double radius;
public Solid(double x, double y, double radius) {
super(x, y, radius);
this.x=x;
this.y=y;
this.radius=radius;
}
public double volume() {//计算体积
return 0;
}
public double s_Area() {//计算表面积
return 0;
}
}

public class Sphere extends Solid{

private double x;
private double y;
private double radius;
final double PI=3.14159;
public void setR(double r) {
radius=r;
}
public double getR() {
return radius;
}
public Sphere(double x, double y, double r) {
super(x, y, r);
this.x=x;
this.y=y;
radius=r;
System.out.println(“Sphere Constructor run”);
}
public final void show() {
System.out.println(“Sphere(Circle(Point(“+x+”,”+y+”),Radius=”+radius+”)”);
}
public final double s_Area() {
double sarea=4*PI*radius*radius;
System.out.println(“s_Area=”+sarea);
return 0;
}
public final double volume() {
double volume=(4.0/3.0)*PI*radius*radius*radius;
System.out.println(“Volume=”+volume);
return 0;
}

public static void main(String[] args) {
double r1;
System.out.println(“请输入r:”);
Scanner sc=new Scanner(System.in);
r1=sc.nextDouble();
Sphere s1=new Sphere(1,2,3);
Sphere s2=s1;
s1.show();
System.out.println();//换行
System.out.print(“s1的面积:”);
s1.area();
System.out.println();
System.out.print(“s1的周长:”);
s1.length();
System.out.println();
System.out.print(“s1的表面积:”);
s1.s_Area();
System.out.println();
System.out.print(“s1的体积:”);
s1.volume();
System.out.println(“………………………………………..”);
s2.setR(r1);
s2.show();
System.out.println();
System.out.print(“s2的面积:”);
s2.area();
System.out.println();
System.out.print(“s2的周长:”);
s2.length();
System.out.println();
System.out.print(“s2的表面积:”);
s2.s_Area();
System.out.println();
System.out.print(“s2的体积:”);
s2.volume();
System.out.println();

}

}

运行截图:

Java定义一个圆类circle_circle类「建议收藏」

 

 

 

遇到的问题:面积、周长、表面积、体积无法输出

解决方法:面积、周长、表面积、体积无法输出原因为程序中包含旧方法,设置面积、周长、表面积、体积函数时直接返回了计算公式导致错误。解决方法为在函数中重新设置变量分别表示面积、周长、表面积、体积,输出该变量,返回值设为0.

明天任务:以点类 Point 及平面图形类 Plane 为基础设计圆类 Circle

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/29576.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信