三道编程题

四、 编程题

1. 构造一个类来描述屏幕上的一个点,该类的构成包括点的x和y两个坐标,以及一些对点进行的操作,包括:取得点的坐标值,对点的坐标进行赋值,编写应用程序生成该类的对象并对其进行操作。



2. 编写一个应用程序,完成文件的拷贝功能,文件名从命令行得到。



3. 利用所学的Java语言知识,完成一个实现秒表功能的Applet程序。它的GUI界面如下所示: 利用所学的Java语言知识,完成一个实现秒表功能的Applet程序。它的GUI界面如下所示:

  要求该程序能够完成以下功能:
  (1) 在界面上方的文本框中,按照"小时:分钟:秒"的顺序实时显示系统时间;
  (2) 当按下界面中间的"Current Time:"按钮时,当前系统时间能够在界面下方的文本框中显示出来。
  提示:可以通过调用java.util.Data类的方法,获得系统时间。Data类的接口如下所示,
   public class Date {
   // 返回当前系统时间的小时值
   public String getHours( ) { …}
   // 返回当前系统时间的分钟值
   public String getMintues( ) { … }
   // 返回当前系统时间的秒钟值
   public String getSeconds( ) { …}
   …
   }
----------

四、 编程题答案

编程第1题
  import java.io.*;

  public class Point
  {
   int x,y;

  public Point(int x,int y)
  {
   this.x = x;
   this.y = y;
  }

  public Point getPoint()
  {
   Point tempPoint = new Point(x,y);
   return tempPoint;
   }

   public void setPoint(Point point)
   {
    this.x = point.x;
    this.y = point.y;
   }


   public static void main(String args[])
   {
    Point Point1 = new Point(3,4);
    System.out.println("Point1:"+"("+Point1.x+","+Point1.y+")");

    Point Point2 = Point1.getPoint();
    System.out.println("Point2:"+"("+Point2.x+","+Point2.y+")");

    Point Point3 = new Point(5,6);
    Point1.setPoint(Point3);
    System.out.println("Point1:"+"("+Point1.x+","+Point1.y+")");
   }  

  }


编程第2题
  import java.io.*;

  class FileCopy
  {
   public static void main(String[] args)
   {
    FileInputStream in;
    FileOutputStream out;

    if (args.length<2)
   {
    System.out.println("Usage: java copy srcfile destfile");
    System.exit(-1);
   }

   try
   {
    in = new FileInputStream(args[0]);
    out = new FileOutputStream(args[1]);
    copyFile(in,out);

   }

   catch (Exception e)
   {
    System.out.println(e);
   }

  }

   private static void copyFile(FileInputStream in, FileOutputStream out)
   {
    int length;
    byte buf[] = new byte[1024];
  
    try{
      while ((length=in.read(buf,0,1024))!=-1)
    {
     out.write(buf, 0, length);
    }

   }

   catch (Exception e)
   {
    System.out.println("Error:"+e);
    System.exit(-1);
   }
  }

 }


编程第3题
  import java.awt.*;
  import java.awt.event.*;
  import java.applet.*;
  import java.util.*;

  public class TimeViewer extends Applet implements ActionListener, Runnable {

   Thread timer;
   TextField in, out;
   Button bb;
   Panel p1, p2, p3;
   boolean state;

   public void init() {
    in = new TextField(20);
    out = new TextField(20);
    bb = new Button("Current Time:");
    p1 = new Panel();
    p2 = new Panel();
    p3 = new Panel();

    setLayout(new GridLayout(3, 1));
    setSize(200,100);

    p1.add(in);
    p2.add(bb);
    p3.add(out);
    add(p1);
    add(p2);
    add(p3);

    bb.addActionListener(this);

    timer = new Thread(this);
    state = true;
    timer.start();
   }

   public void actionPerformed(ActionEvent e) {
    //out.setText(in.getText());
    out.setText(currentTime());
   }

   public void run() {
    while(true) {
    try {
       timer.sleep(1000);
    } catch (InterruptedException e) { }
    in.setText(currentTime());;
    }
  }

  String currentTime() {
   Date now = new Date();
   String str = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
   return str;
  }
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值