习题与思考java实用编程技术_JAVA编程技术复习题

三、程序填空(每空2分,共 20分)

程序一:如下程序测试Math.random生成随机数的奇偶比率,仔细阅读程序和运行结果,补全空白处的代码。

public class T25 {

int[] createArray(int count){

int number[]=(25);//创建长度为count的int数组

for(int i=0;i

int n=(int)(Math.random()*1000);

number[i]=(26);//在number数组中写入生成的随机数

System.out.println("number["+i+"]="+number[i]);

}

return(27);//返回生成的数组

}

double calculateOddRate(int[] number){

int count=(28);//读取数组元素的个数,即要计算平均数的整数个数

double odd=0;//奇数计数

for(int n:number){

if((29)){//如果n是奇数,奇数计数加1

odd++;

}

}

return odd/count;

}

public static void main(String[] args) {

T25 t=new T25();

int[] number=t.createArray(100);

double oddRate=t.calculateOddRate(number);

System.out.println("奇数为:"+oddRate*100+"%");

System.out.println("偶数为:"+(1-oddRate)*100+"%");

}

}

运行结果:

number[0]=907

…..//此处省略98行

number[99]=598

奇数为:52.0%

偶数为:48.0%

程序二:以下程序是通过JDBC读取数据表Student的基本操作,认真阅读程序和运行结果,补全程序的空白处。

表:Students

ID

NAME

GENDER

2

name02

4

name04

部分程序如下

class Student{

private int id;

private String name;

private String gender;

public Student(int id, String name, String gender) {

super();

this.id = id;

this.name = name;

this.gender = gender;

}

…………//此处省略n行

public String toString() {

return "Student [id=" + id + ", name=" + name + ", gender=" +

gender+ "]";

}

}

public class T30 {

Connection getConnection(){

……//此处省略n行

}

List queryAllStudent(){

List stuList=(30);//创建可以存储Student的List

Connection conn=null;

Statement st=null;

ResultSet rs=null;

try {

conn=getConnection();

st=(31).createStatement();//通过连接创建statement

rs=st.executeQuery("SELECT ID,NAME,GENDER FROM

Students");

while((32)){//结果是否有记录

Student stu=new

Student(

rs.getInt("ID"),

rs.getString("NAME"),

rs.getString("GENDER"));

(33);//把stu对象加入到stuList中

}

} catch (SQLException e) {

e.printStackTrace();

}finally{

try {

rs.close();

st.close();

conn.close();

} catch (SQLException e) {}

}

return stuList;

}

void showStudent(List stuList){

for(______(34)_______s:stuList){//指明s的类型

System.out.println(s);

}

}

public static void main(String[] args) {

T30 demo=new T30();

List stuList=demo.queryAllStudent();

demo.showStudent(stuList);

}

}

运行结果

Student [id=2, name=Name02, gender=女]

Student [id=4, name=Name04, gender=女]

四、基本代码编写(共12分)

35、(5分)编写一个main方法,计算如下数组元素的平均值

doublesource[]={2,5,9,10,3};

36、(7分)文件名解析器,仔细阅读如下代码和运行结果,完成WindowsFileNameParse类的代码,执行后得到给定的运行结果。

interface FileNameParse{

void showSourceFileName();

String getDiskName();

String getFullFileName();

String getFileName();

String getExtendName();

String getDir();

}

class WindowsFileNameParse implements FileNameParse{

private String fileName;

WindowsFileNameParse(String fileName){

this.fileName=fileName;

}

public void showSourceFileName(){

System.out.println("解析文件名:"+this.fileName);

}

//

//请完成此类的中其他方法的代码

}

public class T36 {

public static void main(String[] args) {

FileNameParse fp=new WindowsFileNameParse("d:/My

Documents/MyJob/Pages/2012-2013-2/PageA/src/T37.java");

fp.showSourceFileName();

System.out.println("盘符:"+fp.getDiskName());

System.out.println("文件全名(带扩展名):"+fp.getFullFileName());

System.out.println("文件名(不带扩展名):"+fp.getFileName());

System.out.println("文件扩展名:"+fp.getExtendName());

System.out.println("路径(不带盘符):"+fp.getDir());

}

}

运行结果

解析文件名:d:/My

Documents/MyJob/Pages/2012-2013-2/PageA/src/T37.java

盘符:d

文件全名(带扩展名):T37.java

文件名(不带扩展名):T37

文件扩展名:java

路径(不带盘符):/My

Documents/MyJob/Pages/2012-2013-2/PageA/src

附 String类部分的api

doc

public int indexOf(str)

Returns the index within this string of the first occurrence of the

specified substring.

Examples:

"abca".indexOf("a")return

0

Parameters:

str - the substring to search for.

Returns:

the index of the first occurrence of the specified substring, or -1

if there is no such occurrence.

public int lastIndexOf(str)

Returns the index within this string of the last occurrence of the

specified substring. The last occurrence of the empty string "" is

considered to occur at the index value this.length().

Examples: "abca".lastIndexOf("a")return

3

Parameters:

str - the substring to search for.

Returns:

the index of the last occurrence of the specified substring, or -1

if there is no such occurrence.

public substring(int

beginIndex)

Returns a new string that is a substring of this string. The

substring begins with the character at the specified index and

extends to the end of this string.

Examples:

"Harbison".substring(3) returns "bison"

"emptiness".substring(9) returns "" (an empty string)

Parameters:

beginIndex - the beginning index, inclusive.

Returns:

the specified substring.

public substring(int

beginIndex, int endIndex)

Returns a new string that is a substring of this string. The

substring begins at the specified beginIndex and extends to the

character at index endIndex - 1. Thus the length of the substring

is endIndex-beginIndex.

Examples:

"hamburger".substring(4, 8) returns

"urge"

"smiles".substring(1, 5) returns

"mile"

Parameters:

beginIndex - the beginning index, inclusive.

endIndex - the ending index, exclusive.

Returns:

the specified substring.

五、设计并编程(共8分)

37、仔细阅读给定的代码和程序运行结果,完方法size()、del()代码编写。

MyList类是可以存储字符串对象的、基于链表的List的简单实现

class MyListNode {

String element;

MyListNode nextNode = null;

MyListNode(String element) {

this.element = element;

}

}

class MyList {

private MyListNode firstNode = null;

public void add(String element) {//加入字符串到MyList中

MyListNode node = new MyListNode(element);

if (firstNode == null) {

firstNode = node;

} else {

MyListNode lastNode = firstNode;

while (lastNode.nextNode != null) {

lastNode = lastNode.nextNode;

}

lastNode.nextNode = node;

}

}

public int size() {//返回MyList中节点数

//完成此方法代码

}

public String[] toArray() {//将MyList中存储的所有字符串转化成String[]

int count = size();

if (count == 0) {

return null;

}

String[] dest = new String[count];

MyListNode lastNode = firstNode;

int i = 0;

do {

dest[i++] = lastNode.element;

lastNode = lastNode.nextNode;

} while (lastNode != null);

return dest;

}

public void del(String element) {//删除节点元素值为element字符串的节点

///完成此方法代码/

}

}

public class T37 {

public static void main(String[] args) {

MyList myList = new MyList();

myList.add("s001");

myList.add("s002");

myList.add("s003");

myList.add("s004");

myList.add("s005");

System.out.println("SIZE:" + myList.size());

String sa1[] = myList.toArray();

showArray(sa1);

myList.del("s001");

myList.del("s003");

myList.del("s005");

System.out.println("SIZE:" + myList.size());

String sa2[] = myList.toArray();

showArray(sa2);

}

static void showArray(String[] sa) {

System.out.print("[");

for (String s : sa) {

System.out.print(s + "  ");

}

System.out.println("]");

}

}

运行结果

SIZE:5[s001

s002  s003

s004  s005

]

SIZE:2

[s002  s004  ]

答案:

一、选择题

1、B

2、C

3、C

4、A5、D

6、C

7、B

8、A9、B

10、D11、C 12、C13、D

14、C 15、A 16、B17、A

18、B19、D

20、C

二、填空题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值