-2

I want to be able to store and manage ordered pairs of the form: (x, y). To solve this problem I created two classes: EuclideanSpace and Point. This is a homework assignment and so it is set up with these specific methods and the class EuclideanSpace and the inner class Point. I thought I had it almost figured out but when I run it it prints out '0' for the x and the y values after the first entry. For every entry after that it prints 'null'.

import java.util.Scanner;

public class EuclideanSpace {

    Point point[];

    Scanner scanner = new Scanner(System.in);

    public EuclideanSpace() {
        point = new Point[10];
    }// End constructor

    // Adds points to the array
    public boolean addPoint(int x, int y) {

        for (int i = 0; i < 10; i++) {
            if (point[i] == null) {
                point[i] = new Point();
            }// End if
            return true;
        }// End for
        return false;
    }// End addPoint

    // Prints a point in a given index in the point array
    public void printPoint(int i) {
        System.out.println(point[i]);
    }

    // Inner class
    public static class Point {
        private int x;
        private int y;

        public void Point(int x, int y) {
            this.x = x;
            this.y = y;
        }// End Point method

        public String toString() {

            return "X = " + x + "Y = " + y;
        }// End toString

    }// End inner Class

}// End

Driver class:

import java.awt.Point;
import java.util.Scanner;

public class ICDriver {

    static Scanner scanner = new Scanner(System.in);
    // New object from the class EuclideanSpace
    static EuclideanSpace es = new EuclideanSpace();

    public static void main(String[] args) {

        for (int i = 0; i < 10; i++) {
            System.out.println("Add point x coordinate? ");
            int pointX = scanner.nextInt();
            System.out.println("Add point y coordinate? ");
            int pointY = scanner.nextInt();

            es.addPoint(pointX, pointY);
            es.printPoint(i);

        } // End for

    }// End main

}// End
ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
Chap
  • 61
  • 9

1 Answers1

1
//    Correct your addPoint method   
    public boolean addPoint(int x, int y) {

            for (int i = 0; i < 10; i++) {
                if (point[i] == null) {
                    point[i] = new Point(x,y);
                    return true;
                }// End if
            }// End for
            return false;
        }// End addPoint

//Remove void from your constructor

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