南京邮电大学java第三次实验报

 

 

实 验 报 告

( 2017 / 2018学年 第2学期)

 

 

 

课程名称

JAVA语言程序设计

实验名称

Java集成开发环境的安装与使用、

 Java变量、表达式与控制结构

实验时间

2018

  4

 11

指导单位

       计算机学院软件教学中心

指导教师

              许棣华

 

 

学生姓名

王利国

班级学号

B160209

学院(系)

电子与光学工程学院,微电子学院

专    业

微电子科学与工程

实验名称

方法、数组和类

指导教师

许棣华

实验类型

上机

实验学时

2

实验时间

2017.4.11

              

三、实验内容

1. 在前面实验二已定义的学生类Student的基础上,以Student类为父类,为学生类派生出一个子类为大学生类(CollegeStudent)。

CollegeStudent 类在学生类上增加一个专业(profession)数据属性;方法上增加获得专业和设置专业两个方法。并对超类中的toString( )方法进行重写,使得CollegeStudent类中的toString( )方法除了显示学生类的信息外,还要显示它的专业属性。

编写测试程序的主类。在主类中创建一个Student对象和CollegeStudent对象,并显示或修改这两个对象的属性值。

package lg.test;
//测试类

  public class Demo31 {

    public static void main(String[] args) {

        Student one = new Student( "16020912", "王宁宁","男" , 19 );

        CollegeStudent two = new CollegeStudent( "16020913", "王利国","男" , 19 ,"微电子科学与工程"  );

        System.out.println("未进行修改的时候的属性值");

        System.out.println(one.toString());

        System.out.println(two.toString());

        System.out.println("修改后的属性值");

        one.setAge( 20 );

        two.setProfession( "微电子" );

        System.out.println(one.toString());

        System.out.println(two.toString());

  

    }

}

  class Student {

    private String studentID;

    private String name;

    private String sex;

    private int age;

    private static int count;

  

    public static int getCount() {

        return count;

    }

  

    Student(String studentID, String name, String sex, int age) {

        this.studentID = studentID;

        this.name = name;

        this.sex = sex;

        this.age = age;

    }

  

  

    @Override

    public String toString() {

        return "Student{" +

                "studentID='" + studentID + '\'' +

                ", name='" + name + '\'' +

                ", sex='" + sex + '\'' +

                ", age=" + age +

                '}';

    }

    //studen的set和get方法

  

    public String getStudentID() {

        return studentID;

    }

  

    public void setStudentID(String studentID) {

        this.studentID = studentID;

    }

  

    public String getName() {

        return name;

    }

  

    public void setName(String name) {

        this.name = name;

    }

  

    public String getSex() {

        return sex;

    }

  

    public void setSex(String sex) {

        this.sex = sex;

    }

  

    public int getAge() {

        return age;

    }

  

    public void setAge(int age) {

        this.age = age;

    }

  

    public static void setCount(int count) {

        Student.count = count;

    }

}

  

  

  //新建立的CoolegeStudent对象

  class CollegeStudent extends Student{

    private String profession;

  

    CollegeStudent(String studentID, String name, String sex, int age, String profession) {

        super( studentID, name, sex, age );

        this.profession = profession;

    }

  

  

    //属性的get & set方法

    public String getProfession() {

        return profession;

    }

  

    public void setProfession(String profession) {

        this.profession = profession;

    }

  

    @Override

    public String toString() {

        return "CollegeStudent{" +

                "profession='" + profession + '\'' +

                "studentID='" + super.getStudentID() + '\'' +

                ", name='" + super.getName() + '\'' +

                ", sex='" + super.getSex() + '\'' +

                ", age=" + super.getAge() +

                '}';

                

    }

}
View Code

 2. 设计一个人员类(Person),其中包含一个方法pay,代表人员的工资支出。再从Person类派生出助教类(Assistant)、

讲师类(Instructor)、副教授类(Assistant Professor)和教授类(Professor)。其中:工资支出=基本工资+授课时数*每课时兼课金。
但助教基本工资为800,每课时兼课金25,讲师基本工资为1000,每课时兼课金35,
副教授基本工资为1200,每课时兼课金40,教授基本工资为1400,每课时兼课金50。
①    将Person定义为抽象类,pay为抽象方法,设计程序实现多态性。
②    将Person定义为接口,设计程序实现多态性。

第一:通过类来实现
class Person {

    public int basic;

    public int hour;

    public int charge;

  

    public Person() {

    }

  

    public Person(int basic, int charge) {

        this.basic = basic;

        this.charge = charge;

    }

  

    public void pay(int hour) {

        System.out.println( hour + "小时后的工资为" + (basic + hour * charge) );

    }

  

}

  

  class Assistant extends Person {

    public Assistant() {

        super( 800, 25 );

    }

}

  

  class Instructor extends Person {

    public Instructor() {

        super( 1000, 35 );

    }

}

  

  class AssistantProfessor extends Person {

    public AssistantProfessor() {

        super( 1200, 40 );

    }

}

  

  class Professor extends Person {

    public Professor() {

        super( 1400, 50 );

    }

}
View Code
 

