試題
01
:
一、 題目描述
(
程式範例描述
)
:
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. Write a Java program wherein a method
isPalindrome(String s) is defined. isPalindrome is invoked by main method and returns true if the argument is a palindrome word; otherwise returns false. For example, isPalindrome(inputString) returns true if inputString is “akasaka”.
二、 輸入說明:
A string inputString.
三、 輸出說明:
If inputString is a palindrome, print true; otherwise, print false.
四、 裝備限詞
出現次數
1
1
五、 輸入範例:
4
5
1
2
3 racecar decided abba tattarrattat malayalam
六、 輸出範例:
1
2
3 true false true
語句
Scanner sc = new Scanner(System.in); isPalindrome(inputString)
1
4
5 true true
2
試題
02
:
一、 題目描述
(
程式範例描述
)
:
A class called Circle contains an instance variables radius with non-negative integer value. Circle also has getRadius and getArea instance methods that retrieve the radius and area of the circle respectively. A test program TestCircle which uses the Circle class, as follows: class TestCircle { public static void main(String[] args) {
Scanner sc = new Scanner(System.in); int r = sc.nextInt();
Circle c1 = new Circle(r);
System.out.println("The circle has radius of " + c1.getRadius() +
" and area of " + c1.getArea() + " PI.");
}
}
Try to complete the Circle class so as to execute TestCircle properly.
二、 輸入說明:
An integer, 2, for example.
三、 輸出說明:
Print the message “The circle has radius of 2 and area of 4 PI.”
四、 裝備限詞
出現次數
1
語句
TestCircle
的所有內容。
五、 輸入範例:
1
2
3
2
0
10
六、 輸出範例:
3
1
2
3
The circle has radius of 2 and area of 4 PI.
The circle has radius of 0 and area of 0 PI.
The circle has radius of 10 and area of 100 PI.
4
試題
03
:
一、 題目描述
(
程式範例描述
)
:
Design a class named ArrayUtil which supports the following service by its class methods.
Merging two sorted arrays with int element type:
Design a class method named mergeSortedArray which accepts two int-type arrays through parameters. If both input arrays are sorted in ascending order, the method returns a new array containing all the elements in the input arrays in ascending order. Otherwise, return null.
Note 1: The length of the input arrays may not be the same.
Note 2: The TestClass is given as follows. import java.util.Scanner; public class TestClass { public static void main(String[] args) { int[] result;
Scanner sc = new Scanner(System.in); int arr1Size = sc.nextInt(); int arr2Size = sc.nextInt(); int[] arr1 = new int[arr1Size]; int[] arr2 = new int[arr2Size]; int i = 0; while (arr1Size > 0) { arr1[i++] = sc.nextInt(); arr1Size--;
} i = 0; while (arr2Size > 0) { arr2[i++] = sc.nextInt(); arr2Size--;
} if((result = ArrayUtil.mergeSortedArray(arr1, arr2)) != null) {
System.out.println("The merged array:"); for(int e: result) {
System.out.print(e + " ");
}
System.out.println();
} else {
System.out.println("The input arrays cannot be merged!");
}
}
}
二、 輸入說明:
The first two integers, arr1Size and arr2Size, indicate the sizes of arr1 and arr2 respectively. The following arr1Size integers are the elements of arr1 and then
5
the arr2Size integers are the elements of arr2.
三、 輸出說明:
If both input arrays are sorted in ascending order, output the message “The merged array:” followed by the sorted array. Otherwise, output the message
“The input arrays cannot be merged!”.
四、 裝備限詞
出現次數
1
語句
TestClass
的所有內容。
五、 輸入範例:
3
4
1
2
6 5 2 10 14 25 40 53 4 8 20 28 33
6 5 2 10 14 25 40 53 20 4 33 28 8
3 4 12 32 56 9 21 23 45
5 5 23 56 78 90 99 12 22 24 34 45
六、 輸出範例:
1
2
3
4
The merged array:
2 4 8 10 14 20 25 28 33 40 53
The input arrays cannot be merged!
The merged array:
9 12 21 23 32 45 56
The merged array:
12 22 23 24 34 45 56 78 90 99
6