JAVA程序设计相关
请使用JAVA语言来编程,要求如下:1.定义一个形状类,Point,Shape;2.派生出矩形(Rectangle)、圆(Circle)、椭圆(Ellipse);3.完成...
请使用JAVA语言来编程,要求如下:
1. 定义一个形状类, Point, Shape;
2. 派生出矩形(Rectangle)、圆(Circle)、椭圆(Ellipse);
3. 完成其中可能存在的多态方法;
4. 需要有每种形状的输出,包括中心点、面积、周长的输出。
要编译通过的,编的好的会追加分数! 展开
1. 定义一个形状类, Point, Shape;
2. 派生出矩形(Rectangle)、圆(Circle)、椭圆(Ellipse);
3. 完成其中可能存在的多态方法;
4. 需要有每种形状的输出,包括中心点、面积、周长的输出。
要编译通过的,编的好的会追加分数! 展开
2个回答
展开全部
说明:
虽然在语法上可以将Point或Shape定义为接口,但在逻辑上是错误的。
接口是对行为的规范,它不具有对象的特征。抽象类本质是对象的抽象,它具有对象的典型特征,但也有对子类行为的规范。无论是坐标点Point还是形状Shape,它们都具有类的本质特征,不应该将它们定义为接口(虽然语法上并没有错,但在理解上令人费解)。
矩形、圆、椭圆都继承自抽象的形状类,并都包含坐标原点。这样就很好地解决了5个类的关联。另一方面,实际上在几乎所有面向程序设计语言中,并没有圆类,只有椭圆类,因为圆就是一种特殊的椭圆(当a=b时,椭圆就是一种特殊的圆)。
class Point{
protected int x;
protected int y;
public Point(){
}
public Point(int x, int y){
this.x = x;
this.y = y;
}
//返回当前点的横坐标
public int getX(){
return x;
}
//返回当前点的纵坐标
public int getY(){
return y;
}
}
//抽象的形状类
abstract class Shape{
abstract double area(); //抽象的求面积方法
abstract double girth(); //抽象的求周长方法
abstract String getName(); //抽象的返回形状名称的方法
}
//矩形类
class Rectangle extends Shape{
protected Point center; //矩形的中心点坐标
protected double width; //矩形的宽
protected double height; //矩形的高
public Rectangle(){
}
public Rectangle(Point center, double width, double height){
this.center = center;
this.width = width;
this.height = height;
}
//返回矩形的面积
public double area() {
return width * height;
}
//返回矩形的周长
public double girth() {
return 2 * (width + height);
}
//返回矩形的中心点坐标
public Point getCenter(){
return center;
}
//返回当前形状的名称
public String getName() {
return "Rectangle";
}
}
//圆类
class Circle extends Shape{
protected Point center; //圆的中心点坐标
protected double radius; //圆的半径
public Circle(){
}
public Circle(Point center, double radius){
this.center = center;
this.radius = radius;
}
//返回圆的面积
public double area() {
return Math.PI * radius * radius;
}
//返回圆的周长
public double girth() {
return 2 * Math.PI * radius;
}
//返回圆的中心点坐标
public Point getCenter(){
return center;
}
//返回当前形状的名称
public String getName() {
return "Circle";
}
}
//椭圆类
class Ellipse extends Shape{
protected Point center; //椭圆的中心点坐标
protected double a; //椭圆的长半轴的长
protected double b; //椭圆的短半轴的长
public Ellipse(){
}
public Ellipse(Point center, double a, double b){
this.center = center;
this.a = a;
this.b = b;
}
//返回椭圆的面积
public double area() {
return Math.PI * a * b;
}
//返回椭圆的周长
public double girth() {
return 2 * Math.PI * Math.sqrt((a * a + b * b) / 2);
}
//返回椭圆的中心点坐标
public Point getCenter(){
return this.center;
}
//返回当前形状的名称
public String getName() {
return "Ellipse";
}
}
//测试类
public class Test {
public static void main(String[] args) {
Point center = new Point(100, 100); //定义形状的中心点坐标
Shape s; //定义抽象基类对象
s = new Rectangle(center, 3, 4); //用子类实例化抽象基类对象
System.out.println(s.getName() + " : "); //实现了动态多态
System.out.println("Center : " + ((Rectangle)s).getCenter().getX() + ", " + ((Rectangle)s).getCenter().getY());
System.out.println("Area : " + s.area()); //实现了动态多态
System.out.println("Girth : " + s.girth()); //实现了动态多态
System.out.println();
s = new Circle(center, 2); //用子类实例化抽象基类对象
System.out.println(s.getName() + " : ");
System.out.println("Center : " + ((Circle)s).getCenter().getX() + ", " + ((Circle)s).getCenter().getY());
System.out.println("Area : " + s.area());
System.out.println("Girth : " + s.girth());
System.out.println();
s = new Ellipse(center, 5, 3); //用子类实例化抽象基类对象
System.out.println(s.getName() + " : ");
System.out.println("Center : " + ((Ellipse)s).getCenter().getX() + ", " + ((Ellipse)s).getCenter().getY());
System.out.println("Area : " + s.area());
System.out.println("Girth : " + s.girth());
}
}
虽然在语法上可以将Point或Shape定义为接口,但在逻辑上是错误的。
接口是对行为的规范,它不具有对象的特征。抽象类本质是对象的抽象,它具有对象的典型特征,但也有对子类行为的规范。无论是坐标点Point还是形状Shape,它们都具有类的本质特征,不应该将它们定义为接口(虽然语法上并没有错,但在理解上令人费解)。
矩形、圆、椭圆都继承自抽象的形状类,并都包含坐标原点。这样就很好地解决了5个类的关联。另一方面,实际上在几乎所有面向程序设计语言中,并没有圆类,只有椭圆类,因为圆就是一种特殊的椭圆(当a=b时,椭圆就是一种特殊的圆)。
class Point{
protected int x;
protected int y;
public Point(){
}
public Point(int x, int y){
this.x = x;
this.y = y;
}
//返回当前点的横坐标
public int getX(){
return x;
}
//返回当前点的纵坐标
public int getY(){
return y;
}
}
//抽象的形状类
abstract class Shape{
abstract double area(); //抽象的求面积方法
abstract double girth(); //抽象的求周长方法
abstract String getName(); //抽象的返回形状名称的方法
}
//矩形类
class Rectangle extends Shape{
protected Point center; //矩形的中心点坐标
protected double width; //矩形的宽
protected double height; //矩形的高
public Rectangle(){
}
public Rectangle(Point center, double width, double height){
this.center = center;
this.width = width;
this.height = height;
}
//返回矩形的面积
public double area() {
return width * height;
}
//返回矩形的周长
public double girth() {
return 2 * (width + height);
}
//返回矩形的中心点坐标
public Point getCenter(){
return center;
}
//返回当前形状的名称
public String getName() {
return "Rectangle";
}
}
//圆类
class Circle extends Shape{
protected Point center; //圆的中心点坐标
protected double radius; //圆的半径
public Circle(){
}
public Circle(Point center, double radius){
this.center = center;
this.radius = radius;
}
//返回圆的面积
public double area() {
return Math.PI * radius * radius;
}
//返回圆的周长
public double girth() {
return 2 * Math.PI * radius;
}
//返回圆的中心点坐标
public Point getCenter(){
return center;
}
//返回当前形状的名称
public String getName() {
return "Circle";
}
}
//椭圆类
class Ellipse extends Shape{
protected Point center; //椭圆的中心点坐标
protected double a; //椭圆的长半轴的长
protected double b; //椭圆的短半轴的长
public Ellipse(){
}
public Ellipse(Point center, double a, double b){
this.center = center;
this.a = a;
this.b = b;
}
//返回椭圆的面积
public double area() {
return Math.PI * a * b;
}
//返回椭圆的周长
public double girth() {
return 2 * Math.PI * Math.sqrt((a * a + b * b) / 2);
}
//返回椭圆的中心点坐标
public Point getCenter(){
return this.center;
}
//返回当前形状的名称
public String getName() {
return "Ellipse";
}
}
//测试类
public class Test {
public static void main(String[] args) {
Point center = new Point(100, 100); //定义形状的中心点坐标
Shape s; //定义抽象基类对象
s = new Rectangle(center, 3, 4); //用子类实例化抽象基类对象
System.out.println(s.getName() + " : "); //实现了动态多态
System.out.println("Center : " + ((Rectangle)s).getCenter().getX() + ", " + ((Rectangle)s).getCenter().getY());
System.out.println("Area : " + s.area()); //实现了动态多态
System.out.println("Girth : " + s.girth()); //实现了动态多态
System.out.println();
s = new Circle(center, 2); //用子类实例化抽象基类对象
System.out.println(s.getName() + " : ");
System.out.println("Center : " + ((Circle)s).getCenter().getX() + ", " + ((Circle)s).getCenter().getY());
System.out.println("Area : " + s.area());
System.out.println("Girth : " + s.girth());
System.out.println();
s = new Ellipse(center, 5, 3); //用子类实例化抽象基类对象
System.out.println(s.getName() + " : ");
System.out.println("Center : " + ((Ellipse)s).getCenter().getX() + ", " + ((Ellipse)s).getCenter().getY());
System.out.println("Area : " + s.area());
System.out.println("Girth : " + s.girth());
}
}
展开全部
interface Shape {
String getName();
Point getCenterPoint();
double getArea();
double getGirth();
void print();
}
class Point {
public int x = 0;
public int y = 0;
public Point() {
}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "(" + x + "," + y + ")";
}
}
abstract class AbstractShape implements Shape {
@Override
public void print() {
System.out.println("\nSHAPE:" + getName() + "\ncenterPoint="
+ getCenterPoint() + "\ngirth=" + getGirth() + "\narea="
+ getArea());
}
}
class Circle extends AbstractShape {
private int radius;
public Circle() {
}
public Circle(int radius) {
this.radius = radius;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
@Override
public String getName() {
return "圆";
}
@Override
public Point getCenterPoint() {
return new Point(radius, radius);
}
@Override
public double getArea() {
return Math.PI * Math.pow(radius, 2);
}
@Override
public double getGirth() {
return 2 * Math.PI * radius;
}
}
class Rectangle extends AbstractShape {
private int width;
private int length;
public Rectangle() {
}
public Rectangle(int width, int length) {
this.width = width;
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
@Override
public String getName() {
return "矩形";
}
@Override
public Point getCenterPoint() {
return new Point(width / 2, length / 2);
}
@Override
public double getArea() {
return width * length;
}
@Override
public double getGirth() {
return 2 * (width + length);
}
}
class Ellipse extends AbstractShape {
private int a;// 长半轴
private int b;// 短半轴
public Ellipse() {
}
public Ellipse(int a, int b) {
this.a = a;
this.b = b;
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
@Override
public String getName() {
return "椭圆";
}
@Override
public Point getCenterPoint() {
return new Point(a, b);
}
@Override
public double getArea() {
return Math.PI * a * b;
}
@Override
public double getGirth() {
return Math.PI * (1.5 * (a + b) - Math.sqrt(a * b));
}
}
/**
* 测试类
*/
public class Test {
public static void main(String[] args) {
Shape shapes[] = { new Circle(2), new Rectangle(6, 4),
new Ellipse(8, 6) };
for (Shape shape : shapes) {
shape.print();
}
}
}
String getName();
Point getCenterPoint();
double getArea();
double getGirth();
void print();
}
class Point {
public int x = 0;
public int y = 0;
public Point() {
}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "(" + x + "," + y + ")";
}
}
abstract class AbstractShape implements Shape {
@Override
public void print() {
System.out.println("\nSHAPE:" + getName() + "\ncenterPoint="
+ getCenterPoint() + "\ngirth=" + getGirth() + "\narea="
+ getArea());
}
}
class Circle extends AbstractShape {
private int radius;
public Circle() {
}
public Circle(int radius) {
this.radius = radius;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
@Override
public String getName() {
return "圆";
}
@Override
public Point getCenterPoint() {
return new Point(radius, radius);
}
@Override
public double getArea() {
return Math.PI * Math.pow(radius, 2);
}
@Override
public double getGirth() {
return 2 * Math.PI * radius;
}
}
class Rectangle extends AbstractShape {
private int width;
private int length;
public Rectangle() {
}
public Rectangle(int width, int length) {
this.width = width;
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
@Override
public String getName() {
return "矩形";
}
@Override
public Point getCenterPoint() {
return new Point(width / 2, length / 2);
}
@Override
public double getArea() {
return width * length;
}
@Override
public double getGirth() {
return 2 * (width + length);
}
}
class Ellipse extends AbstractShape {
private int a;// 长半轴
private int b;// 短半轴
public Ellipse() {
}
public Ellipse(int a, int b) {
this.a = a;
this.b = b;
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
@Override
public String getName() {
return "椭圆";
}
@Override
public Point getCenterPoint() {
return new Point(a, b);
}
@Override
public double getArea() {
return Math.PI * a * b;
}
@Override
public double getGirth() {
return Math.PI * (1.5 * (a + b) - Math.sqrt(a * b));
}
}
/**
* 测试类
*/
public class Test {
public static void main(String[] args) {
Shape shapes[] = { new Circle(2), new Rectangle(6, 4),
new Ellipse(8, 6) };
for (Shape shape : shapes) {
shape.print();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询