2025年Java硬核程序设计实验“类与对象”详解【细节满满&源码完整】

Java硬核程序设计实验“类与对象”详解【细节满满&源码完整】作为上学年 Java 硬核程序设计实验详解 系列补充 目录 一 实验目的 二 实验内容 1 编程创建一个立方体类 Cube 2 设计并实现一个课程类 代表学校中的一门课程 3 设计并实现一个 信电工程学院学生通信录 系统 三 实验源代码 1 源代码 2

大家好,我是讯享网,很高兴认识大家。

❥ 作为上学年 “Java硬核程序设计实验详解” 系列补充 ❥

目录

☀️| 一、实验目的

☀️| 二、实验内容

⭐️1、编程创建一个立方体类Cube

⭐️2、设计并实现一个课程类,代表学校中的一门课程...

⭐️3、设计并实现一个“信电工程学院学生通信录”系统

☀️| 三、实验源代码

❥1.源代码:

❥2.源代码:

❥3.源代码:

☀️| 四、实验总结


☀️| 一、实验目的

1.掌握类的定义和使用、对象的声明和创建;

2.理解构造方法的作用,掌握构造方法定义和使用;

3.理解类变量与实例变量,类方法与实例方法的区别;

4.掌握包的概念与使用。


☀️| 二、实验内容

⭐️1、编程创建一个立方体类Cube

在其中定义三个变量length,width,height分别表示一个立方体的长、宽和高,再定义一个方法setCube对这三个变量进行初始化,然后定义求立方体的体积和表面积的方法。

(1)编写一个测试类,在main方法中创建立方体对象,求给定尺寸的立方体的体积和表面积,并输入结果。

(2)改用构造方法实现长、宽和高的初始化。

(3)测试类放在包名为com.sy3.exe01.test包中,其它类放在com.sy3.exa01包中。

(知识点:类的定义,对象的创建,构造方法的使用)

(注意:如果需要从键盘输入长宽高的数据时,请从测试类的main方法中输入,程序的其它地方只接受传递的参数)

⭐️2、设计并实现一个课程类,代表学校中的一门课程...

将这门课程的相关信息组成该类的属性(如课程代码、课程名称、课程类别、学时、学分等),并在类中定义各个属性相关的访问方法(如获取和设置课程代码、课程名称,课程类别,获取和修改学时、学分,打印输出课程基本信息等),最后使用主函数测试此类(包括创建对象,调用相应方法等)。

课程号

课程名

学分

学时

课程类型

courId

courName

courCredit

courHours

courType

E052026

Linux操作系统

3

56

专业选修

S052005

面向对象程序设计

3.5

56

专业教育

S052006

面向对象程序设计实验

1

32

专业教育

S052037

数据库原理及应用

3

48

专业教育

S052011

数据库原理及应用实验

1

32

专业教育

S052049

操作系统

4

64

专业教育

要求:

(1)创建一个课程类Course,定义该类的各个属性(如上表第二行),为个属性设置相应的setter/getter方法。

(2)定义一个没有参数和带有全部参数的构造方法。

(3)重写toString()方法,返回课程的信息。

(4)在该类中定义一个类变量passingScore,及格标准为60.

(5)新建测试类,在main方法中创建一个大小为6的课程数组,把上面表格中的6门课信息保存在该数组中。利用增强型循环输出课程的信息。

(6)在测试类中定义一个静态方法printCourses(Course[] courses),通过该方法输出课程中的信息。

(7)在测试类中定义一个静态方法printCourses(Course[] courses,float credit),通过该方法输出学分等于credit的课程信息。

(8)测试类放在包名为com.sy3.exe02.test包中,其它类放在com.sy3.exa02包中。

(知识点:类的定义,对象的创建,构造方法的使用,对象数组,静态变量和静态方法,方法重载)

(注意:课程的信息可以直接给定,不一定要通过键盘输入)

⭐️3、设计并实现一个“信电工程学院学生通信录”系统

