基于 Object 类及包装类的专项实验

2.1 验证性实验

1、用户类(User)的封装与构造方法实现。调试下面的程序,分析程序的功能和运行结果并添加代码注释。

|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| public class User {​ private String username;​ private String password;​ private String tel;​ ​ public User() {}​ ​ public User(String username, String password, String tel) {​ this.username = username;​ this.password = password;​ this.tel = tel;​ }​ ​ public String getUsername() {​ return username;​ }​ public void setUsername(String username) {​ this.username = username;​ }​ ​ public String getPassword() {​ return password;​ }​ ​ public void setPassword(String password) {​ this.password = password;​ }​ ​ public String getTel() {​ return tel;​ }​ ​ public void setTel(String tel) {​ this.tel = tel;​ }​ } |

2、在上述 User 类中覆盖 toString 方法,格式为 "username:xxx,password:xxx,tel:xxx"。调试下面的程序,并分析程序的功能和运行结果并添加代码注释。

|--------------------------------------------------------------------------------------------------------------------|
| @Override​ public String toString() {​ return "username:" + username + ",password:" + password + ",tel:" + tel;​ } |

3、在上述 User 类中覆盖 equals 方法,只要用户名相同则判定为相同对象。调试下面的程序,并分析程序的功能和运行结果并添加代码注释。

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| @Override​ public boolean equals(Object obj) {​ if (this == obj) return true;​ if (obj == null || getClass() != obj.getClass()) return false;​ User user = (User) obj;​ return username.equals(user.username);​ } |

4、编写测试类,调试下面的程序,并分析程序的功能和运行结果并添加代码注释。

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| import java.util.Scanner;​ ​ public class UserTest {​ public static void main(String[] args) {​ Scanner scanner = new Scanner(System.in);​ ​ System.out.println("请输入第一个用户信息:");​ System.out.print("用户名:");​ String username1 = scanner.nextLine();​ System.out.print("密码:");​ String password1 = scanner.nextLine();​ System.out.print("电话:");​ String tel1 = scanner.nextLine();​ User user1 = new User(username1, password1, tel1);​ ​ System.out.println("请输入第二个用户信息:");​ System.out.print("用户名:");​ String username2 = scanner.nextLine();​ System.out.print("密码:");​ String password2 = scanner.nextLine();​ System.out.print("电话:");​ String tel2 = scanner.nextLine();​ }​ } |

5、学生类(Student)的封装与包装类应用。调试下面的程序,分析程序的功能和运行结果并添加代码注释。

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| public class Student { private String name; private Integer age; private Double score; public Student() {} public Student(String name, Integer age, Double score) { this.name = name; this.age = (age != null && age >= 0) ? age : null; this.score = (score != null && score >= 0 && score <= 100) ? score : null; } public String getName() { return name; } public Integer getAge() { return age; } public Double getScore() { return score; } public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = (age != null && age >= 0) ? age : null; } public void setScore(Double score) { this.score = (score != null && score >= 0 && score <= 100) ? score : null; } } |

6、在上述Student 类基础上覆盖 toString 方法,"姓名:xxx,年龄:xxx,成绩:xxx" ,其中无效数据(null )显示为 "未填写"。调试下面的程序,并分析程序的功能和运行结果并添加代码注释。

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| @Override public String toString() { String ageStr = (age != null) ? age.toString() : "未填写"; String scoreStr = (score != null) ? score.toString() : "未填写"; return "姓名:" + name + ",年龄:" + ageStr + ",成绩:" + scoreStr; } |

7、在 Student 类 set 方法基础上,新增 isAgeValid、isScoreValid 方法。调试下面的程序,并分析程序的功能和运行结果并添加代码注释。

|----------------------------------------------------------------------------------------------------------------------------------------------------------------|
| public boolean isAgeValid() { return age != null && age >= 0; } public boolean isScoreValid() { return score != null && score >= 0 && score <= 100; } |

8、编写测试类,调试下面的程序,并分析程序的功能和运行结果并添加代码注释。

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| public class StudentTest { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入学生信息:"); System.out.print("姓名:"); String name = scanner.nextLine(); System.out.print("年龄:"); Integer age = null; try { age = Integer.valueOf(scanner.nextLine()); } catch (NumberFormatException e) { // 输入非数字,age保持null } System.out.print("成绩:"); Double score = null; try { score = Double.valueOf(scanner.nextLine()); } catch (NumberFormatException e) { // 输入非数字,score保持null } Student student = new Student(name, age, score); System.out.println("学生信息:" + student); System.out.println("年龄是否有效:" + student.isAgeValid()); System.out.println("成绩是否有效:" + student.isScoreValid()); scanner.close(); } } |

9、包装类常用方法综合应用。调试下面的程序,并分析程序的功能和运行结果并添加代码注释。

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| public class WrapperMethodTest { public static void main(String[] args) { String strInt = "100"; int num = Integer.parseInt(strInt); System.out.println("字符串\"100\"转int:" + num); String strDouble = "95.5"; Double d = Double.valueOf(strDouble); System.out.println("字符串\"95.5\"转Double:" + d); System.out.println("int最大值:" + Integer.MAX_VALUE); System.out.println("int最小值:" + Integer.MIN_VALUE); System.out.println("double最大值:" + Double.MAX_VALUE); System.out.println("double最小值:" + Double.MIN_VALUE); Integer a = 50; Integer b = 100; System.out.println("a compareTo b:" + a.compareTo(b)); } } |

2.2 自主设计性实验

10、编程实现学生对象信息输出和比较。

要求:定义 Student 类,属性包括学号(id)、姓名(name)、年龄(age),进行封装并提供 get/set 方法及构造方法;重写 toString 方法,格式为 "id:xxx,name:xxx,age:xxx";重写 equals 方法,学号相同则为相同对象。编写测试类,创建两个 Student 对象,输入信息并判断是否相同,打印对象信息。​

11、编程实现商品类(Goods)的比较​

要求:定义 Goods 类,属性有商品编号(goodsId)、商品名称(goodsName)、价格(price),完成封装、构造方法;重写 equals 方法,商品编号相同则为相同商品;重写 toString 方法展示商品信息。编写测试类,输入两个商品信息,判断是否为同一商品并打印结果。​

相关推荐
曾经的三心草3 小时前
基于正倒排索引的Java文档搜索引擎1-实现索引模块-实现Parser类
java·开发语言·搜索引擎
q***01653 小时前
Python爬虫完整代码拿走不谢
开发语言·爬虫·python
vx_bscxy3223 小时前
告别毕设焦虑!Python 爬虫 + Java 系统 + 数据大屏,含详细开发文档 基于web的图书管理系统74010 (上万套实战教程,赠送源码)
java·前端·课程设计
顺心而行...3 小时前
一些问题记录
开发语言
u***j3243 小时前
JavaScript在Node.js中的进程管理
开发语言·javascript·node.js
字节拾光录4 小时前
Java工具库三足鼎立:Hutool、Apache Commons、Guava深度测评与场景化选型指南
java·apache·guava
爱学习的小可爱卢4 小时前
Java UDP编程实战:UDP数据报套接字编程DatagramPacket、DatagramSocket 、InetSocketAddress
java·udp·udp数据报
未来之窗软件服务4 小时前
幽冥大陆(三十五)S18酒店门锁SDK go语言——东方仙盟筑基期
java·前端·golang·智能门锁·仙盟创梦ide·东方仙盟·东方仙盟sdk
r***93484 小时前
【Redis】在Java中以及Spring环境下操作Redis
java·redis·spring
沐知全栈开发4 小时前
前端控制器模式
开发语言