求以下题目的java代码
[color=#000000][b][/b][b][size=16pt][/size][/b][/color]
[b][size=16pt][font=Times New Roman][color=#000000]1.
[/color][/font][/size][/b][color=#000000][b][font=宋体][size=16pt]定义并[/size][/font][/b][b][size=16pt][font=Times New Roman]Test[/font][/size][/b][b][font=宋体][size=16pt]类,定义[/size][/font][/b][b][size=16pt][font=Times New Roman]division[/font][/size][/b][b][font=宋体][size=16pt]方法,功能是返回[/size][/font][/b][b][size=16pt][font=Times New Roman]a/b[/font][/size][/b][b][font=宋体][size=16pt],其中[/size][/font][/b][b][size=16pt][font=Times New Roman]a,b[/font][/size][/b][b][font=宋体][size=16pt]为[/size][/font][/b][b][size=16pt][font=Times New Roman]int[/font][/size][/b][b][font=宋体][size=16pt]类型。在该类的[/size][/font][/b][b][size=16pt][font=Times New Roman]main[/font][/size][/b][b][font=宋体][size=16pt]方法中,调用[/size][/font][/b][b][size=16pt][font=Times New Roman]division[/font][/size][/b][b][font=宋体][size=16pt]方法,要求对可能出现的异常进行捕获。编译并运行该类;[/size][/font][/b][b][size=16pt][/size][/b][/color]
[b][size=16pt][font=Times New Roman][color=#000000]2.
[/color][/font][/size][/b][color=#000000][b][font=宋体][size=16pt]定义并[/size][/font][/b][b][size=16pt][font=Times New Roman]Test[/font][/size][/b][b][font=宋体][size=16pt]类,定义[/size][/font][/b][b][size=16pt][font=Times New Roman]division[/font][/size][/b][b][font=宋体][size=16pt]方法,功能是返回[/size][/font][/b][b][size=16pt][font=Times New Roman]a/b[/font][/size][/b][b][font=宋体][size=16pt],其中[/size][/font][/b][b][size=16pt][font=Times New Roman]a,b[/font][/size][/b][b][font=宋体][size=16pt]为[/size][/font][/b][b][size=16pt][font=Times New Roman]int[/font][/size][/b][b][font=宋体][size=16pt]类型,该方法申明抛出[/size][/font][/b][b][size=16pt][font=Times New Roman]Exception[/font][/size][/b][b][font=宋体][size=16pt]异常,在该类的[/size][/font][/b][b][size=16pt][font=Times New Roman]main[/font][/size][/b][b][font=宋体][size=16pt]方法中,调用[/size][/font][/b][b][size=16pt][font=Times New Roman]division[/font][/size][/b][b][font=宋体][size=16pt]方法,要求对可能出现的异常进行捕获。编译并运行该类;[/size][/font][/b][b][size=16pt][/size][/b][/color]
[b][size=16pt][font=Times New Roman][color=#000000]3.
[/color][/font][/size][/b][color=#000000][b][font=宋体][size=16pt]定义并[/size][/font][/b][b][size=16pt][font=Times New Roman]Test[/font][/size][/b][b][font=宋体][size=16pt]类,通过从[/size][/font][/b][b][size=16pt][font=Times New Roman]Thread[/font][/size][/b][b][font=宋体][size=16pt]类继承的方法让[/size][/font][/b][b][size=16pt][font=Times New Roman]Test[/font][/size][/b][b][font=宋体][size=16pt]类成为一个多线程类,该线程实现循环显示线程名字的功能。在[/size][/font][/b][b][size=16pt][font=Times New Roman]main[/font][/size][/b][b][font=宋体][size=16pt]方法中生成[/size][/font][/b][b][size=16pt][font=Times New Roman]4[/font][/size][/b][b][font=宋体][size=16pt]个线程并运行。[/size][/font][/b][b][size=16pt][/size][/b][/color]
[b][size=16pt][font=Times New Roman][color=#000000]4.
[/color][/font][/size][/b][color=#000000][b][font=宋体][size=16pt]定义并[/size][/font][/b][b][size=16pt][font=Times New Roman]Test[/font][/size][/b][b][font=宋体][size=16pt]类,通过用实现[/size][/font][/b][b][size=16pt][font=Times New Roman]Runnable[/font][/size][/b][b][font=宋体][size=16pt]接口的方法让[/size][/font][/b][b][size=16pt][font=Times New Roman]Test[/font][/size][/b][b][font=宋体][size=16pt]类成为一个多线程类,该线程实现循环显示线程名字的功能。在[/size][/font][/b][b][size=16pt][font=Times New Roman]main[/font][/size][/b][b][font=宋体][size=16pt]方法中生成[/size][/font][/b][b][size=16pt][font=Times New Roman]4[/font][/size][/b][b][font=宋体][size=16pt]个线程并运行。[/size][/font][/b][/color]
[color=#000000][b]
[/b][/color]
[color=#000000][b]
[/b][/color][b][size=16pt][/size][/b] 自己做吧,全部都是基础题。。。。。。。。。。。。 [code]public class Test {
private static int a;
private static int b;
public int division() {
try {
int c;
c = a / b;
return c;
} catch (RuntimeException e) {
e.printStackTrace();
System.out.println("b不能为0,计算失败");
}
return 0;
}
public static void main(String[] args) {
try {
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);
} catch (Exception e) {
e.printStackTrace();
System.out.println("请输入参数a,b");
}
Test t = new Test();
System.out.println(t.division());
}
}[/code] [code]public class Test2 {
private static int a;
private static int b;
public int division()throws Exception {
int c;
c = a / b;
return c;
}
public static void main(String[] args) {
try {
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);
} catch (Exception e) {
e.printStackTrace();
System.out.println("请输入参数a,b");
}
Test2 t = new Test2();
try {
System.out.println(t.division());
} catch (Exception e) {
e.printStackTrace();
System.out.println("b不能为0,计算失败");
}
}
}[/code] [code]public class Test3 {
public static void main(String[] args) {
//创建4个线程
for (int i = 1; i < 5; i++) {
Test33 rob = new Test33(i);
Thread t = new Thread(rob);
t.start();
}
}
}
class Test33 extends Thread {
private int k;
public Test33(int n) {
this.k = n;
}
public void run() {
//循环打印9次
for (int m = 1; m < 10; m++) {
System.out.println("第" + k + "线程" + ",打印第" + m + "次");
}
}
}[/code] 刚学,共勉!
的 嘿嘿,我也是,共勉
有问题互相交流啊 第4个TEST,实现接口完成线程
package Mulder.com;
public class TestJoin {
public static void main(String[] args) {
System.out.println ("主线程开始");
MyThread my = new MyThread();
Thread t = new Thread(my);
t.start();
try {
t.join();//哪一个线程对象调用,就得等哪一个线程运行完
Thread.sleep(2000);//在哪一个线程中,就暂停哪一个线程
System.out.println ("MAIN");
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println ("主线程结束");
}
}
class MyThread implements Runnable {
public void run() {
for (int i = 0; i<5; i++) {
try {
System.out.println (i);
Thread.sleep(2000);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
页:
[1]
