「球类接口java」外接球类型
本篇文章给大家谈谈球类接口java,以及外接球类型对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、用JAVA:利用接口求取圆、球、圆柱体的体积和表面积。各为大哥,小弟急需~~~~~
- 2、Java 圆类
- 3、java编写一个ball(球)父类,包含属性(大小,重量,颜色),包含两个构造方法,一个默认的,
用JAVA:利用接口求取圆、球、圆柱体的体积和表面积。各为大哥,小弟急需~~~~~
public class Test {
public static void main(String[] args)throws Exception {
// javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
// String s = javax.swing.JOptionPane.showInputDialog("请在下面的输入框中输入任意信息,我将再弹出一个对话框来显示此信息:");
// if(s!=null)javax.swing.JOptionPane.showMessageDialog(null,"html你输入的信息是:font color=blueb"+s);
ObjectX x = new Circle(35);
ObjectX y = new Cylinder(40,55);
ObjectX z = new Sphere(75);
x.printInfo();
y.printInfo();
z.printInfo();
double xarea=x.getArea();
double yarea=y.getArea();
double zarea=z.getArea();
double xvol=x.getVolume();
double yvol=y.getVolume();
double zvol=z.getVolume();
//......
}
}
//公共接口
interface ObjectX{
double getArea();//取得表面积
double getVolume();//取得体积
void printInfo();//打印信息
}
//圆形类
class Circle implements ObjectX{
private double radius;
Circle(double r){
radius=r;
}
public double getArea() {
return Math.PI*Math.pow(radius,2);
}
public double getVolume() {
return 0;//因圆没有体积,故返回 0
}
public void printInfo() {
System.out.println("圆 类,半径:"+radius+", 表面积:"+getArea()+", 体积:"+getVolume());
}
public String toString(){return "圆形";}
}
//球类
class Sphere implements ObjectX{
private double radius;
Sphere(double r){
radius=r;
}
public double getArea() {
return Math.PI*Math.pow(radius,2)*4;
}
public double getVolume() {
//(4∏R^3)/3
return Math.PI*Math.pow(radius,3)*4/3;
}
public void printInfo() {
System.out.println("球 类,半径:"+radius+",表面积:"+getArea()+", 体积:"+getVolume());
}
public String toString(){return "球类";}
}
//圆柱类
class Cylinder implements ObjectX{
private double radius;
private double height;
Cylinder(double r,double h){
radius=r;
height=h;
}
public double getArea() {
return Math.PI*Math.pow(radius,2)*2+Math.PI*radius*2*height;
}
public double getVolume() {
return Math.PI*Math.pow(radius,2)*height;
}
public void printInfo() {
System.out.println("圆柱类,半径:"+radius+",高度:"+height+", 表面积:"+getArea()+", 体积是:"+getVolume());
}
public String toString(){return "圆柱类";}
}
Java 圆类
public class Sphere{
private int xpos,ypos,zpos,radius;
public Sphere(){
xpos=0;
ypos=0;
zpos=0;
radius=1;
}
public Sphere(int x, int y, int z, int r){
xpos=x;
ypos=y;
zpos=z;
radius=r;
}
public String toString(){
String s="";
s="x-coordinate: "+xpos+",y-coordinate: "+ypos+",z-coordinate: "+zpos+",radius: "+radius;
return s;
}
public boolean equals(Object obj){
Sphere s=(Sphere)obj;
if(this.xpos==s.xposthis.ypos==s.yposthis.zpos==s.zposthis.radius==s.radius) return true;
else return false;
}
public double surface(){
return Math.PI*4*radius*radius;
}
public double volume(){
return Math.PI*4/3*radius*radius*radius;
}
public int relativeLocation(Sphere s){
int distanceSquare=(this.xpos-s.xpos)*(this.xpos-s.xpos)+(this.ypos-s.ypos)*(this.ypos-s.ypos)+(this.zpos-s.zpos)*(this.zpos-s.zpos);
double distance=Math.sqrt(distanceSquare);
if(distancethis.radius+s.radius) return -1;
else if(distance==this.radius+s.radius) return 0;
else return 1;
}
public static void main(String[] args){
Sphere s1=new Sphere(0,0,0,1);
Sphere s2=new Sphere(10,10,10,10);
System.out.println(s1);
System.out.println("The surface area of s1 is "+s1.surface()+" and the volume is "+s1.volume());
System.out.println(s2);
System.out.println("The surface area of s1 is "+s2.surface()+" and the volume is "+s2.volume());
int i=s1.relativeLocation(s2);
switch(i){
case 1: System.out.println("The two balls intersect each.");break;
case 0: System.out.println("The two balls are tangent to each.");break;
case -1: System.out.println("The two balls are away from each.");break;
}
}
}
java编写一个ball(球)父类,包含属性(大小,重量,颜色),包含两个构造方法,一个默认的,
//球类
public class Ball{
protected Double size; //大小
protected Double weight; //体重
protected String color; //颜色
//默认构造
public Ball(){
}
//带参构造
public Ball(Double weight,Double size,String color){
this.size=size;
this.weight=weight;
this.color=color;
}
}
//篮球类
public class BasketBall extends Ball{
//带参构造
public BasketBall(Double weight, Double size, String color) {
super(weight, size, color);
showMe();//也可在生成篮球的时候调用输入方法
}
//默认构造
public BasketBall() {
showMe();//也可在生成篮球的时候调用输入方法
}
//打印信息
public void showMe(){
System.out.print("的大小是:"+size+";我的体重是:"+weight+"颜色是:"+color);
}
public static void main(String[] args) {
//使用带参构造 new 出篮球对象
BasketBall basketBall=new BasketBall(20.0, 17.0, ";蓝色");
//调用 输入方法
basketBall.showMe();
}
}
关于球类接口java和外接球类型的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。