Java程序设计---实验4

1、实验题目:类的继承和方法重写

定义一个基类作为父类,再定义一个继承父类的子类,在子类中重写父类的方法,使用super关键字调用父类的方法,测试其功能。

public class S4_1 {
    public static void main (String[] args) {
        System.out.println("父类");
        Phone ph=new Phone();
        ph.sendMessage();
        ph.call();
        ph.showNum();
        System.out.println("-----------------");
        System.out.println("子类");
        newPhone newph=new newPhone();
        newph.showNum();
    }
}
//父类 手机
class Phone{
    public void sendMessage() {
        System.out.println("发短信");
    }
    public void call() {
        System.out.println("打电话");
    }
    public void showNum() {
        System.out.println("显示来电号码");
    }
}
//子类 新手机类
class newPhone extends Phone{
    //覆盖父类的来电显示号码功能,并增加自己的显示姓名和图片功能
    public void showNum() {
        //调用父类已经存在的功能使用super
        super.showNum();
        //增加自己特有显示姓名和头像功能
        System.out.println("显示姓名");
        System.out.println("显示头像");
    }

2、实验题目:final关键字的应用

定义一个类,在类中声明成员变量和成员方法,尝试使用final关键词修饰类中的变量、方法及该类,测试并查看结果,必要时加以注释。

public class S4_2 {
    public static void main(String[] args) {
        Dog dog=new Dog("中华田园犬",12);
        dog.shout();;
        System.out.println(dog.name);
        System.out.println(dog.age);
    }
}
//final
class Animal{
    //final
    String name="牧羊犬";
    int age;
    //final
    void shout(){
        System.out.println("动物的叫声是~~~~~");
    }
}
class Dog extends Animal{
    public void shout(){
        System.out.println("狗的叫声是汪汪");
    }
    public Dog(String name,int age){
        this.name=name;
        this.age=age;
    }
}

3、实验题目:研究生薪资管理(注:在职研究生继承学生类,实现雇员接口)

在学校中,学生每个月需要交相应的生活费(2000元),雇员每个月有相应的工资(1000~3000随机生成),而在职研究生(on-the-job postgraduate)既是雇员又是学生,所以在职研究生既需要交学费又会有工资。下面要求编写一个程序来统计在职研究生的收入与学费,如果收入无法满足交学费,则输出“撸起袖子加油干!”信息。(思考:如果使用抽象类,是否能完成该要求?)

public class S4_3 {
    public static void main(String[] args){
        PostGraduate postgraduate = new PostGraduate();//创建一个研究生实例
        postgraduate.judge();//调用judge方法
    }
}
class Student{
    int cost=2000*12;//计算全年学费
}
interface Employer{
    int salary=12*(int)(1000+2000*Math.random());//计算全年工资
}
class PostGraduate extends Student implements Employer{
    public void judge(){
        if(cost>salary){
            System.out.println("撸起袖子加油干!");
        }else{
            System.out.println("你的工资足已抵扣学费。");
        }
    }

}

4、实验题目:创建一个抽象交通工具Vehicle类,它有 wheelNum 和 seatNum 两个成员变量以及抽象方法 display()。 类 Bus 和类 Motorcycle 继承自Vehicle类,实现打印成员变量的 display()方法。在主函数中分别生成Bus对象和Motorcycle对象,上转型为Vehicle对象调用 display()方法。

abstract class Vehicle {//抽象类
    int wheelNum;
    int seatNum;
    protected abstract void display();
}

class Bus extends Vehicle{
    public Bus(int a, int b) {
        this.wheelNum=a;
        this.seatNum=b;
    }
    public void display(){//重写抽象方法
        System.out.println("这是一辆公交车");
        System.out.println("公交车有"+this.wheelNum+"个轮子和"+this.seatNum+"个座位");
    }
}

class Motorcycle extends Vehicle{
    public Motorcycle(int a, int b) {
        this.wheelNum=a;
        this.seatNum=b;
    }
    public void display(){//重写抽象方法
        System.out.println("这是一辆摩托车");
        System.out.println("摩托车有"+this.wheelNum+"个轮子和"+this.seatNum+"个座位");
    }
}

public class S4_4 {
    public static void main(String[] args) {
        Vehicle bus = new Bus(4,30);
        Vehicle motorcycle = new Motorcycle(2, 2);
        bus.display();
        motorcycle.display();
    }
}

5、实验题目:经理与员工工资,主要考察多态

某公司的人员分为员工和经理两类,但经理也属于员工中的一类,公司员工和经理都有自己的姓名,年龄,工号、工资、工龄等属性(通过属性无法区分员工和经理)和工资上涨函数。假设每次给员工涨工资一次能涨10%,经理能涨20%。要求利用多态实现给员工和经理涨工资,测试并通过。

public class S4_5 {
    public static void main(String[] args) {
        Employee employee = new Employee(1000);
        Employee manager = new manager(1000);//多态的实现
        System.out.println("基础工资为:"+ employee.getSalary());
        employee.raise();//调用Employee中的raise方法
        manager.raise();//调用manager类中重写的raise方法
        System.out.println("员工增长10%后为:"+employee.getSalary());
        System.out.println("经理增长20%后为:"+manager.getSalary());
    }
}


class Employee{
    protected String name;
    protected int age;
    protected int id;
    protected double salary;
    protected int workage;

