//习题2.2 import 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 MyDate class 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(\名字:\工资:\雇佣年份:\ }//end print()
public void raiseSalary(double byPercent){ salary*=1+byPercent/100; }//end
public int hireYear(){ return hireDay.getYear(); }
}//end class Employee public class MyTestClass {
public static void main(String[] args) { Employee[]staff=new Employee[3];
staff[0]=new Employee(\
staff[1]=new Employee(\ staff[2]=new Employee(\ester\ int integerValue;
System.out.println(\ for(integerValue=0;integerValue<=2;integerValue++){ staff[integerValue].raiseSalary(5); }//end for()
for(integerValue=0;integerValue<=2;integerValue++){ staff[integerValue].print(); }//end for() }//end main()
}//end class MyTestClass //习题2.4 import 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(\
System.out.println(\布尔类型变量flag=\ System.out.println(\字符型变量yesChar=\ System.out.println(\字节型变量finByte=\ System.out.println(\整型变量intValue=\ System.out.println(\长整型变量longValue=\ System.out.println(\短整型变量shortValue=\ System.out.println(\浮点型变量floatValue=\
System.out.println(\双精度浮点型变量doubleValue=\
}//end main() }
//习题2.9 import 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(\ }
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.10 import 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 Date
public class Teacher { String name;//教师名字
boolean sex;//性别,true表示男性 Date birth;//出生日期 String salaryID;//工资号 String depart;//教师所在系所 String posit;//教师职称 String getName() { return name; }
void setName(String name) { this.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){ this.name=name; this.sex=sex; this.birth=birth; this.salaryID=salaryid; this.depart=depart; this.posit=posit; }//end Teacher() public void print(){
System.out.print(\ System.out.println(this.getName()); System.out.print(\ if(this.getSex()==false){ System.out.println(\女\ } else{
System.out.println(\男\ }
System.out.print(\
System.out.println(this.getBirth().getYear()+\ this.getBirth().getMonth()+\ this.getBirth().getDay()); System.out.print(\ System.out.println(this.getSalaryID()); System.out.print(\ System.out.println(this.getPosit()); System.out.print(\ 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);