0

The error I am having is that the console is printing out all zeros in the point values when it should be showing something similar to (1,2) (3,4) (5,6) (7,8)

I tracked down the error to the setters, where it does not load in the value. Example

 public void setPoint1(double newX, double newY) {
  this.N1x = newX;
  this.N1y = newY;

}

This does not update the private double N1.x to the corresponding constructor value of 1. I believe this to be the problem but I am new to coding and still have a lot to learn.

As I understand it, the call to the setters in the constructor should take the 1 through 8 values from the constructor call and then send then to the setter where they are then distributed to the private doubles then the Point Class operation executes and the values are saved into the point1 through point4 objects which is then called from the main method returning their stored values.

Below is the code I have regarding my problem. Thank you in advance!

public class Quadrilateral {

   //Receives data from constructor 
   //initializes the variables for point objects  
   private double N1x; private double N1y;
   private double N2x; private double N2y;
   private double N3x; private double N3y;
   private double N4x; private double N4y;

   //creates new point objects
   Point point1 = new Point(N1x,N1y);
   Point point2 = new Point(N2x,N2y);
   Point point3 = new Point(N3x,N3y);
   Point point4 = new Point(N4x,N4y);

   // Encapsulation for point 1
   public Point getPoint1() {
      return point1;
   }
   public void setPoint1(double newX, double newY) {
      this.N1x = newX;
      this.N1y = newY;
   }

   // Encapsulation for point 2
   public Point getPoint2() {
      return point2;
   }
   public void setPoint2(double newX, double newY) {
      this.N2x = newX;
      this.N2y = newY;
   }

   // Encapsulation for point 3
   public Point getPoint3() {
      return point3;
   }
   public void setPoint3(double newX, double newY) {
      this.N3x = newX;
      this.N3y = newY;
   }

   // Encapsulation for point 4
   public Point getPoint4() {
      return point4;
   }
   public void setPoint4(double newX, double newY) {
      this.N4x = newX;
      this.N4y = newY;
   }

   //constructor for Quadrilateral
   //takes in 4 sets of point values
   Quadrilateral(
         double N1x, double N1y,
         double N2x, double N2y,
         double N3x, double N3y,
         double N4x, double N4y){

      setPoint1(N1x, N1y);
      setPoint2(N2x, N2y);
      setPoint3(N3x, N3y);
      setPoint4(N4x, N4y);

   }

   // gets the (x,y) values 
   //remember to override to add additional values and change shape

   public String toString() {
      return "Quadrilateral "+"\n"+ 
            "Node points are "+"\n"+
            getPoint1()+"\n"+
            getPoint2()+"\n"+
            getPoint3()+"\n"+
            getPoint4();
   }

public static void main(String[] args) {

   Quadrilateral quadrilateral = new Quadrilateral(1,2,3,4,5,6,7,8);
   System.out.println(quadrilateral.toString());
   }

}

This is the point class

public class Point {
   private double x;
   private double y;

   public double getX() {
      return x;
   }
   public double getY() {
      return y;
   }
   public void setX(double newX) {
      this.x = newX;
   }
   public void setY(double newY) {
      this.y= newY;
   }
   Point(double x, double y){//when this constructor is called it performs encapsulation 
      setX(x);
      setY(y);
   }
   public String toString() {
      return "("+getX()+","+getY()+")";
   }
}
  • Values are copied. When you change `N1x`, the `x` in `point1` won't be affected. – Max Vollmer Mar 16 '19 at 22:02
  • Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Max Vollmer Mar 16 '19 at 22:03
  • Little protip: you only need a minimum and maximum x/y/dimensional value to keep track of a quadratic form (rectangle, cube, etc). The rest can be calculated but is often not needed as opposed to width, area, etc – Rogue Mar 16 '19 at 22:23
  • On a side note, you should avoid calling non-final methods in constructors: https://docs.oracle.com/javase/tutorial/java/IandI/final.html – Modus Tollens Mar 17 '19 at 05:56

2 Answers2

1

If I understand your question right you should do this:

//creates new point objects
Point point1;
Point point2;
Point point3;
Point point4;

and this:

public void setPoint1(double newX, double newY) {
    this.N1x = newX;
    this.N1y = newY;
    point1 = new Point(newX, newY);
}

(Same thing for the other point setters too)

In your code you only initialized the points while the coordinates are null, before you even first set the coordinates in your setPoint methods

Mano176
  • 168
  • 13
  • Thank you so much, that was it! Do you happen to have a reference to how this works? I thought that the Point point1 = new Point(newX, newY); created and initialized the and then took in the values to get the new value. – Zachary Ouellette Mar 16 '19 at 22:58
  • Nah you have to see it step for step: 1 Step. Everything 'class related' gets executed, like 'private double N1x; private double N1y;' and 'Point point1 = new Point(N1x,N1y);' 2 Step. The constructor gets executed, N1x, N1y, etc are now set to your newX, NewY parameters, but the point1, point2, point3 and point4 were already set with the coordinates 'null' in Step1 – Mano176 Mar 16 '19 at 23:07
  • The class then Constructor, is this like order of operations? Is there a formal term for it? And thank you. – Zachary Ouellette Mar 17 '19 at 04:35
  • Yep, its just a simple order. If you practice some more it well get easy and you don't even have to think about it – Mano176 Mar 17 '19 at 10:41
0

Since you are initialising your Point objects, the member variables of Quadrilateral even before constructor is called with N1x, N2x etc. that are not initialised, you are setting their default values which is 0 in Point objects (member variables). Change your Quadrilateral class definition as follows

public class Quadrilateral {

    // creates new point objects
    Point point1 ;
    Point point2;
    Point point3 ;
    Point point4 ;

    // Encapsulation for point 1
    public Point getPoint1() {
        return point1;
    }

    public void setPoint1(double newX, double newY) {
        this.point1 = new Point(newX, newY);        
    }

    // Encapsulation for point 2
    public Point getPoint2() {
        return point2;
    }

    public void setPoint2(double newX, double newY) {
        this.point2 = new Point(newX, newY);
    }

    // Encapsulation for point 3
    public Point getPoint3() {
        return point3;
    }

    public void setPoint3(double newX, double newY) {
        this.point3 = new Point(newX, newY);
    }

    // Encapsulation for point 4
    public Point getPoint4() {
        return point4;
    }

    public void setPoint4(double newX, double newY) {
        this.point4 = new Point(newX, newY);
    }

    // constructor for Quadrilateral
    // takes in 4 sets of point values
    Quadrilateral(double N1x, double N1y, double N2x, double N2y, double N3x, double N3y, double N4x, double N4y) {

        setPoint1(N1x, N1y);
        setPoint2(N2x, N2y);
        setPoint3(N3x, N3y);
        setPoint4(N4x, N4y);

    }

    // gets the (x,y) values
    // remember to override to add additional values and change shape

    public String toString() {
        return "Quadrilateral " + "\n" + "Node points are " + "\n" + getPoint1() + "\n" + getPoint2() + "\n"
                + getPoint3() + "\n" + getPoint4();
    }

    public static void main(String[] args) {

        Quadrilateral quadrilateral = new Quadrilateral(1, 2, 3, 4, 5, 6, 7, 8);
        System.out.println(quadrilateral.toString());
    }

}
Gro
  • 1,613
  • 1
  • 13
  • 19