    public Employee() {
   }

    public Employee(double salary) {
        this.salary = salary;
    }



    public void raise(){
        this.salary+=this.salary*0.1;
    }

    public double getSalary() {
        return salary;
    }
}

class manager extends Employee{
    public manager() {
    }

    public manager(double salary) {
        this.salary = salary;
    }


    public void raise(){//重写Employee中的raise()方法
        this.salary+=this.salary*0.2;
    }

    public double getSalary() {
        return salary;
    }
}

6、实验题目:

把下面的代码补充完整,输出结果为“实现了Inner接口的匿名内部类!”,并测试输出结果。

interface Inner{

  void introduce();

}

class Outer{

       //补齐代码,完成方法主要功能

    

}

class InnerClassTest{

  public static void main(String[] args){

       Outer.method().introduce ();

}

interface Inner{
    void introduce();
}

class Outer {
    public static Inner method(){
        return new Inner() {
            @Override
            public void introduce() {
                System.out.println("实现了Inner接口的匿名内部类");
            }
        };
    }
//补齐代码,完成方法主要功能
}

class S4_6{
    public static void main(String[] args){
        Outer.method().introduce();
    }
}

7、实验题目:设计一个类,在类中能够处理自定义异常类并测试。(选做)

import java.util.*;
public class S4_7{
    public static void main(String[] args) {
        Scanner sca = new Scanner(System.in);
        Gender gen = new Gender();
        Sub sub = new Sub();
        String gender;
        double num1, num2;
        int n = 1;
        while (n ==1) {
            System.out.print("请输入性别:");
            gender = sca.next();
            try {
                gen.setGender(gender);
            } catch (Exception e1) {
                System.out.println(e1.getMessage());
            }
            System.out.print("请输入数字1:");
            num1 = sca.nextDouble();
            System.out.print("请输入数字2:");
            num2 = sca.nextDouble();
            try {
                sub.sub(num1, num2);
            } catch (Exception e2) {
                System.out.println(e2.getMessage());
            }
            System.out.print("是否继续进行。1.继续 2.退出:");
            int option;
            option=sca.nextInt();
            switch(option){
                case 1:
                    n=1;
                    break;
                case 2:
                    n=0;
                    break;
            }
        }
    }
}
class Gender{
    private String gender;
    public String getGender(){
        return gender;
    }
    public void setGender(String gender) throws Exception{
        if(gender.equals("男")|gender.equals("女")){
            this.gender=gender;
        }else{
            throw new GenderException("性别只能是男或女");
        }
    }
}
class GenderException extends Exception{
    public GenderException(String gender){
       super(gender);
    }
}
class Sub{
    double sum=0;
    public double sub(double num1, double num2) throws Exception{
        if(num2!=0){
            sum=num1/num2;
            System.out.println(sum);
        }else{
            System.out.println("分母不能为0");
        }
        return 1;
    }
}
class subException extends Exception{
    public subException(double num1,double num2){
        super();
    }
}
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

用户1234567890

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值