阅读以下说明和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 );
}
}
第1题:
阅读下列Java程序和程序说明,将应填入(n)处的字句写在对应栏内。
【说明】StringEditor类的功能是:已知一个字符串,返回将字符串中的非字母字符都删除后的字符串。
public (1) {
public static String removeNonLetters( (2) ){
StringBuffer aBuffer=(3);
char aCharacter;
for(int i=0; i<original.length();i++){
aCharacter=(4);
if(Character.isLetter(aCharacter))
aBuffer.append( (5) );
}
return new String(aBuffer);
}
}
public class StringEditorTester{
public static void main(String args[]){
String riginal="Hi!, My Name is Mark, 234I think you are my classmate?!!";
System.out.println(StringEditor.removeNonLetters(original));
}
}
第2题:
阅读下列Java程序和程序说明,将应填入(n)处的字句写在对应栏内。
【说明】本程序ExceptionTester实现功能:读入两个整数,第1个数除以第2个数,之后输出。若第2个数为0,则自动进行异常处理。
程序如下:
(1);
public class ExceptionTester{
public static void main(String args[]){
int result;
int number[]=new int[2];
boolean valid;
for(int i=0;i<2;i++){
valid=(2);
while(!valid){
try{
System.out.println("Enter number"+(i+1));
number[i]=Integer.valueOf(Keyboard.getString()).intValue();
valid=true;
}catch(NumberFormatExceptione){
System.out.println("Invalid integer entered.Please try again.");
}
}
}
by{
result=number[0]/number[1];
System.out.print(number[0]+"/"+number[1]+"="+result);
}catch((3)){
System.out.println("Second number is 0,cannot do division!");
}
}
}
其中,Keyboard类的声明为:
impon java.io.*;
public class Keyboard{
static BufferedReader inputStream=new (4)
(new InputStreamReader(System.in));
public static int getInteger(){
try{
return(Integer,valueOf(inputStream.readLlne().trim()).intValue());
}catch(Exceptione){
e.printStackTrace();
return 0;
}
}
public (5) {
by{
return(inputStream.readLine());
} catch(IOExceptione)
{return "0";}
}
}
第3题:
下面是一个字符界面的Java Application程序,它接受用户输入的一个浮点数,并将它的整数部分和小数部分分别输出。请勿改动原有代码,在下画线处填人适当语句,将程序补充完整。
import java.io.*;
public class test16_2{
public static void main(String args[]) {
String s;
double d;
int i;
boolean b=false;
do{
try{
System.out.println("请输入一个浮点数:");
BufferedReader br=
new BufferedReader(new lnputStreamReader(System.in));
s=br.readLine();
i=s.indexOf( );
d=__________.parseDouble(s);
System.out.println(d+"整数部分为:"+__________);
if(i==-1)
System.out.println(d+"小数部分为:0.0");
else
System.out.println(d+"小数部分为:"
+Double.parseDouble(
((s.charAt(0)=='-')?"-":"")
+"0."+s.substring(i+1,s.length())));
b=false;
}
catch(NumberFormatException nfe) {
System.out.prinfln("输入浮点数格式有误。\n");
b=true;
}
catch(IOException ioe)
b=false;
}
}while(b);
}
}
第4题:
下列Application程序输入一个整数,求出它的所有因子,请根据注释,将程序补充完整。
public class test19_2 {
public static void main(String args[])throws IOException {
int n;
//下面7行语句的作用是从键盘输入n的值
InputStreamReader ir;
BufferedReader in;
ir=new lnputStreamReader(System.in);
in=new BufferedReader(ir);
System.out.println("Input n is:");
String s=in.______;//读取1行字符
n=Integer.parseInt(s);
//下面求n的所有因子
______(int i=1;i<=n;i++) {
if(n%i!=0)
______;//跳过打印语句回到循环起始
System.out.print(i+””);
}
System.out.print(“\n”);
}
第5题:
阅读以下说明、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();
}
}
}
第6题:
阅读以下说明和Java程序,将应填入(n)处的字句写在对应栏内。
[说明]
下面程序实现十进制向其它进制的转换。
[Java程序]
ClasS Node{
int data;
Node next;
}
class Transform{
private Node top;
public void print(){
Node p;
while(top!=null){
P=top;
if(P.data>9)
System.out.print((char)(P.data+55));
else
System.out.print(p.data);
top=p.next;
}
}
public void Trans(int d,int i){//d为数字;i为进制
int m;
(1) n=false;
Node p;
while(d>0){
(2);
d=d/i;
p=new Node();
if( (3) ){
p.data=m;
(4);
top=P;
n=true;
}
else{
p.data=m;
(5);
top=P;
}
}
}
}
第7题:
阅读以下说明和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)
第8题:
阅读以下说明和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>
第9题:
阅读以下说明和Java代码,将解答写入对应栏内。
【说明】
下面的程序是从命令行输入3个数传递到public static void main(String args[])方法中 (如java IsTriangle 3 4 5),并判断这3个数能否构成三角形的3条边,并显示相应的结果。请在程序的每条横线处填入适当的语句,使程序的功能完整。
注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。
public class IsTriangle{
public static void main( String args[ ]){
int a[] =new (1) [args.(2)];
for(int i=0;i<3;(3))
{
a[i]=(4)
}
if((5))
System. out. println(a[0] +","+a[1] +","+a[2]"能构成三角形的3条边");
else
System. out. println(a[0] +","+a[1] +","+a[2]"不能构成三角形的3条边);
}
}
第10题:
阅读以下说明和Java代码,填补代码中的空缺,将解答填入答题纸的对应栏内。 【说明】 以下Java代码实现两类交通工具(Flight和Train)的简单订票处理, 类Vehicle、Flight、Train之间的关系如图5-1所示。图5-1
【Java代码】 import java.util.ArrayList; import java.util.List; abstract class Vehicle { void book(int n) { //订 n张票 if (getTicket0()>=n) { decrease Ticket(n); } else { System.out.println(“余票不足!!“); } } abstract int getTicket(); abstract void decreaseTicket(int n); }; class Flight (1) { Private (2) tickets=216; //Flight的票数 Int getTicket(){ Return tickets; } void decreaseTicket(int n){ tickets=tickets -n; } } class Train (3) { Private (4) tickets=2016; //Train的票数 int getTicket() { return tickets; } void decreaseticket(int n) { tickets = tickets - n; } } public class Test { public static void main(String[] args) { System.out.println(“欢迎订票 ! "); ArrayList<Vehicle> v = new ArrayList<Vehicle>(); v.add(new Flight()); v.add(new Train()); v.add(new Flight()); v.add(new Train()); v.add(new Train()); for (int i=0;i<v.size(); i++){ (5) (i+1); //订i+1张票 System.out.println(“剩余票数:” +v.get(i).getTicket()); } } } 运行该程序时输出如下: 欢迎订票! 剩余票数:215 剩余票数:2014 剩余票数: (6) 剩余票数: (7) 剩余票数: (8)
第11题:
下列程序使用系统标准输入System.in从键盘获得输入字符串,请选择正确的一项填入下列程序的横线处。 import java.io.*; public class ex26 { public static void main(String args[]) { byte buffer[] = new byte[128]; int n; try { n = for(int i = 0; i < n; i++) System.out .print ( (char)buffer [i] ); catch (IOException e) { System.out.print (e); } } }
A.System.in.read(buffer)
B.system.in.read(buffer)
C.System.in.read0
D.System.in(buffer)
第12题:
第13题:
试题六(共15分)
阅读以下说明和 Java 代码,将应填入 (n) 处的语句或语句成分写在答题纸的对
应栏内。
【说明】
某数据文件 students.txt的内容为100名学生的学号和成绩,下面的程序将文件中的
数据全部读入对象数组,按分数从高到低进行排序后选出排名前 30%的学生。
【Java代码】
import java.io.*;
class Student {
private String sNO; //学号
private int Credit; //分数
public int getCredit(){
return Credit;
}
public String toString() {
return "sNO = " + this.sNO + ", Credit = " + this.Credit;
}
Student(String sNO, int Credit){
(1) = sNO;
(2) = Credit;
}
}
public class SortStudent {
void sort(Student[] s) { //Sort the array s[] in decending order of Credit
for (int i = 0; i < s.length-1; i++) {
for (int j = i+1; j < s.length; j++) {
if (s[i]. (3) < s[j]. (4) ) {
Student tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
}
}
}
public static void main(String argv[]) {
Student[] testStudent = new Student[size];
try {
BufferedReader in = new BufferedReader(new FileReader("students.txt"));
boolean done = false;
int i = 0;
while (!done) {
String s = in.readLine(); //每次读取一个学生的学号和成绩
if (s != null) {
String tmp[] = s.split(",");
testStudent[i++] = (5) (tmp[0], Integer.parseInt(tmp[1]));
} else
done = true;
}
in.close();
(6) = new SortStudent();
ss.sort(testStudent);
System.out.println("top 30%:");
for (int j = 0; j < size * 0.3; j++)
System.out.println(testStudent[j]);
} catch (IOException e) {
System.out.println("io error!");
}catch (NumberFormatException e) {
System.out.println("not a number!");
}
}
(7) int size = 100; //学生总数
}
第14题:
阅读以下说明和Java码,将应填入(n)处的字名写在对应栏内。
[说明] 编写一个字符界面的Java Application 程序,接受用户输入的10个整数,并输出这10个整数的最大值和最小值。
import java. io. * ;
public class abc
{
public static void main(String args [ ] )
{ int i, n = 10 , max = 0 , min = 0 , temp = 0;
try {
BufferedReader br = new BufferedReader(
new InputStreamReader( System. in) );
(1));
} catch ( IOException e ) { } ;
for(i = 2 ;i <= n; i ++ ) {
try {
BufferedReader br = new BufferedReader(
new InputStreamReader (System. in) );
temp = Integer. parselnt(br. readLine( ) );
if ( temp > max ) (2)
if (temp < min) (3)
} catch ( IOExeeption e ) { } ;
System. out. println( "max =" + max + "\nmin =" + min);
}
}
第15题:
阅读以下程序说明和java代码,将应填入(n)处的字句写在对应栏内。
[说明]
本程序接收输入的学生信息,包括学号、姓名、成绩,原样输出信息并计算学生的平均成绩。其中学生类Stud除了包括no(学号)、name(姓名)和grade(成绩)数据成员外,还有两个静态变量 sum和num,分别存放总分和人数,另有一个构造函数、一个普通成员函数disp()和一个静态成员函数avg()用于计算平均分。
[Java代码]
public class Stud {
public int no;
public String name;
public double grade;
public (1) double sum=0;
public static int num=0;
public Stud(int no,String name,double grade) {
this.no = no;
this.name = name;
this.grade = grade;
this.sum=(2);
(3);
}
public static double avg(){
return (4);
}
public void disp(){
System.out.println(this.no+"\t"+this.name+"\t"+this.grade);
}
public static void main(String[] args) {
Stud []students = {new Stud (1,"Li", 81), new Stud(2,"Zhao",84.5), new Stud(3,"Zhang", 87)};
System.out.pfintln("no\tname\tgrade");
students[0].disp();
students[1].disp();
students[2].disp();
System.out.println("avg="+(5));
}
}
第16题:
阅读以下说明和Java代码,回答问题1和问题2,将解答填写在对应栏内。
【Java代码】
class usethread implements (1) {
int num
usethread(int n){
num=n;
}
public void (2) {
for(int i=0;i<3;i++)
System.out.println("running:"+num);
System.out.println("finished:"+num);
}
public class multhread{
public static void main(String args[]) (3) InterruptedException{
Thread m1=new Thread(new usethread(1));
Thread m2=new Thread(new usethread(2));
m1.start();
m2.start();
m1.join();
m2.join();
}
}
【问题1】
补充完整上面Java代码中(n)处。
【问题2】
写出上面Java代码运行的结果。
第17题:
阅读下面写文件的程序 import java. io. *; public class WriteFile{ public static void main(String[]A) { int[]myArray={10,20,30,40}; try{ FileOutputStream f=new FlieOutStream("ints. dat"); DataOutputStream______=new DataOutputStream(f); for(int i=0; i<myArray. length; i ++) dos. writeInt(myArray[i]); dos. close(); System. out. prinfln("Have written binary file ints. dat"); } catch(IOException ioe){ System. out. println("IO Exception"); } } 在程序下画线处填入的正确选项是
A.myArray
B.dos
C.ioe
D.ints
第18题:
阅读以下函数说明和Java代码,将应填入(n)处的字句写在对应栏内。
【说明】
以下程序实现数据的排序,将n个整数分别按照升序和降序进行排序,类SortInt_1实现升序排序,类SortInt_2实现降序排序。
【Java代码】
class SortInt_1{
int i,i,k,temp;
void SortInt(int a1,int a2[]){//升序排序
for(i=0;i<a1-1;i++){
k=i;
for(j=i+1;j<a1;j++){
if((1)) k=j;
if(k !=i){
temp=a2[i];a2[i]=a2[k];a2[k]=temp;
}
}
}
}
}
class SortInt_2 (2) {
int i,j,k,temp;
void SortInt(int a1, int a2[]){//降序排序
for(i=0; i<a1-1;i++){
k=i;
for(j=i+1;j<a1;j++){
if((3))k=j;
}
if(k !=i){
temp=a2[i];a2[i]=a2[k];a2[k]=temp;
}
}
}
}
public class test{
public static void main(String args[]){
int a[]={10,55,100,35,87,90,100,16};
SortInt_1 NewInt=(4);
NewInt.SortInt(a.lenvh,a);//调用SortInt_1类的方法
System.out.println("升序排列的数据: ");
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
System.out.println();
NewInt=new SortInt_2();//创建类SortInt_2的对象
(5);//调用相应方法进行降序排序
System.out.println("降序排列的数据: ");
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}
}
第19题:
阅读以下说明和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));
}
}
第20题:
阅读下列Java程序和程序说明,将应填入(n)处的字句写在对应栏内。
【说明】本程序实现功能:读入两个整数,第1个数除以第2个数,声明当除数为零时抛出异常类DivideByZeroException。
public class DivideByZeroException (1) {
public DivideByZeroException ( ) {
super("Attcmpted to divide by zero");
}
}
import java.io. *;
public class Example {
private static int quotient(int numerator, in)\”}t denominator) throws
DivideByZeroException {
if (denominator==0)
throw (2);
return(numerator / denominator);
}
public static void main(String args[]) {
int number1=0, number2=0, result0;
try{
System.out.print1n("Enter the first number:");
number1 = Integer. valueOf(Keyboard.getString()).intValue();
System.out.print1n("Enter the second number:");
number2 = Integer. Va1ueOf(Keyboard.getString()).intValue();
result = quotient(number1,number2);
}
catch (NumberFormatException e) {
System.out.print1n("Invalid integer entered!");
System. exit(-1);
}
catch ((3)) {
System.out.print1n(e.to String());
System.exit(-1);
}
Systcm.out.pfint1n(number1 + "/" + number2 + "=" + result);
}
}
其中, Keyboard类的声明为:
import java.io.*;
public class Keyboard{
static BufferedReader inputStream =(4)
(new InputStreamReader(System.in));
public static int getInteger() {
try(
return (Intoger.valueOf(inputStream.readLine().trim()).intValue());
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public static String getString() {
try{
return (inputStream.readLine());
} catch ((5))
{ return "0";}
}
}
第21题:
阅读以下说明和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( ) ;
}
}
第22题:
阅读以下说明和 Java 代码,填补代码中的空缺,将解答填入答题纸的对应栏内。 【说明】 设计 RGB 方式表示颜色的调色板,进行绘图。其类图如图 6-1 所示。该程序的 Java代码附后。图6-1 类图
【Java 代码】 //颜色类 class MyColor { private int red ,green, blue; public MyColor( ) { red = o; green = 0; blue = 0; } public MyColor(int red ,int green ,int blue) { this.red = red; this.green = green; this.blue = blue; } //其他方法略 public String toString( ) { return "Red: " + red + "\tGreen: " + green + "\tBlue " + blue; } } //调色板类 class Palette { public int number; / /颜色数 private (1)palette; //颜色表 public Palette( ) { number = 256; palette = new MyColor[number); } public Palette(MyColor[] palette ,int number) { (2)= number; (3)= palette; } //其他方法略 public String toString( ) { String str = ""; for (int i = 0; i < number; i++) { str +=i+ " : " + palette[i] + "\n"; } return str; } //绘图类 class Drawing { public (4) int COLORNUMBER = 16; public static void main(String[] args) { Palette palette; int red ,green ,blue; MyColor[] color = new MyColor[COLORNUMBER]; for (int i = 0; i < COLORNUMBER; i++) { red = (int) (Math.random( ) * 256); green = (int) (Math.random( ) * 256); blue = (int) (Math.random( ) * 256); color [i] = (5) (red ,green ,blue); } palette = new Palette(color ,COLORNUMBER); System.out.println(palette); } }
第23题:
试题七(共 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)