实验5:Java类与对象
一、实验目的
(1)使用类来封装对象的属性和功能; (2)掌握Java类的定义。 (3)java对象的使用
二、实验任务
(1)按要求编写一个Java程序。
(2)按要求完善一个Java程序,编译、运行这个程序,并写出运行结果。
三、实验内容
1.编写一个Java程序片断,以定义一个表示学生的类Student。这个类的属性有“学号”、“班号”、“姓名”、“性别”、“年龄”,方法有“获得学号”、“获得班号”、“获得性别”、“获得姓名”、“获得年龄”、“获得年龄”。 2.为类Student增加一个方法public String toString( ),该方法把Student类的对象的所有属性信息组合成一个字符串以便输出显示。编写一个Java Application程序,创建Student类的对象,并验证新增加的功能。
class Student{ long studentID; int classID; String name; String sex; int age; public Student(long studentID,int classID,String name,String sex,int age){ this.studentID=studentID; this.classID=classID; this.name=name; this.sex=sex; this.age=age; } public long getStudentID(){ return studentID; } public int getClassID(){ return classID;
} public String getName(){ return name; } public String getSex(){ return sex; } public int getAge(){ return age; } public String toString(){ return \学号:\班号:\姓名:\性别:\年龄:\ } }
public class StudentDemo{ public static void main(String[] args){ Student s1=new Student(90221,2,\ System.out.println(s1.toString()); } }
运行结果: 学号:90221 班号:2 姓名:Tom 性别:male 年龄:20
2.程序填空,程序中包含以下内容:
一个学生类(Student),包含:
属性:学号s_No,姓名s_Name,性别s_Sex,年龄s_Age。
方法:构造方法,显示学号方法showNo(),显示姓名方法showName(),显示性别方法showSex(),显示年龄方法showAge(),修改年龄方法modifyAge()。
主类(X2),包含:
主方法main(),在其中创建两个学生对象s1和s2并初始化,第一个对象的属性为(101,\张三\男\,第二个对象的属性值为(102,\李四\女\,然后分别显示这两个学生的学号、姓名、性别、年龄,然后修改s1的年龄并显示修改后的结果。
【编程分析】按照要求首先编写Student类,其中的属性和方法根据实际情况选择相应的类型和权限修饰符,要通过方法来修改年龄,因此s_Age属性应该为private类型,否则可以直接在主类中进行修改,就没有必要使用方法了。 【参考答案】
定义的Student类:
class Student{ private int s_No; private String s_Name; private String s_Sex; private int s_Age; Student(int no, String name, String sex, int age){ s_No = no; }
【代码1】:s_Name = name;
s_Sex = sex; s_Age = age; }
void showNo(){
【代码2】:System.out.println(\学号:\ //输出学生的学号
}
void showName(){ System.out.println(\姓名:\}
void showSex(){ System.out.println(\性别:\}
void showAge(){ System.out.println(\年龄:\}
void modifyAge(int newAge){ }
【代码3】:s_Age = newAge; //更改学生的年龄
以下为主类:
public class X2 {
public static void main(String[] args) { Student s1 = new Student(101,\张三\男\ Student s2 = new Student(102,\李四\女\ //实例化对象s2,并同时初始化
System.out.println(\第1个学生的信息为:\
【代码4】:s1.showNo(); //调用成员方法显示第一个对象的学号
s1.showName();
【代码5】:s1.showSex(); //调用成员方法显示第一个对象的性别
s1.showAge();
System.out.println(\第2个学生的信息为:\s2.showNo();
【代码6】:s2.showName();//调用成员方法显示第二个对象的姓名
s2.showSex(); s2.showAge();
System.out.println(\修改第1个学生的年龄:\s1.modifyAge(24);
System.out.println(\第1个学生的年龄修改为:\
【代码7】:s1.showAge();//调用成员方法显示第一个对象的年龄 } }
【运行结果】:
3.编写一个程序,程序中包含以下内容: 一个圆类(Circle) ,包含:
属性:圆半径radius;常量:PI。
方法:构造方法;求面积方法area();求周长方法:perimeter()。 主类(X4_3_1),包含:
主方法main(),在主方法中创建圆类的对象c1和c2并初始化,c1的半径为100,c1的半径为200,然后分别显示两个圆的面积和周长。
【编程分析】按照要求创建Circle类,其中的半径可以定义为int类型,PI定义为final double类型,求面积和周长的方法都应定义为double类型,在构造方法中对radius进行初始化。 【参考答案】
public class X3 {
public static void main(String[] args) { Circle c1 = new Circle(100); Circle c2 = new Circle(200); System.out.println(\= \+c1.area()+\= \
System.out.println(\= \+c2.area()+\= \
} }
class Circle{ int radius; final double PI=3.14; Circle(int r){ radius = r; } double area(){ return PI*radius*radius; } double perimeter(){ return 2*PI*radius; } }
【运行结果】
c1.area() = 31400.0 c1.perimenter() = 628.0 c2.area() = 125600.0 c2.perimenter() = 1256.0

