简体   繁体   English

尝试编译时出现Java错误

[英]Java Error when trying to compile

I am having trouble with an assignment in which we have to construct 5 classes to produce a program that performs simple calculations and manipulations of simple shapes expressed in 2D geometry. 我在分配作业时遇到麻烦,在该作业中,我们必须构造5个类才能生成一个程序,该程序可以对2D几何形状表示的简单形状进行简单的计算和操作。

First it asks the user to choose a shape, from a choice of three. 首先,它要求用户从三个选项中选择一个形状。 Then it prompts for details of the shape. 然后,提示您输入形状的详细信息。 A circle is specified by giving the X and then Y coordinate of its center, followed by its radius. 通过给出圆心的X坐标和Y坐标,再加上半径,可以指定一个圆。 A Triangle is specified by giving the X and Y coordinates of each of its three corner points. 通过给出三个角点中每个角点的X和Y坐标来指定三角形。 A rectangle is specified by giving the X and Y coordinates of two of its diagonally opposite corner points. 通过给出其对角线相对的两个角点的X和Y坐标来指定矩形。

Following this data, the user is prompted to specify an X offset and a Y offset. 根据该数据,提示用户指定X偏移和Y偏移。

The program creates the specified shape, and also a similar one, in which each point has been shifted by the X and Y offsets. 该程序将创建指定的形状以及相似的形状,其中每个点都已按X和Y偏移进行了平移。

The program then reports the following on the standard output. 然后,程序将在标准输出中报告以下内容。 The details of the original shape -- giving all the points (one, three, or four) and, for a circle, its radius. 原始形状的细节-给出所有点(一个,三个或四个),以及一个圆的半径。 The area and perimeter of the shape. 形状的面积和周长。 The details of the shifted shape. 变形形状的细节。

The 5 classes are to be ShapeShift , Circle , Triangle , Point and Rectangle . 这5个类是ShapeShiftCircleTrianglePointRectangle

My code so far is as follows: 到目前为止,我的代码如下:

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

Triangle 三角形

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

Circle

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

Point

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 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

All classes compile except the ShapeShift class. ShapeShift类外,所有其他类均进行编译。

I get the following 3 errors: 我收到以下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)

I've been staring at this for so long and would really appreciate a fresh set of eyes. 我一直盯着这个眼睛已经很久了,真的很欣赏新的眼睛。 Thanks in advance to anyone who can help! 在此先感谢任何可以提供帮助的人!

您的以下类CircleTriangleRectangle没有shift()方法。

Only your Point class has a shift method. 只有您的Point类具有shift方法。 Circle , Triangle and Rectangle don't. CircleTriangleRectangle没有。

As I can see, the problem is, that you have not declared any method called shift in your Rectangle , Triangle and Circle class, but you are trying to call it. 如我所见,问题是,您尚未在RectangleTriangleCircle类中声明任何称为shift方法,但您正在尝试调用它。

The only shift method I can see is in the Point class. 我只能看到的shift方法是Point类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM