JAVA 开发实战经典第六章课后习题答案
- 格式:pdf
- 大小:90.94 KB
- 文档页数:9


java开发实战经典课后习题答案
Java开发实战经典课后习题答案
Java是一种广泛应用于软件开发领域的编程语言,它的特点是简单易学、面向对象、跨平台等。对于学习Java开发的人来说,掌握实战经典课后习题的答案是提高编程能力的重要途径。本文将为大家提供一些Java开发实战经典课后习题的答案,希望能够帮助读者更好地理解和掌握Java编程。
一、基础题目
1. 编写一个Java程序,输出"Hello, World!"。
```java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
```
2. 编写一个Java程序,计算1到100之间所有奇数的和。
```java
public class SumOfOddNumbers {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i += 2) {
sum += i; }
System.out.println("1到100之间所有奇数的和为:" + sum);
}
}
```
3. 编写一个Java程序,判断一个数是否为素数。
```java
public class PrimeNumber {
public static void main(String[] args) {
int num = 17;
boolean isPrime = true;
for (int i = 2; i <= Math.sqrt(num); i++) {
1
6 泛型与集合
6.1 单项选择题
1.可实现有序对象的操作是?( )
A.HashMap B.HashSet C.TreeMap D.Stack
2.不是迭代器接口(Iterator)所定义的方法是( )。
A.hasNext() B.next()
C.remove() D.nextElement()
3.下面说法不正确的是( )
A.列表(List)、集合(Set)和映射(Map)都是java.util包中的接口。
B.List接口是可以包含重复元素的有序集合。
C.Set接口是不包含重复元素的集合。
D.Map接口将键映射到值,键可以重复,但每个键最多只能映射一个值。
4.下面那些方法不是接口Collection中已声明的方法( )
A.添加元素的add(Object obj) 方法
B.删除元素的remove(Object obj)方法
C.得到元素个数的length()方法
D.返回迭代器的iterator()方法,迭代器用于元素遍历
5. 下列关于容器的描述中,错误的是 ( )
A.容器是由若干个组建和容器组成的
B.容器是对图形界面中界面元素的一种管理
C.容器是一种对指定宽和高的矩形范围
D.容器都是可以独立的窗口
6. 下列界面元素中,不是容器的是 ( )
A.List B.JFrame C.JDialog D.Panel
7. 应用程序的main方法中有以下语句,则输出的结果是 ( )。 2
Hashtable hashtable=new Hashtable();
hashtable.put("100","aaa");
hashtable.put("200","bbb");
hashtable.put("300","ccc");
《Java语言程序设计:基础篇》课后复习题答案-第六章
Chapter6Single-dimensional Arrays
1.See the section"Declaring and Creating Arrays."
2.You access an array using its index.
3.No memory is allocated when an array is declared.The
memory is allocated when
creating the array.
x is60
The size of numbers is30
4.Indicate true or false for the following statements:
1.Every element in an array has the same type.
Answer:True
2.The array size is fixed after it is declared.
Answer:False
3.The array size is fixed after it is created.
Answer:True
4.The element in the array must be of primitive data type.
Answer:False
5.Which of the following statements are valid array
declarations?
int i=new int(30);
Answer:Invalid
double d[]=new double[30];
Answer:Valid
char[]r=new char(1..30);
Answer:Invalid
int i[]=(3,4,3,2);
Answer:Invalid
float f[]={2.3, 4.5, 5.6}; Answer:Valid
import java.util.Date;
import java.text.SimpleDateFormat;
public class T6_1 {
public static void main(String[] args) {
Date d = new Date();
SimpleDateFormat df = new SimpleDateFormat("E\tH\tm\ts");
System.out.println("星期\t小时\t分钟\t秒");
System.out.println(df.format(d));
}
}
import java.math.*;
public class T6_4 {
public static void main(String[] args) {
BigInteger sum = new BigInteger("0");
String s = new String("1!");
for(int i=1; i<=59; i++,i++) { if(i != 1) {
s += "+" + i + "!";
}
BigInteger par = new BigInteger("1");
for(int j=1; j<=i; j++) {
par = par.multiply(new BigInteger(String.valueOf(j)));
}
sum = sum.add(par);
System.out.println(s + " = " + sum);
}
//System.out.println("1 + 2! + 3! + ... + 30! = " + sum);
}
}