精品午夜福利中文字av_国产成人综合网_av毛片免费观看网站_欧美影视国产综合_野花香视频中文免费观看_亚洲无码要在线视频_又粗又大又用力大交换好大好爽小静_欧美贵妇v办公室高跟鞋_亚洲国产高清a∨网站_免费中文少妇亚洲

知ing

Java面向?qū)ο蟪绦蛟O(shè)計(jì)

耿祥義,張躍平 編 / 清華大學(xué)出版社

青澀當(dāng)初 上傳

查看本書

習(xí)題9

1A,B,D

2Love:Game

313

abc夏日

413579

59javaHello

6

public class Xiti6?{

??public static void main (String args[ ]) {

???????String s1,s2,s3,t1="ABCDabcd";

???????System.out.println("字符串原來是這個(gè)樣子: "+t1);

???????s1=t1.toUpperCase();

???????System.out.println("字符串中的小寫字母變成大寫是這個(gè)樣子: "+s1);

???????s2=t1.toLowerCase();

???????System.out.println("字符串中的大寫字母變成小寫是這個(gè)樣子: "+s2);

???????s3=s1.concat(s2);

???????System.out.println("大寫字符串連接小寫字符串是這個(gè)樣子: "+s3);

??????}

??????

???}

7

class Xiti7?

{ public static void main(String args[ ])

??{ String ?s ="中華人民共和國(guó)";

????char a=s.charAt(0);

????char b=s.charAt(6);

????System.out.println("第一個(gè)字符: ?"+a);

????System.out.println("最后一個(gè)字符: ?"+b);

??}

}

8

import java.util.*;

class Xiti8

{ ?public static void main(String args[]){

?????int year,month;

?????System.out.println("請(qǐng)輸入年份和月份,每輸入一個(gè)數(shù)回車確認(rèn)");

?????Scanner reader=new Scanner(System.in);

?????year= reader.nextInt();

?????month= reader.nextInt();

??????String [] day=new String[42];

??????System.out.println(" 日 ?一 ?二 ?三 ?四 ?五 ?六");

???????Calendar rili=Calendar.getInstance();

???????rili.set(year,month-1,1);//將日歷翻到yearmonth1,注意0表示一月...11表示十二月

???????int 星期幾=rili.get(Calendar.DAY_OF_WEEK)-1;

???????int dayAmount=0;

???????if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)

??????????dayAmount=31;

???????if(month==4||month==6||month==9||month==11)

??????????dayAmount=30;

???????if(month==2)

???????????if(((year%4==0)&&(year%100!=0))||(year%400==0))

??????????????dayAmount=29;

???????????else

??????????????dayAmount=28;

???????for(int i=0;i<星期幾;i++)

????????????day[i]="";

???????for(int i=星期幾,n=1;i<星期幾+dayAmount;i++){

????????????if(n<=9)

?????????????????day[i]=String.valueOf(n)+" " ;

????????????else

?????????????????day[i]=String.valueOf(n);

????????????n++;

???????} ?

???????for(int i=星期幾+dayAmount;i<42;i++)

????????????day[i]="";

???????for(int i=0;i<星期幾;i++)

?????????????{ ?day[i]="**";

?????????????}

???????for(int i=0;i<day.length;i++) ??

????????{ if(i%7==0)

??????????{ ?System.out.println("");

??????????}

???????System.out.print(" ?"+day[i]);

?????????

??????}

????}

}

9

import java.util.*;

class Xiti9

