问题 填空题

阅读以下说明和Java程序,将应填入 (n) 处的字句写在对应栏内。
[说明]
下面程序输出一个矩形面积,以及矩形区域上的假想的作物产量。
[Java程序]
public class MainJava
public static void main(String[] args)
Lot_size small=new Lot_size();
Lot_size medium=new Lot_size();
small.set(5,5,5,25);
medium.set(10,10,10,50);
System.out.println("For a small lot of area"
+small.get_area()+"\n");
System.out.println("the actual crops are $"
+small.get_data2()+"\n");
System.out.println("and ideal crops are $"
+small.get data()+"\n");
System.out.println("For a medium lot of area"
+medium.get_area()+“\n”);
System.out.println("the actual crops are $"
+medium.get_data2()+"\n");
System.out.println ("and ideal crops are $"
+medium.get_data()+"\n");
class Crop_assessment
private int actual_crop;
private int ideal_crop;
public void set(int in_actual,int in ideal)
actual_crop=in_actual;
ideal_crop=in_ideal;

public int get_actual_crop()return (1) ;
public int get_ideal_crop()(return (2) ;class Lot_size
private int length;
private int width;
private Crop_assessment crop= (3) ;
public void set(int 1,int W,int a,int i)
length=1;
width=W;
crop.set(a,i);

public int get_area()return length*width;
public int get_data()freturn (4) ;
public int get_data2()(return (5) ;

答案

参考答案:return actual_crop (2) return ideal_crop (3) new Crop_assessment()
(4) crop.get_ideal_crop (5) crop.get_actual_crop()

解析:本题以Java语言为载体,考查面向对象程序设计中的几个重要概念—类的嵌套及函数的使用。
本题的功能是通过已定义的粮食收成类定义了一个计算特定矩形域上粮食收成的类,在主函数中定义了两个对象,并调用了相应的函数,来输出理想和实际的粮食产量。
首先,由于Crop assessment定义的成员数据缺省为私有的,所以想要获得实际和理想的粮食产量,要通过两个公有成员函数,所以(1)处应填入“return actual_crop”,(2)处应填入“return ideal_crop”。
其次,在类Lot size中,由于我们要反映矩形域上的粮食产量,所以我们在类中嵌套定义了一个类,Java中对象需要实例化,故(3)应填入“new Crop_assessment()”。
最后,由于我们想通过Lot_size类中的get_data和get_data2函数得到粮食产量,但由于这两个成员数据是私有函数,所以我们必须通过定义的对象调用它,所以(4)应填入“crop.get_ideal_crop()”,(5)应填入“crop.get_actual_crop()”。

单项选择题
单项选择题