/* 1. Design 2 classes, the relationship between classes (such as inheritance, composition) 1> car (1) Properties * Number of wheels * speed (2) method * Property corresponding set and get method 2> Bus (1) Properties * Number of wheels * speed * Number of seats ( 2) method * Property corresponding set and get method *///bus is a kind of car, so with the inheritance relationship #import <foundation/foundation.h>//car @interface car:nsobject{ int _ Wheels Number of wheels int _speed;//}//Speed getter and setter-(int) speed;-(void) Setspeed: (int) speed;//wheel number getter and setter-(int) wheels;-(void) Setwheels: (int) wheels; @end @implementation car//Speed getter and setter-(int) speed{ return _speed;} -(void) Setspeed: (int) speed{ _speed = speed;} The number of wheels of getter and setter-(int) wheels{ return _wheels;} -(void) Setwheels: (int) wheels{ _wheels = wheels;} @end//Bus @interface bus:car{ int _seats;//number of seats}//the getter and setter-(int) seats;-(void) setseats: (int) seats;@ End@implementation bus//seat Number getter and setter-(int) seats{ return _seats;} -(void) Setseats: (int) seats{ _seats = seats;} @end
/* 2. Design 2 classes, the relationship between classes (such as inheritance, composition) 1> stature data (1) Properties * Height * weight * Hand length * foot Length (2) method * Property corresponding set and get method 2> person (1) attribute * age * Height * weight * Hand length * foot Length (2) method * Property corresponding set and get method *///people have a body data, so with a combination of relationships//figure data @interface bodydata:nsobject{int _height;//Height I NT _weight; weight int _handlength; Hand length int _leglength; Leg length}//height getter and setter-(int) height;-(void) setheight: (int) height;//Weight getter and setter-(int) weight;-(void) Setweight: (int) weight;//hand length getter and setter-(int) handlength;-(void) sethandlength: (int) handlength;// Leg Length getter and setter-(int) leglength;-(void) setleglength: (int) leglength; @end @implementation bodydata// The height of the getter and setter-(int) height{return _height;} -(void) setheight: (int) height{_height = height;} Weight getter and setter-(int) weight{return _weight;} -(void) Setweight: (int) weight{_weight = weight;} Hand length getter and setter-(int) handlength{return _handlength;} -(void) Sethandlength: (int) handlength{_handlength = handlength;} Leg Long getter and setter-(int) leglength{return _Leglength;} -(void) Setleglength: (int) leglength{_leglength = leglength;} @end//person @interface person:nsobject{int _age;//Age bodydata *_bodydata;//Figure Data}//_age setter and getter-(void) SetA GE: (int) age;-(int) age;//_bodydata setter and getter-(void) Setbodydata: (Bodydata *) bodydata;-(Bodydata *) bodydata;@ end//implements @implementation person//_age setter and getter-(void) Setage: (int) age{_age = age;} -(int) age{return _age;} _bodydata setter and getter-(void) Setbodydata: (Bodydata *) bodydata{_bodydata = Bodydata;} -(Bodydata *) bodydata{return _bodydata;} @endint Main () {person *p = [person new]; Set age [P setage:20]; Set Figure data Bodydata *b = [Bodydata new]; [B setweight:60]; [B setheight:170]; [P SETBODYDATA:B]; return 0;}
/* 3. Design 3 classes, the relationship between classes (such as inheritance, composition) 1> person (1) attribute * Name * Age (2) method * Property corresponding set and get method * Design An object method set both name and Age 2> book (1) Property * Title * Publishing House Name * Author (including name and Age) (2) method * Property corresponding set and get method 3> Student * Name * Age * Study number * Book (carry a book with you) 2> method * Property corresponding set and get method * Design an object method-study : Output title *///book has an author--combination//student is a person--inherit//students own a book--combine #import <foundation/foundation.h>//people @interfa Ce person:nsobject{nsstring *_name;//name int _age;//Age}//name getter and setter-(void) SetName: (NSString *) name;-( NSString *) name;//Age getter and setter-(void) Setage: (int) age;-(int) age;//set both name and age-(void) SetName: (NSString *) name Andage: (int) age; getter and setter-(void) SetName of the name of the @end @implementation person//: (NSString *) name{_name = name;} -(NSString *) name{return _name;} Getter and setter-(void) Setage: (int) age{_age = age;} -(int) age{return _age;} Set both name and age-(void) SetName: (NSString *) name andage: (int) age{_name = name; _age = age; /* [self setname:name]; [Self setage:age]; */} @end//Book@interface book:nsobject{nsstring *_name;//title NSString *_publisher;//publisher name person *_author;//author}//title of GE Tter and setter-(void) SetName: (NSString *) name;-(NSString *) name;//Publisher name getter and setter-(void) Setpublisher: (NSString * ) publisher;-(NSString *) publisher;//author's getter and setter-(void) Setauthor: (Person *) author;-(person *) author;@ End@implementation book//title of getter and setter-(void) SetName: (NSString *) name{_name = name;} -(NSString *) name{return _name;} Getter and setter-(void) Setpublisher for the publisher name: (NSString *) publisher{_publisher = publisher;} -(NSString *) publisher{return _publisher;} The author's getter and setter-(void) Setauthor: (person *) author{_author = author;} -(person *) author{return _author;} @end//student @interface student:person{int _no;//study number book *_book;//Books}//study number getter and setter-(void) Setno: (int) no;-(int no;//and setter-(void) Setbook: (Book *) book;-(books *) book;//learning-(void) study; @end @implementation student// The Getter and setter-(void) of the study number Setno:(int) no{_no = no;} -(int) no{return _no;} The Getter and setter-(void) Setbook: (book *) book{_book = books;} -(book *) book{return _book;} Learning-(void) study{NSLog (@ "Now learning Book is:%@", [_book name]);} @end
/** 4. Design Car Class 1> properties * Speed 2> method * Property corresponding set and get method * One object method compare speed with other cars, return speed difference * A class method compares the speed of two vehicles, the return speed difference */#import <found ation/foundation.h>//car @interface car:nsobject{ int _speed;//Speed}//Speed getter and setter-(void) Setspeed: (int) speed;-(int) speed;//compared to other cars, return speed difference-(int) Comparespeedwithother: (car *) car;//compare two cars speed, return speed difference + (int) COMPARESPEEDBETWEENCAR1: (Car *) car1 andCar2: (car *) car2; @end @implementation car//Speed getter and setter-(void) Setspeed: ( int) speed{ _speed = speed;} -(int) speed{ return _speed;} Compare speed with other cars, return velocity difference-(int) Comparespeedwithother: (Car *) car{ //1th idea //return _speed-[Car speed]; 2nd Way return [Car comparespeedbetweencar1:self andcar2:car];} Compare the speed of the two cars and return the difference in velocity + (int) compareSpeedBetweenCar1: (Car *) car1 andCar2: (Car *) car2{ return [car1 speed]-[car2 speeds];} @end
/** 5. A class point2d is designed 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 to set X and Y * Design an object method to calculate with its The distance he 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); Calculates the value of the square root n (open roots to N) */#import <Foundation/Foundation.h> #import <math.h>//point @interface point2d:nsobject{Doub Le _x; x value double _y; The Y-value}//x-Value getter and setter-(void) SetX: (double) x;-(double) x;//y-value getter and setter-(void) Sety: (double) y;-(double) y;// Set both x and Y (void) SetX: (double) x AndY: (double) y;//calculates the distance from other points-(double) Distancewithother: (POINT2D *) other;//calculates the distance between two points + (double) DistanceBetweenPoint1: (POINT2D *) P1 andPoint2: (POINT2D *) p2; @end @implementation point2d//X-Value getter and setter- (void) SetX: (double) x{_x = x;} -(double) x{return _x;} Getter and setter-(void) sety for Y-values: (double) y{_y = y;} -(double) y{return _y;} Set both x and Y (void) SetX: (double) x AndY: (double) y{//1th idea//_x = x; _y = y; The 2nd Way of thinking[Self setx:x]; [Self sety:y];} Calculate distance from other points-(double) Distancewithother: (POINT2D *) other{//Don't be silly again, call the class method directly to return [POINT2D Distancebetweenpoi Nt1:self Andpoint2:other];} Calculates the distance between two points + (double) DistanceBetweenPoint1: (POINT2D *) P1 andPoint2: (POINT2D *) p2{//Two point distance formula: ((x1-x2) squared + (y1-y2) squared ) open root//x1-x2 double XDelta = [P1 x]-[P2 x]; (x1-x2) square Double Xdeltapingfang = POW (XDelta, 2); Y1-y2 double Ydelta = [P1 y]-[P2 y]; (y1-y2) square Double Ydeltapingfang = POW (Ydelta, 2); return sqrt (Xdeltapingfang + Ydeltapingfang);} @endint Main () {point2d *P1 = [point2d new]; [P1 setx:10 andy:10]; POINT2D *P2 = [point2d new]; [P2 setx:13 andy:14]; Double D1 = [P1 distancewithother:p2]; Double D2 = [point2d distancebetweenpoint1:p1 andpoint2:p2]; NSLog (@ "d1=%f, d2=%f", D1, D2); return 0;}
/** 6. Design a class circle that represents a circle in a two-dimensional plane 1> property * Double radius (RADIUS) * point2d *point (center) 2> Method * Property corresponding set and get method * Design An object method to judge with Whether the other circles intersect (overlapping returns Yes, otherwise returns NO) * Design a class method to determine whether two circles intersect (overlap returns Yes, otherwise returns NO) */#import <Foundation/Foundation.h> #import < math.h>//Point @interface point2d:nsobject{double _x;//x value double _y;//Y value}//x value getter and setter-(void) SetX: (Doub Le) x;-(double) x;//y value getter and setter-(void) Sety: (double) y;-(double) y;//set both x and Y (void) SetX: (double) x AndY: (Double) y;//calculates the distance from the other points-(double) Distancewithother: (POINT2D *) other;//calculates the distance between two points + (double) DistanceBetweenPoint1: (POINT2D *) P1 AndPoint2: (POINT2D *) p2; @end @implementation point2d//X-Value getter and setter-(void) SetX: (double) x{_x = x;} -(double) x{return _x;} Getter and setter-(void) sety for Y-values: (double) y{_y = y;} -(double) y{return _y;} Set both x and Y (void) SetX: (double) x AndY: (double) y{//1th idea//_x = x; _y = y; 2nd idea [self setx:x]; [Self sety:y];} Calculate distance to other points-(double) Distancewithother: (POINT2D *) other{// Don't be silly again, call the class method directly to return [POINT2D distancebetweenpoint1:self andpoint2:other];} Calculates the distance between two points + (double) DistanceBetweenPoint1: (POINT2D *) P1 andPoint2: (POINT2D *) p2{//Two point distance formula: ((x1-x2) squared + (y1-y2) squared ) open root//x1-x2 double XDelta = [P1 x]-[P2 x]; (x1-x2) square Double Xdeltapingfang = POW (XDelta, 2); Y1-y2 double Ydelta = [P1 y]-[P2 y]; (y1-y2) square Double Ydeltapingfang = POW (Ydelta, 2); return sqrt (Xdeltapingfang + Ydeltapingfang);} @end//Round @interface circle:nsobject{double _radius;//Radius point2d *_point;//The Getter and}//of the center setter-radius (void) Setrad IUS: (double) radius;-(double) radius;//center of Getter and setter-(void) SetPoint: (POINT2D *) point;-(POINT2D *) point;// Overlap with other circles (return Yes if overlapping)-(BOOL) Isinteractwithother: (Circle *) other;//determine if two circles overlap (overlapping returns Yes, otherwise returns NO) + (BOOL) IsInteractBetweenCircle1: (Circle *) Circle1 andCircle2: (Circle *) circle2; @end @implementation circle// Getter of radius and setter-(void) Setradius: (double) radius{_radius = radius;} - (double) radius{return _radius;} Getter and setter-(void) SetPoint of the Center: (POINT2D *) point{_point = point;} -(POINT2D *) point{return _point;} overlaps with other circles (returns Yes, otherwise returns NO)-(BOOL) Isinteractwithother: (Circle *) other{return [Circle Isinteractbetweencircle1:self Andcircle2:other];} Determines whether two circles overlap (overlapping returns Yes, otherwise returns NO) + (BOOL) IsInteractBetweenCircle1: (Circle *) Circle1 andCircle2: (Circle *) circle2{// If the distance of two centers >= two circle radius and does not overlap//If the distance of two centers < two circle radius and, then overlap//Two Center point2d *point1 = [Circle1 Point]; POINT2D *point2 = [Circle2 Point]; Distance of two centers double distance = [Point1 Distancewithother:point2]; Radius and double radiussum = [Circle1 radius] + [circle2 radius]; return distance < Radiussum;} @endint Main () {Circle *c1 = [Circle new]; Set radius [C1 setradius:2]; Set Center point2d *p1 = [point2d new]; [P1 setx:10 andy:10]; [C1 SETPOINT:P1]; Circle *C2 = [Circle new]; Set radius [C2 setradius:2]; Set Center point2d *p2 = [PoINT2D new]; [P2 setx:13 andy:14]; [C2 SETPOINT:P2]; The center distance is 5 radius and is 4 so does not overlap BOOL B1 = [C1 isinteractwithother:c2]; BOOL b2 = [Circle isinteractbetweencircle1:c1 andcircle2:c2]; NSLog (@ "%d%d", B1, B2); return 0;}
Class-Practice-Total