Dark Horse Programmer---objective-c basic study---a reflection on exercises after class

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

thinking after a class exercise/* Requirements: Design a class point2d to represent a point in a two-dimensional plane 1> property * Double x* double y 2> method * Property corresponding set and get method * Design an object method simultaneously set X and y* design An object method calculates distance from other points * Design a class method to calculate the distance between two points 3> hint * There is a function in the math.h of the C language: Double pow (double n,double m); There is a function in the math.h of the M-th-C language of the calculation N: Double sqrt (double n); Computes the value of the square root n (opens the roots to N) *//* One, here is my code:
1 #import<Foundation/Foundation.h>2 #import<math.h>3  4 @interfacePoint2d:nsobject5  6 //Point7 {8     Double_x;//the value of x9     Double_y;//Value of yTen } One   A //get and set methods for x -   -- (void) SetX: (Double) x; the- (Double) x; -   - //get and set methods for Y -- (void) Sety: (Double) y; +- (Double) y; -   + //set values for x and y at the same time A- (void) SetX: (Double) x AndY: (Double) y; at   - //calculation and other point distances -- (Double) Distancewithotherpoint: (POINT2D *) p; -   - //calculate the distance between points and points -+ (Double) DistanceBetweenPoint1: (POINT2D *) P1 andPoint2: (POINT2D *) P2; in   - @end //POINT2D Statement to   + //Method - @implementationpoint2d the   *- (void) SetX: (Double) x $ {Panax Notoginseng_x =x; - } the- (Double) x + { A     return_x; the } +- (void) Sety: (Double) y - { $_y =y; $ } -- (Double) y - { the     return_y; - }Wuyi- (void) SetX: (Double) x AndY: (Double) y the { -_x =x; Wu_y =y; -     About      //[self setx:x];//set X process may involve filtering problems, if the value of x does not meet the requirements, we need to deal with $     //[self sety:y]; -       - } -- (Double) Distancewithotherpoint: (POINT2D *) P A {    +     DoubleX1 =[P x]; the     DoubleY1 =[P y]; -     returnsqrt (Pow (_x-x1,2) + POW (_y-y1,2)); $ } the+ (Double) DistanceBetweenPoint1: (POINT2D *) P1 andPoint2: (POINT2D *) P2 the { the     DoubleX1 =[P1 x]; the     DoubleY1 =[P1 y]; -     Doublex2 =[P2 x]; in     Doubley2 =[P2 x]; the     returnsqrt (Pow (x1-x2,2) + POW (y1-y2,2)); the } About   the @end //point2d the   the //Main function + intMain () - { thePOINT2D *PT1 = [point2dNew];BayiPOINT2D *pt2 = [point2dNew]; thePOINT2D *PT3 = [point2dNew]; the     //[pt1 setx:2 andy:2]; -[Pt2 SetX:4AndY:4];  -[Pt3 SetX:9AndY:9]; the     DoubleDistance1 =[PT1DISTANCEWITHOTHERPOINT:PT2]; the     DoubleDistance2 =[Point2ddistancebetweenpoint1:pt2 ANDPOINT2:PT3]; theNSLog (@"The Distande with other point is %f", Distance1); theNSLog (@"The Distande between Point1 and Point2 is%f", Distance2); -     the     return 0; the     the}

Second, the cause of thinkingfirst, how to write a good noteInterpretationI do not pay attention to the comments, university writing code also rarely write comments, do not know how to write, when to write, when should not write, no concept, through this homework can practice how to write good comments, write comments mainly for readability, see good code, see how the teacher is annotated, Develop a habit of writing notes. second, avoid repeatingCode//calculate and other points to distance my method one
1 -(double) Distancewithotherpoint: (POINT2D *) P2{   3     double x1 = [P x]; 4     double y1 = [P y]; 5     return  2) + POW (_y-y1,2)); 6 }

Method Two, because there is a method to find the distance between two points, there are similarities between the two, you need to avoid repeating the code
1 -(double) Distancewithother: (POINT2D *)other2{3     / /  Don't be silly again, call the class method directly 4     return  [point2ddistancebetweenpoint1:self Andpoint2:other]; 5 }

Find common ground between code, write a method, other methods can indirectly callthird, not to substituteCodemore RefinedJaneThe better , sometimes for readability, the code needs to write a little bit moreAsk for a distance between two points, myMethod One
1+ (Double) DistanceBetweenPoint1: (POINT2D *) P1 andPoint2: (POINT2D *) P22 {3     DoubleX1 =[P1 x];4     DoubleY1 =[P1 y];5     Doublex2 =[P2 x];6     Doubley2 =[P2 x];7Returnsqrt (Pow (x1-x2,2) + POW (y1-y2,2));//One line of code resolved but not readable8 //9}

The second method is divided into two steps, clear thinking
1+ (Double) DistanceBetweenPoint1: (POINT2D *) P1 andPoint2: (POINT2D *) P22 {3     //Two-point distance formula: ((x1-x2) squared + (y1-y2) squared) open root4     //x1-x25     DoubleXDelta = [P1 x]-[P2X];6     //(x1-x2) squared7     DoubleXdeltapingfang =pow (XDelta,2);8     //Y1-y29     DoubleYdelta = [P1 y]-[P2Y];Ten     //(y1-y2) squared One     DoubleYdeltapingfang =pow (Ydelta,2); A     returnsqrt (Xdeltapingfang +Ydeltapingfang); -}
IV.compilationProcessExperienceof theproblem///Set both the value of x and the value of Y, method one
1- (void) SetX: (Double) x AndY: (Double) y2 {3_x =x;4_y =y;5}//this does not take into account the problem of filtering that may be involved in setting up X, and if the value of x does not meet the requirements, we need to handle6  7 //Method Two8- (void) SetX: (Double) x AndY: (Double) y9 {Ten[Self setx:x];//This calls the Set method to set the values of x and Y, considering the x filter problem One [self sety:y]; A}

This is my thinking of doing this exercise, welcome criticism!

Dark Horse Programmer---objective-c basic study---a reflection on exercises after class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.