编写一个Java应用程序,从键盘读取用户输入两个字符串,并重载3个函数分别实现这两个字符串的拼接、整数相加和浮点数相加。要进行异常处理,对输入的不符合要求的字符串提示给用户,不能使程序崩溃。(p39,例2-10,2-11)
//programme name Strinput.java
import java.io.*;
public class Strinput
{
public static void main(String args[]) {
String s1,s2,ss,si,sf;
int i1,i2;
float f1,f2;
BufferedReader strin=new BufferedReader(new InputStreamReader(System.in));
try{System.out.print ("输入第一个字符串:" );
s1= strin.readLine();
System.out.print ("输入第二个字符串:" );
s2= strin.readLine();}
catch(Exception e){ System.out.println(e.getMessage());}
i1 = Integer.parseInt(s1);
i2 = Integer.parseInt(s2);
f1 = Float.parseFloat(s1);
f2 = Float.parseFloat(s2);
ss = strAdd(s1,s2);
si = strAdd(i1,i2);
sf = strAdd(f1,f2);
System.out.println ("输入的二个字符串相加结果为:"+ss );
System.out.println ("输入字符串转换为整数相加结果为:"+si );
System.out.println ("输入字符串转换为浮点数相加结果为:"+sf );
}
String strAdd(String str1,String str2) {
return str1+str2;
}
String strAdd(int int1,int int2) {
return String.valueOf(int1+int2);
}
String strAdd(float flt1,float flt2) {
return String.valueOf (flt1+flt2);
}
}
6. 应用FileInputStream类,编写应用程序,从磁盘上读取一个Java程序,并将源程序代码显示在屏幕上。(被读取的文件路径为:E:/myjava/Hello.java)
// Programme Name FISDemo.java
import java.io.*;
public class FISDemo {
public static void main(String args[]) {
byte[] buf=new byte[2056];
try{
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);
}catch(Exception e){
e.printStackTrace( );
}
}
7、编写一个Java程序将当100,101,102,103,104,105个数以数组的形式写入到Dest.txt文件中,并以相反的顺序读出显示在屏幕上。(p190,例7-2)
//programme name IODemo.java
import java.io.*;
public class IODemo {
public 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<data.length;i++)
out.WriteInt(data[i]);
out.close();
DataInputStream in = new DataInputStream (new FileInputStream(“dest.txt”));
for(int i= data.length-1;i>= 0;i--) {
t=in.readInt(data[i]);
System.out.print(“ ”+t);
}
System.out.println( );
in.close();
}catch(IOException e)
{ System.out.println(e.getMessage());}
}
}
8. 利用Applet类和Runnable接口实现滚动字幕,其中字幕文字(“学好Java有工作”)和时间间隔(“200”)需要由页面文件中<Applet>标记的子标记<Param>传递。
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");
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 += 2;
if (x >= getWidth())
x = 0;
}
}
}
<html>
<body>
<Applet code="MoveMessage.class" width=400 height=60>
<param name="message" value="学好java有工作">
<param name="time" value="200">
</Applet>
</body>
</html>
9、编写一个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;i<20;i++){
System.out.print(“ ”+this.getName());
Try {
Sleep(300);
}catch(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();
thread2.start();
thread3.start();
}
}
10. 编写程序,在屏幕上显示带标题的窗口,并添加一个按钮。当用户单击按钮时,结束程序。
// Programme Name ButtonEventDemo.java
import javax.swing.*;
import java.awt.event.*;
public class ButtonEventDemo extends JPanel implements ActionListener{
protected JButton b1; //声明一个按钮对象
public ButtonEventDemo() { //构造方法
ImageIcon ButtonIcon = new ImageIcon("images/green.png"); //创建按钮的图标对象
b1 = new JButton("退出按钮", ButtonIcon); //生成按钮对象
b1.setMnemonic(KeyEvent.VK_E); //设置b1的助记符是Alt+E
b1.setToolTipText("这是退出按钮。"); // 设置按钮提示条
this.add(b1); //往面板对象中加载按钮
b1.addActionListener(this); //本类对象注册为按钮的事件监听器
}
public void actionPerformed(ActionEvent e){ //按钮事件响应方法
System.exit(0); //按b1则退出主程序
}
private static void createGUI() { //创建窗体
JFrame.setDefaultLookAndFeelDecorated(true); //设置java隐含观感
JFrame frame = new JFrame("按钮测试"); //生成应用程序主窗体
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置关闭时隐含操作
ButtonEventDemo CPane = new ButtonEventDemo(); //生成主类对象--面板
CPane.setOpaque(true); //面板要求不透明
frame.setContentPane(CPane); //设置主类对象为主窗体的内容面板
frame.pack(); //主窗体紧缩显示
frame.setVisible(true); //设置主窗体可见
}
public static void main(String[] args) { //将createGUI()列入线程
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGUI();
}
});
}
} |
|