题目
阅读以下说明和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 Application 程序,接受用户输入的10个整数,并输出这10个整数的最大值和最小值。
[Java 代码]
import java. io.* ;
public class abc
{
public static void main ((1))
{int i, n=10, max=0, min=0, temp=0;
try {
BufferedReader br = new BufferedReader (
new InputStreamReader (System.in ) );
max = min =Integer. parselnt (br. readLine () );
}. (2) (IOExccption e ) {} ;
for (i=2 ; i<=n ; i++ ) {
try {
BufferedReader br = new (3) (
new InputStreamReader (System. in ) );
temp =(4) (br. readLine ());
if (temp>max ) max=temp;
if (temp<min) (5)
} catch (IOException e ) {};
}
System.out.println ("max="+max+"\nmin="+min );
}
}
第2题
阅读以下说明、Java代码,将应填入(n)处的字句写在对应栏内。
【说明】
本程序输出10000之内的所有完全数。完全数是指等于其所有因子和(包括1,但不包括这个数本身)的数。例如:6=1×2×3,6=1+2+3,则6是一个完全数。
【程序】
public class PerfectNum
{
Public static void main(String args[])
{
int count=1;
for(int i=1; i<10000; i++)
{
int y=0;
for(int j=1; j<i; j++)
if((1))
y=(2)
if((3))
{
System.out.print((4) +String.valueOf('\t'));
(5)
If(count%3==0)
System.out.printin();
}
}
}
第3题
阅读以下说明和Java代码,将应填入(n)处的字句写在对应栏内。
【说明】
java.util库中提供了Vector模板类,可作为动态数组使用,并可容纳任意数据类型。该类的部分方法说明如下表所示:
【Java代码】
import (1);
public class JavaMain {
static private final int (2)= 6;
public static void main(String[] args){
Vector<Integer> theVector = new Vector< (3) >();
// 初始化 theVector, 将theVector的元素设置为0至5
for (int cEachItem = 0; cEachItem < ARRAY_SIZE; cEachItem++)
theVector.add((4));
showVector(theVector); // 依次输出theVector中的元素
theVector.removeElementAt(3);
showVector(theVector);
}
public static void showVector(Vector<Integer> theVector
if (theVector.isEmpty()) {
System.out.println("theVectcr is empty.");
return;
}
for (int loop = 0; loop < theVector.size(); loop++)
System.out.print(theVector.get(loop));
System.out.print(", ");
}
System.out.println();
}
}
该程序运行后的输出结果为:
0,1,2,3,4,5
(5)
第4题
阅读以下说明和Java代码,将应填入(n)处的字句写在对应栏内。
【说明】
下面程序完成从键盘读入一个字符串,然后采用parseInt方法将其转换为一个相应的整数。
import java.io.*;
public class testThrows{
public static (1) readString() (2) IOException{
int ch;
String r="";
boolean done=false;
while((3)){
ch=System.in.read();
if(ch<0 || ch=0xd) //处理回车符中第一个符号
done=true;
else
r=r+(char)ch;
}
return r;
}
public static void main(Stling args[]){
String str;
(4) {
str=readString();
} (5) (IOException e){
System.out.println("error");
return;
}
System.out.println("input integer: "+Integer.parselnt(str));
}
}
第5题
阅读以下说明、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>
第6题
阅读以下说明和Java以及HTML代码,将应填入(n)处的字句写在对应栏内。
[说明]
编写一个Applet程序,接受HTML文件传递的整数参数,根据该参数指定Applet中文本框的长度。编写对应的HTML文件运行这个Applet。
[Java 代码]
import java. applet.*;
import java. awt.*;
public class chuangdics extends (1)
{
TextField tf=new TextField ();
int tfLength=0;
public void init()
{
try
{
tfLength=Integer. parseInt((2) ("length"));
tf. setColumns (tfLength);
add (tf);
}
catch (NumberFormatException nfe)
{
tf. (3) ("HTML 文件传人的参数格式错误。");
add(tf);
}
}
}
[HTML 代码]
<html>
<head>
<title>(4)</title>
</head>
<body>
<hr>
<applet code=chuangdics width=700 height=150>
<param name=length value=20>
(5)
<hr>
</body>
</htmI>
第7题
试题七(共 15 分)
阅读以下说明和 Java 代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
[说明]
java.util 库中提供了 Vector 模板类,可作为动态数组使用,并可容纳任意数据类型。该类的部分方法说明如下表所示:
[Java 代码]
import (1) ;
public class JavaMain {
static private final int (2) = 6;
public static void main(String[] args){
Vector<Integer> theVector = new Vector< (3) >();
// 初始化 theVector,将 theVector的元素设置为 0 至 5
for (int cEachItem = 0; cEachItem < ARRAY_SIZE; cEachItem++)
theVector.add((4) );
showVector(theVector); // 依次输出 theVector中的元素
theVector.removeElementAt(3);
showVector(theVector);
}
public static void showVector(Vector<Integer> theVector){
if (theVector.isEmpty()) {
System.out.println("theVector is empty.");
return;
}
for (int loop = 0; loop < theVector.size(); loop++) {
System.out.print(theVector.get(loop));
System.out.print(", ");
}
System.out.println();
}
}
该程序运行后的输出结果为:
0, 1, 2, 3, 4, 5
(5)
第8题
阅读以下说明、Java代码和HTML文档,将应填入(n)处的字句写在对应栏内。
【说明】
当用户启动html浏览器并首次打开下面的html文档时,Java Applet小程序在显示面板上显示字符串“欢迎您!”;当html页面被其他窗口遮挡后再次显示时,小程序在显示面板上显示“欢迎您回来!”
[Java代码]
import java.awt.*;
import java.applet.*;
public class HelloApplet extends (1) {
public void paint((2) ){
g.drawString(message,10,20);
message="欢迎您回来!";
}
public void (3) (){
message="欢迎您!";
}
private (4) message;
}
[HTML文档]
<html>
<head>
<title>HTML Test HelloApplet Page</title>
</head>
<body>
Test HenoApplet 小程序<br>
<applet
codebase="."
code="(5)"
name="TestApplet"
width="400"
height="300"
>
</applet>
</body>
</html>
第9题
阅读以下说明和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);
}
}
第10题
() 阅读下列说明和JAVA代码,将应填入空(n)处的字句写在答题纸的对应栏内。【说明】 某中学开展中外中学生野外生存夏令营活动,由于中外学生的语言障碍,随队为外籍学员配置一名翻译。以下代码采用适配器(Adapter)模式模拟翻译适配器。其类图如下:
为了保护您的账号安全,请在“赏学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!