系统采用字符界面,在出现的主界面中按提示输入相应字符以选择“增加”、“删除”、“修改”、“查找”等功能进行操作。学生通信录信息的存储可采用数组的方式。

要求:

(1)新建一个学生信息类StudentInfo,定义该类的各个属性(如下表第二行),变量的访问修饰符为private,为个属性设置相应的setter/getter方法。

(2)为StudentInfo类定义三个构造方法,一个没有参数,一个带有学号、姓名、性别及班级四个参数,一个带有全部参数。

(3)在StudentInfo类中定义显示学生信息的方法 void showStuInfo()。


讯享网

(4)创建一个通讯录类Contacts,在该类中定义一个能最大能容纳100个学生的对象数组students,该数组用于保存学生信息,定义一个表示当前有几个学生信息的变量count,该变量类型为整型。变量的访问修饰符为private,为个属性设置相应的setter/getter方法。

(5)在Contacts类中定义添加学生信息、修改学生信息和删除学生信息,以及查找学生信息的方法。

   ● void append(StudentInfo student)

   //在添加学生信息时,学号要保证是唯一的,且保证没有超过最大100人。

   ● void update(StudentInfo newStudent,String stuId)

   //根据学号进行修改

   ● void delete(String stuId)

   //根据学号删除学生信息

   ● Student findByStuId(String stuId)

   //根据学号查找某个学生信息

   ● Student[] findByStuClass(String stuClass)

   //根据班级查找某个班里学生信息

   ● Student[] find()

   //查找所有学生信息

(6)创建测试类,根据输入的命令参数完成学生通讯录信息的“增加”、“删除”、“修改”、“查找”等功能。

(7)测试类放在包名为com.sy3.exe03.test包中,其它类放在com.sy3.exa03包中。

(知识点:类的定义,对象的创建,构造方法的重载,对象数组,对象与对象之间的关系)

(注意:参考数据如下,所有数据的输入都在测试类里完成)

学号

姓名

性别

出生日期

班级名称

联系电话

号码

stuId

stuName

stuSex

stuBirth

stuClass

stuTel

stu

刘丹

女

1992/10/2

13网络1单

徐小怡

女

1992/10/3

13网络1单

赵蒙

女

1992/10/4

13网络2单

陈冰倩

女

1992/10/5

13网络2单

张琦琦

女

1992/10/6

13网络2单

施锦烨

女

1992/10/7

13网络3单

数据部分:


☀️| 三、实验源代码

❥1.源代码:

package EXPS.Exp03.com.sy3.exa01.test; import EXPS.Exp03.com.sy3.exa01.Cube; import java.util.Scanner; / * 班级:19软嵌2 * 学号: * 姓名:夏旭 * 实验时间:2020-4-6 * 本程序的功能是:实现立方体类Cube */ public class Exp03_01_ { public static void main(String[] args) { Cube cube=new Cube(); System.out.println("请输入长宽高(空格隔开)"); Scanner sc=new Scanner(System.in); int len=sc.nextInt(); int wid=sc.nextInt(); int hei=sc.nextInt(); cube.setCube(len,wid,hei); System.out.println("表面积: "+cube.countSurfacearea()); System.out.println("体积: "+cube.countVolume()); } } package EXPS.Exp03.com.sy3.exa01; public class Cube { private int length; private int width; private int height; public Cube(){ this.length=this.width=this.height=0; } public Cube(int length,int width,int height){ this.length=length; this.width=width; this.height=height; } public int countSurfacearea(){ int a=this.length; int b=this.width; int c=this.height; return (a*b+b*c+a*c)*2; } public int countVolume(){ return this.length*this.width*this.height; } public void setCube(int length, int width, int height) { this.length = length; this.width = width; this.height = height; } public int getLength() { return length; } public void setLength(int length) { this.length = length; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } }

讯享网

❥2.源代码:

