有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

翻译坐标法

我似乎无法正确使用转换方法。有什么建议可以让我的方法更好地翻译这一点吗?i、 当调用此方法时,它应该能够给出新的点。另外,我在创建坡度方法时遇到问题。。。我知道斜率是y2-y1/x2-x1,但我如何将其转化为一种方法。有数学课我可以为斜坡导入吗?谢谢,非常感谢

       import java.util.*;
        public class Point {
        private int x; // to store variables for x & y
        private int y;
        private double slope;



       //default constructor
          public Point () {
           double x = 0;
           double y = 0;
          } 

       //alternate constructor
       public Point (double x1, double y1) {
        double x = x1;
        double y = y1;

         }

       //set coordinates
       public void setCoord (double x1, double y1){
        double x = x1;
        double y = y1;
         }
      //print method
      public void print () {
        System.out.print(x + "," + y);
         }

      //toString Method
       public String toString() {
        return "(" + x + "," + y + ")";
         }

       public int getX() {
         return x;
          }

       public int getY() {
        return y;
          }

       public void equals () {
         if (x==y) 
           System.out.println(" Coordinates are the same ");
         else {
           System.out.println(" Coordinates are different ");
            }
         }

         public void copy(Point temp) {
          x=temp.x;
          y=temp.y;
           }

         public Point getCopy() {
         Point temp = new Point();
           temp.x = x;
           temp.y = y;

            return temp;
            }

         public void distanceFromOrigin(int x1, int y1) {
          double dx = x-x1;
          double dy = y-y1;
          }
       //calculate the distance from one point to another
           public void distance (double x1, double y1) {
            double distance = Math.sqrt((x * x1) + (y * y1));
           }
       //shift the location of a point by a given amount
          public void transform (double dx, double dy) { 
           double transform = ((x+dx) (y+dy));

           }
       // returns true if any given point lines up horizontally with a given point.
           public boolean isHorizontal () {
             return true;

             }
       // returns true if any given point lines up vertically with a given point.
            public boolean isVertical () {
              return true;
            }
       // returns the slope of the line 
            public double slope() {
               return slope;
           }
       }

共 (1) 个答案

  1. # 1 楼答案

    public void equals (Object o) {
        if(o instanceof Point) {
            Point p = (Point)o;
            return p.x == x && p.y == y;
        } else {
            return false;
        }
    }
    
    public Point getCopy() {
        return new Point(x, y);
    }
    
    public double slope(Point otherPoint) {
        return ((double)(otherPoint.y - y)) / ((double)(otherPoint.x / x));
    }
    
    public double distance (Point otherPoint) {
        double dx = otherPoint.x - x;
        double dy = otherPoint.y - y;
        return Math.sqrt((dx * dx) + (dy * dy));
    }
    
    public double distanceFromOrigin() {
        return distance(new Point(0, 0));
    }
    
    public boolean isHorizontal (Point otherPoint) {
        return otherPoint.y == y;
    }
    
    public boolean isVertical(Point otherPoint) {
        return otherPoint.x == x;
    }
    
    public void transform (double dx, double dy) {
        x += dx;
        y += dy;
    }