更多"试题八(15 分,每空3 分)
阅读以下说明和Java 源程"的相关试题:
[简答题]
试题八
阅读以下说明和Java 程序代码,将应填入(n) 处的字句写在答题纸的对应栏内。
[说明]
在下面的 Java 程序代码中,类SalesTicket 能够完成打印票据正文的功能,类HeadDecorator 与FootDecorator 分别完成打印票据的台头和脚注的功能。
已知该程序运行后的输出结果如下所示,请填补该程序代码中的空缺。
这是票据的台头!
这是票据正文!
这是票据的脚注!
------------------------
这是票据的台头!
这是票据的脚注!
[Java 程序代码]
public class SalesTicket {
public void printTicket( ) {
System.out.println("这是票据正文!");
}
}
public class Decorator extends SalesTicket{
SalesTicket ticket;
public Decorator(SalesTicket t){
ticket = t;
}
public void printTicket( ){
if(ticket != null)
ticket.printTicket( );
}
}
public class HeadDecorator extends Decorator{
public HeadDecorator(SalesTicket t) {
(1) ;
}
public void printTicket( ) {
System.out.println("这是票据的台头!");
super.printTicket( );
}
}
public class FootDecorator extends Decorator{
[简答题]试题八(15 分,每空3 分)
阅读以下说明和C++程序,将应填入 (n) 处的字句写在答题纸的对应栏内。
[说明]
下面程序的功能是计算并输出某年某月的天数。
[C++程序]
#include
using namespace std;
(1) Month{Jan, Feb, Mar, Apr,May,Jun, Jul, Aug, Sep,Oct,Nov,Dec};
class Date{
public:
Date(int year, Month m_month){
(2) = year;
if(m_month< Jan || m_month > Dec) month = Jan;
else month = m_month;
};
~Date( ){};
bool IsLeapYear( ){
return ((year % 4 == 0 && year % 100 != 0) || year % 400 ==0);
};
int CaculateDays( ){
switch( (3) ){
case Feb:{
if( (4) ) return 29;
else return 28;
}
case Jan: case Mar: case May: case Jul: case Aug: case Oct:
case Dec: return 31;
case Apr: case Jun: case Sep: case Nov: return 30;
}
};
private:
int year;
Month month;
};
void main( ){
Date day(2000,Feb);
cout << day. (5) ( );
}
[简答题]试题六(15 分,每空3 分)
阅读以下说明和C++程序,将应填入(n) 处的字句写在答题纸的对应栏内。
【说明】
以下程序的功能是计算三角形、矩形和正方形的面积并输出。
程序由4 个类组成:类Triangle、Rectangle 和Square 分别表示三角形、矩形和正方形;抽象类Figure 提供了一个纯虚拟函数getArea( ),作为计算上述三种图形面积的通用接口。
【C++程序】
#include
#include
class Figure {
public:
virtual double getArea( ) = 0; // 纯虚拟函数
};
class Rectangle : (1) {
protected:
double height;
double width;
public:
Rectangle( ){};
Rectangle(double height, double width) {
this->height = height;
this->width = width;
}
double getArea( ) {
return (2) ;
}
};
class Square : (3) {
public:
Square(double width){
(4) ;
}
};
class Triangle : (5) {
double la;
double lb;
double lc;
public:
Triangle(double la, double lb, double lc) {
this->la = la; this->lb = lb; this->lc = lc;
}
double getArea( ) {
double s = (la+lb+lc)/2.0;
return sqrt(s*(s-la)*(s-lb)*(s-lc));
}
};
void main( ) {
[填空题]
试题八
阅读以下说明和Java代码。
[说明]
已知类Stock和类JavaMain都定义在JavaMain.java文件中,类Stock的定义中有四处错误,分别在代码的第01、02、06、07行。请修改错误并给出修改后该行的完整代码,并写出改正错误后程序运行的输出结果。
[Java代码]
01 public class Stock{
02 static{
03 shares=0;
04 share_val=0.0;
05}
06 private Stock( )getData( );}
07 private Stock(int n, double pr=0){
08 shares=n;
09 share_val=pr;
10 getData( );
11}
12 public void getData( ){
13 System.out.print(shares+":"+share_val+"");
14}
15 private int shares; //非静态变量
16 private double share_val; //非静态变量
17} ;
18
19 public class JavaMain{
20 public static void main(String args[]){
21 Stock a=new Stock( );
22 Stock b=new Stock(1,67.5);
23 //其它代码省略,且代码无输出
24}
25}
[填空题]阅读以下说明和Java程序,将应填入 (n) 处的字句写在对应栏内
[说明]
以下程序的功能时三角形、矩形和正方形的面积输出。
程序由5个类组成:areatest是主类,类Triangle,Rectangle和Square分别表示三角形、矩形和正方形,抽象类Figure提供了一个计算面积的抽象方法。
[Java程序]
public class areatest {
public static viod main(string args[]){
Figure[]Figures={
New triangle(2,3,3),new rectangle(5,8),new square(5)
};
for(int i=0; i<Figures.length;i++){
system.out.println(Figures+"area="+Figures.getarea( ));
}
}
}
public abstract class figure {
public abstract double getarea( );
}
public class rectangle extends (1) {
double height;
double width;
public rectangle (double height,double width){
this.height=height;
this.width=width;
}
public string tostring( ){
return"rectangle:height="+height+",width="+width+":";
}
public double getarea( ){
return (2)
}
}
public class square exends (3)
{
public square(double width
[填空题]阅读以下说明和C++程序,将应填入 (n) 处的字句写在对应栏内
[说明]
以下程序的功能是计算三角形、矩形和正方形的面积并输出。
程序由4个类组成:类Triangle,Rectangle和Square分别表示三角形、矩形和正方形;抽象类Figure提供了一个纯虚拟函数getArea( ),作为计算上述三种图形面积的通用接口。
[C++程序]
#include<iostream.h>
#include<math.h>
class Figure{
public:
virtual double getArea( )=0; //纯虚拟函数
};
class Rectangle: (1) {
protected:
double height;
double width;
public:
Rectangle( ){};
Rectangle(double height,double width){
This->height=height;
This->width=width;
}
double getarea( ){
return (2) ;
}
};
class Square: (3)
public:
square(double width){
(4) ;
}
};
class triangle: (5) {
double la;
double lb;
double lc;
public:
triangle(double la,double lb,double lc){
this->la=la;thiS->ib;this->lc;
}
double getArea( ){
double s=(la+lb+lc)/2.0;
return sqrt(s*(s-la)
[简答题]试题二(15 分,每空3 分)
阅读下列函数说明和C 代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
[函数说明]
函数sort(int a[],int n)的功能是对数组a 中的a[0]~a[n-1]这n 个元素进行排序。排序过程如下:第一趟对所有的偶数下标p,比较a[p]和a[p+1],若a[p]>a[p+1],则将两者交换;第二趟对所有的奇数下标q,比较a[q]和a[q+1],若a[q]>a[q+1],则将两者交换;第三趟对偶数下标,第四趟对奇数下标,...,依次类推,直至整个数组元素有序排列为止。
[函数]
void sort(int a[],int n)
{
int i,j,t,tag=1;
for(j=0; ja[i+1]) {t=a[i]; a[i]=a[i+1];a[i+1]=t; tag=0;}
}
tag++;
if ( (3) ) break;
}
}
对包含十个元素的数组a 采用上述方法排序时,元素间的比较次数最少为 (4) 次,最多为 (5) 次。
[填空题]阅读以下说明和Java程序,将应填入 (n) 处的字句写在对应栏内。
[说明]
下面程序是为汽车市场编制的一个程序的一部分。其中automobile是基类。
[Java程序]
class MainJava{
public static void main(String agr[]){
car sedan=new car( );
sedan.initialize(24,20.0,4);
System.out.println ("The sedan can travel"
+ (1) +"miles./n");
System.out.println ("The sedan has"
+ (2) +"doors./n");
}
}
class automobile{
protected int miles_per_gallon; //汽车每加仑行驶公里数
(3) double fuel_capacity; //油箱容积
public void initialize(int in_mpg,int in_fuel){
miles_per_galion=in_mpg;
fuel_capacity=in_fuel;
}
public int get_mpg( ){//提供一辆特定汽车每加仑公里数
return miles_per_gallon;
}
public double get_fuel( ){//提供油箱容积
return fuel_capacity;
}
public double travel_distance( ){//计算满油箱情况下的可行驶总公里数
return (4) ;
}
}
class car (5) {
private int Total_doors;
public void initialize(int in_mpg,double in_fuel,int doors){
Total_doors=doors;
miles_per_gallon=in_mpg;
[简答题]
阅读以下说明和C++程序,将应填入(n)处的字句写在对应栏内。
【说明】
以下程序的功能是计算三角形、矩形和正方形的周长并输出。
程序由4个类组成:类Triangle、Rectangle和Square分别表示三角形、矩形和正方形;抽象类
Figure提供了一个纯虚拟函数getGirth( ),作为计算上述3种图形周长的通用接口。
【C++程序】
# include <
iostream. h >
# include < math. h >
class Figure {
public:
virtual double getGirth( ) =0;
//纯虚拟函数 };
class Rectangle: (1) {
protected:
double height;
double width;
public:
Rectangle( ){};
Rectangle( double height, double width)
{
this→height = height;
this→width = width;
}
double getGirth ( ) {
return (2)
;
}
};
class Square: (3)
{
public:
Square( double width) {
(4) ;
} };
class Triangle:
(5) {
double la;
double
lb;
double lc;
public:
Triangle( double la,double lb,double lc){
this→la = la; this→Lb = lb; this→lc = lc;
}
double ge
[简答题]
阅读以下说明和C++程序,将应填入(n)处的字句写在对应栏内。
【说明】以下程序实现了二叉树的结点删除算法,若树中存在要删除的结点,则删除它,否则返回。 FindNode
( )函数能够在二叉树中找到给定值的结点,并返回其地址和父结点。
【C++程序】
template < class T >
void
BinSTree < T >: :Delete( const T& item)
{
TreeNode < T >
* DelNodePtr, * ParNodePtr, * RepNodePtr;
if(( DelNodePtr
= FindNode (item,ParNodePtr)) = = NULL)
(1)
if(DelNodePtr→right = = NULL)
//被删除结点只有一个子结点的情况
RepNodePtr = DelNodePtr→left;
else if( DelNodePtr→left = = NULL)
(2) ;
else
//
被删除结点有两个子结点的情况
{
TreeNode < T >*
PofRNodePtr = DelNodePtr;
RepNodePtr =
DelNodePtr→left;
while(RepNodePtr→right ! =
NULL)
{
//定位左子树的最右结点
PofRNodePtr =RepNodePtr;
RepNodePtr = RepNodePtr→right;
}
if(PofRNodePtr = = DelNodePtr) //左子树没有右子结点
(3) ;
else
//用左子顷的最右结点替换删除的结点
{
(4)