PTA java10 异常 函数题

6-1 jmu-Java-06异常-finally

代码中向系统申请资源,到最后都要将资源释放。

现有一Resource类代表资源类,包含方法:

  1. open(String str)打开资源,声明为抛出Exception(包含出错信息)。
  2. close()方法释放资源,声明为抛出RuntimeException(包含出错信息)

现在根据open(String str)中str的不同,打印不同的信息。str的内容分为4种情况:

  1. fail fail,代表open和close均会出现异常。打印open的出错信息与close的出错信息。
  2. fail success,代表open抛出异常,打印open出错信息。close正常执行,打印resource release success
  3. success fail ,代表open正常执行,打印resource open success。close抛出异常,打印close出错信息。
  4. success success,代表open正常执行,打印resource open success,close正常执行打印resource release success

注1:你不用编写打印出错信息的代码。
注2:捕获异常后使用System.out.println(e)输出异常信息,e是所产生的异常。

裁判测试程序:

public static void main(String[] args) { Scanner sc = new Scanner(System.in); Resource resource = null; try{ resource = new Resource(); resource.open(sc.nextLine()); /*这里放置你的答案*/ sc.close(); } 

以下输入样例代表输入success success

输入样例

success success

输出样例

resource open success
resource release success
           System.out.println("resource open success");
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
        try
        {
            resource.close();
            System.out.println("resource release success");
        }
        catch (Exception e)
        {
            System.out.println(e);
        }

6-2 jmu-Java-06异常-多种类型异常的捕获

如果try块中的代码有可能抛出多种异常,且这些异常之间可能存在继承关系,那么在捕获异常的时候需要注意捕获顺序。

补全下列代码,使得程序正常运行。

裁判测试程序:


public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String choice = sc.next(); try { if (choice.equals("number")) throw new NumberFormatException(); else if (choice.equals("illegal")) { throw new IllegalArgumentException(); } else if (choice.equals("except")) { throw new Exception(); } else break; } /*这里放置你的答案*/ }//end while sc.close(); }

###输出说明
catch块中要输出两行信息:
第1行:要输出自定义信息。如下面输出样例的number format exception
第2行:使用System.out.println(e)输出异常信息,e是所产生的异常。

输入样例

number illegal except quit

输出样例

number format exception
java.lang.NumberFormatException
illegal argument exception
java.lang.IllegalArgumentException
other exception
java.lang.Exception
            catch (Exception e) {
                if(choice.equals("number"))
                {
                    System.out.println("number format exception");
                }
                if(choice.equals("illegal"))
                {
                    System.out.println("illegal argument exception");
                }
                if(choice.equals("except"))
                {
                    System.out.println("other exception");
                }
                System.out.println(e);
            }

6-3 jmu-Java-06异常-ArrayIntegerStack异常改进版

改造接口章节的ArrayIntegerStack,为其pop()push()peek()方法添加出错时抛出异常的功能。

ArrayIntegerStack类内部使用数组实现。创建时,可指定内部数组大小。

属性:

int capacity;//代表内部数组的大小
int top;//代表栈顶指针。栈空时,初始值为0。
Integer[] arrStack;//用于存放元素的数组

方法:

public Integer push(Integer item); //如果item为null,则不入栈直接返回null。如果栈满,抛出FullStackException(系统已有的异常类)。 public Integer pop(); //出栈。如果栈空,抛出EmptyStackException,否则返回 public Integer peek(); //获得栈顶元素。如果栈空,抛出EmptyStackException。 

思考:

使用异常而不是通过返回null来提示用户程序出错有什么好处?

裁判测试程序:

class ArrayIntegerStack implements IntegerStack{ private int capacity; private int top=0; private Integer[] arrStack; /*其他代码*/ /*你的答案,即3个方法的代码*/ } 

 

public Integer push(Integer item) throws FullStackException{
        if(this.size()>=this.arrStack.length)
            throw new FullStackException();
        if(item==null) return null;
        this.arrStack[this.top]=item;
        this.top++;
        return item;
    }
    public Integer pop() throws EmptyStackException{
        if(this.empty()) throw new EmptyStackException();
        if(this.arrStack[top-1]==null) return null;
        this.top--;
        return this.arrStack[top];
    }
    public Integer peek() throws EmptyStackException{
        if(top==0) throw new EmptyStackException();
        if(arrStack[top-1]==null) return null;
        return arrStack[top-1];
    }

6-4 异常:圆半径不能为负数

