954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

return value problem

I'm trying to create a class that calculates for the next possible coordinate for a Bishop Piece for my chess program and I encountered this problem.

I used a JOptionPane to test for the results and I can't understand how it got that result.

The first JOptionPane shows false because I used the method for the first time.
FALSE MEANS THAT THE LIST IS NOT EMPTY
On the second and third JOptionPane I was expecting to see a false but it showed true.
Can someone explain to me how it became true.

I'm new to java but i have some experience in C#

import java.awt.Point;
import java.util.ArrayList;
import javax.swing.JOptionPane;


public class Movement {
    
     public final static int NORTH=-1; 
     public final static int SOUTH=1;
     public final static int EAST=1;
     public final static int WEST=-1;
  
     public ArrayList<Point> getDiagonalPoints(Point origin){
         ArrayList<Point> list =new ArrayList<Point>();
         JOptionPane.showMessageDialog(null,getPointsOf(WEST,NORTH,origin).isEmpty()); //OUTPUTS FALSE
         list.addAll(getPointsOf(WEST,NORTH,origin));
         JOptionPane.showMessageDialog(null,getPointsOf(WEST,NORTH,origin).isEmpty());
//OUTPUTS TRUE
         list.addAll(getPointsOf(WEST,SOUTH,origin));
         JOptionPane.showMessageDialog(null,getPointsOf(WEST,SOUTH,origin).isEmpty());
         JOptionPane.showMessageDialog(null,list.size());
         //list.addAll(getPointsOf(EAST,SOUTH,origin));
         //list.addAll(getPointsOf(EAST,NORTH,origin));
         return list;
     }
     
     public ArrayList<Point> getPointsOf(int x,int y,Point origin){
         ArrayList<Point> options =new ArrayList<Point>(); 
         
         while(origin.x<8&&origin.x>=0&&origin.y<8&&origin.y>=0){
             options.add(new Point(origin.x,origin.y));
             origin.x+=x;
             origin.y+=y; 
         }
         return options;
     }
}
emc22
Newbie Poster
4 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

You call the method three times with the same input. What makes you expect different output?

jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
 

Method getPointsOf modifies your origin instance of Point class (lines 32,33)
If it is unnecessary use a local "clone".

public static ArrayList<Point> getPointsOf(int x, int y, Point orig) {
        Point origin = new Point(orig);///////////////////
        ArrayList<Point> options = new ArrayList<Point>();
        //System.out.println("=== " + origin.x + " " + origin.y);
        while (origin.x < 8 && origin.x >= 0 && origin.y < 8 && origin.y >= 0) {
            options.add(new Point(origin.x, origin.y));
            //System.out.println("added " + origin.x + " " + origin.y);
            origin.x += x;
            origin.y += y;
        }
        return options;
    }
quuba
Posting Pro
573 posts since Nov 2008
Reputation Points: 123
Solved Threads: 106
 

Method getPointsOf modifies your origin instance of Point class (lines 32,33) If it is unnecessary use a local "clone".

public static ArrayList<Point> getPointsOf(int x, int y, Point orig) {
        Point origin = new Point(orig);///////////////////
        ArrayList<Point> options = new ArrayList<Point>();
        //System.out.println("=== " + origin.x + " " + origin.y);
        while (origin.x < 8 && origin.x >= 0 && origin.y < 8 && origin.y >= 0) {
            options.add(new Point(origin.x, origin.y));
            //System.out.println("added " + origin.x + " " + origin.y);
            origin.x += x;
            origin.y += y;
        }
        return options;
    }


Thank you I understood my mistake now :)

emc22
Newbie Poster
4 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: