package chapter14.homework;
import java.io.*;
public class Chapter14Homework2 {
public static void main(String[] args) {
try{
//這邊忘記 PrintWriter 的用法要多練習 PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("test2.txt")));
pw.println("A long time ago,");
pw.println("There was a little girl.");
pw.close();
}
catch(IOException e)
{
System.out.println("輸出入錯誤");
}
}
}
package chapter14.homework;
import java.io.*;
public class Chapter14Homework3 {
public static void main(String[] args) {
if( args.length != 1)
{
System.out.println("請指定正確檔案名稱");
System.exit(1);
}
try{
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(args[0])));
pw.println("A long time ago,");
pw.println("There was a little girl.");
pw.close();
}
catch(IOException e){
System.out.println("輸出入錯誤");
}
}
}
package chapter15.lab1;
//啟動執行緒
//延伸執行緒類別 Thread
class Car extends Thread
{
private String name;
public Car(String nm)
{
name = nm;
}
//這邊一定要定義一個 run() 方法 public void run()
{
for( int i=0; i<5;i++)
{
System.out.println("正在進行"+name+"的處理");
}
}
}
class Sample1 {
public static void main(String[] args) {
Car car1 = new Car("1號車");
//啟動新的執行緒 car1.start();
for(int i=0; i<5; i++)
{
System.out.println("正在進行main()的處理");
}
}
}
package chapter15.lab2;
//啟動多個執行緒
//延伸執行緒類別 Thread
class Car extends Thread
{
private String name;
public Car(String nm)
{
name = nm;
}
//這邊一定要定義一個 run() 方法 public void run()
{
for( int i=0; i<5;i++)
{
System.out.println("正在進行"+name+"的處理");
}
}
}
class Sample2 {
public static void main(String[] args) {
//執行 2 個執行緒 Car car1 = new Car("1 號車");
car1.start();
Car car2 = new Car("2號車");
car2.start();
for (int i=0; i< 5 ; i++)
{
System.out.println("正在進行main()的處理");
}
}
}
package chapter15.lab3;
//暫停執行緒
class Car extends Thread
{
private String name;
public Car(String nm)
{
name = nm;
}
public void run()
{
for(int i=0; i<5; i++)
{
try{
//處理這個敘述的時候會暫停 1 秒處理執行緒, sleep() 的單位是千分之ㄧ秒 sleep(1000);
System.out.println("正在進行"+name+"的處理");
}
//處理例外狀況 catch(InterruptedException e)
{
}
}
}
}
class Sample3 {
public static void main(String[] args) {
Car car1 = new Car("1號車");
car1.start();
for(int i=0; i<5; i++)
{
System.out.println("正在處理main()");
}
}
}
package chapter15.lab4;
//暫停別的執行緒
class Car extends Thread
{
private String name;
public Car(String nm)
{
name = nm;
}
public void run()
{
for(int i=0; i<5; i++)
{
System.out.println("正在進行"+name+"的處理");
}
}
}
class Sample4 {
public static void main(String[] args) {
//這次暫停的是 main這邊 Car car1 = new Car("1號車");
car1.start();
for(int i=0; i<5; i++)
{
try{
//每次執行這個執行緒都會暫停1秒 Thread.sleep(1000);
System.out.println("正在進行main()的處理");
}
catch(InterruptedException e){}
}
}
}
package chapter15.lab5;
//等待執行緒
class Car extends Thread
{
private String name;
public Car(String nm)
{
name = nm;
}
public void run()
{
for(int i=0; i<5; i++)
{
System.out.println("正在進行"+name+"的處理");
}
}
}
class Sample5 {
public static void main(String[] args) {
Car car1 = new Car("1號車");
car1.start();
try{
//會等待 car1 這個執行緒結束之後,再執行其他的執行緒 car1.join();
}
catch(InterruptedException e){}
System.out.println("正在處理main()");
}
}
package chapter15.lab6;
//實作Runnable 介面 來執行執行緒
//如果執行 Runnable 的介面一定要執行 Runnable.run()
class Car implements Runnable
{
private String name;
public Car(String nm)
{
name = nm;
}
public void run()
{
for (int i=0; i<5; i++)
{
System.out.println("正在進行"+name+"的處理");
}
}
}
class Sample6 {
public static void main(String[] args) {
Car car1 = new Car("1號車");
//產生 Thread 類別的物件, 然後把 car1 變數放進去 Thread th1 = new Thread(car1);
//啟動執行緒 th1.start();
for(int i=0; i<5; i++)
{
System.out.println("正在進行main()的處理");
}
}
}
package chapter15.lab7;
//同步: 執行緒所引起互相扺觸狀況
//客運公司類別
class Company
{
//先初始化車資為 0 private int sum = 0;
//統計賺到的車資, 加上 synchronized 可以做到某執行緒呼叫 add()方法時, 其他執行緒不得再呼叫 add() 方法 public synchronized void add(int a)
{
int tmp = sum;
System.out.println("現在總計"+tmp+"元");
System.out.println("賺了"+a+"元");
tmp = tmp +a;
System.out.println("總計為"+tmp+"元");
sum = tmp;
}
}
//司機類別
class Driver extends Thread
{
//建立一個 Company 類別的變數 comp private Company comp;
public Driver(Company c)
{
comp = c;
}
public void run()
{
for(int i=0; i<3; i++)
{
comp.add(50);
}
}
}
class Sample7 {
public static void main(String[] args) {
//產生客運公司的物件 Company cmp = new Company();
Driver drv1 = new Driver(cmp);
drv1.start();
Driver drv2 = new Driver(cmp);
drv2.start();
}
}
package chapter15.homework2;
class Car implements Runnable
{
private String name;
public Car(String nm)
{
name = nm;
}
//這邊一定要定義一個 run() 方法 public void run()
{
for( int i=0; i<5;i++)
{
System.out.println("正在進行"+name+"的處理");
}
}
}
class Chapter15Homework2 {
public static void main(String[] args) {
//執行 2 個執行緒 Car car1 = new Car("1 號車");
Thread th1 = new Thread(car1);
th1.start();
Car car2 = new Car("2號車");
//跟使用 Thread 子類別 car2.start(); 方法不同的是
//要先產生 Thread 類別物件, 然後把變數放進去
Thread th2 = new Thread(car2);
//再啟動執行緒 ^^ 這次我有記住 th2.start();
for (int i=0; i< 5 ; i++)
{
System.out.println("正在進行main()的處理");
}
}
}
package chapter15.homework3;
class Car implements Runnable
{
private String name;
public Car(String nm)
{
name = nm;
}
public void run()
{
for(int i=0; i<5; i++)
{
try{
//處理這個敘述的時候會暫停 1 秒處理執行緒, sleep() 的單位是千分之ㄧ秒
//由於不是直接繼承 Thread 子類別, 所以 sleep(); 不能直接寫, 要使用 Thread.sleep()
Thread.sleep(1000);
System.out.println("正在進行"+name+"的處理");
}
//處理例外狀況 catch(InterruptedException e)
{
}
}
}
}
class Chapter15Homework3 {
public static void main(String[] args) {
Car car1 = new Car("1號車");
//car1.start();
Thread th = new Thread(car1);
th.start();
for(int i=0; i<5; i++)
{
System.out.println("正在處理main()");
}
}
}
package chapter16.lab1;
//Java Applet 練習
//匯入相關套件
import java.applet.Applet;
import java.awt.Graphics;
//要使用 Java Applet 要從Applet 延伸子類別 public class Sample1 extends Applet
{
//要顯示 Applet的時候呼叫的方法 public void paint(Graphics g)
{
//drawString("顯示字串", x, y) g.drawString("歡迎來到 java Applet", 10, 10);
}
}
package chapter16.lab2;
//劃線的Applet
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class Sample2 extends Applet
{
public void paint(Graphics g)
{
//設定顏色為紅色 g.setColor(Color.red);
//drawLine(線段起點座標 線段終點座標) g.drawLine(10, 10, 100, 100);
//顯示文字 g.drawString("Hello",100,100);
}
}
package chapter16.lab3;
//顯示圖片
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
public class Sample3 extends Applet
{
Image img;
//這個方法只有在 Applet 啟動才會呼叫一次 public void init()
{
img = getImage(getDocumentBase(),"Sunset.jpg");
}
public void paint(Graphics g)
{
g.drawImage(img, 10, 10, this);
}
}
package chapter16.lab4;
//與滑鼠產生互動
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.Color;
//實作 MouseListener public class Sample4 extends Applet implements MouseListener
{
//初始 x, y 座標 int x = 10;
int y = 10;
public void init()
{
//呼叫 addMouseListener 方法, 接收滑鼠已經按下這個事件準備工作 addMouseListener(this);
}
public void mouseClicked(MouseEvent e)
{
}
//滑鼠游標進入 Applet 範圍之後 public void mouseEntered(MouseEvent e)
{
}
//滑鼠游標離開 Applet 範圍之後 public void mouseExited(MouseEvent e){}
//當滑鼠按下要執行的事情 public void mousePressed(MouseEvent e)
{
//記住滑鼠按下的位置 x = e.getX();
y = e.getY();
//更新螢幕顯示 repaint();
}
public void mouseReleased(MouseEvent e){}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.fillOval(x, y, 10, 10);
}
}
package chapter16.lab5;
//與滑鼠產生互動
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Sample5 extends Applet
{
int x = 10;
int y = 10;
public void init()
{
//使用 MouseAdapter() 這個類別會讓相關設定更簡單 addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
x = e.getX();
y = e.getY();
repaint();
}
});
}
public void paint(Graphics g)
{
g.fillOval(x, y, 10, 10);
}
}
package chapter16.lab6;
//使用 AWT (Abstract Window Toolkit)
import java.applet.Applet;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
//實作 ActionListener , 來讓按鈕被按下執行某些動作 public class Sample6 extends Applet implements ActionListener
{
Button bt;
public void init()
{
bt = new Button("start");
//接收按鈕被按下的準備工作 bt.addActionListener(this);
//使用 add() 方法將它加到 Applet 上面 add(bt);
}
public void actionPerformed(ActionEvent ae)
{
//按鈕被按下的處理 bt.setLabel("stop"); }
}
package chapter16.lab7;//讓數字動起來
import java.applet.Applet;
import java.awt.Graphics;
public class Sample7 extends Applet implements Runnable
{
int num;
public void init()
{
Thread th;
th = new Thread(this);
th.start();
}
public void run()
{
try{
for(int i=0; i<10; i++)
{
num = i;
repaint();
Thread.sleep(1000);
}
}
catch(InterruptedException e){}
}
public void paint(Graphics g)
{
String str = "數字" + num + "!";
g.drawString(str, 10, 10);
}
}
import java.io.*;
public class Chapter14Homework2 {
public static void main(String[] args) {
try{
//這邊忘記 PrintWriter 的用法要多練習 PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("test2.txt")));
pw.println("A long time ago,");
pw.println("There was a little girl.");
pw.close();
}
catch(IOException e)
{
System.out.println("輸出入錯誤");
}
}
}
package chapter14.homework;
import java.io.*;
public class Chapter14Homework3 {
public static void main(String[] args) {
if( args.length != 1)
{
System.out.println("請指定正確檔案名稱");
System.exit(1);
}
try{
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(args[0])));
pw.println("A long time ago,");
pw.println("There was a little girl.");
pw.close();
}
catch(IOException e){
System.out.println("輸出入錯誤");
}
}
}
package chapter15.lab1;
//啟動執行緒
//延伸執行緒類別 Thread
class Car extends Thread
{
private String name;
public Car(String nm)
{
name = nm;
}
//這邊一定要定義一個 run() 方法 public void run()
{
for( int i=0; i<5;i++)
{
System.out.println("正在進行"+name+"的處理");
}
}
}
class Sample1 {
public static void main(String[] args) {
Car car1 = new Car("1號車");
//啟動新的執行緒 car1.start();
for(int i=0; i<5; i++)
{
System.out.println("正在進行main()的處理");
}
}
}
package chapter15.lab2;
//啟動多個執行緒
//延伸執行緒類別 Thread
class Car extends Thread
{
private String name;
public Car(String nm)
{
name = nm;
}
//這邊一定要定義一個 run() 方法 public void run()
{
for( int i=0; i<5;i++)
{
System.out.println("正在進行"+name+"的處理");
}
}
}
class Sample2 {
public static void main(String[] args) {
//執行 2 個執行緒 Car car1 = new Car("1 號車");
car1.start();
Car car2 = new Car("2號車");
car2.start();
for (int i=0; i< 5 ; i++)
{
System.out.println("正在進行main()的處理");
}
}
}
package chapter15.lab3;
//暫停執行緒
class Car extends Thread
{
private String name;
public Car(String nm)
{
name = nm;
}
public void run()
{
for(int i=0; i<5; i++)
{
try{
//處理這個敘述的時候會暫停 1 秒處理執行緒, sleep() 的單位是千分之ㄧ秒 sleep(1000);
System.out.println("正在進行"+name+"的處理");
}
//處理例外狀況 catch(InterruptedException e)
{
}
}
}
}
class Sample3 {
public static void main(String[] args) {
Car car1 = new Car("1號車");
car1.start();
for(int i=0; i<5; i++)
{
System.out.println("正在處理main()");
}
}
}
package chapter15.lab4;
//暫停別的執行緒
class Car extends Thread
{
private String name;
public Car(String nm)
{
name = nm;
}
public void run()
{
for(int i=0; i<5; i++)
{
System.out.println("正在進行"+name+"的處理");
}
}
}
class Sample4 {
public static void main(String[] args) {
//這次暫停的是 main這邊 Car car1 = new Car("1號車");
car1.start();
for(int i=0; i<5; i++)
{
try{
//每次執行這個執行緒都會暫停1秒 Thread.sleep(1000);
System.out.println("正在進行main()的處理");
}
catch(InterruptedException e){}
}
}
}
package chapter15.lab5;
//等待執行緒
class Car extends Thread
{
private String name;
public Car(String nm)
{
name = nm;
}
public void run()
{
for(int i=0; i<5; i++)
{
System.out.println("正在進行"+name+"的處理");
}
}
}
class Sample5 {
public static void main(String[] args) {
Car car1 = new Car("1號車");
car1.start();
try{
//會等待 car1 這個執行緒結束之後,再執行其他的執行緒 car1.join();
}
catch(InterruptedException e){}
System.out.println("正在處理main()");
}
}
package chapter15.lab6;
//實作Runnable 介面 來執行執行緒
//如果執行 Runnable 的介面一定要執行 Runnable.run()
class Car implements Runnable
{
private String name;
public Car(String nm)
{
name = nm;
}
public void run()
{
for (int i=0; i<5; i++)
{
System.out.println("正在進行"+name+"的處理");
}
}
}
class Sample6 {
public static void main(String[] args) {
Car car1 = new Car("1號車");
//產生 Thread 類別的物件, 然後把 car1 變數放進去 Thread th1 = new Thread(car1);
//啟動執行緒 th1.start();
for(int i=0; i<5; i++)
{
System.out.println("正在進行main()的處理");
}
}
}
package chapter15.lab7;
//同步: 執行緒所引起互相扺觸狀況
//客運公司類別
class Company
{
//先初始化車資為 0 private int sum = 0;
//統計賺到的車資, 加上 synchronized 可以做到某執行緒呼叫 add()方法時, 其他執行緒不得再呼叫 add() 方法 public synchronized void add(int a)
{
int tmp = sum;
System.out.println("現在總計"+tmp+"元");
System.out.println("賺了"+a+"元");
tmp = tmp +a;
System.out.println("總計為"+tmp+"元");
sum = tmp;
}
}
//司機類別
class Driver extends Thread
{
//建立一個 Company 類別的變數 comp private Company comp;
public Driver(Company c)
{
comp = c;
}
public void run()
{
for(int i=0; i<3; i++)
{
comp.add(50);
}
}
}
class Sample7 {
public static void main(String[] args) {
//產生客運公司的物件 Company cmp = new Company();
Driver drv1 = new Driver(cmp);
drv1.start();
Driver drv2 = new Driver(cmp);
drv2.start();
}
}
package chapter15.homework2;
class Car implements Runnable
{
private String name;
public Car(String nm)
{
name = nm;
}
//這邊一定要定義一個 run() 方法 public void run()
{
for( int i=0; i<5;i++)
{
System.out.println("正在進行"+name+"的處理");
}
}
}
class Chapter15Homework2 {
public static void main(String[] args) {
//執行 2 個執行緒 Car car1 = new Car("1 號車");
Thread th1 = new Thread(car1);
th1.start();
Car car2 = new Car("2號車");
//跟使用 Thread 子類別 car2.start(); 方法不同的是
//要先產生 Thread 類別物件, 然後把變數放進去
Thread th2 = new Thread(car2);
//再啟動執行緒 ^^ 這次我有記住 th2.start();
for (int i=0; i< 5 ; i++)
{
System.out.println("正在進行main()的處理");
}
}
}
package chapter15.homework3;
class Car implements Runnable
{
private String name;
public Car(String nm)
{
name = nm;
}
public void run()
{
for(int i=0; i<5; i++)
{
try{
//處理這個敘述的時候會暫停 1 秒處理執行緒, sleep() 的單位是千分之ㄧ秒
//由於不是直接繼承 Thread 子類別, 所以 sleep(); 不能直接寫, 要使用 Thread.sleep()
Thread.sleep(1000);
System.out.println("正在進行"+name+"的處理");
}
//處理例外狀況 catch(InterruptedException e)
{
}
}
}
}
class Chapter15Homework3 {
public static void main(String[] args) {
Car car1 = new Car("1號車");
//car1.start();
Thread th = new Thread(car1);
th.start();
for(int i=0; i<5; i++)
{
System.out.println("正在處理main()");
}
}
}
package chapter16.lab1;
//Java Applet 練習
//匯入相關套件
import java.applet.Applet;
import java.awt.Graphics;
//要使用 Java Applet 要從Applet 延伸子類別 public class Sample1 extends Applet
{
//要顯示 Applet的時候呼叫的方法 public void paint(Graphics g)
{
//drawString("顯示字串", x, y) g.drawString("歡迎來到 java Applet", 10, 10);
}
}
package chapter16.lab2;
//劃線的Applet
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class Sample2 extends Applet
{
public void paint(Graphics g)
{
//設定顏色為紅色 g.setColor(Color.red);
//drawLine(線段起點座標 線段終點座標) g.drawLine(10, 10, 100, 100);
//顯示文字 g.drawString("Hello",100,100);
}
}
package chapter16.lab3;
//顯示圖片
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
public class Sample3 extends Applet
{
Image img;
//這個方法只有在 Applet 啟動才會呼叫一次 public void init()
{
img = getImage(getDocumentBase(),"Sunset.jpg");
}
public void paint(Graphics g)
{
g.drawImage(img, 10, 10, this);
}
}
package chapter16.lab4;
//與滑鼠產生互動
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.Color;
//實作 MouseListener public class Sample4 extends Applet implements MouseListener
{
//初始 x, y 座標 int x = 10;
int y = 10;
public void init()
{
//呼叫 addMouseListener 方法, 接收滑鼠已經按下這個事件準備工作 addMouseListener(this);
}
public void mouseClicked(MouseEvent e)
{
}
//滑鼠游標進入 Applet 範圍之後 public void mouseEntered(MouseEvent e)
{
}
//滑鼠游標離開 Applet 範圍之後 public void mouseExited(MouseEvent e){}
//當滑鼠按下要執行的事情 public void mousePressed(MouseEvent e)
{
//記住滑鼠按下的位置 x = e.getX();
y = e.getY();
//更新螢幕顯示 repaint();
}
public void mouseReleased(MouseEvent e){}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.fillOval(x, y, 10, 10);
}
}
package chapter16.lab5;
//與滑鼠產生互動
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Sample5 extends Applet
{
int x = 10;
int y = 10;
public void init()
{
//使用 MouseAdapter() 這個類別會讓相關設定更簡單 addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
x = e.getX();
y = e.getY();
repaint();
}
});
}
public void paint(Graphics g)
{
g.fillOval(x, y, 10, 10);
}
}
package chapter16.lab6;
//使用 AWT (Abstract Window Toolkit)
import java.applet.Applet;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
//實作 ActionListener , 來讓按鈕被按下執行某些動作 public class Sample6 extends Applet implements ActionListener
{
Button bt;
public void init()
{
bt = new Button("start");
//接收按鈕被按下的準備工作 bt.addActionListener(this);
//使用 add() 方法將它加到 Applet 上面 add(bt);
}
public void actionPerformed(ActionEvent ae)
{
//按鈕被按下的處理 bt.setLabel("stop"); }
}
package chapter16.lab7;//讓數字動起來
import java.applet.Applet;
import java.awt.Graphics;
public class Sample7 extends Applet implements Runnable
{
int num;
public void init()
{
Thread th;
th = new Thread(this);
th.start();
}
public void run()
{
try{
for(int i=0; i<10; i++)
{
num = i;
repaint();
Thread.sleep(1000);
}
}
catch(InterruptedException e){}
}
public void paint(Graphics g)
{
String str = "數字" + num + "!";
g.drawString(str, 10, 10);
}
}
沒有留言:
張貼留言