合肥工业大学——java(最新版)——第二次作业

作业2

  1. 假设Fruit、Apple、Orange、GoldenDelicious和Macintosh声明如下图所示。

图1 GoldenDelicious和Macintosh是Apple的子类,Apple和Orange是Fruit的子类
假设给出下面的声明:
在这里插入图片描述

Fruit fruit = new GoldenDelicious();
Orange orange = new Orange();
回答下面的问题:
(1) fruit instanceof Fruit的值为true吗?
(2) fruit instanceof Orange的值为true吗?
(3) fruit instanceof Apple的值为true吗?
(4) fruit instanceof GoldenDelicious的值为true吗?
(5) fruit instanceof Macintosh的值为true吗?
(6) orange instanceof Orange的值为true吗?
(7) orange instanceof Fruit的值为true吗?
(8) orange instanceof Apple的值为true吗?
(9) 假设makeAppleCider方法定义在Apple类中,fruit可以调用这个方法吗?orange可以调用这个方法吗?
(10) 假设makeOrangeJuice方法定义在Orange类中。fruit可以调用这个方法吗?orange可以调用这个方法吗?
(11) 语句Orange p = new Apple()是否合法?
(12) 语句Macintosh p = new Apple()是否合法?
(13) 语句Apple p = new Macintosh ()是否合法?

  1. 设计一个包含5个类的Java程序,名为Person的父类有两个子类,学生类Student和员工类Employee。Employee类有两个子类,教师类Faculty和 职员类Staff。所有人都有编号ID、姓名、地址、电话号码和电子邮件地址。学生类Student有班级状态(大一、大二、大三或大四)。教师类Faculty有主讲课程、上课时间、专业信息。职员类Staff有职务、入职日期信息。覆盖每个类中的toString()方法,输出相应的类名、编号ID和姓名。
    编写一个测试程序,随机创建8个Student、Faculty或Staff对象,放在一个数组或者Vector类对象中,依次调用他们的toString()方法显示信息。

  2. 何为异常?为什么要进行异常处理?

  3. Error和Exception类有何不同?

  4. 设计一个程序,其功能是从命令行输入整数字符串,再将该整数字符串转换为整数,输入的数据可能具有以下格式:
    12345
    123 45
    123x yz456
    对可能会发生异常进行捕获和处理。

答:
1.首先 A instanceof B表示:A是B的实例,判断一个对象是否是其本身或其子类。
(1)(3)(4) TRUE fruit是GoldenDelicioous的实例化对象,而GoldenDelicioous是Apple的子类同时也是Fruit的子类。
(2)(5) FALSE
(6)(7) TRUE orange是Orange的实例化对象,而Orange是Fruit的一个子类,故orange是Fruit的一个子类对象。
(8) FALSE
(9) FALSE父类可以new子类,但是一般调用不了子类中的特有方法。
(10) T orange可以调用 F fruit不可以调用
(11)(12) FALSE 子类不可以new 父类
(13) TRUE 父类可以new子类

