java编程题
Rectangle类:
/**
* 定义一个矩形类Rectangle,包含有长length,宽width属性、构造方法(要求写出初始化长和宽)和计算面积方法getArea()。
* 编写一个长方体Cuboid
* ,继承自矩形类,具有长length、宽width、高height属性,构造方法和计算体积的方法getVolume()。编写一个测试类Test
* (其中长为5,宽为4,高为3),要求输出其底面积和体积。
*
* @author Retror
*
*/
public class Rectangle {
private double length;
private double width;
public Rectangle() {
}
public Rectangle(double length, double width) {
super();
this.length = length;
this.width = width;
}
public double getArea(){
return this.length*this.width;
}
}
Cuboid类:
/**
* 定义一个矩形类Rectangle,包含有长length,宽width属性、构造方法(要求写出初始化长和宽)和计算面积方法getArea()。
* 编写一个长方体Cuboid
* ,继承自矩形类,具有长length、宽width、高height属性,构造方法和计算体积的方法getVolume()。编写一个测试类Test
* (其中长为5,宽为4,高为3),要求输出其底面积和体积。
* @author Retror
*
*/
public class Cuboid extends Rectangle {
private double length;
private double width;
private double height;
public Cuboid() {
super();
}
public Cuboid(double length, double width, double height) {
super();
this.length = length;
this.width = width;
this.height = height;
}
public double getVolume(){
return this.length*this.height*this.width;
}
@Override
public double getArea() {
return this.length*this.width;
}
}
测试类:
public class Test1 {
public static void main(String[] args) {
Cuboid cuboid=new Cuboid(10, 5, 8.1);
System.out.println("底面积:"+cuboid.getArea());
System.out.println("体积:"+cuboid.getVolume());
}
}
运行效果:
底面积:50.0
体积:405.0
希望能帮到你,望采纳。
public class Test {
public static void main(String[] args) {
Cuboid cuboid = new Cuboid(5, 4, 3);
int are = cuboid.getArea();
int volume = cuboid.getVolume();
}
}
public class Rectangle {
protected int length = 0;
protected int width = 0;
public Rectangle() {
}
public Rectangle(int length, int width) {
super();
this.length = length;
this.width = width;
}
public int getArea() {
return width*length;
}
}
public class Cuboid extends Rectangle {
int height = 0;
public Cuboid() {
}
public Cuboid(int l, int w, int h) {
length = l;
width = w;
height = h;
}
public int getVolume() {
return length*width*height;
}
}
public class Rectangle {
int length;
int width;
public Rectangle(int l,int w) {
length=l;
width=w;
}
public int getArea(){
return length*width;
}
}
public class Cuboid extends Rectangle{
int height;
public Cuboid(int l, int w,int h) {
super(l, w);
this.height=h;
}
public int getVolume(){
return length*width*height;
}
}
public class Test {
public static void main(String[] args) {
Cuboid c = new Cuboid(5, 4, 3);
System.out.print("底面积:");
System.out.println(c.getArea());
System.out.print("体积:");
System.out.println(c.getVolume());
}
}
广告 您可能关注的内容 |