admin管理员组文章数量:1534214
2023年12月14日发(作者:)
《Java语言程序设计》实验报告书
信息技术与商务管理系
目录
实验三:类与对象 .................................................................................................................... 3
1 学时 ..................................................................................................................................... 3 2 实验目的与要求 ................................................................................................................. 3
3 实验环境 ............................................................................................................................. 3
4 准备工作 ............................................................................................................................. 3
5 实验内容 ............................................................................................................................. 3
6 实验步骤 ............................................................................................................................. 3
7 思考题 ................................................................................................................................. 8
2
- - 实验三:类与对象
1 学时
4学时
2 实验目的与要求
复习以前学习的知识,要求学生牢固类与对象的概念及关系,并能够解决实际问题。
3 实验环境
Jdk1.5或以上版本
4 准备工作
确认JDK、Eclipse开发环境可用
5 实验内容
1、练习类与对象的创建
2、练习构造方法
3、练习static修饰符的使用
6 实验步骤
建java源文件,在原文件中编写代码,运行看结果。重复上述步骤。
1. 创建猴子类
1) 编写一个类Monkey,用来表示猴子类;
给Monkey类确定属性(学生随意设定,例如名字、类别、年龄等);
编写展示猴子能够爬树的方法(方法内只需打印猴子的“名称+能够跳”)。
public class monkey {
String name ;
double age;
String name(String a){
return a;
}
double age(double b){
return b;
}
public static void main(String[] args) {
monkey c=new monkey();
=11;
="monkey";
(+"is"++"can jump");
}
}
2. 定义一个表示图书的类
1) 名字为Book
2) 属性包含:书名、价格、出版社、作者等信息
3) 方法包含:编写一个方法,输出一本书的基本信息。
public class book {
String name;
3
- -
}
double price;
String press;
book(String a,double b,String c){
name=a;
price=b;
press=c;
}
void getname(){
("姓名"+name);
}
void getprine(){ ("价格"+price);
}
void getpress(){ ("出版社"+press);
}
public static void main(String[] args) {
book b1;
b1=new book("java", 30, "东软");
e();
ss();
ne();
();
}
3. 创建商品类
1) 编写一个商品类Good;
2) 给Good类确定属性(学生随意设定,例如名称、生产日期、价格、品牌等);
3) 编写一个计算折扣价格的方法,要求该方法带有一个参数,表示折扣的百分比。
public class Good {
double price;
string name;
Good(double a ,string b){
name=a;
price=b;
}
double getprice(double e){
price =price - price*e;
return price;
}
public static void main(String[] args) {
Good a =new Good(10,10);
ce(12);
}
4. 创建Rectangle类与对象
1) 创建一个Rectangle类;
2) 属性:两个double成员变量,width和height。
3) 方法:计算矩形的周长length()和面积area()。
4) 编写测试类,创建Rectangle对象,并给两个成员变量赋值,调用周长和面积的方法,输出周长和面积。
4
- - public class
Rectangle{
double width ;
double heigh;
double length;
double area;
Rectangle ( double a ,double b){
width=a;
heigh=b;
}
double length(double a ,double b){
length= (a+b)*2;
return length;
}
double area(double a,double b){
area=a*b;
return area;
}
public static void main(String[] args) {
Rectangle a=new
Rectangle (12,12);
(12,12);
(12, 12);
n((12, 12));
n((12, 12));
}
}
5. Loan类与对象
1) 定义一个类Loan表示贷款
2) 属性包含:年利率(默认为2.5%)
贷款年限(默认为1)
贷款额(默认为1000)
贷款发生的日期
3) 构造方法包括:默认的构造方法
带参的构造方法
4) 方法包含:计算月支付额的方法
计算总支付额的方法
5) 编写一个测试类,定义两个Loan类的对象,分别调用两个对象的各个方法。
public class loan {
double rate=0.025;
double age_limt=1;
double credit =1000;
double date=1;
double statement;
double month;
loan(double a,double b,double c,double d){
rate=a;
age_limt=b;
credit=c;
date=d;
}
double statement(double a ,double b,double c ,double d){
return statement= (b*c*a+c)/12;
}
double getmonth(double a,double b,double c,double d){
return month = b*a*c+c;
}
/**
5
- -
* @param args
*/
public static void main(String[] args) {
loan r=new loan(0.025,1,1000,1);
ent(0.025,1,1000,1);
th(0.025, 1, 1000, 1);
(th(0.025, 1, 1000, 1));
}}
6. 构造方法
1) 编写一个类Student
2) 属性:姓名name,学号number(int)
3) 方法:无参构造方法,有参构造方法(两个参数,分别给姓名和学号赋初始值)
4) 编写测试类,实例化两个Student对象
Student s1 = new Student();
= "liuyang";
= 100001;
Student s2 = new Student("guojing",100002);
public class Student {
private String name ="";
private int number=0;
Student next;
Student (String a,int b ){
=name;
=number;
}
String getname(String a){
a="xuyi";
return a;
}
int getnumber(int b){
b=1001;
return b;
}
public static void main(String[] args) {
Student a= new Student("xuyi",1001);
= new Student("yangxue",1002);
=new Student("zhanglei",1003);
Student d = new Student("w",1005);
for(int i=0;i<=3;i++){
(a);
a=;
if (a!=null){();}
else (+);}
}
}
体会使用构造方法来给对象属性赋初始值的形式。
7. 练习static修饰符的使用
编写类Teacher;
属性:教师的课时数量和计算课时的系数(所有教师的课时系数相同,使用static修饰),均为double类型;
方法:courseCompute(),可计算教师的当量课时(=课时量*系数),返回值类型为double。
编写一个测试类进行测试,创建两个教师对象,分别具有不同的课时数量,课时系数为1.2,输出计算后的两位老师的当量课时。
将系数修改后,输出修改后的当量课时。
public class teacher {
double hour;
static double coefficient;
6
- - double teacher(){
return hour*coefficient;
}
public static void main(String[] args) {
teacher a=new teacher();
=12;
cient=0.5;
(r());
}
}
8. 简单类型变量和对象类型变量的区别
1) 写出下题的运行结果,并分析使用了哪些知识点。
2) 写出下题的运行结果,并分析使用了哪些知识点。
7
- -
7 思考题
8
- -
版权声明:本文标题:《java语言程序设计》实验报告 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1702490909a9767.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论