丁保国的博客

收集整理工作生活,点点滴滴

  :: :: 博问 :: 闪存 :: :: :: :: 管理 ::

Java程序设计

1.  创建一个圆类Circle,该类可以接收圆半径,并具有计算圆周长和面积的方法。

import java.math.*;

import javax.swing.*;

 

class Circle{

      int r;

      Circle(int r){

       this.r=r;

      }

      public void getS(){

       System.out.println("面积是:"+Math.PI*r*r);

      }

      public void getL(){

       System.out.println("周长是:"+2*Math.PI*r);

      }

      public static void main(String[] args){

       String s=JOptionPane.showInputDialog("请输入半径:");

       System.out.println("半径是:"+s);

       Circle c=new Circle(Integer.parseInt(s));

       c.getS();

       c.getL();

      }

}

 

2.  设计矩形类Square,可以接收边长(一个参数时为正方形,二个参数时分别为长和宽)。类中具有求矩形面积的方法。

import java.math.*;

 

class Square{

      int a,b;

      Square(int a,int b){

       this.a=a;

       this.b=b;

      }

      Square(int a){

       this(a,a);

      }

      public void getS(){

       System.out.println("长是:"+a);

       System.out.println("宽是:"+b);

       System.out.println("面积是:"+a*b);

      }

      public static void main(String[] args){

       new Square(3).getS();

       new Square(3,6).getS();

      }

}

 

3.  创建一个三角形类Tri,该类中包含成员有:三个边长(创建对象时接收,如创建对象时没给出边长的参数,默认三边长均为1)和计算三角形面积的方法。

import java.math.*;

 

class Tri{

      int a,b,c;

      Tri(){

       a=b=c=1;

      }

      Tri(int a,int b,int c){

       this.a=a;

       this.b=b;

       this.c=c;

      }

      public void getS(){

       double p=a+b+c;

       System.out.println("三角形三边是:"+a+""+b+""+c);

       System.out.println("面积是:"+Math.sqrt((p-a)*(p-b)*(p-c)));

      }

      public static void main(String[] args){

       new Tri(5,4,6).getS();

      }

}

 

4.  创建文件“test.txt”,并向文件中写入若干行。(FileWriter

import java.io.*;

class Shuru{

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

       FileWriter fw=new FileWriter("test.txt",true);

       fw.write("aaa\t\n");

       fw.write("bbbb");

       fw.close();

    }

}

 

5.  读文件“test.txt”,并将其内容显示。(FileReader

import java.io.*;

class Shuchu{

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

       FileReader fr=new FileReader("test.txt");

       int b;

       while((b=fr.read())!=-1){

           System.out.print((char)b);

       }

       fr.close();

    }

}

 

6.  复制文件“test.txt”,生成一个新文件“newtest.txt”。(分别用字节流和字符流两种方法复制)

字符流法:

import java.io.*;

class Copy{

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

       FileWriter fw=new FileWriter("testtry.txt");

       FileReader fr=new FileReader("test.txt");

       int b;

       while((b=fr.read())!=-1){

           fw.write((char)b);

       }

       fr.close();

       fw.close();

    }

}

字节流法:

import java.io.*;

class Copy{

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

       FileOutputStream out=new FileOutputStream("testtry.txt");

       FileInputStream in=new FileInputStream("test.txt");

       int l=in.available();

       byte[] b=new byte[l];

       in.read(b);

       out.write(b);

       in.close();

       out.close();

    }

}

 

 

 

7.  编程创建容放了一个按钮和文本框窗口,如下图所示。单击“Click”按钮能自动计数,并在下面的文本框中显示出“Your click numbers: (次数)”的信息。

 

 

import java.awt.event.*;

import javax.swing.*;

import java.awt.*;

 

class Click extends JFrame implements ActionListener{

       int counter;

       JTextField t;

       JButton b;

       Click(){

              super("Click");

              setDefaultCloseOperation(3);

              Container c=getContentPane();

              c.setLayout(new FlowLayout());

              t=new JTextField(20);

              b=new JButton("Click");

              b.addActionListener(this);

              c.add(t);

              c.add(b);

              setBounds(30,30,200,100);

              setVisible(true);

       }

