《java程序设计》实验指导书(完整) 联系客服

发布时间 : 星期一 文章《java程序设计》实验指导书(完整)更新完毕开始阅读ad00a72b4631b90d6c85ec3a87c24028915f8505

.

}

}

6.验证书中的例题。 三、试验要求

1、预习试验容并写出上机报告。 2、实验中出现的问题及实验体会。

实验三 面向对象的程序设计(一)

一、实验目的

1. 熟悉类的创建方法。 2. 掌握对象的声明与创建。

3. 能利用面向对象的思想解决一般问题。 二、实验容

1. 以下程序能否通过编译?上机验证并指明错误原因与改法 Class Location{ Private int x,y;

Public void Location(int a,int b) { X=a; y=b;}

Public int getX(){return x;} Public int getY(){return y;}

Public static void main(String args[]){ Location loc=new Location(12,20); System.out.println(loc.getX()); System.out.println(loc.getY()); }

2. 创建一个图书类,类中包含的属性有:书名、作者、出版社;包含的方法有:设置书籍状态,查看书籍状态。书籍状态有在馆和外借两种。

public class Lib{ //创建一个图书类,类中包含的属性有:书名、作者、出版社;包含的方法有:设置书籍状态,查看书籍状态。书籍状态有在馆和外借两种。 // private String Bname; private String Aname;

Word资料

.

private String Baddress; //书籍状态 private boolean zt; //包含方法set get public void setzt(boolean zt){ this.zt=zt; } public boolean getzt(boolean zt){ return zt; } public static void main(String[] args){ }

}3. 设计一个Birthday类,其成员变量有:year,month,day;提供构造方法、输出Birthday对象值的方法和计算年龄的方法。编写程序测试这个类。

public class Birthday{ //设计一个Birthday类,其成员变量有:year,month,day;提供构造方法、输出Birthday对象值的方法和计算年龄的方法。编写程序测试这个类。 private int year; private int month; private int day; public Birthday(int year,int month,int day){ this.year=year; this.month=month; this.day=day; } public void printBirthDay(){ System.out.println(year+\ } public int printAge(){ return 2012-year; } public static void main(String[] args){ } }4.(选作)编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类Person,并作为学生数据操作类Student和教师数据操作类Teacher的基类。

public class Person {

//定义Person类

public int bianhao; public String name;

public Person(int biaohao,String name){ }

public void input(int bianhao,String name){

this.bianhao=bianhao; this.name=name;

Word资料

.

}

}

this.bianhao=bianhao; this.name=name;

public void printXS(){ //定义显示(这边有点疑问) }

public static void main(String[] args) { }

System.out.println(bianhao+\+name);

class Student extends Person{ //定义学生类 }

class Teacher extends Person{ }

String zhicheng; String bumen;

private Teacher(int bianhao,String name,String zhicheng,String bumen){ }

super(bianhao,name); this.bianhao=bianhao; this.name=name; this.zhicheng=zhicheng; this.bumen=bumen;

//定义教师类

int banhao; int chengji;

private Student(int bianhao,String name,int banhao,int cj){ }

super(bianhao,name); this.banhao=banhao; this.chengji=cj;

5.验证书中的例题。 三、实验要求

1. 事先预习,写出预习报告 2. 上机后写出实验报告

Word资料

.

实验四 面向对象的程序设计(二)

一、实验目的 1.熟悉类的定义

2.掌握对象的声明、实例化及成员的引用 3.掌握构造方法及实例方法的区别与用法 二、实验容

1.编写一个类,描述汽车,其中用字符型描述车的牌号,用浮点型描述车的价格。编写一个测试类,其中有一个修改价格的方法,对汽车对象进行操作,根据折扣数修改汽车的价格,最后在main()方法中输出修改后的汽车信息。

class Car{ }

public class TestCar{ }

public static void main(String[] args){ }

Car c=new Car(\奔驰S6OO\,50000); c.dismessage(); String chePai; float price; float price1;

Car(String chePai,float price){ }

void dismessage(){ }

System.out.println(\这辆车的品牌是\+chePai+\原价是\+price+\打折后为\+price1); this.chePai=chePai; this.price1=price*4/5; this.price=price;

2. 设计一个银行帐户类,成员变量包括账号、储户姓名、开户时间、身份证、存款余额等帐户信息,成员方法包括存款、取款操作。

public class Test { public static void main(String args[]){ Bank b1 = new Bank(\鹿鹿\鹿容\ b1.cun(100000.00);b1.qu(10000.00);b1.info(); } }

class Bank{ private String user; private String name; private String time; private int id; private double money; Bank(String user,String name,String time,int id,double money){ this.user = user;this.name = name;this.time = time;this.id = id;this.money = money; } public void cun(double inMoney){ money = money+inMoney;

Word资料