{ ?public static void main(String args[]){

?????int year1,month1,day1,year2,month2,day2;

?????Scanner reader=new Scanner(System.in);

?????System.out.println("請(qǐng)輸入第一個(gè)日期的年份 月份 日期 ,每輸入一個(gè)數(shù)回車確認(rèn)");

?????year1= reader.nextInt();

?????month1= reader.nextInt();

?????day1= reader.nextInt();

?????System.out.println("請(qǐng)輸入第二個(gè)日期的年份 月份 日期 ,每輸入一個(gè)數(shù)回車確認(rèn)");

?????year2= reader.nextInt();

?????month2= reader.nextInt();

?????day2= reader.nextInt();

??????Calendar calendar=Calendar.getInstance();

??????calendar.set(year1,month1,day1); ?

??????long timeYear1=calendar.getTimeInMillis();

??????calendar.set(year2,month2,day2); ?

??????long timeYear2=calendar.getTimeInMillis();

??????long 相隔天數(shù)=Math.abs((timeYear1-timeYear2)/(1000*60*60*24));

??????System.out.println(""+year1+""+month1+""+day1+"日和"+

????????????????????????????year2+""+month2+""+day2+"日相隔"+相隔天數(shù)+"");

???} ?

}

10

public class Xiti10

{ ?public static void main(String args[])

???{ ?double a=0,b=0,c=0;

??????a=12;

??????b=24;

??????c=Math.max(a,b);

??????System.out.println(c);

??????c=Math.min(a,b);

??????System.out.println(c);

?????c=Math.pow(2,3);

??????System.out.println(c);

??????c=Math.abs(-0.123);

??????System.out.println(c);

??????c=Math.asin(0.56);

??????System.out.println(c);

??????c=Math.cos(3.14);

??????System.out.println(c);

??????c=Math.exp(1);

??????System.out.println(c);

??????c=Math.log(8);

??????System.out.println(c);

???}

}

習(xí)題10

1BorderLayout布局。

2不可以。

3A,C

4

import java.util.StringTokenizer;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Xiti4

{ ?public static void main(String args[])

???{ ComputerFrame fr=new ComputerFrame();

?????fr.setTitle("計(jì)算的窗口");

???}

}

class ComputerFrame extends JFrame implements TextListener

{ ??TextArea text1,text2;

????int count=1;

????double sum=0,aver=0;

??public ComputerFrame()

??{ setLayout(new FlowLayout());

??????text1=new TextArea(6,20);

??????text2=new TextArea(6,20);

??????add(text1);

??????add(text2);

??????text2.setEditable(false);

??????text1.addTextListener(this);

??????setSize(300,320);

??????setVisible(true);

??????addWindowListener(new WindowAdapter()

????????????{ ?public void windowClosing(WindowEvent e)

???????????????{ ?System.exit(0);

???????????????}

????????????});

??????validate();

?}

?public void textValueChanged(TextEvent e)

?{ ?String s=text1.getText();

??????sum=0;

??????aver=0; ?

??????StringTokenizer fenxi=new StringTokenizer(s," ,'\n'");

?????int n=fenxi.countTokens();

?????count=n;

?????double a[]=new double[n];

?????for(int i=0;i<=n-1;i++)

?????{ ?String temp=fenxi.nextToken(); ?

????????try { a[i]=Double.parseDouble(temp);

?????????????sum=sum+a[i];

???????????}

????????catch(Exception ee)

???????????{ count--;

???????????}

?????}

?????aver=sum/count;

?????text2.setText(null);

?????text2.append("\n:"+sum);

?????text2.append("\n平均值:"+aver);

???}

}

5

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class Xiti5

{ ?public static void main(String args[])

???{ ComputerFrame fr=new ComputerFrame();

?????fr.setTitle("計(jì)算");

???}

}

class ComputerFrame extends Frame implements ActionListener

