}
}
x =Integer.parseInt(args[0]); y =Integer.parseInt(args[1]); op=args[2].charAt(0); switch(op)
{
case '+' : str=str+x+\ break; case '-' : str=str+x+\ break; case '*' : str=str+x+\ break; case '/' : str=str+x+\ break;
default : System.out.println(\不能识别的运算符 \ System.exit(0); }
System.out.println(str);
二、异常、多线程和输入输出处理
练习7 异常处理(1)
了解java中异常的处理:除运行时异常外,其他异常必须要捕捉或声明抛弃。例如,多线程编程或输入输出编程中异常的处理。
1.通过下面的程序了解异常的处理、以及异常发生时程序的执行情况。 import java.io.*; public class Class1 {
public static void main(String args[]) { int a=5; }
int b=0;
System.out.println(a/b); try { System.out.println(\ System.out.println(a/b); }
System.out.println(\
catch(ArithmeticException e)
{ System.out.println(\除数为0,这是不行的!\ finally
}
{ System.out.println(\ }
System.out.println(\异常已发生,但不影响程序的执行!\
}
练习8 多线程(5)
掌握:java中实现多线程的两种方法,能编写多线程的简单程序。多线程的相关知识还可与管道流结合、或应用他们编写小程序界面的简单动画等(参考练习15)。
1.请编写一个类,类名为subThread ,它是Thread 类的子类。该类中定义了含一个字符串参数的构造函数和run( )方法,方法先在命令行显示线程的名称,然后随机休眠小于1秒的时间,最后显示线程结束信息: \线程名。编写Application,在其中创建subThread类的三个对象t1、t2、t3,它们的名称分别为\、\、\,并启动这三个线程。 public class Class1 {
public static void main( String[ ] args ) {
Thread t1 = new subThread( \Thread t2 = new subThread( \Thread t3 = new subThread( \t1.start( );t2.start( );t3.start( );
}
}
class subThread extends Thread { public subThread( String str ) { super( str ); }
public void run( )
{ System.out.println( \ try { sleep( ( int )( Math.random( ) * 1000 ) ); }
catch( InterruptedException e ) { }
System.out.println( \ } }
2.请编写一个类,类名为subThread ,它是Thread 类的子类。该类中定义了含一个字符串参数的构造函数和run( )方法,方法中有一个for循环,循环一共进行5次,循环体中先在命令行显示该线程循环到了第几次,然后随机休眠小于1秒的时间,循环结束后显示线程结束信息: 线程名+\。编写Application,在其中创建subThread类的三个对象t1、t2、t3,它们的名称分别为\、\、\,并启动这三个线程。 public class Class1 {
public static void main( String[ ] args ) {
Thread t1 = new subThread( \Thread t2 = new subThread( \Thread t3 = new subThread( \t1.start( ); t2.start( ); t3.start( );
} }
class subThread extends Thread {
public subThread( String str )
{ super( str ); }
public void run( ) { for(int i=1;i<=5;i++) {
try
System.out.println( getName( )+\
{ sleep( ( int )( Math.random( ) * 1000 ) ); } catch( InterruptedException e ) { } }
}
System.out.println( \
}
3.请编写一个类,类名为MulThread ,类中定义了含一个字符串参数的构造函数,并实现了Runnable接口,接口中的run( )方法如下实现:方法中先在命令行显示该线程信息,然后随机休眠小于1秒的时间,最后后显示线程结束信息: \线程名。编写Application,在其中通过Runnable创建MulThread类的三个线程对象t1、t2、t3,并启动这三个线程。 public class Class1 { }
public static void main( String[ ] args ) { }
Runnable r1 =new MulThread( \Runnable r2 =new MulThread( \Runnable r3 =new MulThread( \Thread t1 = new Thread( r1 ); Thread t2 = new Thread( r2 ); Thread t3 = new Thread( r3 ); t1.start( );t2.start( );t3.start( );
class MulThread implements Runnable {
String s;
public MulThread(String str) { s=str;} public void run( ) {
System.out.println(s); try {
Thread.sleep( ( int ) ( Math.random( ) * 1000 ) ); }
catch( InterruptedException e ){ }
System.out.println( \ \
}
}
4.编写小程序实现一个数字时钟。 import java.awt.*; import java.applet.*;
import java.util.Calendar;
public class Applet1 extends Applet implements Runnable {
Thread timeThread; Font wordFont; int year,month,day; int weekday;
int hour,minute,second; public void init() { }
public void start() {
if(timeThread==null) {
timeThread=new Thread(this); timeThread.start();
this.setBackground(Color.black);
wordFont=new Font(\楷体_gb2312\
} }
public void stop() {
if(timeThread!=null) { }
timeThread.stop(); timeThread=null;
}
public void run() {
while(true) { Calendar time=Calendar.getInstance();//创建类的实例
year=time.get(Calendar.YEAR);
month=time.get(Calendar.MONTH);
day=time.get(Calendar.DAY_OF_MONTH ); weekday=time.get(Calendar.DAY_OF_WEEK); hour=time.get(Calendar.HOUR); minute=time.get(Calendar.MINUTE); second=time.get(Calendar.SECOND); repaint ();
}
try
{ Thread.sleep( 300); } catch (InterruptedException e) { }
}
public void paint (Graphics g) { String s1=year+\年\月\日\
String s2=\星期\
String s3=hour+\g.setFont (wordFont); g.setColor (Color.green); g.drawString (s1, 20, 50); g.drawString (s2, 20, 120); g.drawString (s3, 20, 200);
} }
5.编写小程序实现Runnable接口,通过多线程实现在小程序窗口中不断的显示自然数:从1直到100。 import java.awt.*; import java.applet.*;
public class Javaapplet extends Applet implements Runnable {
int counter=0; Thread t;
public void init() { t=new Thread(this); }
t.start();
public void run() { while( counter<=100 ) {
}
counter++; try
{ Thread.sleep(1000); } catch ( InterruptedException e ) { } repaint();
}
public void paint( Graphics g ) {
setBackground(Color.black); g.setColor(Color.green);
g.setFont(new Font(\Times New Roman\g.drawString( String.valueOf(counter),60,60 );