这是函数题模板。这里写题目要求。计算圆的面积,其中PAI取3.14,圆半径为负数时应抛出异常,输出相应提示。

函数接口定义:

裁判测试程序样例:

在这里给出函数被调用进行测试的例子。例如: import java.util.Scanner; public class Main { public static void main(String args[ ]) { double s=0; Scanner sr=new Scanner(System.in); double r=sr.nextDouble(); sr.close(); try{ Circle c1=new Circle(r); s = c1.area(); System.out.printf("%.1f",s); } catch (NumRangeException e){ e.print(); } } } /* 请在这里填写答案 */ 

输入样例:

在这里给出一组输入。例如:

-3

输出样例:

在这里给出相应的输出。例如:

错误:圆半径-3.0为负数
class NumRangeException extends Exception
{
    double r;
 
    public NumRangeException(double r) {
        this.r = r;
    }
    public void print()
    {
        System.out.printf("错误:圆半径%.1f为负数",r);
    }
}
class Circle
{
    double r;
 
    public Circle(double r) {
        this.r = r;
    }
    public double area() throws NumRangeException
    {
        if(r < 0) throw new NumRangeException(r);
        return r*r*3.14;
    }
}

6-5 异常:物品安全检查

车站检查危险品的设备,如果发现危险品会发出警告。编程模拟设备发现危险品的情况。编程要求如下:

  1. 通过继承Exception类,编写一个DangerException类。
    a) 该异常类有构造方法,该构造方法使用super调用父类构造方法,使用字符串:“属于危险品!”,对父类变量detailMessage进行初始化。
  2. 编写商品类:Goods,该类包含以下成员:
    a) 私有的name属性(String类型),表示商品名称。
    b) 私有的isDanger属性(boolean型),表示商品是否为危险品,如果为危险品,则值为true,否则为fales。
    c) 分别为两个私有变量编写set和get方法
  3. 编写一个Machine类,该类的方法checkBag(Goods goods)。当发现参数goods是危险品时,即:goods的isDanger属性为true时,该方法抛出DangerException异常的对象。

函数接口定义:


裁判测试程序样例:

public class Main { public static void main(String args[]) { String[] name ={"苹果","炸药","西服","硫酸","手表","硫磺"}; Goods[] goods = new Goods[name.length]; for(int i= 0;i

输入样例:

在这里给出一组输入。例如:


输出样例:

在这里给出相应的输出。例如:

苹果,检查通过

炸药属于危险品!
炸药,被禁止!

西服,检查通过

硫酸属于危险品!
硫酸,被禁止!

手表,检查通过

硫磺属于危险品!
硫磺,被禁止!
class DangerException extends Exception{
    public DangerException(){
        super("属于危险品!");
    }
}
class Goods{
    private String name;
    private boolean isDanger;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isDanger() {
        return isDanger;
    }

    public void setDanger(boolean danger) {
        isDanger = danger;
    }
}
class Machine{
    public void checkBag(Goods goods) throws DangerException{
        if(goods.isDanger()==true)
        throw new DangerException();
    }
}

6-6 求圆面积自定义异常类

计算圆的面积,其中PI取3.14,圆半径为负数时应抛出异常,输出相应提示。根据提供的主类信息,编写Circle类和CircleException类,以及在相关方法中抛出异常。

函数接口定义:

裁判测试程序:

在这里给出主类 import java.util.*; public class Main { public static void main(String[] args) { double s=0; Scanner sc=new Scanner(System.in); double r1,r2; r1=sc.nextDouble(); r2=sc.nextDouble(); Circle c1=new Circle(r1); Circle c2=new Circle(r2); try{ s = c1.area(); System.out.println(s); s = c2.area(); System.out.println(s); } catch (CircleException e){ e.print(); } } } /* 请在这里填写答案 编写Circle 和CircleException类*/ 

输入样例:

在这里给出一组输入。例如:

3.5 -3.5

输出样例:

在这里给出相应的输出。例如:

38.465
圆半径为-3.5不合理
class Circle{
    double r;
    public Circle(double r){
        this.r=r;
    }
    public double area() throws CircleException{
        if(r<0)
            throw new CircleException(r);
        return 3.14*r*r;
    }
}
class CircleException extends Exception{
    double r;
    public CircleException(double r)
    {
        this.r=r;
    }
    public void print()
    {
        System.out.println("圆半径为"+this.r+"不合理");
    }
}

你可能感兴趣的:(PTA,山东理工大学,Java,java,开发语言)