知ing

Java面向对象程序设计

耿祥义,张跃平 编 / 清华大学出版社

青涩当初 上传

查看本书

习题9

1ABD

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("字符串原来是这个样子: "+t1);

       s1=t1.toUpperCase();

       System.out.println("字符串中的小写字母变成大写是这个样子: "+s1);

       s2=t1.toLowerCase();

       System.out.println("字符串中的大写字母变成小写是这个样子: "+s2);

       s3=s1.concat(s2);

       System.out.println("大写字符串连接小写字符串是这个样子: "+s3);

      }

      

   }

7

class Xiti7 

{ public static void main(String args[ ])

  { String  s ="中华人民共和国";

    char a=s.charAt(0);

    char b=s.charAt(6);

    System.out.println("第一个字符:  "+a);

    System.out.println("最后一个字符:  "+b);

  }

}

8

import java.util.*;

class Xiti8

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

     int year,month;

     System.out.println("请输入年份和月份,每输入一个数回车确认");

     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("请输入第一个日期的年份 月份 日期 ,每输入一个数回车确认");

     year1= reader.nextInt();

     month1= reader.nextInt();

     day1= reader.nextInt();

     System.out.println("请输入第二个日期的年份 月份 日期 ,每输入一个数回车确认");

     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 相隔天数=Math.abs((timeYear1-timeYear2)/(1000*60*60*24));

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

                            year2+""+month2+""+day2+"日相隔"+相隔天数+"");

   }  

}

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);

   }

}

习题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("计算的窗口");

   }

}

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("计算");

   }

}

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("请输入数字字符");

          }

     }

    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("请输入数字字符");

          }

     }

     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("请输入数字字符");

          }

      }

      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("请输入数字字符");

          }

      }

     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("移动");  

       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("单击我运动另一个按钮");

      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) {}

} 


查看更多