Testing a class and a couple of errors! please help.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nimamc
    New Member
    • Oct 2008
    • 2

    Testing a class and a couple of errors! please help.

    I'm trying to test a class I have written(I'm an absolute beginner!). I'm getting these errors:

    TestPunt.java:3 1: afstand() in Punt cannot be applied to (Punt) System.out.prin tln("p1.afstand (p2): "+p1.afstand(p2 ));
    Punt.java:53: double cannot be dereferenced return (this.getX().eq uals(that.getX( )) && this.getY().equ als(that.getY() ));
    Punt.java:49: double cannot be dereferenced return (this.getX().eq uals(that.getX( )) && this.getY().equ als(that.getY() ));
    These are the codes:

    Punt.java :

    Code:
    public class Punt
    {
    
    	private double xCoord;
    	private double yCoord;
    	
    	public Punt(double x, double y)
    	{
    		xCoord = x;
    		yCoord = y;
    	}
    	
    	public void setX(double x)
    	{
    		xCoord = x;
    	}
    	
    	public void setY(double y)
    	{
    		yCoord = y;
    	}
    	
    	public double getX()
    	{
    		return xCoord;
    	}
    	
    	public double getY()
    	{
    		return yCoord;
    	}
    	
    	public String toString()
    	{
    		String res = "<Punt(";
    		res =  res + getX();
    		res =  res  + ",";
    		res =  res + getY();
    		res =  res  + ")>";
    		return res;
    		
    	}
    
    	public boolean equals(Object other)
    	{
    		if(other instanceof Punt)
    		{
    			Punt that = (Punt) other;
    			return (this.getX().equals(that.getX()) && this.getY().equals(that.getY()));
    		}
    		else
    		{
    			return false;
    		}
    	}
    	
    	public double afstand() //distance
    	{
    		return Math.sqrt(getX()*getX()+getY()*getY());
    	}
    
    }
    and I'm trying to test Punt.java with TestPunt.java :


    Code:
    public class TestPunt
    {
        public static void main(String []args)
    	{
    		Punt p1 = new Punt(1,2);
    		
    		System.out.println("p1.getX(): " + p1.getX());
    		System.out.println("p1.getY(): " + p1.getY());
    		System.out.println();
    
    		System.out.println("p1.setX(4)");
    		p1.setX(4);
    		System.out.println("p1.getX(): " + p1.getX());
    		System.out.println();
    
    		System.out.println("p1.setY(6)");         
    		p1.setY(6);
    		System.out.println("p1.getY(): " + p1.getY());
    		System.out.println("p1.toString(): "+p1.toString());
    
      		Punt p2 = new Punt(2,4);
    
      		System.out.println("p2.toString(): "+p2.toString());
      		System.out.println("p1.equals(p1): "+p1.equals(p1));
      		System.out.println("p1.equals(p2): "+p1.equals(p2));
      		System.out.println("p1.afstand(p2): "+p1.afstand(p2));
     	} 
    }


    I'm probably doing something very stupid, could you please say what that stupid thing is that I'm doing and don't laugh :P

    Thanks.
  • nimamc
    New Member
    • Oct 2008
    • 2

    #2
    No need to reply anymore, I already solved it :D Thanks anyways.

    Comment

    • Dököll
      Recognized Expert Top Contributor
      • Nov 2006
      • 2379

      #3
      Hey there Punt Tester!

      An absolute beginner also, can you tell us what you are trying to ahve thr program do. What are you testing for, and what will the results be?

      Sorry for your troubles, hope you get working!

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Dököll
        Hey there Punt Tester!

        An absolute beginner also, can you tell us what you are trying to ahve thr program do. What are you testing for, and what will the results be?

        Sorry for your troubles, hope you get working!
        Just read the compiler error diagnostic messages, they're a giveaway:

        - method afstand() doesn't take any parameters;
        - getX() and getY() return primitives; primitives don't have methods.

        kind regards,

        Jos

        Comment

        Working...