2.`

import java.util.Random;
import java.util.Scanner;

class Person {
    String ID;
    String name;
    String address;
    String phone;
    String e_mail;

    public Person() {
        System.out.print("姓名:");
        Scanner input=new Scanner(System.in);
        name=input.nextLine();
        System.out.print("编号:");
        ID=input.nextLine();
        System.out.print("手机号:");
        phone=input.nextLine();
        System.out.print("地址:");
        address=input.nextLine();
        System.out.print("邮箱:");
        e_mail=input.nextLine();
    }
    public Person(String ID, String name, String address, String phone, String e_mail) {
        this.ID = ID;
        this.name = name;
        this.address = address;
        this.phone = phone;
        this.e_mail = e_mail;
    }

    @Override
    public String toString() {
        return "Person{" +
                "ID='" + ID + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

class Student extends Person {
    String grade;

    public Student() {
        Scanner input=new Scanner(System.in);
        System.out.print("学生年级:");
        grade=input.nextLine();
    }

    public Student(String ID, String name, String address, String number, String e_mail, String grade) {
        super(ID, name, address, number, e_mail);
        this.grade = grade;
    }

    @Override
    public String toString() {
        return "Student{" +
                "ID='" + ID + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

class Employee extends Person {
    public Employee() {
    }

    public Employee(String ID, String name, String address, String number, String e_mail) {
        super(ID, name, address, number, e_mail);
    }

    @Override
    public String toString() {
        return "Employee{" +
                "ID='" + ID + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

class Faculty extends Employee {
    String subject;
    String time;
    String profession;

    public Faculty() {
        Scanner input=new Scanner(System.in);
        System.out.print("主讲课程:");
        subject=input.nextLine();
        System.out.print("授课时间:");
        time=input.nextLine();
        System.out.println("专业:");
        profession=input.nextLine();
    }

    public Faculty(String ID, String name, String address, String number, String e_mail, String subject, String time, String profession) {
        super(ID, name, address, number, e_mail);
        this.subject = subject;
        this.time = time;
        this.profession = profession;
    }

    @Override
    public String toString() {
        return "Faculty{" +
                "ID='" + ID + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

class Staff extends Employee {
    String job;
    String entry_date;

    public Staff() {
        Scanner input=new Scanner(System.in);
        System.out.print("职员职务:");
        job=input.nextLine();
        System.out.print("入职日期:");
        entry_date=input.nextLine();
    }

    public Staff(String ID, String name, String address, String number, String e_mail, String job, String entry_date) {
        super(ID, name, address, number, e_mail);
        this.job = job;
        this.entry_date = entry_date;
    }

    @Override
    public String toString() {
        return "Staff{" +
                "ID='" + ID + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

public class oop {
    public static void main(String[] args) {
        Person[] p = new Person[8];
        Random r = new Random();
        for (int i = 0; i < 8; i++) {
            switch (r.nextInt(3)) {
                case 0:
                    System.out.println("已创建一个Student");
                    p[i] = new Student();
                    break;
                case 1:
                    System.out.println("已创建一个Faculty");
                    p[i] = new Faculty();
                    break;
                case 2:
                    System.out.println("已创建一个Staff");
                    p[i] = new Staff();
                    break;
            }
        }
        for (int i = 0; i < 8; i++) {
            System.out.println(p[i].toString());
        }
    }
}`

  1. (1)异常指程序运行过程中出现的非正常现象,例如 用户输入错误,除数为零、需要处理的文件不存在、数组下标越界等情况。
    (2)java的异常处理可以让程序具有更好的容错性,是程序更加健壮。当程序运行出现意外情况的时候。系统会自动生成一个Exception对象来通知,从而实现将“业务功能逻辑” 和“错误处理逻辑。

  2. Error表示系统级的错误和程序不必处理的异常,是恢复起来不是不可能但很困难的情况下的一种严重问题:比如内存溢出,不可能指望程序能处理这样的情况。 Error类及派生的子类具有特征:它们处理的是较少发生的系统内部错误,程序员通常对它们无能为力,只能在其发生时由用户按照系统提示关闭程序。
    Exception表示需要捕捉或者需要程序进行处理的异常,是一种设计或实现问题;也就是说,它表示如果程序运行正常,从不会发生的情况。Exception类及其派生的子类具有特征:它们解决是由程序本身及环境所产生异常,它们可以被捕获并进行相应的处理。

5.``import java.io.*;
public class oop
{
public static void main(String[] args)
{
System.out.println(“请输入一个整数字符串:”);
try //此处可能会产生IOException异常
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int a=Integer.parseInt(in.readLine()); //此处可能会产生NumberFormatException异常
System.out.println(“您输入的整数是:”+a);
}
catch(IOException e) //捕获IOException异常
{
System.out.println(“IO错误”);
}
catch(NumberFormatException ne) //捕获NumberFormatException异常
{
System.out.println(“您输入的不是一个整数字符串”);
}
}
}
class UseDefineException
{
public static boolean prime(int n) throws ArgumentOutOfBoundException
{
if(n<0)
{
ArgumentOutOfBoundException ae=new ArgumentOutOfBoundException();
throw ae; //抛出这个异常
}
else
{
boolean isPrime=true;
for(int i=2;i<n;i++)
if(n%i==0)
{
isPrime=false;
break;
}
return isPrime;
}
}
public static void main(String[] args)
{
if(args.length!=1) //输入格式错误
{
System.out.println(“输入格式错误!”);
System.exit(0); //输入格式错误,系统退出!
}
int m=Integer.parseInt(args[0]); //读入整数
try
{
boolean result=prime(m); //调用方法判断是否为素数
System.out.println(“对您输入的整数”+m+“是否为素数的判断结果是:”+result);
}
catch(ArgumentOutOfBoundException e) //捕捉可能抛出的异常
{
System.out.println(“异常名称:”+e.toString());
}
}
}
//自定义异常类
class ArgumentOutOfBoundException extends Exception
{
ArgumentOutOfBoundException()
{
System.out.println(“输入错误!欲判断的数不能为负!”);
}
}`

`

  • 17
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

再沉淀一年!

本人高校大学生,谢谢你的激励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值