Results 1 to 4 of 4
  1. #1
    lolik is offline Member
    Join Date
    Apr 2017
    Posts
    5
    Rep Power
    0

    Default can`t understand were is the promlem

    hi everyone, can someone look at my program and help me find the problem that the taster found?

    my prog---->

    Java Code:
    public class Point
    {
        private double _radius;
        private double _alpha;
        private final double DEFAULT_VAL = 0;
        
        private double radToDeg(Double rad)
        {
            double deg = rad * (180.0 / (Math.PI));
            return deg;
        }
    
        private double degToRad(Double deg)
        {
            double rad = deg * (Math.PI / 180);
            return rad;
        }
    
        public Point(double x, double y)
        {
            if (x <= DEFAULT_VAL)
                x = DEFAULT_VAL;
            if (y <= DEFAULT_VAL)
                y = DEFAULT_VAL;
            _radius = Math.sqrt((Math.pow(x, 2)) + (Math.pow(y, 2)));
            _radius = Math.round(_radius * 10000)/(double) 10000;
            _alpha = radToDeg(Math.atan((double) y / (double) x));
            _alpha = Math.round(_alpha*10000)/(double) 10000;
    
        }
    
        public Point(Point other)
        {
            if (other!=null)
    
    
                {
                    _radius = other._radius;
                    
                    _alpha = other._alpha;
                   
                }
        }
        
        public double getX()
        {
            double x = _radius*Math.cos(_alpha); 
            _radius = Math.round(_radius * 10000)/(double) 10000;
             _alpha = Math.round(_alpha*10000)/(double) 10000;
    
            return (x>=DEFAULT_VAL)?x:DEFAULT_VAL;
             
        }
        public double getY()
        {
            double y = _radius*Math.sin(_alpha); 
            _radius = Math.round(_radius * 10000)/(double) 10000;
             _alpha = Math.round(_alpha*10000)/(double) 10000;
    
            return (y>=DEFAULT_VAL)?y:DEFAULT_VAL;
            
            
        }
        public void setX(double num)
        {
            if (num>= DEFAULT_VAL)
           {
               double y = getY();
               double x = num;
               _radius = Math.sqrt(y * y + x * x);
               _radius = Math.round(_radius * 10000)/(double) 10000;
               _alpha = Math.atan(y/x);
               _alpha = Math.round(_alpha*10000)/(double) 10000;
           }
         }
        public void setY(double num)
        {
            if (num>= DEFAULT_VAL)
           {
               double x = getX();
               double y = num;
               _radius = Math.sqrt(y * y + x * x);
               _radius = Math.round(_radius * 10000)/(double) 10000;
               _alpha = Math.atan(y/x);
               _alpha = Math.round(_alpha*10000)/(double) 10000;
           }
         }
        public String toString()
         {
             return "("+ getX()+ "," +getY()+ ")";
            
         }
        public boolean equals(Point other)
         {
             if (other==null)
                return  false;
              else  
              return (other._radius==this._radius)&&(other._alpha==this._alpha);
            }
        public boolean isAbove(Point other)
        {
             if (other==null)
                return  false;
              else  
              return (other.getY()<this.getY());
        }
        public boolean isUnder(Point other)
        {
            return !isAbove(other);
        }
        public boolean isLeft(Point other)
        {
            if (other==null)
                return  false;
              else  
              return (other.getX()>this.getX());
    
        }
        public boolean isRight(Point other)
        {
            return !isLeft(other);
        }
        public double distance(Point p)
        {
            double x1;
            double y1;
            
            if(p!=null)
            
            x1 = p.getX();
            y1 = p.getY();
           
            return Math.sqrt(Math.pow(getX()-p.getX(), 2) + (Math.pow(getY()-p.getY(), 2)));
           
        }
        public void move(double dx, double dy)
        {
            setX(getX()+dx);
            setY(getY()+dy);
        }
    }


    the taster:
    Java Code:
    public class PointTester {
    
        public static void main(String[] args) {
            System.out.println("********** Test Q1 Point - Start **********");
            System.out.println("testing first constructor and getters:");
            Point p0 = new Point(0,0); 
            if (p0.getX() != 0 || p0.getY() != 0){
                System.out.println("\t ERROR - default constructor and getters,expected (0.0, 0.0) actual= (" + p0.getX() + ", " + p0.getY() +")");
            } else
                System.out.println("\t OK - default constructor and getters expected (0.0, 0.0) actual=(" + p0.getX() + ", " + p0.getY() +")");
            p0 = new Point(0,4); 
            if (p0.getX() != 0 || p0.getY() != 4.0){
                System.out.println("\t ERROR - default constructor and getters expected (0.0, 4.0) actual= (" + p0.getX() + ", " + p0.getY() +")");;
            } else
                System.out.println("\t OK - default constructor and getters expected (0.0, 4.0) actual=(" + p0.getX() + ", " + p0.getY() +")");
            p0 = new Point(3,0); 
            if (p0.getX() != 3.0 || p0.getY() != 0){
                System.out.println("\t ERROR - default constructor and getters expected (3.0, 0.0) actual= (" + p0.getX() + ", " + p0.getY() +")");;
            } else
                System.out.println("\t OK - default constructor and getters expected (3.0, 0.0) actual=(" + p0.getX() + ", " + p0.getY() +")");
    
            p0 = new Point(3.3, 4.4);
            if (p0.getX() != 3.3 || p0.getY() != 4.4){
                System.out.println("\t ERROR - default constructor and getters (x, y): expected(3.3, 4.4); actual=" + p0);
            } else 
                System.out.println("\t OK - default constructor and getters (x, y): expected(3.3, 4.4); actual=" + p0);
            System.out.println("testing copy constructor: ");
            Point p1 = new Point(p0);
            if (p1.getX() != 3.3 || p1.getY() != 4.4){
                System.out.println("\t ERROR - copy constructor (other p): expected(3.3, 4.4) ; actual=" + p1);
            } else 
                System.out.println("\t OK - copy constructor (other p): expected(3.3, 4.4) ; actual=" + p1);
    
            System.out.println("testing setters:"); 
    
            p0 = new Point(3,4); 
            p0.setX(5);
            if (p0.getX() != 5 || p0.getY() != 4){
                System.out.println("\t ERROR - setX point was (3.0,4.0) set x to 5.0 expected (5.0, 4.0) actual= (" + p0.getX() + ", " + p0.getY() +")");;
            } else
                System.out.println("\t OK - setX point was (3.0,4.0) set x to 5.0 expected (5.0, 4.0)  actual=(" + p0.getX() + ", " + p0.getY() +")");
            p0 = new Point(3,4); 
            p0.setY(5);
            if (p0.getX() != 3 || p0.getY() != 5){
                System.out.println("\t ERROR - setY point was (3.0,4.0) set y to 5.0 expected (3.0, 5.0) actual= (" + p0.getX() + ", " + p0.getY() +")");;
            } else
                System.out.println("\t OK - setY point was (3.0,4.0) set y to 5.0 expected (3.0, 5.0)  actual=(" + p0.getX() + ", " + p0.getY() +")");
            System.out.println("testing equals:"); 
    
            p1=new Point(2,3);
            Point p2=new Point(2,3);
            if (! p1.equals(p2)){ 
                System.out.println("\t ERROR - p1.equals(p2) expected true ; actual=" + p1.equals(p2) + ";  p1=" + p1 + " p2=" + p2);
            } else 
                System.out.println("\t OK - p1.equals(p2) expected true ; actual=" + p1.equals(p2) + ";  p1=" + p1 + " p2=" + p2);//check above and under p1 & p5 same high, p4 above
            System.out.println("testing isAbove:"); 
            p1=new Point(2,4);
            p2=new Point(2,3);
            if (! p1.isAbove(p2)){ 
                System.out.println("\t ERROR - p1.isAbove(p2) expected true ; actual=" + p1.isAbove(p2) + ";  p1=" + p1 + " p2=" + p2);
            } else 
                System.out.println("\t OK - p1.isAbove(p2) expected true ; actual=" + p1.isAbove(p2) + ";  p1=" + p1 + " p2=" + p2);
            System.out.println("testing isUnder:"); 
            p1=new Point(2,3);
            p2=new Point(2,4);
            if (! p1.isUnder(p2)){ 
                System.out.println("\t ERROR - p1.isUnder(p2) expected true ; actual=" + p1.isUnder(p2) + ";  p1=" + p1 + " p2=" + p2);
            } else 
                System.out.println("\t OK - p1.isUnder(p2) expected true ; actual=" + p1.isUnder(p2) + ";  p1=" + p1 + " p2=" + p2);
            System.out.println("testing isLeft:"); 
            p1=new Point(2,4);
            p2=new Point(3,4);
            if (! p1.isLeft(p2)){ 
                System.out.println("\t ERROR - p1.isLeft(p2) expected true ; actual=" + p1.isLeft(p2) + ";  p1=" + p1 + " p2=" + p2);
            } else 
                System.out.println("\t OK - p1.isLeft(p2) expected true ; actual=" + p1.isLeft(p2) + ";  p1=" + p1 + " p2=" + p2);
            System.out.println("testing isRight:"); 
            p1=new Point(3,4);
            p2=new Point(2,4);
            if (! p1.isRight(p2)){ 
                System.out.println("\t ERROR - p1.isRight(p2) expected true ; actual=" + p1.isRight(p2) + ";  p1=" + p1 + " p2=" + p2);
            } else 
                System.out.println("\t OK - p1.isRight(p2) expected true ; actual=" + p1.isRight(p2) + ";  p1=" + p1 + " p2=" + p2);
    
            System.out.println("testing move:"); 
            p0=new Point(1,1);
            p0.move(2,2); 
            if (p0.getX() != 3 || p0.getY() != 3) {
                System.out.println("\t ERROR - p0 was (1.0,1.0) p0.move(2.0,2.0) expected p0(3.0,3.0); actual=" + p0);
            } else
                System.out.println("\t OK - p0 was (1.0,1.0) p0.move(2.0,2.0) expected p0(3.0,3.0); actual="+ p0);
            System.out.println("testing distance:"); 
            p0 = new Point (0,0);
            p2 = new Point (4.0, 3.0);
            if (p0.distance(p2) != 5) {
                System.out.println("\t ERROR - p0.distance(p2) expected 5 ; actual=" + p0.distance(p2) + " p0=" + p0 + "; p2=" + p2);
            } else
                System.out.println("\t OK - p0.distance(p2) expected 5 ; actual=" + p0.distance(p2) + " p0=" + p0 + "; p2=" + p2);
    
            System.out.println("********** Test Q1 Point - Finished **********\n");
        }}
    Last edited by Tolls; 04-28-2017 at 12:12 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: can`t understand were is the promlem

    You haven't said what error you are getting.

    Also, when posting code could you please wrap it in code tags so it retains its formatting.
    Unformatted code is hard to follow.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    lolik is offline Member
    Join Date
    Apr 2017
    Posts
    5
    Rep Power
    0

    Default Re: can`t understand were is the promlem

    ********** Test Q1 Point - Start **********
    testing first constructor and getters:
    OK - default constructor and getters expected (0.0, 0.0) actual=(0.0, 0.0)
    ERROR - default constructor and getters expected (0.0, 4.0) actual= (0.0, 3.5759866544022314)
    OK - default constructor and getters expected (3.0, 0.0) actual=(3.0, 0.0)
    ERROR - default constructor and getters (x, y): expected(3.3, 4.4); actual=(0.0,1.5039601647369094)
    testing copy constructor:
    ERROR - copy constructor (other p): expected(3.3, 4.4) ; actual=(0.0,1.5039601647369094)
    testing setters:
    ERROR - setX point was (3.0,4.0) set x to 5.0 expected (5.0, 4.0) actual= (5.0000649568571145, 1.3671354677607739)
    ERROR - setY point was (3.0,4.0) set y to 5.0 expected (3.0, 5.0) actual= (0.0, 4.999999999966269)
    testing equals:
    OK - p1.equals(p2) expected true ; actual=true; p1=(3.5033096433243656,0.0) p2=(3.5033096433243656,0.0)
    testing isAbove:
    OK - p1.isAbove(p2) expected true ; actual=true; p1=(3.683272369243531,2.536372027910564) p2=(3.5033096433243656,0.0)
    testing isUnder:
    OK - p1.isUnder(p2) expected true ; actual=true; p1=(3.5033096433243656,0.0) p2=(3.683272369243531,2.536372027910564)
    testing isLeft:
    ERROR - p1.isLeft(p2) expected true ; actual=false; p1=(3.683272369243531,2.536372027910564) p2=(0.0,1.3672365133971902)
    testing isRight:
    ERROR - p1.isRight(p2) expected true ; actual=false; p1=(0.0,1.3672365133971902) p2=(3.683272369243531,2.536372027910564)
    testing move:
    ERROR - p0 was (1.0,1.0) p0.move(2.0,2.0) expected p0(3.0,3.0); actual=(2.742820143290952,3.2033909379839054)
    testing distance:
    ERROR - p0.distance(p2) expected 5 ; actual=3.3772857805801673 p0=(0.0,0.0); p2=(3.3772857805801673,0.0)
    ********** Test Q1 Point - Finished **********

    this is the resoulte but i dont understand what to fix

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: can`t understand were is the promlem

    Well, that first error is from lines 11-14 in the tester code.

    So the result of;
    Java Code:
    p0 = new Point(0,4);
    is producing incorrect values.
    To me, it looks like those values are supposed to reflected back accurately.
    So, the question is, why is your (double, double) constructor doing all those calculations?

    The class is a Point, so I wouldn't expect a Point to have a radius, or an alpha (whatever that is).
    i would expect it to be coordinates.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. I want to understand this...
    By WHM2 in forum New To Java
    Replies: 6
    Last Post: 01-14-2014, 09:51 PM
  2. I cant understand OOP
    By eng_hyzoom in forum New To Java
    Replies: 4
    Last Post: 11-19-2010, 04:10 PM
  3. GUI help. Don t understand
    By s0meb0dy in forum AWT / Swing
    Replies: 2
    Last Post: 10-27-2010, 09:40 PM
  4. Trying to understand
    By ladykrimson in forum New To Java
    Replies: 20
    Last Post: 10-12-2010, 11:10 PM
  5. I don´t understand
    By Manikyr in forum New To Java
    Replies: 6
    Last Post: 02-22-2009, 11:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •