java程序设计(第二版)课后习题答案

3.0 文小白 2023-09-15 130 0 1.62MB 284 页 10文币
侵权投诉
//习题 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("名字:"+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 Employee
public class MyTestClass {
public static void main(String[] args) {
Employee[]staff=new Employee[3];
staff[0]=new Employee("Harry Hacker",35000,new MyDate(1989,10,1));
staff[1]=new Employee("Carl Carcker",75000,new MyDate(1987,12,15));
staff[2]=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++){
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("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.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("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.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;
摘要:

//习题2.2importjava.util.*;classMyDate{privateintyear;privateintmonth;privateintday;publicMyDate(inty,intm,intd){//构造函数,构造方法year=y;month=m;day=d;}//endpublicMyDate(inty,intm,intd)publicintgetYear(){//返回年returnyear;}//endgetYear()publicintgetMonth(){//返回月returnmonth;}//endgetMonth()publicintgetDay(){//...

展开>> 收起<<
java程序设计(第二版)课后习题答案.doc

共284页,预览10页

还剩页未读, 继续阅读

作者:文小白 分类:教育专区 价格:10文币 属性:284 页 大小:1.62MB 格式:doc 时间:2023-09-15

开通VIP享超值会员特权

  • 多端同步记录
  • 高速下载文档
  • 免费文档工具
  • 分享文档赚钱
  • 每日登录抽奖
  • 优质衍生服务
/ 284
客服
关注