黑马程序员——编程实践
来源:互联网 发布:屠龙宝刀点击就送 知乎 编辑:程序博客网 时间:2024/05/29 03:21
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
1.创建一个 Dog 类,成员变量有,int age; int weight; int height; 分别写三个 set 函数,给三个成员变量赋值。 再写一个 printInformation 函数,打印三个成员变量的值
设计一个点类MyPoint,横坐标为x,纵坐标为y,分别对x,y进行设置和打印。#import <Foundation/Foundation.h>@interface MyPoint : NSObject{ double _x; double _y;}//初始化方法-(id)initWithX:(double)x andY:(double)y;//x的设置方法和获取方法-(void)setX:(double)x;-(double)x;//y的设置方法和获取方法-(void)setY:(double)y;-(double)y;//同时设置x和y的方法-(void)setX:(double)x andY:(double)y;//显示点的值-(void)show;@end#import "MyPoint.h"@implementation MyPoint//初始化方法-(id)initWithX:(double)x andY:(double)y{ if (self=[super init]) { _x=x; _y=y; } return self;}//x的设置方法和获取方法-(void)setX:(double)x;{ _x=x;}-(double)x;{ return _x;}//y的设置方法和获取方法-(void)setY:(double)y;{ _y=y;}-(double)y;{ return _y;}//同时设置x和y的方法-(void)setX:(double)x andY:(double)y;{ _x=x; _y=y;}//显示点的值-(void)show;{ NSLog(@"point x=%f,y=%f",_x,_y);}@end#import <Foundation/Foundation.h>#import "MyPoint.h"int main(int argc, const char * argv[]){ @autoreleasepool { MyPoint *point=[[MyPoint alloc]init]; [point setX:100]; [point setY:200]; [point show]; [point setX:-100 andY:-200]; [point show]; } return 0;}2.设计一个圆类MyCircle,半径为r,创建一个半径为5的圆对象,打印周长和面积。
#import <Foundation/Foundation.h>@interface MyCIrcle : NSObject{ double _r; double _l; double _area;}-(void)setR:(double)r; //andL:(double)l andArea:(double)area;-(void)show;@end************************************************************************************************************ #import "MyCIrcle.h"#define PI 3.1415@implementation MyCIrcle-(void)setR:(double)r;{}-(void)show;{ NSLog(@"l=%lf\narea=%lf",_l=2*PI*_r,_area=PI*_r*_r);}@end************************************************************************************************************#import <Foundation/Foundation.h>#import "MyCIrcle.h"#define PI 3.1415int main(int argc, const char * argv[]){ @autoreleasepool { MyCIrcle *circle=[[MyCIrcle alloc]init]; [circle setR:5]; [circle show]; } return 0;}************************************************************************************************************方法二#import <Foundation/Foundation.h>@interface MyCIrcle : NSObject{ double _r; double _l; double _area;}-(id)initWithR:(double)r;-(void)show;@end************************************************************************************************************#import "MyCIrcle.h"#define PI 3.1415@implementation MyCIrcle-(id)initWithR:(double)r;// andL:(double)l andArea:(double)area;{ if (self=[super init]) { _r=r; } return self;}-(void)show;{ NSLog(@"l=%lf\narea=%lf",_l=2*PI*_r,_area=PI*_r*_r);}@end************************************************************************************************************#import <Foundation/Foundation.h>#import "MyCIrcle.h"#define PI 3.1415int main(int argc, const char * argv[]){ @autoreleasepool { MyCIrcle *anotherCircle=[[MyCIrcle alloc]initWithR:5]; [anotherCircle show]; } return 0;}************************************************************************************************************方法三#import <Foundation/Foundation.h>@interface MyCIrcle : NSObject{ double _r; double _l; double _area;}-(void)setR:(double)r;-(double)r;-(void)show;@end************************************************************************************************************#import "MyCIrcle.h"#define PI 3.1415@implementation MyCIrcle-(void)setR:(double)r;{ _r=r;}-(double)r;{ return _r;}-(void)show;{ NSLog(@"l=%lf\narea=%lf",_l=2*PI*_r,_area=PI*_r*_r);}@end************************************************************************************************************#import <Foundation/Foundation.h>#import "MyCIrcle.h"#define PI 3.1415int main(int argc, const char * argv[]){ @autoreleasepool { MyCIrcle *circle=[[MyCIrcle alloc]init]; [circle setR:5]; [circle show]; } return 0;}
3.有一个长方形MyRect类; 类字段: int length; int width; int area;
方法: 设置长和宽 打印面积 实现这个类的设计
#import <Foundation/Foundation.h>@interface MyRect : NSObject{ int _length; int _width; int _area;}-(void)setLength:(int)length;-(int)length;-(void)setWidth:(int)width;-(int)width;-(void)show;@end#import "MyRect.h"@implementation MyRect-(void)setLength:(int)length;{ _length=length;}-(int)length;{ return _length;}-(void)setWidth:(int)width;{ _width=width;}-(int)width;{ return _width;}-(void)show;{ NSLog(@"area=%d",_width*_length);}@end#import <Foundation/Foundation.h>#import "MyRect.h"int main(int argc, const char * argv[]){ @autoreleasepool { MyRect *rect=[[MyRect alloc] init]; [rect setLength:6]; [rect setWidth:8]; [rect show]; } return 0;方法二#import <Foundation/Foundation.h>@interface MyRect : NSObject{ int _length; int _width; int _area;}-(id)initWithsetLength:(int)length andWidth:(int)width;-(void)show;@end#import "MyRect.h"@implementation MyRect-(id)initWithsetLength:(int)length andWidth:(int)width;{ if (self=[super init]) { _length=length; _width=width; } return self;}-(void)show;{ NSLog(@"area=%d",_width*_length);}@end#import <Foundation/Foundation.h>#import "MyRect.h"int main(int argc, const char * argv[]){ @autoreleasepool { MyRect *antherRect=[[MyRect alloc]initWithsetLength:8 andWidth:6]; [antherRect show]; } return 0;}方法三#import <Foundation/Foundation.h>@interface MyRect : NSObject{ int _length; int _width; int _area;}-(void)setLength:(int)length andWidth:(int)width;-(void)show;@end#import "MyRect.h"@implementation MyRect-(void)setLength:(int)length andWidth:(int)width;{ _length=length; _width=width;}-(void)show;{ NSLog(@"area=%d",_width*_length);}@end#import <Foundation/Foundation.h>#import "MyRect.h"int main(int argc, const char * argv[]){ @autoreleasepool { MyRect *rect = [[MyRect alloc] init]; [rect setLength:9 andWidth:8]; [rect show]; } return 0;}4.设计一个学生类Student,它有属性学号,成绩,性别;分别对这些属性进行赋 值和显示。
#import <Foundation/Foundation.h>//学生类@interface Student : NSObject{ int _sid; double _score; char _gender;}//初始化方法-(id)initWithSid:(int)sid andScore:(double)score andGender:(char)gender;-(void)setSid:(int)sid;-(int)sid;-(void)setScore:(double)score;-(double)score;-(void)setGender:(char)gender;-(char)gender;-(void)setSid:(int)sid andScore:(double)score andGender:(char)gender;-(void)show;@end#import "Student.h"@implementation Student-(id)initWithSid:(int)sid andScore:(double)score andGender:(char)gender;{ if (self=[super init]) { _sid=sid; _score=score; _gender=gender; } return self;}-(void)setSid:(int)sid;{ _sid=sid;}-(int)sid;{ return _sid;}-(void)setScore:(double)score;{ _score=score;}-(double)score;{ return _score;}-(void)setGender:(char)gender;{ _gender=gender;}-(char)gender;{ return _gender;}-(void)setSid:(int)sid andScore:(double)score andGender:(char)gender;{ _sid=sid; _score=score; _gender=gender;}-(void)show;{ NSLog(@"sid=%d score=%lf gender=%c",_sid,_score,_gender);} @end#import <Foundation/Foundation.h>#import "Student.h"int main(int argc, const char * argv[]){ @autoreleasepool { Student *student=[[Student alloc] init]; [student setSid:1408028]; [student setScore:99]; [student setGender:'B']; [student show]; [student setSid:1408029 andScore:98 andGender:'B']; [student show]; Student *anotherStudent=[[Student alloc]initWithSid:12345 andScore:89 andGender:'B']; [anotherStudent show]; } return 0;}5.编写一个分数类:
类名: Fraction
成员变量: 分子,分母(均为整型)
实例方法:
1.初始化方法 带两个整型参数分别代表分子分母
2.带两个参数的设置方法,分别代表分子分母3.打印这个分数
@interface QFFraction : NSObject { int _numerator; //分子 int _denominator; // 分母}- (void) setNumerator:(int)newNumerator;- (int) numerator;- (void) setDenominator:(int)newDenominator;- (int) denominator;// 同时设置分子 分母- (void) setNumerator:(int)newNumerator andDenominator:(int)newDenominator;// 约分- (void) reductionOfFraction;@end************************************************************************************************************#import <Foundation/Foundation.h>@interface MyFraction : NSObject{int _numerator; //分子int _denominator; // 分母}- (void) setNumerator:(int)numerator;- (int) numerator;- (void) setDenominator:(int)denominator;- (int) denominator;// 同时设置分子 分母- (void) setNumerator:(int)numerator andDenominator:(int)denominator;// 约分- (void) reductionOfFraction;@end************************************************************************************************************#import "MyFraction.h"@implementation MyFraction-(void)setNumerator:(int)numerator;{ _numerator=numerator;}-(int)numerator;{ return _numerator;}-(void)setDenominator:(int)denominator;{ _denominator=denominator;}-(int)denominator;{ return _denominator;}// 同时设置分子 分母- (void) setNumerator:(int)numerator andDenominator:(int)denominator;{ _numerator=numerator; _denominator=denominator;}// 约分- (void) reductionOfFraction;{ NSLog(@"%d/%d",_numerator,_denominator);}@end************************************************************************************************************#import <Foundation/Foundation.h>#import "MyFraction.h"int main(int argc, const char * argv[]){ @autoreleasepool { MyFraction *fraction = [[MyFraction alloc] init]; [fraction setNumerator:5]; [fraction setDenominator:9]; [fraction reductionOfFraction]; [fraction setNumerator:4 andDenominator:8]; [fraction reductionOfFraction]; } return 0;}6. 编写一个四则运算器,从终端输入两个数和一个字符,可以进行加减乘除四则运算。(使用类方法)#import <Foundation/Foundation.h>@interface MyComputer : NSObject+(void)setA:(double)a andB:(double)b andC:(char)character;@end#import "MyComputer.h"@implementation MyComputer+(void)setA:(double)a andB:(double)b andC:(char)character{ switch (character) { case '+':NSLog(@"%lf",a+b);break; case '-':NSLog(@"%lf",a-b);break; case '*':NSLog(@"%lf",a*b);break; case '/':NSLog(@"%lf",a/b);break; default:break; }}@end#import <Foundation/Foundation.h>#import "MyComputer.h"int main(int argc, const char * argv[]){ @autoreleasepool { [MyComputer setA:2 andB:4 andC:'+']; [MyComputer setA:2 andB:4 andC:'-']; [MyComputer setA:2 andB:4 andC:'*']; [MyComputer setA:2 andB:4 andC:'/']; } return 0;}
0 0
- 黑马程序员——编程实践
- 黑马程序员— 网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员 — 网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 黑马程序员—网络编程
- 深入理解C++中public、protected及private用法
- 我的2014-告别
- Java nio 学习笔记 相关知识
- Windchill系统配置tomcat热部署
- Android学习笔记之样式和主题之选择器
- 黑马程序员——编程实践
- Eclipse中jsp、js文件编辑时,卡死现象解决汇总
- Android AlertDialog以及Dialog设置视图时区别
- 如何判断C# webbrower页面是否加载完毕方法
- [LeetCode]46.Permutations
- CAB文件打包以及制作证书签名
- Linux中断子系统
- linux定时运行命令脚本——crontab
- jsp传值到后台乱码