创建名为point类描述点。创建名为shape的类描述图形,shape的属性是 代表图形左上角的坐标的location,point类型包含area()计算图形面积。继承shape创建的元,增加必要的属性和方法;继承创建矩形类,增加必要的属性和方法。class Point{
public int x;
public int y;
public Point(int x,int y){
this.x=x;
this.y=y;
}
public void getX(){
return x;
}
public int setX(int x){
this.x=x;
}
public void getY(){
return y;
}
public int setY(int y){
this.y=y;
}
}
public int x;
public int y;
public Point(int x,int y){
this.x=x;
this.y=y;
}
public void getX(){
return x;
}
public int setX(int x){
this.x=x;
}
public void getY(){
return y;
}
public int setY(int y){
this.y=y;
}
}
return x;
}
public int setX(int x){
this.x=x;
} 写得就有很多错误getX void是无返回,怎么会有return
int setX,返回int类型,没有returnpublic int getX(){
return x;
}
public void setX(int x){
this.x=x;
}
以前应该会学封装吧,注意下,你具体要建怎样的类,可以描述清楚些~~~要不不知道你想说什么
这句话是什么意思?
我以前写过
public class Point{
private int x;
private int y;
public Point(int x,int y){
this.x=x;
this.y=y;
}
public int getX(){
return x;
}
public void setX(int x){
this.x=x;
}
public int getY(){
return y;
}
public void setY(int y){
this.y=y;
}
public int square(int x,int y){
return x*y;
}
}
import java.util.*;
public class ShapeXY
{
public ShapeXY(PointXY first,PointXY second) //圆和矩形都只要两点就能将其确定,
{ //圆--圆心点 和随便另一个点
this.first = first; //矩形--构成对角线的两个点
this.second = second;
}
public void setPoint()
{
firstX = first.getPointX();
firstY = first.getPointY();
secondX = second.getPointX();
secondY = second.getPointY();
}
public int getX() //两点横坐标之差
{
return x = Math.abs(secondX-firstX);
}
public int getY() //两点纵坐标之差
{
return y = Math.abs(secondY-firstY);
}
public double getArea() //计算面积,子类应覆盖的方法
{
return 0;
}
public static void inputXY() //由键盘控制输入坐标
{
Scanner in = new Scanner(System.in);
System.out.print("输入第一个点坐标"+" ");
firstArgs = new PointXY(in.nextInt(),in.nextInt());
System.out.print("输入第二点坐标"+" ");
secondArgs = new PointXY(in.nextInt(),in.nextInt());
} public static PointXY getArgsFirst() // 将输入的坐标传给构造器
{
return firstArgs;
}
public static PointXY getArgsSecond() //同上
{
return secondArgs;
}
private PointXY first,second;
private static PointXY firstArgs,secondArgs;
private int firstX,firstY,secondX,secondY;
private int x,y,area;
}public class PointXY
{
public PointXY(int x,int y)
{
this.x=x;
this.y=y;
}
public int getPointX()
{
return x;
}
public int getPointY()
{
return y;
}
private int x;
private int y;
}
public class CircleXY extends ShapeXY
{
public CircleXY(PointXY first,PointXY second)
{
super(first,second);
}
public double getArea() // 圆的面积
{
super.setPoint();
r = Math.sqrt(super.getX()*super.getX()+super.getY()*super.getY());
circleArea=pi*r*r;
return circleArea;
}
private PointXY leftUp,rightDown;
private double r;
private double circleArea;
private final double pi=3.14;
}import java.util.*;
public class RectangleXY extends ShapeXY
{
public RectangleXY(PointXY leftUp,PointXY rightDown)
{
super(leftUp,rightDown);
}
public double getArea() //矩形面积
{
super.setPoint();
RectangleArea = super.getX()*super.getY();
return RectangleArea;
}
private PointXY leftUp,rightDown;
private int RectangleArea;
}
public class ShapeTest
{
public static void main(String[] args)
{
System.out.println("输入构成矩形的两点坐标");
ShapeXY.inputXY(); //输入矩形坐标
ShapeXY rect = new RectangleXY(ShapeXY.getArgsFirst(),ShapeXY.getArgsSecond());
System.out.println("the Rectangle's area is "+rect.getArea());
System.out.println("输入构成圆的两点坐标");
ShapeXY.inputXY(); //输入圆的坐标
ShapeXY cicleM = new CircleXY(ShapeXY.getArgsFirst(),ShapeXY.getArgsSecond());
System.out.println("the Circle's area is "+cicleM.getArea());
}
}