Java面向对象程序设计复习题附答案解析

内容发布更新时间 : 2024/5/18 16:46:09星期一 下面是文章的全部内容请认真阅读。

System.out.println(x*x); } } }

编译运行上面的程序:

(1)从键盘输入10,回车后输出的结果如何?

(2)从键盘输入exit,回车后程序能正确执行吗?为什么? (1)100

(2)不能;因为方法Integer.parseInt(str)不能将字符串“exit”转化为整数,抛出异常。

3. 阅读下面的程序代码

import java.io.* ; public class Test{

public static void main(String args[ ]){ int i , s=0;

int a[ ]={ 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 ,90}; for( i=0 ; i

System.out.println(\ } }

程序执行后,在命令行的输出结果是什么? 270

4. 阅读下面的程序代码 import java.util.*;

public class Example9_14 {

public static void main(String args[]) {

String s=\市话费:28.89元,长途话费:128.87元,上网费:298元。\ String delim = \市话长途话上网费元:,。\

StringTokenizer fenxi=new StringTokenizer(s,delim);//用delim中的字符的任意组合作为分隔标记

double totalMoney=0;

while(fenxi.hasMoreTokens()) {

double money=Double.parseDouble(fenxi.nextToken()); System.out.println(money); totalMoney += money; }

System.out.println(\总费用:\元\ } }

程序执行后,在命令行的输出结果是什么? 市话费:28.89元 长途话费:128.87元, 上网费:298元。 总费用:455.76元

5. 阅读下面的程序代码 import java.util.*;

class Student implements Comparable { int english=0; String name;

Student(int english,String name) { this.name=name; this.english=english; }

public int compareTo(Object b) { Student st=(Student)b;

return (this.english - st.english); } }

public class Example13_8 {

public static void main(String args[]) {

TreeSet mytree=new TreeSet(); Student st1,st2,st3,st4;

st1=new Student(90,\赵一\ st2=new Student(66,\钱二\ st3=new Student(86,\孙三\ st4=new Student(76,\李四\ mytree.add(st1); mytree.add(st2); mytree.add(st3); mytree.add(st4);

Iterator te=mytree.iterator(); while(te.hasNext()) { Student stu=te.next();

System.out.println(\ } } }

程序执行后,在命令行的输出结果是什么? 钱二 66 李四76 孙三86 赵一90

6. 写出下面的程序编译、运行后的结果。 public class Test{

public static void main(String args[]) { new Student(\ new Student(\ new Student(\

System.out.println(\ Student.print(); } }

class Student {

protected String name; protected char sex; protected int chinese; protected int english;

protected Student next; static Student list;

Student (String name, char sex, int chinese, int english) { this.name=name; this.sex=sex; this.chinese=chinese; this.english=english; this.next=list; list=this; }

static void print() { Student friend=list; if (friend==null)

System.out.println(\ else { do{

System.out.println(friend.toString()); friend=friend.next; }while(friend!=null); } }

public String toString() {

return new String(name+\ } }

7. 写出以下程序的功能。

import java.io.*; public class TestFile {

public static void main(String args[]) throws Exception {

BufferedReader br = new BufferedReader(

new InputStreamReader(System.in));

BufferedWriter bw = new BufferedWriter(new FileWriter(“input.txt\ String s; while (true)

{

System.out.print(\请输入一个字符串: \ System.out.flush(); s=br.readLine(); if (s.length()==0) break; bw.write(s); bw.newLine(); } bw.close(); } }

8. 阅读以下程序,写出输出结果。

class Animal { Animal() {

System.out.print (\ \ } }

public class Dog extends Animal { Dog() {

System.out.print (\ }

public static void main(String[] args) { Dog snoppy= new Dog(); }

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4 ceshi