{ TextField text1,text2,text3;

??Button button1,button2,button3,button4;

??Label label;

??public ComputerFrame()

??{setLayout(new FlowLayout());

???text1=new TextField(10);

???text2=new TextField(10);

???text3=new TextField(10);

???label=new Label(" ",Label.CENTER);

???label.setBackground(Color.green);

???add(text1);

???add(label);

???add(text2);

???add(text3);

???button1=new Button(""); ???

???button2=new Button("");

???button3=new Button("");

???button4=new Button("");

???add(button1);

???add(button2);

???add(button3);

???add(button4);

???button1.addActionListener(this);

???button2.addActionListener(this);

???button3.addActionListener(this); ?

???button4.addActionListener(this);

???setSize(300,320);

???setVisible(true);

???addWindowListener(new WindowAdapter()

????????????{ ?public void windowClosing(WindowEvent e)

???????????????{ ?System.exit(0);

???????????????}

????????????});

???validate();

??} ??

??public void actionPerformed(ActionEvent e)

??{ double n;

????if(e.getSource()==button1)

????{ ?double n1,n2; ?

???????try{ n1=Double.parseDouble(text1.getText());

????????????n2=Double.parseDouble(text2.getText());

????????????n=n1+n2;

????????????text3.setText(String.valueOf(n));

????????????label.setText("+");

??????????}

???????catch(NumberFormatException ee)

??????????{ text3.setText("請(qǐng)輸入數(shù)字字符");

??????????}

?????}

????else if(e.getSource()==button2)

????{ ?double n1,n2; ?

???????try{??n1=Double.parseDouble(text1.getText());

????????????n2=Double.parseDouble(text2.getText());

????????????n=n1-n2;

????????????text3.setText(String.valueOf(n));

????????????label.setText("-");

??????????}

???????catch(NumberFormatException ee)

??????????{ text3.setText("請(qǐng)輸入數(shù)字字符");

??????????}

?????}

?????else if(e.getSource()==button3)

??????{double n1,n2; ?

???????try{ n1=Double.parseDouble(text1.getText());

????????????n2=Double.parseDouble(text2.getText());

????????????n=n1*n2;

????????????text3.setText(String.valueOf(n));

????????????label.setText("*");

??????????}

???????catch(NumberFormatException ee)

??????????{ text3.setText("請(qǐng)輸入數(shù)字字符");

??????????}

??????}

??????else if(e.getSource()==button4)

??????{double n1,n2; ?

???????try{ n1=Double.parseDouble(text1.getText());

????????????n2=Double.parseDouble(text2.getText());

????????????n=n1/n2;

????????????text3.setText(String.valueOf(n));

????????????label.setText("/");

??????????}

???????catch(NumberFormatException ee)

??????????{ text3.setText("請(qǐng)輸入數(shù)字字符");

??????????}

??????}

?????validate();

??}

}

6

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Xiti6

{ ??public static void main(String args[])

????{ ?new WindowPanel();

????}

}

class Mypanel extends JPanel implements ActionListener

{ ?Button button; ??

???TextField text;

???Mypanel() ?

???{ ?button=new Button(" ");

??????text=new TextField(12);

??????add(button);

??????add(text);

??????button.addActionListener(this);

???}

???public void actionPerformed(ActionEvent e)

???{ ?String name=text.getText();

??????if(name.length()>0)

????????button.setLabel(name);

??????validate();

???}

}

class WindowPanel extends Frame

{ ??Mypanel panel1,panel2;

????WindowPanel()

????{ panel1=new Mypanel();

??????panel2=new Mypanel();

??????panel1.setBackground(Color.red);

??????panel2.setBackground(Color.blue);

??????add(panel1,BorderLayout.SOUTH);

??????add(panel2,BorderLayout.NORTH);

??????setSize(300,320);

??????setVisible(true);

??????addWindowListener(new WindowAdapter()

????????????{ ?public void windowClosing(WindowEvent e)

???????????????{ ?System.exit(0);

???????????????}

????????????});

??????validate();

????}

}

7.參見10.13, 參照本章例子10.21。

8

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Xiti8

{ public static void main(String args[])

???{ ??MoveFrame f=new MoveFrame();

???????f.setBounds(12,12,300,300);

???????f.setVisible(true);

???????f.setTitle("移動(dòng)"); ?

???????f.validate();

???????f. addWindowListener(new WindowAdapter() ???

??????????????????????{ ??public void windowClosing(WindowEvent e)

???????????????????????????{ ?System.exit(0);

???????????????????????????}

??????????????????????}

???????????????????);

????}

}

class MoveFrame extends JFrame implements ActionListener

