阅读以下说明和JAVA 2代码,将应填入(n)处的字句写在对应栏内。
[说明]
以下程序为类类型的变量应用实例,通过异常处理检验了类CCircle的变量的合法性,即参数半径应为非负值。仔细阅读代码和相关注释,将程序补充完整。
[JAVA代码]
//定义自己的异常类
class CCircleException extends Exception
{
}
// 定义类 CCircle
class CCircle
{
private double radius;
public void setRadius ( double r ) (1)
{
if ( r<0 ) {
(2)
}
else
(3)
}
Public void show ( ) {
System. out. println ( "area="+3.14*radius*radius );
}
}
public class ciusample
{
public static void main ( String args[] )
{
CCircle cir=new CCircle( );
(4) {
cir. setRadius ( -2.0 )
}
(5)
{
System. out. println ( e+" throwed" ) ;
}
cir. show( ) ;
}
}
第1题:
阅读以下说明和Java代码,将应填入(n)处的字句写在对应栏内。
【说明】
下面的Java程序演示了程序竞争资源(Mutex的实例对象)而引起程序死锁的一种例子。
【Java程序】
import java.applet.*;
import java.awt.*;
//此处声明一个互斥类
class Mutex { }
class A extends (1)
{
private Mutex first,second;
public A(Mutex f,Mutex s)
{
first = f;
second = s;
}
public void run()
{
//锁定first变量
(2) (first)
{
try
{ //本线程挂起,等待重新调度
Thread.sleep(1); //注意此处(1)不是小题序号
}
catch(InterruptedException e){}
System. out. println("threadA got first mutex");
(2) (second) //锁定second变量
{ //do something
System. out. println("threadA got second mutex");
} //释放second变量
} //释放first变量
}
}
class B extends (1)
{
private Mutex first,second;
public B(Mutex f,Mutex s)
{
(3) ;
second = s;
}
public void run()
{
(2) (second) //锁定second变量
{
//do something
try
{
Thread.sleep(((int)(3*Math.random()))*1000);
//本线程挂起,等待重新调度
}
catch(InterruptedException e){}
System.out.println("threadB got second mutex");
(2) (first) //锁定first变量
{
//do something
System.out.println("threadB got first mutex");
} //释放first变量
} //释放second变量
}
}
public class DeadlockExample
{
public static void main(String arg[])
{
Mutex mutexX = new Mutex();
Mutex mutexY = new Mutex();
AthreadA = new A(mutexX,mutexY);
B threadB = new B (4);
threadA.(5);
threadB.start();
}
}
第2题:
阅读下列Java程序和程序说明,将应填入(n)处的字句写在对应栏内。
【说明】下面的程序先构造Point类,再顺序构造Ball类。由于在类Ball中不能直接存取类Point中的xCoordinate及yCoordinate属性值,Ball中的toString方法调用Point类中的toString方法输出中心点的值。在MovingBall类的toString方法中,super.toString调用父类Ball的toString方法输出类Ball中声明的属性值。
public class Point
{
private double xCoordinate;
private double yCoordinate;
public Point 0 }
public Point(ouble x, double y)
{
xCoordinate = x;
yCoordinate = y;
}
public String toString()
{
return "( + Double.toString(Coordinate)+ ","
+ Double.toString(Coordinate) + ");
}
//other methods
}
public class Ball
{
(1); //中心点
private double radius; //半径
private String colour; ///颜色
public Ball() { }
public Ball(double xValue, double yValue, double r)// 具有中心点及半径的构造方法
{
center=(2);//调用类Point 中的构造方法
radius = r;
}
public Ball(double xValue, double yValue, double r, String c)
// 具有中心点、半径及颜色的构造方法
{
(3);//调用3个参数的构造方法
colour = c;
}
public String toString()
{
return "A ball with center" + center, toString() + ", radius"
+ Double.toString(radius) + ", colour" + colour;
}
//other methods
}
public class MovingBall. (4)
{
private double speed;
public MovingBall() { }
public MovingBall(double xValue, double yValue, double r, String e, double s)
{
(5);// 调用父类Ball中具有4个参数的构造方法
speed = s;
}
public String toString( )
{ return super, toString( ) + ", speed "+ Double.toString(speed); }
//other methods
}
public class Tester{
public static void main(String args[]){
MovingBall mb = new MovingBall(10,20,40,"green",25);
System.out.println(mb);
}
}
第3题:
阅读以下说明和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){
(4);
}
public string tostring(){
return"square:width="+width":";
}
}
public class triangle entends (5)
{
double la;
double lb;
double lc;
public triangle(double la,double lb,double lc){
this.la=la;this.lb=lb;this.lc=lc;
}
public string tostring()(
return"triangle:sides="+la+","+lb+","+lc+":";
}
public double get area(){
double s=(la+lb+lc)/2.0;
return math.sqrt(s*(s-la)*(s-lb)*(s-lc));
}
}
第4题:
阅读以下说明、Java代码和HTML文档,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
当用户启动html浏览器并首次打开下面的HTML文档时,Java Applet小程序在显示面板上显示字符串“Welcome!”;当html页面被其他窗口
遮挡后再次显示时,小程序在显示面板上显示“Welcome back!”。
【Java代码】
import javA.awt.*;
import javA.applet;
public class HelloApplet extends (1) {
public void paim((2)){
g.drawString(message,10,20);
message="welcome back!”;
}
public void (3) (){
message="Welcome!”;
}
private (4) message;
}
【HTML文档】
<html>
<head>
<title>HTML Test HelloApplet Page</title>
</head>
<body>
Test HelloApplet小程序<br>
<applet
codebase="."
code="(5) "
name="TestApplet"
width="400"
height="300"
>
</applet>
</body>
</html>
第5题:
试题八(共15分)
阅读以下说明和Java程序代码,将应填入(n) 处的字句写在答题纸的对应栏内。
[说明]
在下面的 Java 程序代码中,类SalesTicket 能够完成打印票据正文的功能,类HeadDecorator 与FootDecorator 分别完成打印票据的台头和脚注的功能。已知该程序运行后的输出结果如下所示,请填补该程序代码中的空缺。这是票据的台头!这是票据正文!这是票据的脚注!------------------------这是票据的台头!
这是票据的脚注!
[Java程序代码]
public class SalesTicket {
public void printTicket() {
System.out.println("这是票据正文!");
}
}
public class Decorator extends SalesTicket{
SalesTicket ticket;
}
}
public class FootDecorator extends Decorator{
public FootDecorator(SalesTicket t) {
(2) ;
}
public void printTicket() {
super.printTicket();
System.out.println("这是票据的脚注!");
}
}
public class Main {
public static void main(String[] args) {
T = new HeadDecorator( (3) );
T. (4) ;
System.out.println("------------------------");
T = new FootDecorator( (5) );
T.printTicket();
}
}
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();
第6题:
阅读以下说明和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 }
第7题:
阅读以下说明和Java代码,将应填入(n)处的字句写在对应栏内。
【说明】
下面代码里,instr是一个抽象类,它被两个子类wind和rain分别实现,而brass又继承了wind类,并覆盖了play方法。
【程序】
(1) class instr{
(2) Void play();
}
class wind (3) instr{
void play(){System.out.println("wind play!");}
}
class rain extends instr {
void play(){System.out.println("rain play!");}
}
class brass extends (4) {
void play(){System.out.println("brass play!"));}
}
public class music{
static void tuneAll(instr e[]){
for(int i=0;i<e.length;i++)
e[i].play();
}
public (5) void main(String[]args){
instr orch[]=new instr[3];
int i=0;
orch[i++]=new wind();
orch[i++]=new rain();
orch[i++]=new brass();
tuneAll(orch);
}
}
第8题:
阅读以下说明和Java源程序,将应填入(n)处的字句写在对应栏内。
【说明】
以下程序能够计算三角形、矩形和正方形的周长并输出。
程序由5个类组成:AreaTest是主类,类Triangle、Rectangle和Square分别表示三角形、矩形和正方形,抽象类Figure提供了一个计算周长的抽象方法。
【程序】
public class girthTest{
public static void 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[i]+"girth="+figures[i].getGirth());
}
}
}
public abstract class Figure{
public abstract double getGirth();
}
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 getGirth(){
return (2);
}
}
public class Square extends (3) {
public Square(double width){
(4);
}
public Stdng toString(){
return "Square:width='+width+":";
}
}
public class Triangle extends (5) {
double la;
double lb;
double lc;
public Triangle(double la,double lb,double lc){
this.la=la;this.lb=lb;this.lc=lc;
}
public String toString(){
return "Triangle:sides=" +la+"," +lb+"," +lc+":";
}
public double getGirth(){
return la+lab+lc;
}
}
第9题:
阅读以下说明和JAVA2代码,将应填入(n)处的字句写在对应栏内。
【说明】
以下程序实现了利用鼠标任意移动该圆形的位置,仔细阅读代码和相关注释,将程序补充完整。
【代码6】
import java.awt.*;
import java.awt.event.*;
public class CIUSAMPLE extends Frame. implements MouseMotionListener, MouseListener
{
static CIUSAMPLE frm=new CIUSAMPLE ();
int x=70,y=60,posX=70,posY=60,dx,dy;
public static void main ( String args[])
{
frm.setTitle ("Dragging a circle");
frm.setSize ( 200,150 );
(1)
frm.addMouseMotionListener ( frm );
frm. setVisible ( true );
}
public void mousePressed ( MouseEvent e )
{
(2)
dy=e.getY () -posY;
}
public void mouseDragged ( MouseEvent e )
{
(3)
y=e.getY () -dy;
if ( dx>0 && dx<50 && dy>0 && dy<50 ) //如果指我落在正方形区域内
{
Graphics g=getGraphics ();
(4)
}
}
public void paint ( Graphics g )
{
g.setColor ( Color. pink ); // 设置绘图颜色为粉红
g.fillOval ( x,y,50,50 ); //以基准点为图形的左上角绘出圆形
(5)
posY=y;
}
public void mouseMoved ( MouseEvent e ) { }
public void mouseReleased ( MouseEvent e ) { }
public void mouseEntered ( MouseEvent e ) { }
public void mouseExited ( MouseEvent e ) { }
public void mouseClicked ( MouseEvent e ) { }
}
第10题:
阅读以下说明和C++代码,填入(n)处。
[说明]
以下C++代码使用虚函数实现了同一基类shape派生出来的Class rectangle、Class triangle、Class circle实现了计算矩形、圆形面积的计算。仔细阅读以下代码,将(n)处语句补充完整。
[代码5-1]
include<iostream.h>
define PI 3.14159
class shape {//基类
protected:
(1);
public:
(2);
(3);
};
[代码5-2]
class rectangle: public shape {
public:
rectangle (int x2,int y2,int r2): (4) {};
double area ( ) {return x*y; };
};
class circle: public shape {
public:
circle (int x3,int y3,int r3):(5){};
double area ( ) {return r*r*PI; };
};
[代码5-3]
void main ( )
{
rectangle r (10,20,0);
circle c (0,0,30);
shape (6);
cout<<"长方形面积="<<s1->area ( ) <<endl;
cout<<"圆形面积="<<s2->area ( ) <<endl;
}
[运行结果]
长方形面积=200
圆形面积=2827.43