java程序设计(辛运帏第二版)课后习题答案.doc_第1页
java程序设计(辛运帏第二版)课后习题答案.doc_第2页
java程序设计(辛运帏第二版)课后习题答案.doc_第3页
java程序设计(辛运帏第二版)课后习题答案.doc_第4页
java程序设计(辛运帏第二版)课后习题答案.doc_第5页
已阅读5页,还剩278页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

/习题2.2import java.util.*;class MyDate private int year; private int month; private int day; public MyDate(int y,int m,int d)/构造函数,构造方法 year=y; month=m; day=d; /end public MyDate(int y,int m,int d) public int getYear()/返回年 return year; /end getYear() public int getMonth()/返回月 return month; /end getMonth() public int getDay()/返回日 return day; /end getDay() /end class MyDateclass Employee private String name; private double salary; private MyDate hireDay; public Employee(String n,double s,MyDate d) name=n; salary=s; hireDay=d; /end public Employee(String n,double s,MyDate d) public void print() System.out.println(名字:+name+n工资:+salary+n雇佣年份:+hireYear()+n); /end print() public void raiseSalary(double byPercent) salary*=1+byPercent/100; /end public int hireYear() return hireDay.getYear(); /end class Employeepublic class MyTestClass public static void main(String args) Employeestaff=new Employee3; staff0=new Employee(Harry Hacker,35000,new MyDate(1989,10,1); staff1=new Employee(Carl Carcker,75000,new MyDate(1987,12,15); staff2=new Employee(Tony Tester,38000,new MyDate(1990,3,12); int integerValue; System.out.println(The information of employee are:); for(integerValue=0;integerValue=2;integerValue+) staffintegerValue.raiseSalary(5); /end for() for(integerValue=0;integerValue=2;integerValue+) staffintegerValue.print(); /end for() /end main()/end class MyTestClass/习题2.4import java.util.*;public class DataType public static void main(String args) boolean flag; char yesChar; byte finByte; int intValue; long longValue; short shortValue; float floatValue; double doubleValue; flag=true; yesChar=y; finByte=30; intValue=-7000; longValue=200l; shortValue=20000; floatValue=9.997E-5f; doubleValue=floatValue*floatValue; System.out.println(the values are:); System.out.println(布尔类型变量flag=+flag); System.out.println(字符型变量yesChar=+yesChar); System.out.println(字节型变量finByte=+finByte); System.out.println(整型变量intValue=+intValue); System.out.println(长整型变量longValue=+longValue); System.out.println(短整型变量shortValue=+shortValue); System.out.println(浮点型变量floatValue=+floatValue); System.out.println(双精度浮点型变量doubleValue=+doubleValue); /end main()/习题2.9import java.util.*;class PubTest1 private int ivar1; private float fvar1,fvar2; public PubTest1() fvar2=0.0f; public float sum_f_I() fvar2=fvar1+ivar1; return fvar2; public void print() System.out.println(fvar2=+fvar2); public void setIvar1(int ivalue) ivar1=ivalue; public void setFvar1(float ivalue) fvar1=ivalue; public class PubMainTest public static void main(String args) PubTest1 pubt1=new PubTest1(); pubt1.setIvar1(10); pubt1.setFvar1(100.02f); pubt1.sum_f_I(); pubt1.print(); /习题2.10import java.util.*;class Date private int year; private int month; private int day; public Date(int day, int month, int year) /构造函数,构造方法 this.year = year; this.month = month; this.day = day; /end public MyDate(int y,int m,int d) public int getYear() /返回年 return year; /end getYear() public int getMonth() /返回月 return month; /end getMonth() public int getDay() /返回日 return day; /end getDay() /end class Datepublic class Teacher String name;/教师名字 boolean sex;/性别,true表示男性 Date birth;/出生日期 String salaryID;/工资号 String depart;/教师所在系所 String posit;/教师职称 String getName() return name; void setName(String name) = name; boolean getSex() return sex; void setSex(boolean sex) this.sex = sex; Date getBirth() return birth; void setBirth(Date birth) this.birth = birth; String getSalaryID() return salaryID; void setSalaryID(String salaryID) this.salaryID = salaryID; String getDepart() return depart; void setDepart(String depart) this.depart = depart; String getPosit() return posit; void setPosit(String posit) this.posit = posit; public Teacher() System.out.println(父类无参数的构造方法!); /如果这里不加上这个无参数的构造方法将会出错! public Teacher(String name,boolean sex,Date birth, String salaryid,String depart,String posit) =name; this.sex=sex; this.birth=birth; this.salaryID=salaryid; this.depart=depart; this.posit=posit; /end Teacher() public void print() System.out.print(the teachername:); System.out.println(this.getName(); System.out.print(the teachersex:); if(this.getSex()=false) System.out.println(女); else System.out.println(男); System.out.print(the teacherbirth:); System.out.println(this.getBirth().getYear()+-+ this.getBirth().getMonth()+-+ this.getBirth().getDay(); System.out.print(the teachersalaryid:); System.out.println(this.getSalaryID(); System.out.print(the teacherposit:); System.out.println(this.getPosit(); System.out.print(the teacherdepart:); System.out.println(this.getDepart(); /end print() public static void main(String args) Date dt1=new Date(11,23,1989); Date dt2=new Date(2,6,1975); Date dt3=new Date(11,8,1964); Date dt4=new Date(10,4,1975); Date dt5=new Date(8,9,1969); /创建各系教师实例,用来测试 Teacher t1=new Teacher(王莹,false,dt1,123,经济学,prefessor); ResearchTeacher rt=new ResearchTeacher(杨zi青,true,dt2,421,软件工程, associate prefessor,software); LabTeacher lat=new LabTeacher(王夏瑾,false,dt3,163,外语, pinstrucor,speech lab); LibTeacher lit=new LibTeacher(马二孩,true,dt4,521,大学物理, prefessor,physicalLib); AdminTeacher at=new AdminTeacher(王xi,false,dt5,663,环境, prefessor,dean); /分别调用各自的输出方法,输出相应信息/ System.out.println(-); t1.print();/普通教师信息 System.out.println(-); rt.print();/研究系列教师信息 System.out.println(-); lat.print();/普通教师信息 System.out.println(-); lit.print();/实验系列教师信息 System.out.println(-); at.print();/行政系列教师信息 System.out.println(-); /end main()/end public class Teacherclass ResearchTeacher extends Teacher private String resField; public ResearchTeacher(String name, boolean sex, Date birth, String salaryid, String depart, String posit, String resField) = name; this.sex = sex; this.birth = birth; this.salaryID = salaryid; this.depart = depart; this.posit = posit; this.resField = resField; /end public ResearchTeacher() String getResField() return resField; void setResField(String resField) this.resField=resField; public void print() System.out.print(research teacher info is:); System.out.print(the teachername:); System.out.println(this.getName(); System.out.print(the teachersex:); if (this.getSex() = false) System.out.println(女); else System.out.println(男); System.out.print(the teacherbirth:); System.out.println(this.getBirth().getYear() + - + this.getBirth().getMonth() + - + this.getBirth().getDay(); System.out.print(the teachersalaryid:); System.out.println(this.getSalaryID(); System.out.print(the teacherposit:); System.out.println(this.getPosit(); System.out.print(the teacherdepart:); System.out.println(this.getDepart(); System.out.print(the teacherresField:); System.out.println(this.getResField(); /end print()/end class ResearchTeacherclass LabTeacher extends Teacher private String labName; public LabTeacher(String name, boolean sex, Date birth, String salaryid, String depart, String posit, String labName) = name; this.sex = sex; this.birth = birth; this.salaryID = salaryid; this.depart = depart; this.posit = posit; this.labName = labName; /end public ResearchTeacher() String getLabName() return labName; void setLabName(String labName) this.labName=labName; public void print() System.out.print(lab teacher info is:); System.out.print(the teachername:); System.out.println(this.getName(); System.out.print(the teachersex:); if (this.getSex() = false) System.out.println(女); else System.out.println(男); System.out.print(the teacherbirth:); System.out.println(this.getBirth().getYear() + - + this.getBirth().getMonth() + - + this.getBirth().getDay(); System.out.print(the teachersalaryid:); System.out.println(this.getSalaryID(); System.out.print(the teacherposit:); System.out.println(this.getPosit(); System.out.print(the teacherdepart:); System.out.println(this.getDepart(); System.out.print(the teacherlabName:); System.out.println(this.labName); /end print()/end class LabTeacherclass LibTeacher extends Teacher private String libName; public LibTeacher(String name,boolean sex,Date birth, String salaryid,String depart, String posit,String libName) = name; this.sex = sex; this.birth = birth; this.salaryID = salaryid; this.depart = depart; this.posit = posit; this.libName=libName; /end public ResearchTeacher() String getLibName() return libName; void setLibName(String libName) this.libName=libName; public void print() System.out.print(lib teacher info is:); System.out.print(the teachername:); System.out.println(this.getName(); System.out.print(the teachersex:); if (this.getSex() = false) System.out.println(女); else System.out.println(男); System.out.print(the teacherbirth:); System.out.println(this.getBirth().getYear() + - + this.getBirth().getMonth() + - + this.getBirth().getDay(); System.out.print(the teachersalaryid:); System.out.println(this.getSalaryID(); System.out.print(the teacherposit:); System.out.println(this.getPosit(); System.out.print(the teacherdepart:); System.out.println(this.getDepart(); System.out.print(the teacherlibName:); System.out.println(this.libName); /end print()/end class LibTeacherclass AdminTeacher extends Teacher private String managePos; public AdminTeacher(String name,boolean sex,Date birth, String salaryid,String depart, String posit,String managePos) = name; this.sex = sex; this.birth = birth; this.salaryID = salaryid; this.depart = depart; this.posit = posit; this.managePos=managePos; /end public ResearchTeacher() String getManagePos() return managePos; void setManagePos(String managePos) this.managePos=managePos; public void print() System.out.print(adminteacher info is:); System.out.print(the teachername:); System.out.println(this.getName(); System.out.print(the teachersex:); if (this.getSex() = false) System.out.println(女); else System.out.println(男); System.out.print(the teacherbirth:); System.out.println(this.getBirth().getYear() + - + this.getBirth().getMonth() + - + this.getBirth().getDay(); System.out.print(the teachersalaryid:); System.out.println(this.getSalaryID(); System.out.print(the teacherposit:); System.out.println(this.getPosit(); System.out.print(the teacherdepart:); System.out.println(this.getDepart(); System.out.print(the teachermanagePos:); System.out.println(this.managePos); /end print()/end class AdminTeacher习题2.11public class Course private String courseID; private String courseName; private String courseType; private int classHour; private float credit; public Course(String courseID, String courseName, String courseType, int classHour, float credit) this.courseID=courseID; this.courseName=courseName; this.courseType=courseType; this.classHour=classHour; this.credit=credit; /end public Course() String getID() return courseID; void setID(String id) this.courseID = id; String getName() return courseName; void setName(String name) this.courseName = name; String getType() return courseType; void setType(String type) this.courseType = type; int getClassHour() return classHour; void setClassHour(int hour) this.classHour = hour; float getCredit() return classHour; void setCredit(float credit) this.credit= credit; public void print() System.out.println(the basic info of this course as followed:); System.out.println(courseID=+this.getID(); System.out.println(courseName=+this.getName(); System.out.println(courseType=+this.getType(); System.out.println(classHour=+this.getClassHour(); System.out.println(credit=+this.getCredit(); public static void main(String args) Course cs=new Course(d12,java程序设计(第二版),cs,64,3.0f); System.out.println(-); cs.print(); System.out.println(修改课程学分为4.0f); cs.setCredit(4); cs.print(); /习题2.12public class MyGraphic String lineColor; String fillColor; MyGraphic(String lc,String fc) this.lineColor=lc; this.fillColor=fc; void print() System.out.println(line color is +this.lineColor+ t fill color is +this.fillColor); public static void main(String args) float rd=(float)4.5; MyCircle mc=new MyCircle(rd,black,white); MyRectangle mr=new MyRectangle(4,6,red,blue); System.out.println(Circle info ); mc.print(); System.out.println(circumference is + mc.calCircum(); System.out.println(square is + mc.calSquare(); System.out.println(rectangle info: ); mr.print(); System.out.println(circumference is + mr.calCircum(); System.out.println(square is + mr.calSquare(); /end main()/end public class MyGraphicclass MyRectangle extends MyGraphic float rLong; float rWidth; MyRectangle (float rl,float rw,String lc,String fc) super(lc,fc); this.rLong=rl; this.rWidth=rw; /end MyRectangle () float calCircum() return (float)(this.rLong+this.rWidth)*2); float calSquare() return (float)(this.rLong*this.rWidth); /end class MyRectangleclass MyCircle extends MyGraphic float radius; MyCircle (float rd,String lc,String fc) super(lc,fc); this.radius=rd; /end MyRectangle () float calCircum() return (float)(this.radius*3.12*2); float calSquare() return (float)(this.radius*this.radius*3.14); /end class MyCircle/习题2.13public class Vehicle String brand; String color; int price; int number; public Vehicle(String b, String c) this.brand = b; this.color = c; public Vehicle(String b, String c, int p, int n) this(b, c); this.price = p; this.number = n; void print() System.out.println(n-); System.out.println(the vehicle info as followed :); System.out.println(brand= + this.brand + t); System.out.println(color= + this.color + t); System.out.println(price= + this.price + t); System.out.println(number= + this.number + t); /end void print() public static void main(String args) Vehicle c1=new Vehicle(vehicle1,white); Vehicle c2=new Vehicle(vehicle2,white,300,1); Car cr=new Car(car1,red,300,4,400); Truck tk2=new Truck(truck1,black,300,400); c1.print(); c2.print(); cr.print(); tk2.print(); /end main() /end public class Vehicleclass Car extends Vehicle int speed; Car(String b, String c, int p, int n,int s) super(b,c,p,n); this.speed=s; void print() super.print(); System.out.print

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论