Java面向对象程序设计实验报告(实验二 面向对象基础练习)

 ✨作者:命运之光 
✨ 专栏:Java面向对象程序设计实验报告

目录

✨一、需求设计

✨二、概要设计

✨三、详细设计

✨四、调试结果

✨五、测试结果

✨附录:源程序代码(带注释)

测试类demo2

Address类

Employee类


实验二 面向对象基础练习

实验环境:Eclipse+JDK

实验目的:

1.掌握面向对象的编程思想;

2.掌握类和对象的定义及使用;

3.掌握封装的使用。(成员变量私有化(private)设置)

实验内容:

1.定义并测试一个代表员工的Employee类。员工属性包括“编号”、“姓名”、“基本薪水”、“薪水增长额”;还包括 “计算增长后的工资总额”。

2.编写并测试一个代表地址的Address类,地址信息由:国家,省份,城市,街道,邮编组成,并可以返回完整的地址信息。

 


以下是实验报告内容

✨✨前言:由于五一假期导致最后一节Java上机实验课没有上,所以不是很清楚实验报告的具体要求,我就按照之前数据结构实验报告的格式写了这次的Java上级报告,有需要的还是直接复制粘贴就行了,不过这次我写的比较敷衍,自己要上交实验报告的话,最好还是稍微修改美化一下🦄。

✨一、需求设计

1.定义并测试一个代表员工的Employee类。员工属性包括“编号”、“姓名”、“基本薪水”、“薪水增长额”;还包括 “计算增长后的工资总额”。

2.编写并测试一个代表地址的Address类,地址信息由:国家,省份,城市,街道,邮编组成,并可以返回完整的地址信息。

✨二、概要设计

建立三个Java类,一个为测试类,一个为第一题的Employee类,一个为第二题的Address类。将Employee类和Address类的属性设置为私有属性,构造空参与无参构造,用set()和get()方法来输入输出值,在Employee类中创建薪水增长率方法来实现薪水增长率。

三、详细设计

新建一个class类类名为Employee,依据题意定义私有成员属性“编号”、“姓名”、“基本薪水”、“薪水增长额”,分别为:

    private String id;

    private String name;

    private double money;

    private double rise;

写入set(),get()方法

 public void setId(String id) {

        this.id = id;

    }

    public void setName(String name) {

        this.name = name;

    }



    public void setMoney(double money) {

        this.money = money;

    }



    public String getId() {

        return id;

    }



    public String getName() {

        return name;

    }

    public double getMoney() {

        return money;

    }

最后定义薪水增长额方法

public double giverise(double rise) {

        return rise * money;

    }

Employee类设计完毕。

新建一个class类类名为Address,依据题意定义私有成员属性为国家,省份,城市,街道,邮编分别为:

    private String country;

    private String province;

    private String city;

    private String street;

    private String code;

写入set(),get()方法

    public void setCountry(String country) {

        this.country = country;

    }

    public void setProvince(String province) {

        this.province = province;

    }

    public void setCity(String city) {

        this.city = city;

    }

    public void setCode(String code) {

        this.code = code;

    }

    public void setStreet(String street) {

        this.street = street;

    }

    public String getCountry() {

        return country;

    }

    public String getProvince() {

        return province;

    }

    public String getCity() {

        return city;

    }

    public String getCode() {

        return code;

    }

    public String getStreet() {

        return street;

}

写入空参和无参构造分别为:

public Address(){}//空参构造

    public Address(String country,String province,String city,String street,String code)

    {

        //全参构造

        this.country=country;

        this.province=province;

        this.city=city;

        this.street=street;

        this.code=code;

    }

Address类设计完毕。

四、调试结果

第一题输入测试样例为:

雇员编号为10001

雇员姓名为ZhangSan

基本工资为1800.0

第二题输入测试样例为:

国家为中国

省份为陕西

城市为渭南

街道为ABC,

邮政编码为612000

调试结果截图如下:

五、测试结果

依据实验要求依此传入值,雇员编号为7369,雇员姓名为SMITH,基本工资为1500.0。以下为测试结果截图:

 

依据实验要求依此传入值,国家为中国,省份为陕西,城市为西安市,街道为MDXY,邮政编码为710000。以下为测试结果截图:

 

附录:源程序代码(带注释)

测试类demo2

package 命运之光;

public class demo2 {

    public static void main(String[] args) {

        Employee e=new Employee();//空参创建

        e.setId("7369");

        e.setName("SMITH");

        e.setMoney(1000.0);//基本薪水

        System.out.println("-------第一题-------");

        System.out.println("雇员编号:"+e.getId());

        System.out.println("雇员姓名:"+e.getName());

        System.out.println("基本工资:"+e.giverise(1.5));//基本工资=基本薪水*薪水增长率

        System.out.println("-------第二题-------");

        Address a=new Address();//空参创建

        a.setCountry("中国");

        a.setProvince("陕西");

        a.setCity("西安市");

        a.setStreet("MDXY");

        a.setCode("710000");

        System.out.println("国家:"+a.getCountry());

        System.out.println("省份:"+a.getProvince());

        System.out.println("城市:"+a.getCity());

        System.out.println("街道:"+a.getStreet());

        System.out.println("邮政编码:"+a.getCode());  

    }



}

Address类

package 命运之光;

public class Address {

    private String country;

    private String province;

    private String city;

    private String street;

    private String code;

    public Address(){}//空参构造

    public Address(String country,String province,String city,String street,String code)

    {

        //全参构造

        this.country=country;

        this.province=province;

        this.city=city;

        this.street=street;

        this.code=code;

    }

    public void setCountry(String country) {

        this.country = country;

    }

    public void setProvince(String province) {

        this.province = province;

    }

    public void setCity(String city) {

        this.city = city;

    }

    public void setCode(String code) {

        this.code = code;

    }

    public void setStreet(String street) {

        this.street = street;

    }

    public String getCountry() {

        return country;

    }

    public String getProvince() {

        return province;

    }

    public String getCity() {

        return city;

    }

    public String getCode() {

        return code;

    }

    public String getStreet() {

        return street;

    }

}

Employee类

package 命运之光;

public class Employee {

    private String id;

    private String name;

    private double money;

    private double rise;

    public Employee(){

        //空参构造

    }

    public Employee(String id,String name,double money,double rise)

    {

        //全参构造

        this.id = id;

        this.name = name;

        this.money = money;

        this.rise=rise;

    }

    public void setId(String id) {

        this.id = id;

    }

    public void setName(String name) {

        this.name = name;

    }



    public void setMoney(double money) {

        this.money = money;

    }



    public String getId() {

        return id;

    }



    public String getName() {

        return name;

    }

    public double getMoney() {

        return money;

    }

    public double giverise(double rise) {

        return rise * money;

    }

}

 

 点击下方个人名片,可添加博主的个人QQ,交流会更方便哦~
 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓

  • 12
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

命运之光

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

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

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

打赏作者

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

抵扣说明:

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

余额充值