文档库 最新最全的文档下载
当前位置:文档库 › Java实验三报告

Java实验三报告

一、实验目的

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

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

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

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

二、实验内容

1.题目:

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

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

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

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

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

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

2、设计并实现一个课程类,代表学校中的一门课程,将这门课程的相关信息

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

要求:

(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包中。

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

三、实验源代码

1.题目:

源代码:

3.1:

Cube类:

package com.sy3.exe01.test;

public class Cube {

float length,width,height;

public Cube(){

length=0;

width=0;

height=0;

}

public float getLength() {

return length;

}

public void setLength(float length) {

this.length = length;

}

public float getWidth() {

return width;

}

public void setWidth(float width) {

this.width = width;

}

public float getHeight() {

return height;

}

public void setHeight(float height) {

this.height = height;

}

public Cube(float length,float width,float height) {

this.length=length;

this.width=width;

this.height=height;

}

public float Tij(){

return (float)Math.round(length*width*height*100)/100;

}

public float Biaomj(){

return

(float)Math.round(length*width*2+length*height*2+width*height*2*100)/100;

}

}

Cub类:

package com.sy3.exe01.test;

import com.sy3.exe01.*;

import com.sy3.exe01.*;

import java.util.Scanner;

public class Cub {

public static void main(String[] args) {

float length,width,height;

new Scanner(System.in);

"请输入长,宽,高:");

length=input.nextFloat();

width=input.nextFloat();

height=input.nextFloat();

Cube cube=new Cube(length,width,height);

System.out.print("体积为:"+cube.Tij()+"表面积为:"+cube.Biaomj());

}

}

3.2:

Course类:

package com.sy3.exe02.test;

public class Course {

public String courId;

public String courName;

public float courCredit;

public int courHours;

public String courType;

private int passingScore=60;

public void setcourId(String courId){ this.courId=courId;

}

public void setcourName(String courName){ this.courName=courName;

}

public void setcourCredit(float courCredit){ this.courCredit=courCredit;

}

public void setcourHours(int courHours){ this.courHours=courHours;

}

public void setcourType(String courType){ this.courType=courType;

}

public String getcourId(){

return courId;

}

public String getcourName(){

return courName;

}

public float getcourCredit(){

return courCredit;

}

public int getcourHours(){

return courHours;

}

public String getcourType(){

return courType;

}

public Course(){

courId="";

courName="";

courCredit=0.0f;

courHours=0;

courType="";

}

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 toString(){

return"课程号:"+courId+"课程名:"+courName+"学分"+courCredit+"学时"+courHours+"课程类型"+courType;

}

public void shuchu(int count,Course course[]){

System.out.println("课程号"+"\t"+"课程名"+"\t\t\t"+"学分"+"\t\t"+"学时"+"\t\t"+"课程类型");

System.out.println("courId"+"\t"+"courName"+"\t\t"+"courCredit"+"\t"+"c ourHours"+"\t"+"courType");

for(int i=0;i

System.out.println(course[i].courId+"\t"+course[i].courName+"\t"+course [i].courCredit+"\t\t"+course[i].courHours+"\t\t"+course[i].courType);

}

}

}

CourseText类:

package com.sy3.exe02.test;

import com.sy3.exe02.*;

import java.util.Scanner;

public class CourseText {

static int count=0;

public static void main(String[] args) {

Course []course=new Course[6];

Course stu=new Course("E052026","Linux操作系统

",3,56,"专业选修");

course[count++]=stu;

Course stu1=new Course("S052005","面向对象程序设计

",(float)3.5,56,"专业教育");

course[count++]=stu1;

Course stu2=new Course("S052006","面向对象程序设计实验",1,32,"专业教育");

course[count++]=stu2;

Course stu3=new Course("S052037","数据库原理及应用 ",3,48,"专业教育");

course[count++]=stu3;

Course stu4=new Course("S052011","数据库原理及应用实验",1,32,"专业教育");

course[count++]=stu4;

Course stu5=new Course("S052049","操作系统

",4,64,"专业教育");

course[count++]=stu5;

System.out.println("利用增强型循环输出课程的信息");

stu5.shuchu(count,course);

printCourses(course);

System.out.print("请输入你要查找的学分:");

Scanner input=new Scanner(System.in);

float a=input.nextFloat();

printCourses(course,a);

input.close();

}

static Course stu=new Course();

public static void printCourses(Course[] course){

System.out.println("\n");

System.out.println("输出课程信息");

stu.shuchu(count,course);

}

public static void printCourses(Course[] course,float credit){ System.out.println("课程号"+"\t"+"课程名"+"\t\t\t"+"学分"+"\t\t"+"学时"+"\t\t"+"课程类型");

for(int i=0;i

if(credit==course[i].courCredit){

System.out.println(course[i].courId+"\t"+course[i].courName+"\t"+course [i].courCredit+"\t\t"+course[i].courHours+"\t\t"+course[i].courType);

}

}

}

StudentInfo类:

package com.sy3.exe03.test;

import java.util.Scanner;

public class StudentInfo {

String stuId;

String stuName;

String stuSex;

String stuBirth;

String stuClass;

String stuTel;

String stuQQ;

public void setstuId(String stuId){

t his.stuId=stuId;

}

public void setstuName(String stuName){

t his.stuName=stuName;

}

public void setstuSex(String stuSex){

t his.stuSex=stuSex;

}

public void setstuBirth(String stuBirth){ t his.stuBirth=stuBirth;

}

public void setstuClass(String stuClass){ t his.stuClass=stuClass;

}

public void setstuTel(String stuTel){

t his.stuTel=stuTel;

}

public void setstuQQ(String stuQQ){

t his.stuQQ=stuQQ;

}

public String getstuId(){

r eturn stuId;

}

public String getstuName(){

r eturn stuName;

}

public String getstuSex(){

r eturn stuSex;

}

public String getstuBirth(){

r eturn stuBirth;

}

public String getstuClass(){

r eturn stuClass;

}

public String getstuTel(){

r eturn stuTel;

}

public String getstuQQ(){

r eturn stuQQ;

}

public StudentInfo(){

s tuId="";

s tuName="";

s tuSex="";

s tuBirth="";

s tuClass="";

s tuTel="";

s tuQQ="";

}

public StudentInfo(String stuId,String stuName,String stuSex,String stuClass){

t his.stuId=stuId;

t his.stuName=stuName;

t his.stuSex=stuSex;

t his.stuClass=stuClass;

}

public StudentInfo(String stuId,String stuName,String stuSex,String

stuBirth,String stuClass,String stuTel,String stuQQ){

t his.stuId=stuId;

t his.stuName=stuName;

t his.stuSex=stuSex;

t his.stuBirth=stuBirth;

t his.stuClass=stuClass;

t his.stuTel=stuTel;

t his.stuQQ=stuQQ;

}

public void shulu(){

S canner input=new Scanner(System.in);

t his.stuId=input.next();

this.stuName=input.next();

this.stuSex=input.next();

this.stuBirth=input.next();

this.stuClass=input.next();

this.stuTel=input.next();

this.stuQQ=input.next();

}

public void showStuInfo(){

S ystem.out.print("学号:"+stuId+"姓名:"+stuName+"性别:"+stuSex+"出身日期:"+stuBirth);

S ystem.out.print("班级名称:"+stuClass+"联系电话:"+stuTel+"QQ号码:"+stuQQ);

}

}

Contacts类:

package com.sy3.exe03.test;

import java.util.Scanner;

public class Contacts {

StudentInfo students[]=new StudentInfo[100];

private int count=0;

int i,j;

Scanner input=new Scanner(System.in);

public void setcount(int count){

this.count=count;

}

public int getcount(){

return count;

public void append(StudentInfo student){

System.out.println("请输入学生信息(学号、姓名、性别、出生日期、班级名称、联系电话、QQ号码):");

student.shulu();

students[count++]=student;

}

public void update(StudentInfo newStudent,String stuId){

for(i=0;i

if((students[i].stuId).equals(stuId)){

System.out.println("请输入你要修改的学生信息(学号、姓名、性别、出生日期、班级名称、联系电话、QQ号码):");

newStudent.shulu();

students[i]=newStudent;

}

}

}

public void delete(String stuId){

for(i=0;i

if((students[i].stuId).equals(stuId)){

for(j=i;j

students[j]=students[j+1];

count--;

}

}

}

}

public void findByStuId(String stuId){

f or(i=0;i

if((students[i].stuId).equals(stuId)){

System.out.print("学号:"+students[i].stuId+"姓

名:"+students[i].stuName+"性别:"+students[i].stuSex+"出身日

期:"+students[i].stuBirth);

S ystem.out.println("班级名称:"+students[i].stuClass+"联系电

话:"+students[i].stuTel+"QQ号码:"+students[i].stuQQ);

}

}

i f(i>=count)

System.out.print("不存在!");

}

public void findByStuClass(String stuClass){

f or(i=0;i

if((students[i].stuClass).equals(stuClass)){

System.out.print("学号:"+students[i].stuId+"姓

名:"+students[i].stuName+"性别:"+students[i].stuSex+"出身日

期:"+students[i].stuBirth);

S ystem.out.println("班级名称:"+students[i].stuClass+"联系电

话:"+students[i].stuTel+"QQ号码:"+students[i].stuQQ);

}

}

}

public void find(){

f or(i=0;i

System.out.print("学号:"+students[i].stuId+"姓

名:"+students[i].stuName+"性别:"+students[i].stuSex+"出身日

期:"+students[i].stuBirth);

S ystem.out.println("班级名称:"+students[i].stuClass+"联系电

话:"+students[i].stuTel+"QQ号码:"+students[i].stuQQ);

}

}

}

StudentInfoText类:

package com.sy3.exe03.test;

import java.util.Scanner;

public class Contacts {

StudentInfo students[]=new StudentInfo[100];

private int count=0;

int i,j;

Scanner input=new Scanner(System.in);

public void setcount(int count){

this.count=count;

}

public int getcount(){

return count;

}

public void append(StudentInfo student){

System.out.println("请输入学生信息(学号、姓名、性别、出生日期、班级名称、联系电话、QQ号码):");

student.shulu();

students[count++]=student;

}

public void update(StudentInfo newStudent,String stuId){

for(i=0;i

if((students[i].stuId).equals(stuId)){

System.out.println("请输入你要修改的学生信息(学号、姓名、性别、出生日期、班级名称、联系电话、QQ号码):");

newStudent.shulu();

students[i]=newStudent;

}

}

}

public void delete(String stuId){

for(i=0;i

if((students[i].stuId).equals(stuId)){

for(j=i;j

students[j]=students[j+1];

count--;

}

}

}

}

public void findByStuId(String stuId){

f or(i=0;i

if((students[i].stuId).equals(stuId)){

System.out.print("学号:"+students[i].stuId+"姓

名:"+students[i].stuName+"性别:"+students[i].stuSex+"出身日

期:"+students[i].stuBirth);

S ystem.out.println("班级名称:"+students[i].stuClass+"联系电

话:"+students[i].stuTel+"QQ号码:"+students[i].stuQQ);

}

}

i f(i>=count)

System.out.print("不存在!");

}

public void findByStuClass(String stuClass){

f or(i=0;i

if((students[i].stuClass).equals(stuClass)){

System.out.print("学号:"+students[i].stuId+"姓

名:"+students[i].stuName+"性别:"+students[i].stuSex+"出身日

期:"+students[i].stuBirth);

S ystem.out.println("班级名称:"+students[i].stuClass+"联系电话:"+students[i].stuTel+"QQ号码:"+students[i].stuQQ);

}

}

}

public void find(){

f or(i=0;i

System.out.print("学号:"+students[i].stuId+"姓

名:"+students[i].stuName+"性别:"+students[i].stuSex+"出身日期:"+students[i].stuBirth);

S ystem.out.println("班级名称:"+students[i].stuClass+"联系电话:"+students[i].stuTel+"QQ号码:"+students[i].stuQQ);

}

}

}

实验结果:

3.1:

3.2:

3.3:

四、实验结论

要注意两个或者是三个子类是属于同一个父类下的,在同一个包下需要建立两个类。要注意这两个子类的先后关系,分清楚每一个子类的工作,哪一个是运行,哪一个是属于主函数的子类,最后要注意的就是程序里面的问题了。

相关文档