欢迎来到天天文库
浏览记录
ID:11941903
大小:284.50 KB
页数:50页
时间:2018-07-15
《《java编程语言原理与范例》课后实验源代码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、第一章实验一packagech01;importjava.text.SimpleDateFormat;importjava.util.Date;classTimerextendsThread{privateSimpleDateFormatsdf=newSimpleDateFormat("yyyy年MM月dd日HH:mm:ss");publicvoidrun(){while(true){System.out.print("r现在时间是:");Datenow=newDate();System.out.prin
2、t(sdf.format(now));try{sleep(1000);}catch(InterruptedExceptione){e.printStackTrace();}}}}publicclassClock{publicstaticvoidmain(String[]args){Timertimer=newTimer();timer.start();}}实验二packagech01;importjava.awt.event.MouseAdapter;importjava.awt.event.MouseEv
3、ent;importjava.util.Random;importjavax.swing.JButton;importjavax.swing.JFrame;publicclassMagicButtonextendsMouseAdapter{JFramewin;JButtonbutton=newJButton("你点不到我");Randomrand=newRandom();voidinitUI(){win=newJFrame();win.setLayout(null);button.setSize(100,4
4、0);button.addMouseListener(this);win.add(button);win.setSize(400,300);win.setResizable(false);win.setLocationRelativeTo(null);win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);win.setVisible(true);}publicstaticvoidmain(String[]args){MagicButtondemo=newMag
5、icButton();demo.initUI();}publicvoidmouseEntered(MouseEvente){intmouseX=button.getX()+e.getX();intmouseY=button.getY()+e.getY();while(true){intbuttonX=rand.nextInt(win.getWidth()-button.getWidth());intbuttonY=rand.nextInt(win.getHeight()-button.getHeight()
6、);button.setLocation(buttonX,buttonY);if(!button.getBounds().contains(mouseX,mouseY)){break;}}}}第二章实验一/*********************************2.交换两个变量的值(不允许使用中间变量)。**********************************/packagech03;publicclassExp2_2{publicstaticvoidmain(String[]args
7、){inta=2,b=3;ints=a*b;a=s/a;b=s/a;System.out.println("a="+a+",b="+b);}}实验二/*********************************3.逆序输出一个7位整数,如8639427输出为7249368(不允许使用循环语句)。**********************************/packagech03;publicclassExp2_3{publicstaticvoidmain(String[]args){longa
8、=8639427;System.out.print(a%10);System.out.print(a/10%10);System.out.print(a/100%10);System.out.print(a/1000%10);System.out.print(a/10000%10);System.out.print(a/100000%10);System.out.print(a/1000000%10);}}实验三
此文档下载收益归作者所有