• 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类:

    1. public class Circle {
    2. private double radius;
    3. public Circle() {
    4. }
    5. public Circle(double radius) {
    6. this.radius = radius;
    7. }
    8. public double getRadius() {
    9. return radius;
    10. }
    11. public void setRadius(double radius) {
    12. this.radius = radius;
    13. }
    14. public double getArea(double r) {
    15. return Math.PI * Math.pow(r, 2);
    16. }
    17. }

    Cone类: 

    1. public class Cone {
    2. private double h;
    3. private double r;
    4. public Cone() {
    5. }
    6. public Cone(double h, double r) {
    7. this.h = h;
    8. this.r = r;
    9. }
    10. public double getH() {
    11. return h;
    12. }
    13. public void setH(double h) {
    14. this.h = h;
    15. }
    16. public double getR() {
    17. return r;
    18. }
    19. public void setR(double r) {
    20. this.r = r;
    21. }
    22. public double getVolume(double h, double r) {
    23. return Math.PI * Math.pow(r, 2) * h;
    24. }
    25. }

    Cylinder类: 

    1. public class Cylinder {
    2. private double h;
    3. private double r;
    4. public Cylinder() {
    5. }
    6. public Cylinder(double h, double r) {
    7. this.h = h;
    8. this.r = r;
    9. }
    10. public double getH() {
    11. return h;
    12. }
    13. public void setH(double h) {
    14. this.h = h;
    15. }
    16. public double getR() {
    17. return r;
    18. }
    19. public void setR(double r) {
    20. this.r = r;
    21. }
    22. public double getVolume(double h, double r) {
    23. return Math.PI * Math.pow(r, 2) * h / 3;
    24. }
    25. }

    Test类: 

    1. import java.math.BigDecimal;
    2. import java.math.RoundingMode;
    3. import java.util.Scanner;
    4. public class Test {
    5. public static void main(String[] args) {
    6. Scanner sc = new Scanner(System.in);
    7. Circle c1 = new Circle();
    8. Cone c2 = new Cone();
    9. Cylinder c3 = new Cylinder();
    10. System.out.println("===================圆的面积==================");
    11. System.out.println("请输入圆的半径:");
    12. double r1 = sc.nextDouble();
    13. System.out.printf("%.4f\n", c1.getArea(r1));
    14. System.out.println("===================圆柱的体积==================");
    15. System.out.println("请输入圆柱的半径:");
    16. double r2 = sc.nextDouble();
    17. System.out.println("请输入圆柱的高:");
    18. double h2 = sc.nextDouble();
    19. System.out.printf("%.4f\n", c2.getVolume(h2, r2));
    20. System.out.println("===================圆锥的体积==================");
    21. System.out.println("请输入圆锥的半径:");
    22. double r3 = sc.nextDouble();
    23. System.out.println("请输入圆锥的高:");
    24. double h3 = sc.nextDouble();
    25. System.out.printf("%.4f\n", c3.getVolume(h3, r3));
    26. }
    27. }

    (2):

    MyClass类 : 

    1. public class MyClass {
    2. private int data;
    3. public MyClass() {
    4. }
    5. public MyClass(int data) {
    6. this.data = data;
    7. }
    8. public int getData() {
    9. return data;
    10. }
    11. public void setData(int data) {
    12. this.data = data;
    13. }
    14. }

     SubClass类 :

    1. public class SubClass extends MyClass{
    2. private int Mydata;
    3. public SubClass() {
    4. }
    5. public SubClass(int mydata) {
    6. Mydata = mydata;
    7. }
    8. public int getMydata() {
    9. return Mydata;
    10. }
    11. public void setMydata(int mydata) {
    12. Mydata = mydata;
    13. }
    14. }

    Test类 :

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

    (3):

    Employee类 :

    1. public class Employee {
    2. private String job;
    3. private String name;
    4. public Employee() {
    5. }
    6. public Employee(String job, String name) {
    7. this.job = job;
    8. this.name = name;
    9. }
    10. public String getJob() {
    11. return job;
    12. }
    13. public void setJob(String job) {
    14. this.job = job;
    15. }
    16. public String getName() {
    17. return name;
    18. }
    19. public void setName(String name) {
    20. this.name = name;
    21. }
    22. public void earnings(){
    23. System.out.println("");
    24. };
    25. }

    Boss类 : 

    1. public class Boss extends Employee{
    2. private int day;
    3. public Boss(int day) {
    4. this.day = day;
    5. }
    6. public Boss(String job, String name, int day) {
    7. super(job, name);
    8. this.day = day;
    9. }
    10. @Override
    11. public void earnings() {
    12. int salary = 1500 * this.day;
    13. System.out.println(this.getJob() + this.getName() +"的工资为:" + salary);
    14. }
    15. public int getDay() {
    16. return day;
    17. }
    18. public void setDay(int day) {
    19. this.day = day;
    20. }
    21. }

     HourlyWorker类 :

    1. public class HourlyWorker extends Employee{
    2. private int hour;
    3. public HourlyWorker(int hour) {
    4. this.hour = hour;
    5. }
    6. public HourlyWorker(String job, String name, int hour) {
    7. super(job, name);
    8. this.hour = hour;
    9. }
    10. @Override
    11. public void earnings() {
    12. int salary = 75 * this.hour;
    13. System.out.println(this.getJob() + this.getName() +"的工资为:" + salary);
    14. }
    15. public int getHour() {
    16. return hour;
    17. }
    18. public void setHour(int hour) {
    19. this.hour = hour;
    20. }
    21. }

    PieceWorker类 : 

    1. public class PieceWorker extends Employee{
    2. private int products;
    3. public PieceWorker(int products) {
    4. this.products = products;
    5. }
    6. public PieceWorker(String job, String name, int products) {
    7. super(job, name);
    8. this.products = products;
    9. }
    10. @Override
    11. public void earnings() {
    12. int salary = 10 * products;
    13. System.out.println(this.getJob() + this.getName() +"的工资为:" + salary);
    14. }
    15. public int getProducts() {
    16. return products;
    17. }
    18. public void setProducts(int products) {
    19. this.products = products;
    20. }
    21. }

     Test类 :

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

    (4):

    Calculability接口:

    1. public interface Calculability {
    2. double area();
    3. }

    Taper类 : 

    1. public class Taper {
    2. private double h;
    3. Triangle bottom = new Triangle();
    4. public Taper() {
    5. }
    6. public Taper(double h, Triangle bottom) {
    7. this.h = h;
    8. this.bottom = bottom;
    9. }
    10. public double getH() {
    11. return h;
    12. }
    13. public void setH(double h) {
    14. this.h = h;
    15. }
    16. public Triangle getBottom() {
    17. return bottom;
    18. }
    19. public void setBottom(Triangle bottom) {
    20. this.bottom = bottom;
    21. }
    22. public double volume(){
    23. return bottom.area() * h / 3;
    24. }
    25. }

     Triangle类 :

    1. public class Triangle implements Calculability{
    2. private double width;
    3. private double height;
    4. public Triangle() {
    5. }
    6. public Triangle(double width, double height) {
    7. this.width = width;
    8. this.height = height;
    9. }
    10. public double getWidth() {
    11. return width;
    12. }
    13. public void setWidth(double width) {
    14. this.width = width;
    15. }
    16. public double getHeight() {
    17. return height;
    18. }
    19. public void setHeight(double height) {
    20. this.height = height;
    21. }
    22. @Override
    23. public double area() {
    24. return width * height / 2;
    25. }
    26. }

    Exp6_1类: 

    1. import javax.sound.midi.Soundbank;
    2. import java.util.Scanner;
    3. import java.util.SortedMap;
    4. public class Exp6_1 {
    5. public static void main(String[] args) {
    6. Scanner sc = new Scanner(System.in);
    7. Triangle t = new Triangle();
    8. Taper taper = new Taper();
    9. System.out.println("请输入底面三角形的边长:");
    10. double w = sc.nextDouble();
    11. t.setWidth(w);
    12. System.out.println("请输入底面三角形的高:");
    13. double h = sc.nextDouble();
    14. t.setHeight(h);
    15. System.out.println("请输入椎体的高:");
    16. double H = sc.nextDouble();
    17. taper.setH(H);
    18. taper.setBottom(t);
    19. System.out.printf("%.3f\n",taper.volume());
    20. }
    21. }

    五、实验小结

  • 相关阅读:
    Unity3D占用内存太大怎么解决呢? -补
    理解 期望和方差(均值/估计值)
    segmenter
    报错:Error: module property was removed from Dependency
    5年测试工程师被裁后发出灵魂一问:不会自动化就要被看不起吗?
    【PAT甲级】1141 PAT Ranking of Institutions
    Java每日笔试题错题分析(2)
    ADB入门教程
    【iOS】—— 响应者链和事件传递链
    关于报表打印
  • 原文地址:https://blog.csdn.net/qq_61228493/article/details/127910979