基于 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 方法展示商品信息。编写测试类,输入两个商品信息,判断是否为同一商品并打印结果。​

相关推荐
xcl09253 小时前
商超智能运营实战:数据驱动提升门店效率与体验指南
java·mfc·宠物
Flynt5 小时前
OpenJDK禁AI代码半年了,现在执行得怎么样?答案是:全靠自觉
java·开源·ai编程
古法安卓10 小时前
Android-日志系统源码解析
android·java·android studio
小夏coding11 小时前
从"一把梭"到"精妙拆解" —— 滑动窗口计时框架的设计演进
java·后端
MacroZheng12 小时前
同事问我:"Claude Code经常失忆,不怕它把项目搞炸?",我:"怕,三个Markdown文件给它装个永不丢失的外置大脑!"
java·人工智能·后端
十年Java程序媛14 小时前
深度实战:JDK21 虚拟线程 + HikariCP 生产正确配比、坑点、监控全套
java·spring boot
xiaoqiMikko14 小时前
有人在搜一个不存在的 Tomcat 版本
java·tomcat
用户31268748772017 小时前
ConcurrentHashMap 怎么保证线程安全?从分段锁到 CAS+synchronized
java
vipxieliang17 小时前
ValidX vs Apache Commons Validator:功能与性能对比
java·spring boot
SimonKing17 小时前
升级Spring Boot 4后,从 Jackson 2 到 3,到底有哪些变化
java·后端·程序员