【Java】Java第16周練習題
Java 第16周練習題
一 判斷題
T 1-1 System.out.println(); 語句的作用是在結尾顯示一個換行符。
T 1-2 Java中所有的I/O都是通過流來實現的。
T 1-3 Java系統的標準輸入物件是System.in,標準輸出物件有兩個,分別是System.out和System.err。
T 1-4 介面中的方法預設是public abstract方法。
T 1-5 介面中的常量預設是public static的變數。
T 1-6 類在實現介面的方法時,必須顯式地使用public修飾符。
T 1-7 一個類只能有一個父類,但一個介面可以有一個以上的父介面。
F 1-8 一個數組可以存放許多不同型別的數值。
T 1-9 製造(new)陣列時,要指定陣列長度,以便為陣列分配記憶體。
T 1-10 使用者可以自定義自己的異常類。
T 1-11 當一個方法在執行過程中產生一個異常,則這個方法會終止,但是整個程式不一定終止執行。
T 1-12 Java的字元型別採用的是Unicode編碼,每個Unicode碼佔16個位元。
二 選擇題
2-1 FilterOutputStream is the parent class for BufferedOutputStream, DataOutputStream and PrintStream. Which classes are valid argument for the constructor of a FilterOutputStream? ( )
A. InputStream
B. OutputStream
C. RandomAccessFile
D. StreamTokenizer
2-2 對於類與物件的關係,以下說法錯誤的是( )。
A. 類是物件的型別
B. 物件由類來建立
C. 類是同類物件的抽象
D. 物件是建立類的模板
2-3 以下關於Java的區域性內部類的說法錯誤的是( )
A. 區域性內部類只能在當前類中使用
B. 區域性內部類不能包含靜態成員
C. 區域性內部類可以訪問外部類的所有成員
D. 在區域性內部類中定義的內部類不能被private修飾符修飾
注:在外部類方法體中定義的類叫區域性內部類,區域性內部類不能有訪問修飾符,且只能訪問方法體中的final型別的區域性變數。本題正確答案給的是A,但是個人覺得應該是C。
2-4 下列String類的( )方法返回指定字串的一部分。
A. extractstring()
B. substring()
C. Substring()
D. Middlestring()
2-5 以下程式段輸出結果的是( )
public class Test {
public static void main(String args[]){
String str="ABCDE";
str.substring(3);
str.concat("XYZ");
System.out.print(str);
}
}
A. DE
B. DEXYZ
C. ABCDE
D. CDEXYZ
2-6 JPanel元件的預設佈局管理器是( )。
A. BorderLayout
B. FlowLayout
C. GridLayout
D. CardLayout
2-7 Swing元件必須新增到Swing頂層容器相關的( )。
A. 分隔板上
B. 內容面板上
C. 選項卡上
D. 複選框內
2-8 以下關於建構函式的描述錯誤的是( )。
A. 建構函式的返回型別只能是void型。
B. 建構函式是類的一種特殊函式,它的方法名必須與類名相同。
C. 建構函式的主要作用是完成對類的物件的初始化工作。
D.一般在建立新物件時,系統會自動呼叫建構函式。
2-9 下面哪個函式是public void aMethod(){…}的過載函式?( )
A. void aMethod( ){…}
B. public int aMethod(){…}
C. public void aMethod ( ){…}
D. public int aMethod ( int m){…}
2-10 關於被私有訪問控制符private修飾的成員變數,以下說法正確的是( )
A. 可以被三種類所引用:該類自身、與它在同一個包中的其他類、在其他包中的該類的子類
B. 可以被兩種類訪問和引用:該類本身、該類的所有子類
C. 只能被該類自身所訪問和修改
D. 只能被同一個包中的類訪問
2-11 下列方法頭中哪一個不與其他方法形成過載(overload)關係?( )
A. void mmm()
B. void mmm(int i)
C. void mmm(String s)
D. int mm()
2-12 下面哪個選項中的程式碼沒有定義內部類,或者錯誤的定義了內部類? ( C )
//A:
public Class Line {
int length;
Class Point {
//內部類程式碼
}
}
//B:
public Class Line {
public Point getPoint() {
return new Point(){
//內部類程式碼
};
}
}
//C:
public Class Line {
//外部類程式碼
}
Class Point {
//內部類程式碼
}
//D:
public Class Line {
public int calcLength() {
Class Point {
//內部類程式碼
}
}
}
2-13 以下哪句是錯誤的?
A. 編譯器會搜尋要import的類的編譯結果檔案而不是原始檔
B. import是告訴編譯器要import的包的資訊
C. import是把要import的類的原始碼插入到import語句所在的地方
D. 編譯器在編譯的時候需要能訪問得到要import的類的編譯結果檔案
2-14 Java程式預設引用的包是( )。
A. java.text包
B. java.awt包
C. java.lang包
D. java.util包
三 函式題
4-1 定義一個直角三角形類
建立一個直角三角形類(regular triangle)RTriangle類,實現下列介面IShape。兩條直角邊長作為RTriangle類的私有成員,類中包含引數為直角邊的構造方法。
interface IShape {// 介面
public abstract double getArea(); // 抽象方法 求面積
public abstract double getPerimeter(); // 抽象方法 求周長
}
直角三角形類的定義:
直角三角形類的建構函式原型如下:
RTriangle(double a, double b);
其中 a 和 b 都是直角三角形的兩條直角邊。
裁判測試程式樣例:
import java.util.Scanner;
import java.text.DecimalFormat;
interface IShape {
public abstract double getArea();
public abstract double getPerimeter();
}
/*你寫的程式碼將嵌入到這裡*/
public class Main {
public static void main(String[] args) {
DecimalFormat d = new DecimalFormat("#.####");
Scanner input = new Scanner(System.in);
double a = input.nextDouble();
double b = input.nextDouble();
IShape r = new RTriangle(a, b);
System.out.println(d.format(r.getArea()));
System.out.println(d.format(r.getPerimeter()));
input.close();
}
}
輸入樣例:
3.1 4.2
輸出樣例:
6.51
12.5202
參考程式碼:
class RTriangle implements IShape{
private double a;
private double b;
public RTriangle(double a,double b) {
this.a=a;
this.b=b;
}
public double getArea(){
return a*b*0.5;
}
public double getPerimeter(){
return a+b+Math.sqrt(a*a+b*b);
}
}
四 程式設計題
5-1 求幾何形狀的面積之和
(求幾何形狀的面積之和)編寫一個方法,求陣列中所有幾何形狀物件的面積之和。方法簽名如下:
public static double sumArea(shape[] a)
編寫測試程式,繼承抽象類shape得到圓形類Circle和矩形類Rectangle。
abstract class shape {// 抽象類
/* 抽象方法 求面積 */
public abstract double getArea();
/* 抽象方法 求周長 */
public abstract double getPerimeter();
}
建立四個物件(兩個圓和兩個矩形)的陣列,然後使用sumArea方法求出它們的總面積。(保留4位小數)
輸入格式:
輸入 1.1 (第1個圓形的半徑) 1.8 (第2個圓形的半徑) 2.3 3.8 (第1個矩形的寬和高) 5.9 16.8 (第2個矩形的寬和高)
輸出格式:
The total area is 121.8401 (總面積,保留4位小數)
輸入樣例:
2.18
3.16
2.9 5.76
4.8 9.23
輸出樣例:
The total area is 107.3088
參考程式碼:
import java.util.Scanner;
import java.text.DecimalFormat;
abstract class shape {
public abstract double getArea();
public abstract double getPerimeter();
}
class yuan extends shape{
private double r;
public yuan(double ar){
r=ar;
}
public double getArea(){
return Math.PI*r*r;
}
public double getPerimeter(){
return 2*Math.PI*r;
}
}
class juxing extends shape{
private double w;
private double h;
public juxing(double aw, double ah){
w=aw;
h=ah;
}
public double getArea(){
return w*h;
}
public double getPerimeter(){
return 2*(w+h);
}
}
public class Main {
public static double sumArea(shape[] a){
double sum=0;
for(int i=0;i<a.length;i++){
sum+=a[i].getArea();
}
return sum;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
DecimalFormat decimalFormat= new DecimalFormat("#.####");
shape[] a=new shape[4];
double r=in.nextDouble();
a[0]=new yuan(r);
r=in.nextDouble();
a[1]=new yuan(r);
double w=in.nextDouble();
double h=in.nextDouble();
a[2]=new juxing(w,h);
w=in.nextDouble();
h=in.nextDouble();
a[3]=new juxing(w,h);
in.close();
System.out.println("The total area is "+decimalFormat.format(sumArea(a)));
}
}
5-2 數字格式異常
(NumberFormatException數字格式異常)編寫一個程式,提示使用者讀取兩個整數,然後顯示他們的和。程式應該在輸入不正確時提示使用者再次輸入數字。
輸入格式:
i 9 (第1次輸入)
l 8 (第2次輸入)
5 6 (第3次輸入)
輸出格式:
Incorrect input and re-enter two integers: (第1次輸出提示)
Incorrect input and re-enter two integers: (第2次輸出提示)
Sum is 11 (輸出結果)
輸入樣例:
i 9
l 8
5 6
輸出樣例:
Incorrect input and re-enter two integers:
Incorrect input and re-enter two integers:
Sum is 11
參考程式碼:
import java.util.Scanner;
import java.io.IOException;
import java.text.DecimalFormat;
public class MouseInfo {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int a,b;
while(true){
try{
a=in.nextInt();
b=in.nextInt();
System.out.println("Sum is "+(a+b));
break;
}catch(java.util.InputMismatchException e){
System.out.println("Incorrect input and re-enter two integers:");
in.nextLine();
continue;
}
}
}
}
5-4 查詢成績並折算後輸出
檔案:期中考試成績.txt中有若干學生的姓名和數學期中考試成績。 Smith 67 Anderson 75 Lewis 83 Cook 58 David 96 請你編寫一個簡單的查詢成績程式,當從鍵盤輸入一個姓名時查詢到他的數學期中考試分數並按照21%折算後輸出。如果沒找到則顯示Not found. 由於目前的OJ系統暫時不能支援使用者讀入檔案,我們編寫程式從鍵盤輸入檔案中的姓名和成績,當輸入的名字為noname時,表示結束。noname後面有一個名字,需要查詢其成績。
輸入格式:
Smith 67
Anderson 75
Lewis 83
Cook 58
David 96
noname (表示結束)
Bill
輸出格式:
Not found.
輸入樣例:
Smith 67
Anderson 75
Lewis 83
Cook 58
David 96
noname
Lewis
輸出樣例:
17.43
參考程式碼:
import java.util.*;
public class Main{
public static void main(String [] args){
List<Map<String,String>> datas = new ArrayList<Map<String,String>>();
Scanner in = new Scanner(System.in);
String name,grade;
while(true){
name = in.next();
if(name.equals("noname")){
break;
}
grade = in.next();
Map<String,String> map = new HashMap<String,String>();
map.put(name,grade);
datas.add(map);
}
String na = in.next();
for (int i = 0; i < datas.size(); i++) {
if(datas.get(i).containsKey(na)){
System.out.println(Integer.parseInt(datas.get(i).get(na))*0.21);
return;
}
}
System.out.println("Not found.");
}
}
5-5 找素數
請編寫程式,從鍵盤輸入兩個整數m,n,找出等於或大於m的前n個素數。
輸入格式:
第一個整數為m,第二個整數為n;中間使用空格隔開。例如:
103 3
輸出格式:
從小到大輸出找到的等於或大於m的n個素數,每個一行。例如:
103
107
109
輸入樣例:
9223372036854775839 2
輸出樣例:
9223372036854775907
9223372036854775931
參考程式碼:
import java.math.BigInteger;
import java.util.Scanner;
/**
* Created by jinlong on 2016/11/15.
*/
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
BigInteger m;
int n;
m=in.nextBigInteger();
n=in.nextInt();
int cnt=0;
while (cnt<n){
if (m.isProbablePrime(100)){
System.out.println(m);
cnt++;
}
m=m.nextProbablePrime();
}
in.close();
}
}
5-6 計算正五邊形的面積和周長
從下列的抽象類shape類擴展出一個正五邊形(regular pentagon)類RPentagon,這個類將正五邊形的邊長作為私有成員,類中包含初始化這個值的構造方法。
public abstract class shape {// 抽象類
/* 抽象方法 求面積 */ public abstract double getArea();
/* 抽象方法 求周長 */ public abstract double getPerimeter(); }
請程式設計從鍵盤輸入正五邊形的邊長值,建立一個正五邊形物件,然後輸出正五邊形的面積和正五邊形的周長。計算正五邊形的面積公式為: S=5a^2/(4*tan(36度))其中a為邊長。 或者:S=(1/4)a^2*√(25+10√5) 輸出結果保留4位小數。
輸入格式:
輸入正五邊形的邊長。例如:
5
輸出格式:
輸出正五邊形的面積和周長。第一行輸出面積,第二行輸出周長。例如: 43.0119
25
輸入樣例:
16.8
輸出樣例:
485.5875
84
參考程式碼:
import java.text.DecimalFormat;
import java.util.Scanner;
abstract class shape {
public abstract double getArea();
public abstract double getPerimeter();
}
class RPentagon extends shape{
private double side;
public RPentagon(double side) {
this.side = side;
}
@Override
public double getArea() {
//return 5*side*side/(4*Math.tan(36.0/180*Math.PI));
return 0.25*side*side*Math.sqrt(25+10*Math.sqrt(5));
}
@Override
public double getPerimeter() {
return side*5;
}
}
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
DecimalFormat decimalFormat= new DecimalFormat("#.####");
double a=in.nextDouble();
RPentagon rPentagon= new RPentagon(a);
System.out.println(decimalFormat.format(rPentagon.getArea()));
System.out.println(decimalFormat.format(rPentagon.getPerimeter()));
in.close();
}
}