Java实验报告(三)

目录

一、实验目的与任务

二、实验内容、要求及安排方式

1、实验内容与要求:

(1)

(2)

(3)

(4)

2、要求:

3、实验安排方式:

三、实验环境

四、核心代码及运行效果

(1):

(2):

(3):

(4):

五、实验小结


一、实验目的与任务

1、掌握构造方法和成员方法重载的应用;

2、理解类的继承性的作用;

3、领会面向对象编程的多态性;

4、熟练掌握抽象类abstract的概念;

5、熟练掌握接口interface的概念;

6、熟练包package的概念以及编译运行的方法;

7、熟练掌握内部类inner class的概念;

8、理解面向对象的程序设计方法。

二、实验内容、要求及安排方式

1、实验内容与要求:

(1)

        定义一个圆类Circle,成员变量:半径 radius;成员方法:构造方法、get和set半径的方法、计算面积和周长的方法。定义圆柱和圆锥类,定义相应的变量成员和成员方法。使用以上类编程,输出圆的面积和圆柱、圆锥的体积。

(2)

      声明一个类MyClass,包含一个整型变量data和封装这个变量的两个方法getData()和setData()。声明一个该类的子类SubClass,包含一个整型变量Mydata和封装这个变量的两个方法getMydata()和setMydata(),编写主程序检查SubClass类中的所有变量与方法(包括继承自父类的变量和方法)。

(3)

        下面给出一个根据雇员类型利用多态性完成工资单计算的程序。定义一个类Employee作为超类,Employee的子类有Boss(每星期发给他固定工资,而不计工作时间)、PieceWorker(按其生产的产品数发放工资)、HourlyWorker(根据工作时间长短发放工资)。对所有雇员类型都使用earnings()方法完成其工资单的计算,但每个人挣的工资按他所属的雇员类计算,所有雇员类都是从超类Employee派出生的。所以在超类中声明earnings()方法,该方法没有实质性工作,而是在每个子类都提供恰当的earnings()方法的重写。为了计算雇员的工资,程序仅使用雇员对象的一个超类引导并调用earnings()方法。

(4)

        编写一个应用程序,实现以下功能:

        ①声明一个接口(Calculability)

                接口中包含一个方法area()。

        ②声明一个三角形类继承该接口,类名为Triangle

                类中包含两个变量、一个带参数的构造方法和一个计算三角形面积的方法:

                三角形的底边长w

                三角形的高h

                构造方法Triangle(double width,double height)。

                计算三角形面积的方法area(),该方法覆盖接口(Calculability)的同名方法,计算三角形的面积(w*h/2)。

        ③声明一个锥体类(Taper)

                包含一个接口对象bottom(锥体的底)和一个变量(锥体的高)height,一个带参数的构造方法,一个换底方法getbottom(),一个锥体体积的计算方法volume()。

        ④声明一个主类Exp6_1

                在主方法中声明接口和锥体类的对象,输出锥体的底面积和锥体的体积(bottom*height/3)。

2、要求:

        能够上机编辑、调试java程序;

3、实验安排方式:

        每组1人,独立完成上机实验;

三、实验环境

硬件环境:微型计算机一台。

软件环境:Window XP/7/8/10操作系统、Eclipse、JDK。

四、核心代码及运行效果

无注释版

(1):

Circle类:

public class Circle {

    private double radius;

    public Circle() {
    }

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double getArea(double r) {
        return Math.PI * Math.pow(r, 2);
    }
}

Cone类: 

public class Cone {

    private double h;
    private double r;

    public Cone() {
    }

    public Cone(double h, double r) {
        this.h = h;
        this.r = r;
    }

    public double getH() {
        return h;
    }

    public void setH(double h) {
        this.h = h;
    }

    public double getR() {
        return r;
    }

    public void setR(double r) {
        this.r = r;
    }

    public double getVolume(double h, double r) {
        return Math.PI * Math.pow(r, 2) * h;
    }
}

Cylinder类: 

public class Cylinder {
    
    private double h;
    private double r;
    
    public Cylinder() {
    }
    
    public Cylinder(double h, double r) {
        this.h = h;
        this.r = r;
    }
    
    public double getH() {
        return h;
    }

    public void setH(double h) {
        this.h = h;
    }

    public double getR() {
        return r;
    }

    public void setR(double r) {
        this.r = r;
    }
    
    public double getVolume(double h, double r) {
        return Math.PI * Math.pow(r, 2) * h / 3;
    }
}

Test类: 

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        Circle c1 = new Circle();
        Cone c2 = new Cone();
        Cylinder c3 = new Cylinder();

        System.out.println("===================圆的面积==================");
        System.out.println("请输入圆的半径:");
        double r1 = sc.nextDouble();
        System.out.printf("%.4f\n", c1.getArea(r1));

        System.out.println("===================圆柱的体积==================");
        System.out.println("请输入圆柱的半径:");
        double r2 = sc.nextDouble();
        System.out.println("请输入圆柱的高:");
        double h2 = sc.nextDouble();
        System.out.printf("%.4f\n", c2.getVolume(h2, r2));

        System.out.println("===================圆锥的体积==================");
        System.out.println("请输入圆锥的半径:");
        double r3 = sc.nextDouble();
        System.out.println("请输入圆锥的高:");
        double h3 = sc.nextDouble();
        System.out.printf("%.4f\n", c3.getVolume(h3, r3));
    }
}