       public void actionPerformed(ActionEvent e){

              Object o=e.getSource();

              if(o==b){

                     counter++;

                     t.setText("Your click numbers:"+counter);

              }

       }

       public static void main(String[] args){

              new Click();

       }

}

 

 

 

8.  设计如下图所示的加法器。

 

      

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

 

class Sum extends JFrame implements ActionListener{

       JTextField t1,t2,t3;

       JLabel l;

       JButton b;

       Sum(){

              super("Sum");

              setDefaultCloseOperation(3);

              setBounds(30,30,500,100);

              setVisible(true);

             

              Container c=getContentPane();

              c.setLayout(new FlowLayout());

             

              t1=new JTextField(5);

              t2=new JTextField(5);

              t3=new JTextField(5);

              l=new JLabel("+");

              b=new JButton("=");

              b.addActionListener(this);

             

              c.add(t1);c.add(l);c.add(t2);c.add(b);c.add(t3);

              setVisible(true);

             

       }

       public void actionPerformed(ActionEvent e){

              Object o=e.getSource();

              if(o==b){

                     int i=Integer.parseInt(t1.getText())+Integer.parseInt(t2.getText());

                     t3.setText(""+i);

              }

       }

       public static void main(String[] args){

              new Sum();

       }

}

 

 

 

9.     设计一个如下图所示的简单记事本。

 

 

import java.io.*;

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

 

class CreateFile extends JFrame implements ActionListener{

       JButton b;

       JTextArea t;

       CreateFile(){

              super("Create a text File");

              setDefaultCloseOperation(3);

              setBounds(30,30,300,300);

              Container c=getContentPane();

              c.setLayout(new FlowLayout());

             

              b=new JButton("Save");

              b.addActionListener(this);

              t=new JTextArea(10,20);

             

              c.add(b);c.add(t);

             

              setVisible(true);

             

       }

       public void actionPerformed(ActionEvent e){

              Object o=e.getSource();

              if(o==b){

                     try{

                            String s=t.getText();

                            FileWriter fw=new FileWriter("text.txt");

                            fw.write(s);

                            fw.close();

                     }catch(Exception ee){

                     }

              }

       }

       public static void main(String[] args){

              new CreateFile();

       }

}

 

 

 

 

10.  设计如下图所示的文本编辑器。

 

 

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

 

class TextEdit extends JFrame implements ActionListener{

       JButton b1,b2,b3,b4;

       JTextArea t;

       JLabel l;

       JPanel p1,p2;

       TextEdit(){

              super("信手涂鸦");

              setDefaultCloseOperation(3);

              setBounds(30,30,400,200);

              Container c=getContentPane();

              c.setLayout(new FlowLayout());

             

              b1=new JButton("Clear");

              b2=new JButton("Open");

              b3=new JButton("Save");

              b4=new JButton("Exit");

              t=new JTextArea(5,5);

              l=new JLabel("Clear--清空,Open--打开,Save--保存,Exit--退出");

              p1=new JPanel();

              p2=new JPanel();

              p1.setLayout(new GridLayout(4,1));

              p1.add(b1);p1.add(b2);p1.add(b3);p1.add(b4);

              p2.setLayout(new BorderLayout());

              p2.add(t);p2.add(l,BorderLayout.SOUTH);

             

              c.add(p1);c.add(p2);

              setVisible(true);

             

       }

       public void actionPerformed(ActionEvent e){

              //

       }

       public static void main(String[] args){

              new TextEdit();

       }

}

 

 

 

11.  编写一个Applet,可按如图所示显示一首唐诗。(用drawString显示)并编写Html文件,将Applet嵌入并运行。

 

import java.awt.*;

import javax.swing.*;

 

public class AppletDraw extends JApplet{

    public void paint(Graphics g){

       g.setFont(new Font("宋体",Font.BOLD,35));

       g.setColor(Color.RED);

       g.drawString("登鹤雀楼",30,40);

       g.setFont(new Font("宋体",Font.BOLD,20));

       g.setColor(Color.BLACK);

       g.drawString("白日依山尽,",30,80);

       g.drawString("黄河入海流.",30,100);

       g.drawString("欲穷千里目,",30,120);

       g.drawString("更上一层楼.",30,140);

    }

}

 

posted on 2007-07-21 14:30  丁保国  阅读(374)  评论(0编辑  收藏  举报