我遇到了一项任务,我们必须构建5个类来生成一个程序,该程序执行简单的计算和处理以2D几何体表示的简单形状。
首先,它要求用户从三个选项中选择一个形状。 然后它会提示输入形状的详细信息。 通过给出X然后Y坐标来指定圆 其中心,其后是半径。 通过给出X和Y坐标指定三角形 其三个角点中的每一个。 通过给出X和Y坐标指定矩形 两个对角线对角点。
根据此数据,系统会提示用户指定X偏移量 和Y偏移。
程序创建指定的形状,也是类似的形状, 其中每个点都被X和Y偏移移位。
程序然后在标准输出上报告以下内容。 原始形状的细节 - 给出所有要点 (一,三或四),对于一个圆,它的半径。 形状的面积和周长。 变形的细节。
这5个班级将是ShapeShift
,Circle
,Triangle
,Point
和Rectangle
。
到目前为止我的代码如下:
矩形
public class Rectangle
{
private final Point a;
private final Point b;
// Constructor Method
public Rectangle(Point requiredA, Point requiredB)
{
a = requiredA;
b = requiredB;
}
// Get point c for the third commnadline
public Point c()
{
return new Point(a.getXCoordinate(), b.getYCoordinate());
}// c
// Get point d for the forth commandline
public Point d()
{
return new Point(b.getXCoordinate(), a.getYCoordinate());
} //d
public double areaOfRectangle()
{
return (a.distance(c()) * a.distance(d()));
}
public double perimeterOfRectangle()
{
return (2 * (a.distance(c())) + (a.distance(d())));
}
} // Rectangle
三角
public class Triangle
{
// Triangle, has 8 values including 2 shift values.
private final Point TrianglePoint1;
private final Point TrianglePoint2;
private final Point TrianglePoint3;
// Constructor Method
public Triangle(Point requiredTrianglePoint1,
Point requiredTriagnlePoint2,
Point requiredTrianglePoint3)
{
TrianglePoint1 = requiredTrianglePoint1;
TrianglePoint2 = requiredTriagnlePoint2;
TrianglePoint3 = requiredTrianglePoint3;
}
public double perimeterOfTriangle()
{
return (TrianglePoint1.distance(TrianglePoint2) +
TrianglePoint2.distance(TrianglePoint3) +
TrianglePoint3.distance(TrianglePoint1));
} // PerimeterTriangle
// Work and return the area of the triangle
public double areaOfTriangle()
{
double semiPerimeter = (TrianglePoint1.distance(TrianglePoint2) +
TrianglePoint2.distance(TrianglePoint3) +
TrianglePoint3.distance(TrianglePoint1) / 2);
return Math.sqrt(semiPerimeter *
(semiPerimeter - TrianglePoint1.distance(TrianglePoint2)) *
(semiPerimeter - TrianglePoint2.distance(TrianglePoint3)) *
(semiPerimeter - TrianglePoint3.distance(TrianglePoint1)));
} // AreaTriangle
环
public class Circle
{
// Use the circle to find the centre and the radius.
private final Point centre;
private final double radius;
// Construct the two values
public Circle(Point requiredCentre, double requiredRadius)
{
centre = requiredCentre;
radius = requiredRadius;
}
public double areaOfCircle()
{
return (Math.PI * (Math.pow(radius, 2)));
} // areaOfCircle
public double perimeterOfCircle()
{
return (2 * Math.PI * radius);
} //Perimeter
}// Circle
点
public class Point
{
// Two Coordinate values for each point
private final double coordinateX;
private final double coordinateY;
// Construct the two values
public Point(double requiredCoordinateX, double requiredCoordinateY)
{
coordinateX = requiredCoordinateX;
coordinateY = requiredCoordinateY;
}
// Get the X Coordinate
public double getXCoordinate()
{
return coordinateX;
}
// Get Y Coordinate
public double getYCoordinate()
{
return coordinateY;
}
// The layout, when demonstrating the two values.
public String toString()
{
return "(" + coordinateX + "," + coordinateY + ")";
} // String
// Calculate the moved coordinates, when added to xShift and yShift
public Point shift(double xShift, double yShift)
{
return new Point (coordinateX + xShift,
coordinateY + yShift);
} //movedPoint
// Calculate the distance between two points
public double distance(Point other)
{
return Math.sqrt(Math.pow((coordinateX - other.coordinateX), 2) +
Math.pow((coordinateY - other.coordinateY), 2));
} // Distance calculated
} // Point
变身
import java.util.Scanner;
public class ShapeShift
{
// A scanner to interact with the user.
private static Scanner inputScanner = new Scanner(System.in);
// Helper method to read a point from the input.
public static Point inputPoint(String prompt)
{
System.out.print(prompt);
double x = inputScanner.nextDouble();
double y = inputScanner.nextDouble();
return new Point(x, y);
} // inputPoint
// The X and Y amount to shift the first shape to get the second.
public static double xShift, yShift;
public ShapeShift(double shiftX, double shiftY)
{
xShift = shiftX;
yShift = shiftY;
}
// Helper method to read the X and Y shifts.
private static void inputXYShifts()
{
System.out.print("Enter the offset as X Y: ");
double xShift = inputScanner.nextDouble();
double yShift = inputScanner.nextDouble();
} // inputXYShifts
// The main method.
public static void main(String[] args)
{
// Obtain shape choice.
System.out.print("Choose circle (1), triangle (2), rectangle (3): ");
int shapeChoice = inputScanner.nextInt();
// Process the shape based on the choice.
switch (shapeChoice)
{
// Circle.
case 1:
Point centre = inputPoint("Enter the centre as X Y: ");
System.out.print("Enter the radius: ");
double radius = inputScanner.nextDouble();
Circle originalCircle = new Circle(centre, radius);
inputXYShifts();
Circle shiftedCircle = originalCircle.shift(xShift, yShift);
System.out.println();
System.out.println(originalCircle);
System.out.println("has area " + originalCircle.areaOfCircle()
+ ", perimeter "
+ originalCircle.perimeterOfCircle());
System.out.println("and when shifted by X offset " + xShift
+ " and Y offset " + yShift + ", gives");
System.out.println(shiftedCircle);
break;
// Triangle.
case 2:
Point pointA = inputPoint("Enter point A as X Y: ");
Point pointB = inputPoint("Enter point B as X Y: ");
Point pointC = inputPoint("Enter point C as X Y: ");
Triangle originalTriangle = new Triangle(pointA, pointB, pointC);
inputXYShifts();
Triangle shiftedTriangle = originalTriangle.shift(xShift, yShift);
System.out.println();
System.out.println(originalTriangle);
System.out.println("has area " + originalTriangle.areaOfTriangle()
+ ", perimeter "
+ originalTriangle.perimeterOfTriangle());
System.out.println("and when shifted by X offset " + xShift
+ " and Y offset " + yShift + ", gives");
System.out.println(shiftedTriangle);
break;
// Rectangle.
case 3:
Point diag1End1 = inputPoint("Enter one corner as X Y: ");
Point diag1End2 = inputPoint("Enter opposite corner as X Y: ");
Rectangle originalRectangle = new Rectangle(diag1End1, diag1End2);
inputXYShifts();
Rectangle shiftedRectangle = originalRectangle.shift(xShift, yShift);
System.out.println();
System.out.println(originalRectangle);
System.out.println("has area " + originalRectangle.areaOfRectangle()
+ ", perimeter "
+ originalRectangle.perimeterOfRectangle());
System.out.println("and when shifted by X offset " + xShift
+ " and Y offset " + yShift + ", gives");
System.out.println(shiftedRectangle);
break;
// Bad choice.
default:
System.out.println("That wasn't 1, 2 or 3!");
break;
} // switch
} // main
} // class ShapeShift
除了ShapeShift
类之外,所有类都编译。
我收到以下3个错误:
ShapeShift.java:78: error: cannot find symbol
Circle shiftedCircle = originalCircle.shift(xShift, yShift);
symbol: method shift(double,double)
ShapeShift.java:96: error: cannot find symbol
Triangle shiftedTriangle = originalTriangle.shift(xShift, yShift);
symbol: method shift(double,double)
ShapeShift.java:113: error: cannot find symbol
Rectangle shiftedRectangle = originalRectangle.shift(xShift, yShift);
symbol: method shift(double,double)
我一直盯着这看了很久,真的很欣赏一双清新的眼睛。提前感谢任何可以提供帮助的人!
答案 0 :(得分:1)
您在以下课程shift()
,Circle
和Triangle
中没有Rectangle
方法。
答案 1 :(得分:1)
只有您的Point
班级有shift
方法。 Circle
,Triangle
和Rectangle
没有。
答案 2 :(得分:0)
正如我所看到的,问题是,您尚未在shift
,Rectangle
和Triangle
类中声明任何名为Circle
的方法,但您正在尝试叫它。
我能看到的唯一shift
方法是Point
类。