讯享网package EXPS.Exp03.com.sy3.exa02.test; import EXPS.Exp03.com.sy3.exa02.Course; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List; /  * 班级:19软嵌2  * 学号:  * 姓名:夏旭  * 实验时间:2020-4-6  * 本程序的功能是:实现一个课程类  */ public  class Exp03_02_ {     public static void main(String[] args) {         String [][] courseArray={                 {"E052026",    "Linux操作系统",   "3"    ,"56", "专业选修"},                 {"S052005","面向对象程序设计","3.5","56","专业教育"},                 {"S052006","面向对象程序设计实验","1","32","专业教育"},                 {"S052037","数据库原理及应用","3","48","专业教育"},                 {"S052011","数据库原理及应用实验","1","32","专业教育"},                 {"S052049","操作系统","4","64","专业教育"},         };         Course []lis=new Course[6];         int idx=0;         for(String[] i:courseArray){             lis[idx++]=new Course(i[0],i[1],Float.parseFloat(i[2]),Integer.parseInt(i[3]),i[4]);         }         printCourses(lis);     }     public static List<Course> printCourses(Course[] courses, float credit){         List<Course> res=new ArrayList<>();         for(int i=0;i<courses.length;i++){             if(credit==courses[i].getCourCredit()){                 res.add(courses[i]);             }         }         return res;     }     public static void printCourses(Course[] courses){         for(Course i:courses){             System.out.println(i.toString());         }     } } package EXPS.Exp03.com.sy3.exa02; public class Course {     private String courId;     private String courName;     private float courCredit;     private int courHours;     private String courType;     static {         int passingScore=60;     }     public Course(String courId,String courName,float courCredit,int courHours,String courType){         this.courId=courId;         this.courName=courName;         this.courCredit=courCredit;         this.courHours=courHours;         this.courType=courType;     }     public String getCourId() {         return courId;     }     public void setCourId(String courId) {         this.courId = courId;     }     public String getCourName() {         return courName;     }     public void setCourName(String courName) {         this.courName = courName;     }     public double getCourCredit() {         return courCredit;     }     public void setCourCredit(float courCredit) {         this.courCredit = courCredit;     }     public int getCourHours() {         return courHours;     }     public void setCourHours(int courHours) {         this.courHours = courHours;     }     public String getCourType() {         return courType;     }     public void setCourType(String courType) {         this.courType = courType;     }     @Override     public String toString() {         return "Course{" +                 "courId='" + courId + '\'' +                 ", courName='" + courName + '\'' +                 ", courCredit=" + courCredit +                 ", courHours=" + courHours +                 ", courType='" + courType + '\'' +                 '}';     } }

❥3.源代码:

package EXPS.Exp03.com.sy3.exa03.test; import EXPS.Exp03.com.sy3.exa03.Contacts; import EXPS.Exp03.com.sy3.exa03.StudentInfo; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /  * 班级:19软嵌2  * 学号:  * 姓名:夏旭  * 实验时间:2020-4-6  * 本程序的功能是:实现一个“信电工程学院学生通信录”系统  */ public class Exp03_03_ {     private static Boolean flag=false;     public static void main(String[] args)  {         Contacts contacts=new Contacts();         contacts.setList(loadData());         System.out.println("数据导入完成");         Scanner sc=new Scanner(System.in);         while(!flag){             printMenu();             int type=sc.nextInt();             if(type<1 || type>5){                 System.out.println("输入有误,请重新输入");                 continue;             }             if(type==1){                 System.out.println("请依次输入学号,姓名,性别,生日,班级,电话,");                 sc.skip("\n");                 String dataLine= sc.nextLine();                 String [] dataArray=dataLine.split(" ");                 if(dataArray.length!=7){                     System.out.println("信息格式有误,将退回");                     continue;                 }                 StudentInfo node=new StudentInfo();                 buildNode(node,dataArray);                 contacts.append(node);             }             else  if(type==2){                 System.out.println("请输入要删除的学号");                 sc.skip("\n");                 String sId=sc.next();                 contacts.delete(sId);             }             else  if(type==3){                 System.out.println("请输入要修改的学号");                 sc.skip("\n");                 String sId=sc.next();                 System.out.println("请输入数据");                 sc.skip("\n");                 String sData=sc.nextLine();                 String []dataArray=sData.split(" ");                 StudentInfo node=new StudentInfo();                 buildNode(node,dataArray);                 contacts.update(node,sId);             }             else if(type==4){                 boolean queryFlag=false;                 while(!queryFlag){                     printQueryMenu();                     sc.skip("\n");                     int qType=sc.nextInt();                     if(qType==1){                         System.out.println("输入学号");                         sc.skip("\n");                         String sId=sc.next();                         StudentInfo res=contacts.findByStuId(sId);                         if(res!=null){                             System.out.println(res.toString());                         }                         else{                             System.out.println("未找到数据");                         }                     }                     else if (qType==2){                         System.out.println("输入班级");                         sc.skip("\n");                         String sClass=sc.next();                         List<StudentInfo> members=contacts.findByStuClass(sClass);                         if(members.size()!=0){                             for(int i=0;i<members.size();i++){                                 System.out.println(members.get(i).toString());                             }                         }                         else{                             System.out.println("未找到数据");                         }                     }                     else if(qType==3){                         List<StudentInfo> members=contacts.find();                         if(members.size()!=0){                             for(int i=0;i<members.size();i++){                                 System.out.println(members.get(i).toString());                             }                         }                         else{                             System.out.println("数据空");                         }                     }                     else{                         queryFlag=true;                     }                 }             }             else{                 flag=true;             }         }     }     public static ArrayList<StudentInfo> loadData() {         ArrayList<StudentInfo> res=new ArrayList<>();         res.add(new StudentInfo("","刘丹","女","1992/10/2","13网络1单",""," "));         res.add(new StudentInfo("","徐小怡","女","1992/10/3","13网络1单","",""));         res.add(new StudentInfo("","赵蒙","女","1992/10/4","13网络2单","","" ));         res.add(new StudentInfo("","陈冰倩","女","1992/10/5","13网络2单","","" ));         res.add(new StudentInfo("","张琦琦","女","1992/10/6","13网络2单","",""));         //res.append(new StudentInfo("","施锦烨","女","1992/10/7","13网络3单","",""));         return res;     }     public static void printMenu(){         System.out.println("-----------------");         System.out.println("1.增    2.删     3.改       4.查    5.退出");         System.out.println("-----------------");     }     public static void buildNode(StudentInfo node,String[] dataArray){         node.setStuId(dataArray[0]);         node.setStuBirth(dataArray[1]);         node.setStuSex(dataArray[2]);         node.setStuBirth(dataArray[3]);         node.setStuClass(dataArray[4]);         node.setStuTel(dataArray[5]);         node.setStu(dataArray[6]);     }     public static void  printQueryMenu(){         System.out.println("-----------------");         System.out.println("1.根据学号查找    2.根据班级查找     3.查找全部数据       4.返回");         System.out.println("-----------------");     } } package EXPS.Exp03.com.sy3.exa03; import java.util.ArrayList; import java.util.List; public class Contacts {     private  final int maxPool=100;     private int count;     private ArrayList<StudentInfo> list;     public int getMaxPool() {         return maxPool;     }     public int getCount() {         return count;     }     public void setCount(int count) {         this.count = count;     }     public ArrayList<StudentInfo> getList() {         return list;     }     public void setList(ArrayList<StudentInfo> list) {         this.list = list;     }     public void append(StudentInfo student){         if(this.count>=maxPool){             System.out.println("添加失败,序列已满");             return;         }         String Id=student.getStuId();         for(int i=0;i<list.size();i++){             String tempId=list.get(i).getStuId();             if(Id.equals(tempId)){                 System.out.println("添加失败,该学号已存在");                 return;             }         }         list.add(student);         System.out.println("添加完成");     }     public void update(StudentInfo newStudent,String stuId){         for(int i=0;i<list.size();i++){             if(stuId.equals(list.get(i).getStuId())){                 list.remove(i);                 list.add(newStudent);                 System.out.println("修改完成");                 return;             }         }         System.out.println("未找到数据");     }     public void delete(String stuId){         for(int i=0;i<list.size();i++){             if(stuId.equals(list.get(i).getStuId())){                 list.remove(i);                 System.out.println("删除成功");                 return;             }         }         System.out.println("未找到数据 ");     }     public StudentInfo findByStuId(String stuId){         for(int i=0;i<list.size();i++){             if(stuId.equals(list.get(i).getStuId())){                 return list.get(i);             }         }         return null;     }     public List<StudentInfo> findByStuClass(String stuClass){         List<StudentInfo> res=new ArrayList<>();         for(int i=0;i<list.size();i++){             if(stuClass.equals(list.get(i).getStuClass())){                 res.add(list.get(i));             }         }         return res;     }     public List<StudentInfo> find(){         return list;     } } package EXPS.Exp03.com.sy3.exa03; import javax.print.DocFlavor; public class StudentInfo {     private String stuId;     private String stuName;     private String stuSex;     private String stuBirth;     private String stuClass;     private String stuTel;     private String stu;     public StudentInfo() {     }     public StudentInfo(String stuId,String stuName,String stuSex,String stuClass){         this.stuId = stuId;         this.stuName = stuName;         this.stuSex = stuSex;         this.stuClass = stuClass;     }     public StudentInfo(String stuId, String stuName, String stuSex, String stuBirth, String stuClass, String stuTel, String stu) {         this.stuId = stuId;         this.stuName = stuName;         this.stuSex = stuSex;         this.stuBirth = stuBirth;         this.stuClass = stuClass;         this.stuTel = stuTel;         this.stu = stu;     }     public String getStuId() {         return stuId;     }     public void setStuId(String stuId) {         this.stuId = stuId;     }     public String getStuName() {         return stuName;     }     public void setStuName(String stuName) {         this.stuName = stuName;     }     public String getStuSex() {         return stuSex;     }     public void setStuSex(String stuSex) {         this.stuSex = stuSex;     }     public String getStuBirth() {         return stuBirth;     }     public void setStuBirth(String stuBirth) {         this.stuBirth = stuBirth;     }     public String getStuClass() {         return stuClass;     }     public void setStuClass(String stuClass) {         this.stuClass = stuClass;     }     public String getStuTel() {         return stuTel;     }     public void setStuTel(String stuTel) {         this.stuTel = stuTel;     }     public String getStu() {         return stu;     }     public void setStu(String stu) {         this.stu = stu;     }     public void showStuInfo(){         System.out.println("stuId="+stuId);         System.out.println("stuName="+stuName);         System.out.println("stuSex="+stuSex);         System.out.println("stuBirth="+stuBirth);         System.out.println("stuClass="+stuClass);         System.out.println("stuTel="+stuTel);         System.out.println("stu="+stu);     }     @Override     public String toString() {         return "StudentInfo{" +                 "stuId='" + stuId + '\'' +                 ", stuName='" + stuName + '\'' +                 ", stuSex='" + stuSex + '\'' +                 ", stuBirth='" + stuBirth + '\'' +                 ", stuClass='" + stuClass + '\'' +                 ", stuTel='" + stuTel + '\'' +                 ", stu='" + stu + '\'' +                 '}';     } }

☀️​​​​​​​| 四、实验总结

通过这次实验,我掌握了类的定义和使用、对象的声明和创建。理解了构造方法的作用以及类变量与实例变量,掌握了构造方法的定义和使用以及类方法与实例方法的区别。

小讯
上一篇 2025-03-06 09:48
下一篇 2025-03-23 10:17

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/99159.html