- 完成书本8.5.2实验二
Java代码:
Goods类
package fifth_first;
public class Goods {
boolean isDanger;
String name;
Goods(String name) {
this.name = name;
}
public void setIsDanger(boolean boo) {
isDanger = boo;
}
public boolean isDanger() {
return isDanger;
}
public String getName() {
return name;
}
}
Machine类
package fifth_first;
public class Machine {
public void checkBag(Goods goods) throws DangerException {
if (goods.isDanger()) {
throw new DangerException();
}
}
}
DangerException类
package fifth_first;
public class DangerException extends Exception {
String message;
DangerException() {
message = "危险品!";
}
public void toShow() {
System.out.print(message + " ");
}
}
Check类(演示类)
package fifth_first;
public class Check {
public static void main(String[] args) {
Machine machine = new Machine();
Goods apple = new Goods("苹果");
apple.setIsDanger(false);
Goods explosive = new Goods("炸药");
explosive.setIsDanger(true);
try {
machine.checkBag(explosive);
System.out.println(explosive.getName() + "检查通过");
} catch (DangerException e) {
e.toShow();
System.out.println(explosive.getName() + "被禁止!");
}
try {
machine.checkBag(apple);
System.out.println(apple.getName() + "检查通过");
} catch (DangerException e) {
e.toShow();
System.out.println(apple.getName() + "被禁止!");
}
}
}
- 创建登录异常,登陆时,如果用户名不是zhangsan,则引发登录异常。
Create类(注册用户)
package fifth_second;
public class Create {
String ID;
String password;
Create(String ID, String password){
this.ID = ID;
this.password = password;
}
}
Login类(登陆)
package fifth_second;
import java.util.*;
public class Login {
String ID;
String password;
Scanner r1 = new Scanner(System.in);
public void check(Create a) throws LoginException{
System.out.print("请输入登陆账号:");
this.ID = r1.next();
System.out.print("请输入登陆密码:");
this.password = r1.next();
r1.close();
if(!this.ID.equals(a.ID) || !this.password.equals(a.password)) {
throw new LoginException();
}
}
}
LoginException类(抛异常)
package fifth_second;
public class LoginException extends Exception{
String message;
LoginException(){
message = "登陆异常!";
}
public void toShow() {
System.out.println("用户名或密码不匹配," + message);
}
}
Demo类(演示)
package fifth_second;
public class Demo {
public static void main(String[] args) {
Create user = new Create("zhangsan", "123");
Login login = new Login();
try {
login.check(user);
System.out.println("登陆成功");
} catch (LoginException l) {
l.toShow();
}
}
}
- 使用java的输入、输出流将一个文本文件的内容复制到另一个文件里,要求出现IOException异常时,打印出:“出现了文件拷贝异常”,但无论是否有异常,都要关闭文件流。
方法一:文件字节输入流、输出流
Java代码:
package fifth_third;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileByteCopy {
public static void main(String[] args) {
byte[] a = "文件字节流".getBytes();
byte[] b = new byte[100];
FileOutputStream out1 = null;
FileInputStream in = null;
FileOutputStream out2 = null;
try {
//创建文件
File f1 = new File("D:\\file\\a.txt");
f1.createNewFile();
File f2 = new File("D:\\file\\c.txt");
f2.createNewFile();
//先向文件f1中写入信息
out1 = new FileOutputStream(f1);
out1.write(a);
//声明文件字符输入流、输出流
in = new FileInputStream(f1);
out2 = new FileOutputStream(f2);
//边读f1的内容,边将其内容拷贝到文件f2中
while (in.read(b) != -1) {
out2.write(b);
System.out.println(new String(b));
}
} catch (IOException e) {
System.out.println("出现了文件拷贝异常");
}
finally {
if(out1 == null && out2 ==null && in == null) {
try {
out1.close();
out2.close();
in.close();
} catch(IOException e) {
System.out.println(e);
}
}
}
}
}
方法二:文件字符输入流、输出流
Java代码:
package fifth_third;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileCharCopy {
public static void main(String[] args) {
String content = "文件字符流";
char[] a = content.toCharArray();
char[] b = new char[100];
FileWriter out1 = null;
FileReader in = null;
FileWriter out2 = null;
try {
// 创建文件
File f1 = new File("D:\\file\\e.txt");
f1.createNewFile();
File f2 = new File("D:\\file\\f.txt");
f2.createNewFile();
// 先向文件f1中写入信息
out1 = new FileWriter(f1);
out1.write(a);
// 声明文件字符输入流、输出流
in = new FileReader(f1);
out2 = new FileWriter(f2);
// 边读f1的内容,边将其内容拷贝到文件f2中
while (in.read(b) != -1) {
out2.write(b);
System.out.println(new String(b));
}
} catch (IOException e) {
System.out.println("出现了文件拷贝异常");
} finally {
if (out1 == null && out2 == null && in == null) {
try {
out1.close();
in.close();
out2.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
}
}
4) 请写出匿名类,其父类为Bird:(fly方法),并被 Demo: watch(Bird)方法调用
Bird.fly:该方法描述出小鸟怎么飞的,不同的鸟飞翔方式不一样,如"展翅高飞","低空盘旋","大鹏展翅"
Bird.getName():返回该鸟的鸟名,种类
Demo.watch():强调看到了鸟,比如:"我看到一只??,它"+????
Java代码:
Bird类
package fifth_fourth;
public abstract class Bird {
public abstract void fly();
public abstract void getName();
}
MaQue类(Bird的子类)
package fifth_fourth;
public class MaQue extends Bird{
public void getName() {
System.out.print("麻雀,");
}
public void fly() {
System.out.println("它低空盘旋 ");
}
}
Demo(演示)
package fifth_fourth;
public class Demo {
public static void main(String[] args) {
Demo demo = new Demo();
MaQue maque = new MaQue();//有类名的子类
demo.watch(maque);
//匿名类1
demo.watch(new Bird() {
public void getName() {
System.out.print("不知名的鸟,");
}
public void fly() {
System.out.println("它展翅高飞");
}
});
//匿名类2
demo.watch(new Bird() {
public void getName() {
System.out.print("不知名的鸟,");
}
public void fly() {
System.out.println("它大鹏展翅");
}
});
}
public void watch(Bird bird) {
System.out.print("我看到一只");
bird.getName();
bird.fly();
}
}