我在分配作业时遇到麻烦,在该作业中,我们必须构造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
ShapeShift
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)
我一直在盯着这个眼睛已经很久了,真的很欣赏新的眼睛。在此先感谢任何可以提供帮助的人!
最佳答案
您的以下类shift()
,Circle
和Triangle
中没有Rectangle
方法。