实验名称Java上机实验二姓名:山水不言学号:xxxxxx一、实验内容○1编程创建一个Box类。
要求:定义三个实例变量分别表示立方体的长、宽和高,定三个构造方法(必须包含一个无参构造方法,一个满参构造方法)对这3个变量进行初始化,然后定义一个方法求立方体的体积。
创建一个对象,求给定尺寸的立方体的体积。
实验思路:创建一个Box类,将它的长宽高以及体积实例变量设为private,类中声明一个求体积的public方法。
设置三个构造函数来对者三个变量初始化:1、构造函数有三个参数,分别对应实例的长、宽和高。
构造函数可以对变量赋值合法性进行判断。
2、构造函数有两个参数则对应实例的长和宽,高默认为1,构造函数可以对函数合法性进行判断。
3、构造函数没有参数实例的长宽高默认为0在另一个类里创建3个Box对象,调用volume函数分别求体积。
实验源代码:package com.box;public class box_demo {public static void main(String[] args) {// TODO自动生成的方法存根Box box1=new Box();Box box2=new Box(12.3,34.1);Box box3=new Box(12.3,34.1,10.0);System.out.print("the volume of thebox1:"+box1.volume()+"\n");System.out.print("the volume of thebox2:"+box2.volume()+"\n");System.out.print("the volume of thebox3:"+box3.volume());}}package com.box;public class Box {private double height;private double width;private double length;private double volume;public Box(){this.height=0;this.length=0;this.width=0;}public Box(double Box_len,double Box_wid,double Box_height) {if(Box_len<0)this.length=0;elsethis.length=Box_len;if(Box_wid<0)this.width=0;elsethis.width=Box_wid;if(Box_height<0)this.height=0;elsethis.height=Box_height;}public Box(double Box_len,double Box_wid){if(Box_len<0)this.length=0;elsethis.length=Box_len;if(Box_wid<0)this.width=0;elsethis.width=Box_wid;this.height=1;}public double volume(){this.volume=this.length*this.width*this.height;return this.volume;}}○2学生类的创建和使用(1)创建一个Student类,包括的域有学号、班号、姓名、性别年龄等,且都是private类型。
(2)声明一个构造方法,以初始化对象的所有域。
(3)声明分别获得各属性(学号,班号,姓名,性别,年龄)的各个public方法。
(4)声明修改各属性(学号,班号,姓名,性别,年龄)的各个public方法。
(5)声明一个为public型的toString()方法,把该类中的所有域信息组合成一个字符串。
(6)在类中声明统计班级总人数的私有域count的到班级总人数的public方法(可在构造方法里进行Student对象个数的增加)。
(7)将类Student放在子包student中。
(8)在子包student外,创建测试类Student的主类。
在主类中,使用Student类创建两个Student对象,输出对象的所有域信息;修改对象的姓名和年龄,修改后显示对象的姓名和年龄;比较两个Student对象的年龄大小,输出年龄较大的Student对象。
实验思路:1、创建一个Student类,两个构造方法,无参的构造函数:默认姓名和性别为空,其余项都为0。
有全参的构造函数:分别为对象的实例变量赋值。
2、写获得各属性的public方法,返回的是:this.变量名3、写修改各属性的public方法,判断参数的合法性,修改变量值4、写toString函数,利用Integer.toString()函数把整型变量转化为字符串,把所有域信息连接,中间用’\t’隔开。
5、写一个public static函数getclass_number()获得班级人数,返回值是static 型变量count6、在student包外,比较两个Student对象的年龄需要调用Student对象获得年龄的方法get_stu_age(),用两个临时变量来保存函数返回的年龄,比较大小,年龄大的利用get_stu_name()来获得对象的名字。
实验源代码:package com.server;import com.student.Student;public class demo {public static void main(String[] args) {Student stu1,stu2;stu1=new Student(13,1704,"慕寨","男",48);stu2=new Student(14,1704,"衫泽","女",27);System.out.println("no"+"\tclass"+"\tname"+"\tsex"+"\tage") ;System.out.print(stu1.toString());System.out.print(stu2.toString());stu1.set_name("慕寒");stu1.set_age(29);stu2.set_name("杉泽");stu2.set_age(28);System.out.println("update:");System.out.println("new name:"+stu1.get_stu_name()+"new age"+stu1.get_stu_age());System.out.println("new name:"+stu2.get_stu_name()+"new age:"+stu2.get_stu_age());int st1_age=stu1.get_stu_age();int st2_age=stu2.get_stu_age();System.out.println("the older one:");if(st1_age>st2_age){System.out.print(stu1.get_stu_name());}else{System.out.print(stu1.get_stu_name());}}}package com.student;public class Student {private int s_no;private int c_no;private String name;private String sex;private int age;private static int count;public Student(){s_no=0;c_no=0;name="";sex="";age=0;}public Student(int stu_no,int class_no,String stu_name, String stu_sex,int stu_age){this.s_no=stu_no;this.c_no=class_no;=stu_name;if(stu_sex!="男"&&stu_sex!="女")this.sex="男";elsethis.sex=stu_sex;if(stu_age<0)this.age=0;elsethis.age=stu_age;count++;}public int get_stu_no(){return this.s_no;}public int get_class_no(){return this.c_no;}public String get_stu_name(){return ;}public String get_stu_sex(){return this.sex;}public int get_stu_age(){return this.age;}/*修改对象*/public void set_stu_no(int new_sno){if(new_sno<=0)return;this.s_no=new_sno;}public void set_class_no(int new_cno){if(new_cno<=0)return;this.c_no=new_cno;}public void set_name(String new_name){=new_name;}public void set_sex(String new_sex){if(new_sex!="男"&&new_sex!="女")return;this.sex=new_sex;}public void set_age(int new_age){if(new_age<0)return;this.age=new_age;}public String toString(){String stu_string;stu_string=Integer.toString(this.s_no)+"\t"+Integer.toStrin g(this.c_no)+"\t"++"\t"+this.sex+"\t"+Integer.toString(this.ag e)+"\n";return stu_string;}public static int getclass_number(){return count;}}二、实验心得在此次实验中我将创建类和使用类的代码放在两个.Java文件中,对于每个类来说,正确的使用需要有合适的对外接口。