【问题标题】:Creating an object while using array lists and given code在使用数组列表和给定代码时创建对象
【发布时间】:2017-12-03 18:15:49
【问题描述】:

这是家庭作业,不,我不希望你为我做这件事。我正在参加在线课程以提高我的 Java 技能。我们的任务是创建一个程序,该程序使用 Point.java 类来接受两个坐标并计算距离。然后它将继续询问坐标并告知距离,直到用户退出。我有第一部分下来。我们得到了代码:

public class Point {

   private double x;
   private double y;

   public Point(){
      this.x = 0.0;
      this.y = 0.0;
   }

   public Point(double x,double y){
      this.x = x;
      this.y = y;
   }

   public double distance(Point pt){
      return Math.sqrt((x - pt.x)*(x - pt.x) + (y - pt.y)*(y - pt.y));
   }

   public double getX(){
      return x;
   }

   public double getY(){
      return y;
   }

}

如何从给定的坐标创建对象,将它们放入数组列表中,并使用给定的类?我无法接触到真正的教授。计算机在线评分。

这是我目前的代码:

import java.util.Scanner;

public class Assignment{

   public static void main(String[] args){

      try {

         System.out.println("Welcome.");
         System.out.println("To quit at anytime please type \"Q\". Enter value for first point X:");

         Scanner scan = new Scanner (System.in);

         while (scan.hasNext()) {
            if (scan.hasNextDouble()) { 
               double x = scan.nextDouble();
               //System.out.println(x); //Printing for verification

               System.out.println("Please enter vlaue for first point Y:");
               double y = scan.nextDouble();
               //System.out.println(y); //Printing for verification

               //Point point1 = new Point(x,y);
               System.out.println("First point created.");

               System.out.println("Please enter value for second point X:");
               double x2 = scan.nextDouble();
               //System.out.println(x2); //Printing for verification

               System.out.println("Please enter value for second point Y:");
               double y2 = scan.nextDouble();
               //System.out.println(x2); //Printing for verification


               break;
            }
            else {
               String input = scan.next();
               if (input.equalsIgnoreCase("Q")) {
                  System.out.println("Exiting");
                  break;
               }

               else {
               System.out.println("You did not enter a valid value. Please enter a number or \"Q\" to quit.");
               }               
            }


         }

      }

      catch(Exception e){
         System.out.println("Exiting Program.");

      }

   }

}

【问题讨论】:

    标签: java arrays object arraylist


    【解决方案1】:

    获得所需的变量(X 和 Y)后,就该创建对象了。 要创建对象,请使用以下语法

    Point point = new Point(x, y)
    

    其中 x 和 y 是您的输入变量。现在你有了一个点对象。

    然后,现在您可以使用 Point 类中的方法,例如

    double x = point.getX();
    double y = point.getY(); 
    

    https://docs.oracle.com/javase/tutorial/java/javaOO/index.html

    这里是更多关于对象及其工作原理的额外阅读的 javadocs。

    要初始化一个数组列表,您需要创建一个数组列表对象。 您可能希望将其设为全局变量或在主函数之外声明它。

    List<Point> points = new ArrayList<>();
    

    现在你已经建立了arraylist,你可以使用add、remove、get等函数对其进行修改。将 arraylist 视为可变大小的数组。

    要将一个点对象添加到您的数组列表中,这里是一个如何使用它并遍历它的示例。

    导入 java.util.ArrayList;

    公共类主{

    private List<Point> points = new ArrayList<>();
    public static void main(String[] args) {
        Point point1 = new Point(x, y);
        Point point2 = new Point(x2, y2);
    
        points.add(point1);
        points.add(point2);
    
        for(Point p : points) {
            System.out.println("POINT X: " + p.getX());
        }
    }
    

    【讨论】:

      【解决方案2】:

      首先,您需要导入 ArrayList 类。我不记得您需要导入的确切 API,但它应该是一个简单的 google 搜索。

      其次,在你创建了你的两个点之后,你可以创建一个 ArrayList 并将它们添加到其中:

      ArrayList<Point> pointList = new ArrayList<Point>();
      pointList.add(point1);
      pointList.add(point2);
      

      然后你就可以继续完成剩下的作业了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-25
        • 2018-03-26
        • 1970-01-01
        • 1970-01-01
        • 2019-05-03
        • 2020-01-25
        相关资源
        最近更新 更多