{ ?JButton controlButton,movedButton; ??

???public MoveFrame()

???{ ?controlButton=new JButton("單擊我運(yùn)動(dòng)另一個(gè)按鈕");

??????controlButton.addActionListener(this);

??????movedButton=new JButton();

??????movedButton.setBackground(new Color(12,200,34));

??????setLayout(null);

??????add(controlButton);

??????add(movedButton);

??????controlButton.setBounds(10,30,130,30);

??????movedButton.setBounds(100,100,10,10);

???}

??public void actionPerformed(ActionEvent e)

???{ ?int x=movedButton.getBounds().x;

??????int y=movedButton.getBounds().y;

??????x=x+5;

??????y=y+1;

??????movedButton.setLocation(x,y);

??????if(x>200)

??????{ x=100;

????????y=100;

??????}

???}

}

?

9

import java.awt.*;

import java.awt.event.*;

public class Xiti9

{ ?public static void main(String args[])

????{ ?Win win=new Win();

????}

}

class Win extends Frame implements KeyListener

{ ??Button b[]=new Button[8];

????int x,y;

????Win()

????{ setLayout(new FlowLayout());

??????for(int i=0;i<8;i++)

?????????{ ?b[i]=new Button(""+i);

????????????b[i].addKeyListener(this);

????????????add(b[i]);

?????????}

??????addWindowListener(new WindowAdapter() { ??

??????????????????????????public void windowClosing(WindowEvent e){

?????????????????????????????System.exit(0);

???????????????????????????}

???????????????????????});

??????setBounds(10,10,300,300);

??????setVisible(true);

??????validate();

????}

public void keyPressed(KeyEvent e)

{ ????int moveDistance=1;

??????Component com=(Component)e.getSource();

??????int x=(int)com.getBounds().x;

??????int y=(int)com.getBounds().y;

??????Component component[]=this.getComponents();

??????if(e.getKeyCode()==KeyEvent.VK_UP)

????????{ ?y=y-moveDistance;

???????????com.setLocation(x,y);

???????????Rectangle comRect=com.getBounds();

???????????for(int k=0;k<component.length;k++)

???????????{ ?Rectangle orthRect=component[k].getBounds();

??????????????if(comRect.intersects(orthRect)&&com!=component[k])

??????????????{ ?y=y+moveDistance;

?????????????????com.setLocation(x,y);

?????????????????break;

??????????????}

???????????}

??????????if(y<=0) y=10;

????????}

??????else if(e.getKeyCode()==KeyEvent.VK_DOWN)

???????{ ??y=y+moveDistance;

???????????com.setLocation(x,y);

???????????Rectangle comRect=com.getBounds();

???????????for(int k=0;k<component.length;k++)

???????????{ ?Rectangle orthRect=component[k].getBounds();

??????????????if(comRect.intersects(orthRect)&&com!=component[k])

??????????????{ ?y=y-moveDistance;

?????????????????com.setLocation(x,y);

?????????????????break;

??????????????}

???????????}

??????????if(y>=300) y=300;

????????}

??????else if(e.getKeyCode()==KeyEvent.VK_LEFT)

????????{ ?x=x-moveDistance;

???????????com.setLocation(x,y);

???????????Rectangle comRect=com.getBounds();

???????????for(int k=0;k<component.length;k++)

???????????{ ?Rectangle orthRect=component[k].getBounds();

??????????????if(comRect.intersects(orthRect)&&com!=component[k])

??????????????{ ?x=x+moveDistance;

?????????????????com.setLocation(x,y);

?????????????????break;

??????????????}

???????????}

??????????if(x<=0) x=0;

????????}

??????else if(e.getKeyCode()==KeyEvent.VK_RIGHT)

????????{ ?x=x+moveDistance;

???????????com.setLocation(x,y);

???????????Rectangle comRect=com.getBounds();

???????????for(int k=0;k<component.length;k++)

???????????{ ?Rectangle orthRect=component[k].getBounds();

??????????????if(comRect.intersects(orthRect)&&com!=component[k])

??????????????{ ?x=x-moveDistance;

?????????????????com.setLocation(x,y);

?????????????????break;

??????????????}

???????????}

??????????if(x>=300) x=300;

????????}

????}

????public void keyTyped(KeyEvent e) {}

????public void keyReleased(KeyEvent e) {}

}?


查看更多