(2):

MyClass类 : 

public class MyClass {
    
    private int data;
    
    public MyClass() {
    }
   
    public MyClass(int data) {
        this.data = data;
    }
    
    public int getData() {
        return data;
    }
    public void setData(int data) {
        this.data = data;
    }
}

 SubClass类 :

public class SubClass extends MyClass{
    
    private int Mydata;
    
    public SubClass() {
    }
    
    public SubClass(int mydata) {
        Mydata = mydata;
    }
    
    public int getMydata() {
        return Mydata;
    }
    public void setMydata(int mydata) {
        Mydata = mydata;
    }
}

Test类 :

public class Test {
    public static void main(String[] args) {
        
        SubClass sc = new SubClass();
        
        sc.setMydata(2);
        sc.setData(5);
        
        System.out.println("获取子类变量和检测子类方法");
        System.out.println(sc.getData());
        System.out.println("获取父类变量和检测父类方法");
        System.out.println(sc.getMydata());
    }
}

(3):

Employee类 :

public class Employee {
    
    private String job;
    private String name;
    
    public Employee() {
    }
    public Employee(String job, String name) {
        this.job = job;
        this.name = name;
    }
    
    public String getJob() {
        return job;
    }
    public void setJob(String job) {
        this.job = job;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    public void earnings(){
        System.out.println("");
    };
}

Boss类 : 

public class Boss extends Employee{
    
    private int day;
    
    public Boss(int day) {
        this.day = day;
    }

    public Boss(String job, String name, int day) {
        super(job, name);
        this.day = day;
    }

    
    @Override
    public void  earnings() {
        int salary = 1500 * this.day;
        System.out.println(this.getJob() + this.getName() +"的工资为:" + salary);
    }
    
    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }
}

 HourlyWorker类 :

public class HourlyWorker extends Employee{
    
    private int hour;
    
    public HourlyWorker(int hour) {
        this.hour = hour;
    }
    public HourlyWorker(String job, String name, int hour) {
        super(job, name);
        this.hour = hour;
    }

    
    @Override
    public void earnings() {
        int salary = 75 * this.hour;
        System.out.println(this.getJob() + this.getName() +"的工资为:" + salary);
    }
    
    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }
}

PieceWorker类 : 

public class PieceWorker extends Employee{
    
    private int products;
    
    public PieceWorker(int products) {
        this.products = products;
    }
    public PieceWorker(String job, String name, int products) {
        super(job, name);
        this.products = products;
    }
    
    @Override
    public void earnings() {
        int salary = 10 * products;
        System.out.println(this.getJob() + this.getName() +"的工资为:" + salary);
    }
    
    public int getProducts() {
        return products;
    }

    public void setProducts(int products) {
        this.products = products;
    }
}

 Test类 :

public class Test {
    public static void main(String[] args) {
        
        Employee []e = new Employee[3];
        
        Employee b = new Boss("boss","张三",4);
        Employee p = new PieceWorker("pieceWorker","李四",500);
        Employee hw = new HourlyWorker("hourlyWork","王五",40);
        
        e[0] = b;
        e[1] = p;
        e[2] = hw;
        
        for (Employee employee : e) {
            employee.earnings();
        }
    }
}

(4):

Calculability接口:


public interface Calculability {
    double area();
}

Taper类 : 

public class Taper {
    
    private double h;
    
    Triangle bottom = new Triangle();

    public Taper() {
    }

    public Taper(double h, Triangle bottom) {
        this.h = h;
        this.bottom = bottom;
    }
    
    public double getH() {
        return h;
    }

    public void setH(double h) {
        this.h = h;
    }

    public Triangle getBottom() {
        return bottom;
    }

    public void setBottom(Triangle bottom) {
        this.bottom = bottom;
    }
    
    public double volume(){
        return bottom.area() * h / 3;
    }
}

 Triangle类 :

public class Triangle implements Calculability{
    
    private double width;
    private double height;
    
    public Triangle() {
    }
    public Triangle(double width, double height) {
        this.width = width;
        this.height = height;
    }
    
    public double getWidth() {
        return width;
    }
    public void setWidth(double width) {
        this.width = width;
    }
    public double getHeight() {
        return height;
    }
    public void setHeight(double height) {
        this.height = height;
    }
    
    @Override
    public double area() {
        return  width * height / 2;
    }
}

Exp6_1类: 

import javax.sound.midi.Soundbank;
import java.util.Scanner;
import java.util.SortedMap;

public class Exp6_1 {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        Triangle t = new Triangle();
        
        Taper taper = new Taper();
        
        System.out.println("请输入底面三角形的边长:");
        double w = sc.nextDouble();
        t.setWidth(w);
        System.out.println("请输入底面三角形的高:");
        double h = sc.nextDouble();
        t.setHeight(h);
        System.out.println("请输入椎体的高:");
        double H = sc.nextDouble();
        taper.setH(H);
        taper.setBottom(t);

        System.out.printf("%.3f\n",taper.volume());
    }
}

五、实验小结

  • 4
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

追上

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

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

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

打赏作者

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

抵扣说明:

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

余额充值