第二:通过抽象方法实现

abstract class Person {

    public int hour;

  //    public int basic;

//    public int charge;

  

//    public Person() {

//    }

  

//    public Person(int basic, int charge) {

//        this.basic = basic;

//        this.charge = charge;

//    }

  

    public abstract void pay(int hour);

  

}

  

  class Assistant extends Person {

    @Override

    public void pay(int hour) {

        System.out.println( hour + "小时后的工资为" + (800 + hour * 25) );

    }

  //    public Assistant() {

//        super( 800, 25 );

//    }

  }

  

  class Instructor extends Person {

    @Override

    public void pay(int hour) {

        System.out.println( hour + "小时后的工资为" + (1000 + hour * 35) );

    }

  //    public Instructor() {

//        super( 1000, 35 );

//    }

  }

  

  class AssistantProfessor extends Person {

    @Override

    public void pay(int hour) {

        System.out.println( hour + "小时后的工资为" + (1200 + hour * 40) );

    }

  //    public AssistantProfessor() {

//        super( 1200, 40 );

//    }

  }

  

  class Professor extends Person {

    @Override

    public void pay(int hour) {

        System.out.println( hour + "小时后的工资为" + (1400 + hour * 50) );

  

    }

  //    public Professor() {

//        super( 1400, 50 );

//    }

    

  }
View Code

第三:通过接口实现

interface Person {

    void pay(int hour);

}

  

  class Assistant implements Person {

    @Override

    public void pay(int hour) {

        System.out.println( hour + "小时后的工资为" + (800 + hour * 25) );

    }

  //    public Assistant() {

//        super( 800, 25 );

//    }

  }

  

  class Instructor implements Person {

    @Override

    public void pay(int hour) {

        System.out.println( hour + "小时后的工资为" + (1000 + hour * 35) );

    }

  //    public Instructor() {

//        super( 1000, 35 );

//    }

  }

  

  class AssistantProfessor implements Person {

    @Override

    public void pay(int hour) {

        System.out.println( hour + "小时后的工资为" + (1200 + hour * 40) );

    }

  //    public AssistantProfessor() {

//        super( 1200, 40 );

//    }

  }

  

  class Professor implements Person {

    @Override

    public void pay(int hour) {

        System.out.println( hour + "小时后的工资为" + (1400 + hour * 50) );

  

    }

  //    public Professor() {

//        super( 1400, 50 );

//    }

  

  }
View Code

3. 从键盘输入两个数,进行相除,显示商。当输入串中含有非数字时或除数为0时,通过异常处理机制,使程序能正确运行。

package leetcode;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * @Author liguo
 * @Description . 从键盘输入两个数,进行相除,显示商。当输入串中含有非数字时或除数为0时,通过异常处理机制,使程序能正确运行。
 * @Data 2018-04-03
 */
public class Demo {
    public static void main(String[] args) {
        int oper1 = 0;      //定义被除数
        int oper2 = 0;      //定义除数
        Scanner in = new Scanner( System.in );
        try {
            //数据输入和输出
            System.out.print( "请输入被除数:" );
            oper1 = in.nextInt();
            System.out.print( "请输入除数:" );
            oper2 = in.nextInt();
            System.out.println( "计算结果:" + oper1 / oper2 );
        } catch (ArithmeticException e2) {
            System.out.println( "异常1:除数为零!,请重新输入除数" );
            oper2 = in.nextInt();
            System.out.println( "计算结果:" + oper1 / oper2 );
        } catch (InputMismatchException e1) {
            System.out.println( "异常2:输入不为数字!,请重新输入" );
//            int one = in.nextInt();
//            int two = in.nextInt();
//            System.out.println( "计算结果:" + one / two );
            System.out.print( "请输入被除数:" );
            String a = in.next();
            oper1 = in.nextInt();
            System.out.print( "请输入除数:" );
            oper2 = in.nextInt();
            System.out.println( "计算结果:" + oper1 / oper2 );
        }
//            catch (NumberFormatException e4) {;
//            System.out.println( "FormatException4:" + e4.getMessage() );
//            oper1 = in.nextInt();
//            oper2 = in.nextInt();
//            System.out.println( "计算结果:" + oper1 / oper2 );
//        }
        finally {
            System.out.println( "程序结束" );
        }
    }
}
View Code

四、实验小结(包括问题和解决方法、心得体会等)

使用try-catch进行异常处理,遇到很多问题

1:不知道捕获异常的种类

2:关于异常的处理

3:输入输出格式异常的处理,关于Scanner方法,使用in.nexInt()方法,其读取的

都是控制台中的第一行。

//网上测试的好多异常处理,亲测并不能使用。

猜你喜欢

转载自www.cnblogs.com/liguo-wang/p/9130057.html