《java程序设计》实验指导书(完整)(2)

2019-09-01 17:52

XX学院java实验报告

状态,查看书籍状态。书籍状态有在馆和外借两种。 //

private String Bname; private String Aname; 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){

6

XX学院java实验报告

}

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

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 chengji;

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

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

public void input(int bianhao,String name){ }

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

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

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

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

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

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

7

XX学院java实验报告

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

一、实验目的 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;

8

XX学院java实验报告

}

}

public void qu(double outMoney){ if(money-outMoney>=0){

money = money-outMoney; } }

public void info(){

System.out.println(\余额还有\}

3. 编写一个java程序,设计一个汽车类Vehicle,包含的属性有车轮的个数wheels和车重weight。小汽车类Car是Vehicle的子类,包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。

public class Vehicle { }

class smallCar extends Vehicle{ }

class Truck extends smallCar{

int wheels; double weights;

Vehicle(int wheels,double weights){ this.wheels=wheels; }

void disMessage(){

System.out.println(\这个车车轮个数是\+wheels+\重量是\+weights+\斤\); }

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

Vehicle v=new Vehicle(8,10.00); smallCar c=new smallCar(6); Truck t=new Truck(10); v.disMessage(); c.disM(); t.disM2(); t.disM3();

this.weights=weights;

//这是构造方法

int loader;

smallCar(int loader){ }

void disM(){

System.out.println(\这个小汽车可载\+loader+\人\); }

super(8,10.00); this.loader=loader;

int payload;

Truck(int payload){

9

XX学院java实验报告

}

void disM2(){

System.out.println(\这卡车载重为\+payload+\); }

void disM3(){

System.out.println(\这卡车有\+wheels+\个轮子\+\车重有\+weights+\斤\+\可载super(6);

this.payload=payload;

\+loader+\人\+\载重为\+payload+\斤\); } }

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

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

10


《java程序设计》实验指导书(完整)(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:100篇初中生阅读题(含答案)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: