JAVA上机题
- 格式:docx
- 大小:23.70 KB
- 文档页数:18
第二次上机 //定义一个复数类,完成复数的加减运算,并进行测试。 public class Complex //定义复数类 {
float real, image;//定义数据成员,实部、虚部 void setComplex (float real, float image)//方法,为变量成员赋值 { this.real = real; this.image = image; } Complex add(Complex c2)//复数相加 { Complex k = new Complex(); k.real = this.real + c2.real;//this可以省略 k.image= this.image + c2.image; return k; } Complex subtract(Complex c2)//复数相减 {
Complex k = new Complex(); k.real = real - c2.real; k.image= image - c2.image; return k; }
void print()//输出复数 {System.out.print(real); if (image<0) {System.out.print("-");System.out.println(-image+"i");} else {System.out.print("+");System.out.println(image+"i");} } }
class Test//定义测试类 { public static void main(String[] args){ Complex c1 = new Complex(); c1.setComplex(5, -7); Complex c2= new Complex(); c2.setComplex(8, 18); Complex sum = c1.add(c2);//两个复数相加 Complex sub = c1.subtract(c2);//两个复数相加 System.out.print("第一个复数为:"); c1.print();//输出复数 System.out.print("第二个复数为:"); c2.print(); System.out.print("两个复数相加的结果为:"); sum.print(); System.out.print("两个复数相减的结果为:"); sub.print(); } } //定义并实现一个矩形类,有长、宽两个属性(长、宽均设置成private成员),由方法计算矩形的周长和面积,并进行测试。 class Rectangle //定义矩形类 {
private double height, wide;//成员变量 void setValue(double height, double wide )//方法,为数据成员height和wide赋值 {this.height = height; //局部变量与成员变量同名,此时必须使用this来引用成员变量 this.wide = wide;}
void setHeight(double height )//方法,为数据成员height赋值 {this.height = height; }//局部变量与
void setWide(double wide )//方法,为数据成员wide赋值 {this.wide = wide;}
double area(){ //方法,计算面积 return(height * wide); } double size(){ //方法,计算周长 return(2*height + 2*wide);
} void display()//方法,输出数据成员 {System.out.println("矩形高="+height+","+"矩形宽="+wide);} } class Test//定义测试类 {
public static void main(String[] args) { Rectangle r = new Rectangle(); //创建对象,调用相应构造函数 r.setValue(2, 5);//为矩形对象r的宽、高赋值 r.display();//调用方法 System.out.println("面积为:"+r.area()); System.out.println("周长为:"+r.size()); Rectangle r2 = new Rectangle(); //创建对象,调用相应构造函数 r2.setValue(6, 10); r2.display();//调用方法 System.out.println("面积为:"+r2.area()); System.out.println("周长为:"+r2.size()); r2.setWide(18);//调用方法,为矩形r2的宽赋值,即改变r2的宽度 r2.display();//调用方法 System.out.println("面积为:"+r2.area()); System.out.println("周长为:"+r2.size()); } 第三次上机 //定义一个复数类,完成复数的加减运算,并进行测试。要求:定义2个以上的构造函数,并用其完成属性值的初始化。 public class Complex //定义复数类 {
float real, image;//定义数据成员,实部、虚部 Complex() //构造函数 {} Complex(float real,float image) //重载构造函数 {this.real = real; this.image = image;}
void setComplex (float real, float image)//方法,为变量成员赋值 { this.real = real; this.image = image; } Complex add(Complex c2)//复数相加 { Complex k = new Complex(); k.real = this.real + c2.real;//this可以省略 k.image= this.image + c2.image; return k; } Complex subtract(Complex c2)//复数相减 {
Complex k = new Complex(); k.real = real - c2.real; k.image= image - c2.image; return k; }
void print()//输出复数 {System.out.print(real); if (image<0) {System.out.print("-");System.out.println(-image+"i");} else {System.out.print("+");System.out.println(image+"i");} } }
class Test//定义测试类 { public static void main(String[] args){ Complex c1 = new Complex();//用不带参数的构造函数初始化对象c1 c1.setComplex(5, -7);//用setComplex方法给对象的数据成员赋值 Complex c2= new Complex(8,10);//用带2个参数的构造函数初始化对象c2 Complex sum = c1.add(c2);//两个复数相加 Complex sub = c1.subtract(c2);//两个复数相加 System.out.print("第一个复数为:"); c1.print();//输出复数 System.out.print("第二个复数为:"); c2.print(); System.out.print("两个复数相加的结果为:"); sum.print(); System.out.print("两个复数相减的结果为:"); sub.print(); } } //定义并实现一个矩形类,有对角线顶点坐标属性,定义方法计算矩形的周长和面积,并进行测试。要求:定义2个以上的构造函数,并用其完成属性值的初始化。 class Rectangle //定义矩形类 {
private double x1,y1,x2,y2;//成员变量,代表两个顶点坐标 Rectangle()//构造函数 {} Rectangle(double x1, double y1, double x2, double y2)//重载构造函数 {this.x1 = x1; //局部变量与成员变量同名,此时必须使用this来引用成员变量 this.y1 = y1; this.x2 = x2; this.y2 = y2;} void setValue(double x1, double y1, double x2, double y2)//方法,为数据成员赋值 {this.x1 = x1; //局部变量与成员变量同名,此时必须使用this来引用成员变量