java经典程序及常见算法

上传人:xiao****1972 文档编号:83805984 上传时间:2019-03-01 格式:DOC 页数:14 大小:126KB
返回 下载 相关 举报
java经典程序及常见算法_第1页
第1页 / 共14页
java经典程序及常见算法_第2页
第2页 / 共14页
java经典程序及常见算法_第3页
第3页 / 共14页
java经典程序及常见算法_第4页
第4页 / 共14页
java经典程序及常见算法_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《java经典程序及常见算法》由会员分享,可在线阅读,更多相关《java经典程序及常见算法(14页珍藏版)》请在金锄头文库上搜索。

1、if(year%4=0 & year%100!=0)|(year%400=0) System.out.println(year+ 年是闰年。);else System.out.println(year+ 年不是闰年。); -class Circle /定义父类-园类private double radius; /成员变量-园半径Circle() /构造方法radius=0.0;Circle(double r) /构造方法 radius=r; double getPerimeter() /成员方法-求园周长return 2*Math.PI*radius;double getArea() /成员方

2、法-求园面积return Math.PI*radius*radius;void disp() /成员方法-显示园半径、周长、面积System.out.println(园半径=+radius);System.out.println(园周长=+getPerimeter();System.out.println(园面积=+getArea();class Cylinder extends Circle /定义子类-圆柱类private double hight; /成员变量-园柱高Cylinder(double r,double h) /构造方法 super(r);hight=h;public dou

3、ble getVol() /成员方法-求园柱体积return getArea()*hight;public void dispVol() /成员方法-显示园柱体积System.out.println(圆柱体积=+getVol();public class TestCylinder /定义主类public static void main(String args) /主程入口 Circle Ci=new Circle(10.0); / 生成园类实例 Ci.disp(); / 调用园类的方法 Cylinder Cyl=new Cylinder(5.0,10.0); /生成圆柱类实例 Cyl.dis

4、p(); /调用父类方法 Cyl.dispVol(); /调用子类方法 - BufferedReader strin=new BufferedReader(new InputStreamReader(System.in);-FileInputStream fileIn=new FileInputStream(e:/myjava/Hello.java); int bytes=fileIn.read(buf,0,2056); String str=new String(buf,0,bytes); System.out.println(str);- public class IODemo publi

5、c static void main( String args ) int data = 100,101,102,103,104,105;int t;try DataOutputStream out = new DataOutputStream (new FileOutputStream(“dest.txt”); for(int i=0;i= 0;i-) t=in.readInt(datai); System.out.print(“ ”+t); System.out.println( ); in.close();catch(IOException e) System.out.println(e

6、.getMessage(); -import java.awt.Color;import java.awt.Graphics;import javax.swing.JApplet;public class MoveMessage extends JApplet implements Runnable String str;int time;private Thread thread;public void init() setBackground(Color.PINK);str = getParameter(message);String timeArg = getParameter(time

7、);time = Integer.parseInt(timeArg);thread = new Thread(this);public void start() thread.start();public void run() int x = 0;Graphics g = getGraphics();while (true) try Thread.sleep(time); catch (Exception e) e.printStackTrace();g.clearRect(0, 0, getWidth(), getHeight();g.drawString(str, x, 30);x +=

8、2;if (x = getWidth()x = 0;-编写一个Java程序实现多线程,在线程中输出线程的名字,隔300毫秒输出一次,共输出20次。(p202,例8-1) / programme name TestThread; / 声明一个子线程类Threaddemo; class ThreadDemo extends Thread public ThreadDemo(String str) super(str); public void run() for(int i=0;i20;i+)System.out.print(“ ”+this.getName();Try Sleep(300);ca

9、tch(InterruptedException e) System.out.println(e.getMessage(); Return;System.out.println(“ /end”);public class TestThread public static void main( String args ) ThreadDemo thread1=new ThreadDemo(“T1”); ThreadDemo thread2=new ThreadDemo(“T2”); ThreadDemo thread3=new ThreadDemo(“T3”); thread1.start();

10、 thread2.start(); thread3.start(); -11. 利用线程方法编写JApplet程序,实现在浏览器端实时动态显示本地系统时钟/ Programme Name Watch.javaimport java.applet.Applet;import java.awt.*;import java.text.DateFormat;import java.util.*;public class Watch extends Applet public void paint(Graphics g) Date d= new Date(); DateFormat ldf = Date

11、Format.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); /System.out.println(现在系统时间是(long):+ ldf.format(d); String time = ldf.format(d).toString(); g.drawString(time, 100, 100); try Thread.sleep(1000); catch (InterruptedException e) repaint(); JavaAppletDemo / 保存为Watch.html 文件-Scanner s = new Scanner(System.in); System.out.println(请输入一个字符串:); String str = s.nextLine();int n = s.nextInt();

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号