1、Java2实用教程实验指导模版代码Java-2实用教程实验指导模版代码上机实践1 初识Java实验1 一个简单的应用程序程序模板public class Hello public static void main (String args ) 【代码1】 ; import .*;public class FirstApplet extends Applet public void paint(Graphics g) ; 【代码1】;class SunRise public static void main(String args ) SquareEquation equation=new Sq
2、uareEquation(4,5,1); (); (-3,4,5); (); 上机实践4 继承与接口实验1 继承程序模板class People protected double weight,height; public void speakHello() yayawawa); public void averageHeight() height=173; average height:+height); public void averageWeight() weight=70; average weight:+weight); class ChinaPeople extends Peop
3、le 【代码1】 ;import DateExample public static void main(String args ) String str=(输入第一个日期的年份:); int yearOne=(str); str=(输入该年的月份:); int monthOne=(str); str=(输入该月份的日期:); int dayOne=(str); str=(输入第二个日期的年份:); int yearTwo=(str); str=(输入该年的月份:); int monthTwo=(str); str=(输入该月份的日期:); int dayTwo=(str); Calendar
4、 calendar=【代码1】 ;class BigIntegerExample public static void main(String args) BigInteger n1=new BigInteger(654321), n2=new BigInteger(456789), result=null; n1的二进制表示:+(2); n2的八进制表示:+(8); result=【代码1】;import .*;public class GuessExample extends Applet implements ActionListener int number; TextField in
5、putNumber; Label feedBack; Button nuttonGetNumber,buttonEnter; public void init() nuttonGetNumber=new Button(得到一个随机数); feedBack=new Label (无反馈信息,; ; inputNumber=new TextField(0,5); buttonEnter=new Button(确定); 【代码1】 ;import .*;import class SignalExample extends Applet implements ActionListener Button
6、 buttonRed,buttonGreen,buttonYellow; SignalCanvas signal; public void init() buttonRed=【代码1】 ;import .*;import .*;public class Boy extends Applet implements ActionListener Label a=new Label42; Button titleName=new Button7; String name=星期日,星期一,星期二,星期三, 星期四,星期五,星期六; Button nextMonth,previousMonth; int
7、 year=2005,month=1; CalendarBean calendar; Label showMessage=new Label(,; public void init() 【代码1】 etText(dayi); nextMonth=new Button(下月); previousMonth=new Button(上月); (this); (this); Panel pNorth=new Panel(), pSouth=new Panel(); (previousMonth); (nextMonth); (showMessage); (日历:+()+年+ ()+月 ); Scrol
8、lPane scrollPane=new ScrollPane(); (pCenter); 【代码5】etText(dayi); else if()=previousMonth) month=month-1; if(month1) month=12; (month); String day=(); for(int i=0;i42;i+) ai.setText(dayi); (日历:+()+年+()+月 ); class CalendarBean String day; int year=2005,month=0; public void setYear(int year) =year; pub
9、lic int getYear() return year; public void setMonth(int month) =month; public int getMonth() return month; public String getCalendar() String a=new String42; et(year,month-1,1); int 星期几=日历.get-1; int day=0; if(month=1|month=3|month=5|month=7|month=8|month=10|month=12) day=31; if(month=4|month=6|mont
10、h=9|month=11) day=30; if(month=2) if(year%4=0)&(year%100!=0)|(year%400=0) day=29; else day=28; for(int i=星期几,n=1;ih/2) goDown(man); if(yh/2) goUp(man); if(xw/2) goRight(man); public void mouseReleased(MouseEvent e) public void mouseEntered(MouseEvent e) public void mouseExited(MouseEvent e) public v
11、oid mouseClicked(MouseEvent e) public void goDown(Person man) boolean move=true; Rectangle manRect=(); int x=().x; int y=().y; y=y+50; (x,y); Rectangle belowRect=(); for(int i=0;i10;i+) Rectangle personRect=personi.getBounds(); if(personRect)&!=i) move=false; if(belowRect) move=false; if(move=true)
12、(x,y); public void goUp(Person man) boolean move=true; Rectangle manRect=(); int x=().x; int y=().y; y=y-50; (x,y); Rectangle aboveRect=(); for(int i=0;i10;i+) Rectangle personRect=personi.getBounds(); if(personRect)&!=i) move=false; if(aboveRect) move=false; if(move=true) (x,y); public void goLeft(
13、Person man) boolean move=true; Rectangle manRect=(); int x=().x; int y=().y; x=x-50; (x,y); Rectangle leftRect=(); for(int i=0;i10;i+) Rectangle personRect=personi.getBounds(); if(personRect)&!=i) move=false; if(leftRect) move=false; if(move=true) (x,y); public void goRight(Person man) boolean move=
14、true; Rectangle manRect=(); int x=().x; int y=().y; x=x+50; (x,y); Rectangle rightRect=(); for(int i=0;i=200) y=10; (x,y); try (200); catch(InterruptedException e2) if(dead=true) 【代码5】 ;public class SecretExample public static void main(String args ) File fileOne=new File(), fileTwo=new File(); char
15、 b=new char100; try FileReader in=【代码1】;public class ReadExample public static void main(String args ) File file=new File(c:/1000,); File tempFile=new File(); try FileReader inOne=【代码1】;public class RandomExample public static void main(String args);import .*;public class ObjectExample public static
16、 void main(String args ) Calendar calendar1=(); ;import .*;import .*;import .*;public class ReadFile extends Applet implements ActionListener,Runnable File file; Choice choice; TextArea text; Thread thread; String item=null; Button button; URL url; public void init() choice=new Choice(); thread=new
17、Thread(this); int N=(getParameter(总数); for(int i=1;i=N;i+) (getParameter(i); button=new Button(开始读取); text=new TextArea(12,40); (this); add(choice); add(button); add(text); public void actionPerformed(ActionEvent e) (null); item=(); item=(); if(!() thread=new Thread(this); try (); catch(Exception ex
18、p) public void run() try url=new URL(getCodeBase(),item); InputStream in= 【代码1】 ;import .*;import .*;import Client extends Frame implements Runnable,ActionListener Button connection; Socket socket=null; ObjectInputStream in=null; ObjectOutputStream out=null; Thread thread; public Client() socket=new
19、 Socket(); connection=new Button(连接服务器,读取文本区对象); add(connection,; (this); thread = new Thread(this); setBounds(100,100,360,310); setVisible(true); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) (0); ); public void run() while(true) try TextArea text=(TextArea)(); add(
20、text,; validate(); catch(Exception e) break; public void actionPerformed(ActionEvent e) if()=connection) try if() else InetAddress address=(); InetSocketAddress socketAddress=【代码1】;import .*;import .*;import .*;public class Server public static void main(String args) ServerSocket server=null; Server
21、Thread thread; Socket you=null; while(true) try server=【代码1】tart(); ;import .*; import .*;class ImageCanvas extends Canvas Image image=null; public ImageCanvas() setSize(200,200); public void paint(Graphics g) if(image!=null) (image,0,0,getSize().width,getSize().height,this); public void setImage(Image image) =image; class Client extends Fram
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1