- 1、本文档共4页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
【问题描述】定义一个抽象基类Shape,它包含三个抽象方法center()、diameter()、getArea(),从Shape?类派生出Square?和Circle?类,这两个类都用center()计算对象的中心坐标,diameter()计算对象周长,getArea()方法计算对象的面积。编写应用程序使用Rectangle类和Circle?类。
【输入形式】输入圆的中心坐标、半径;输入正方形中心、边长【输出形式】输出圆的中心坐标、圆周长、圆面积;输出正方形中心坐标、周长、面积【样例输入】
【样例输出】please input the circle center:?4 4?
??????????????????? please input the circle radius:3
????????????????? ??the circle center is (4, 4)
??????????????????? the circle diameter is 18.84
??????????????????? the circle area is 28.26
??????????????????? please input the?square center:5 5
??????????????????? please input the square radius:2
????????????????????the square center is (5,5)
??????????????????? the square diameter is 8
????????????????????the square area is 4
【样例说明】样例输出中横线是输入。【评分标准】一个测试样例
import java.text.DecimalFormat;
import java.util.Scanner;
public class Test72 {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int x,y,r;
System.out.println(please input the circle center: );
x=scan.nextInt();
y=scan.nextInt();
System.out.println(please input the circle radius: );
r=scan.nextInt();
Circle circle = new Circle(r);
double area = circle.getArea();
double diameter = circle.diameter();
System.out.println(the circle center is ( + x + , + y + ));
System.out.println(the circle diameter is + new DecimalFormat(0.00).format(diameter));
System.out.println(the circle area is + new DecimalFormat(0.00).format(area));
System.out.println(please input the square center: );
x=scan.nextInt();
y=scan.nextInt();
System.out.println(please input the square side length: );
r=scan.nextInt();
Rectangle rectangle = new Rectangle(r, r);
area = rectangle.getArea();
diameter = rectangle.diameter();
System.out.println(the square center is ( + x + , + y + ));
System.out.println(the square diameter is + new DecimalFormat(0).format(diameter));
System.out.println(the square area is + new DecimalFormat(0).format(area));
}
}
abstract class Shape{
p
文档评论(1)