請說出下列【代碼】的輸出結果
Java
class A{
int x = 2;
static int y = 5;
int f(){
y=x+y;
return x+y;
}
}
public class Code1 {
public static void main(String[] args){
A a = new A();
A b = new A();
a.x=10;
System.out.println(a.f());//【代碼1】
System.out.println(b.f());//【代碼2】
b.y=10;
System.out.println(a.f());//【代碼3】
}
}
【代碼1】25 【代碼2】19 【代碼3】30
這道題考察的是靜態成員變量的特點,無論一個類實例了多少個對象,這些對象都共享一個靜態成員變量。
a對象將x的值改爲10後調用了f方法,y的值變爲了15,返回的值是x+y即10+15爲25。
b對象調用f方法,y的值在15的基礎上又加了x(2)變成了17,返回x+y即2+17位19。之後b.y=10,那麼a中的y也變成了10
a對象再一次調用f方法,y的值在10的基礎上又加了10變成20,返回x+y即20+10=30.
請說出下列【代碼】的輸出結果
Java
import javax.swing.JTextField;
class MySchool {
JTextField showMess;
void setJTextField(JTextField t) {
showMess = t;
}
void write(String s) {
showMess.setText(s);
}
JTextField getJTextField() {
return showMess;
}
}
public class Code2 {
public static void main(String[] args) {
JTextField text = new JTextField("學生");
System.out.println(text.gettext());// 【代碼1】
MySchool school = new MySchool();
school.setJTextField(text);
school.write("上課不玩手機");
System.out.println(text.getText());// 【代碼2】
JTextField no = school.getJTextField();
no = new JTextField("上課遲到");
System.out.println(school.getJTextField().getText());// 【代碼3】
}
}
【代碼1】調用getText方法,獲取文本框中的字符串內容。輸出“學生”。類型相同的兩個對象一旦引用相同,他們就有完全相同的變量(實體)。將JTextField類的對象text傳給了school的setJTextField方法,將text的引用賦值給了showMess,那麼調用write方法修改了shwoMess中的內容後,text的內容也跟着改變,所以【代碼2】輸出上課不玩手機,【代碼3】是調用school的getJTextField()方法返回showMess對象後調用getText方法返回其中分內容,即爲上課不玩手機。
請說出下列【代碼】的輸出結果。
Java
import java.util.Scanner;
import java.util.StringTokenizer;
public class Code3 {
public static void main(String args[]) {
String s = "height:1229#width:2912";
StringTokenizer fenxi = new StringTokenizer(s, "wid:th,eg#");
System.out.println(fenxi.countTokens()); // 【代碼1】
String str = fenxi.nextToken();
System.out.println(fenxi.nextToken()); // 【代碼2】
Scanner scan = new Scanner(s);
scan.useDelimiter("[12:29#]+");
str = scan.next();
System.out.println(scan.next()); // 【代碼3】
}
}
這道題考察StringTokenizer類的用法。StringTokenizer fenxi = new StringTokenizer(s, "wid:th,eg#");
將字符序列s分解,第二個參數的字符序列中的字符的任意排列被作爲了分隔標記。
因此,s被分解成了height:1229和2912。調用countTokens()方法返回分析器中計數變量的值,即爲2。
接着調用了兩次nextToken()方法逐個獲取String對象的字符序列中的語言符號(單詞),所以第二次輸出的是2912。接着使用Scanner對象的useDelimiter(正則表達式)方法將正則表達式作爲分隔標記。所以s分解成了height和width,第二次調用next方法輸出width。
已知應用程序中已經有如下的刻畫矩形的Rect類:
Java
abstract class Rect{
int width;//矩形的寬
int height;//矩形的高
final void setWidth(int w){//不能重寫
width = w>0?w:0;
}
final void setHeight(int h){
height = h>0?h:0;
}
abstract int getArea();//計算矩形的面積
}
請完成如下任務:
(1)編寫一個Rect類的子類Rectangle,在你的Rectangle子類中新增加一個返回矩形周長的方法(方法的名字考生可自己定義)。
(2)在應用程序的主類的main方法中用你的Rectangle類創建對象,完成輸出寬是12高是8的矩形的面積和周長的任務。
Java
class Rextangle extends Rect{
int getArea(){ //父類中的該方法是抽象的,必須重寫
return width*height;
}
int getLength(){
return 2*(width+height);
}
}
class Code{
public static void main(String args[]){
Rectangle a = new Rectangle();
a.setWidth(12);//或a.width=12;
a.setHeight(8);//或a.height=8;
System.out.println(a.getArea());
System.out.println(a.getLength());
}
}
編寫一個GUI程序。功能要求如下:
窗口(需要編寫JFrame的子類)的標題爲“計算園面積和周長”,窗口布局爲FlowLayout佈局,設置窗口大小爲(400,180),設置窗口可見,單機窗口右上角關閉圖標可以關閉該窗口。
窗口中有一個文本框(JTextField),兩個標籤(JLable)和兩個按鈕(JButton)。兩個按鈕上的文字分別是“計算園面積”和“計算園周長”。
用戶在文本框中輸入圓的半徑,單擊“計算圓面積”按鈕,程序在一個標籤中顯示園的面積,單擊“計算圓周長”按鈕,程序在另一個標籤中顯示園的周長。
Java
public class test {
public static void main(String args[]) {
StuWindow stu = new StuWindow();
}
}
Java
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class StuWindow extends JFrame {
JTextField inputRadius;// 輸入半徑
JLabel showArea;// 顯示面積
JLabel showCirlceLength;// 顯示周長
JButton computerArea;// 計算面積
JButton computerCirlceLength;// 計算周長
Police listener; // listener是監視器
public StuWindow() {
setLayout(new FlowLayout());
setSize(400, 300);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
validate();
inputRadius = new JTextField(10);
showArea = new JLabel();
showCirlceLength = new JLabel();
computerArea = new JButton("計算圓面積");
computerCirlceLength = new JButton("計算圓周長");
add(inputRadius);
add(computerArea);
add(computerCirlceLength);
add(showArea);
add(showCirlceLength);
listener = new Police();
computerArea.addActionListener(listener);
computerCirlceLength.addActionListener(listener);
listener.setJButton(computerArea, computerCirlceLength);
listener.setJLabel(showArea, showCirlceLength);
listener.setJTextField(inputRadius);
}
}
Java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Police implements ActionListener {
JTextField inputRadius;
JLabel showArea;
JLabel showCirlceLength;
JButton computerArea;
JButton computerCirlceLength;
public void setJButton(JButton... b) {
computerArea = b[0];
computerCirlceLength = b[1];
}
public void setJLabel(JLabel... b) {
showArea = b[0];
showCirlceLength = b[1];
}
public void setJTextField(JTextField... b) {
inputRadius = b[0];
}
@Override
public void actionPerformed(ActionEvent e) {
double r = Double.parseDouble(inputRadius.getText());
if (e.getSource() == computerArea) {
showArea.setText("面積:" + Math.PI * r * r);
}
if (e.getSource() == computerCirlceLength) {
showCirlceLength.setText("周長:" + 2 * Math.PI * r);
}
}
}