1.java的jvm虛擬機(jī)可自動(dòng)處理__________.
A) 拋出異常 B)捕獲異常 C)運(yùn)行異常 D)聲明異常
2.try與catch的匹配是___________.
A)catch中對(duì)象名指向try中可能拋出的異常類或父類
B)catch中對(duì)象名指向try中可能拋出的異常類的子類
C)catch中對(duì)象名指向try中可能拋出的異常的不同類
D)catch中對(duì)象名指向try中可能拋出的異常的不同類的父類
3.catch(Exception e){語(yǔ)句的正確排列次序是__________.
A)最前 B)最后 C)中間 D)任意位置
4.一個(gè)try語(yǔ)句后必須在catch和__________.
A)break語(yǔ)句 B)continue語(yǔ)句 C)return 語(yǔ)句 D)finally語(yǔ)句
5.為獲得異常堆棧使用軌跡,應(yīng)該利用_____________.
A)fillinStack()方法 B)printStackTrace()方法
C)fillinStackTrace()方法 D)toString()方法
6.請(qǐng)利用Java類為中java.util.GregorianCalendar這個(gè)已有的類,編出一個(gè)可根據(jù)命令行參數(shù)打印月歷的Java程序.(使用已有類)
7.請(qǐng)根據(jù)教程74頁(yè)描述的Employee和Manager類,并(繼承)補(bǔ)充條件:
姓名 年薪 加入公司時(shí)間 經(jīng)理年津貼
Manager: CarlCraker $80 000 1987.12.15 $5 000
Employee: Harry Hacker $50 000 1989.10.1
Employee: Tommy Tester $40 000 1990.3.15
編寫(xiě)一個(gè)完整的JAVA程序,打印出按照5%增加年薪后的工資表。
8.改寫(xiě)上一道題的程序,在打印工資表之前按照年薪自低到高排序后打印出來(lái)。(接口、對(duì)象數(shù)組排序)
9.請(qǐng)編寫(xiě)一個(gè)完整的JAVA程序,依次打印出命令行參數(shù)字符串?dāng)?shù)組各元素。(字符串?dāng)?shù)組)
答案
1,c 2,a 3.b 4,d 5.b
6.本程序可利用的方法如下:
GregorianCalendar.get(Calendar.DAY_OF_MONTH)
.get(Calendar.MONTH)
.get(Calendar.DAY_OF_WEEK)
.get(Calendar.DAY_OF_MONTH ,1)
可利用計(jì)數(shù)循環(huán)、條件循環(huán)打印月歷。
7.程序代碼:
Import java.util.*;
Public class ManagerTest1
{
Public static void main(String[] args)
{
Manager boss=new Manager(“Carl Cracker”,80000,1987,12,15);
Boss.setBonus(5000);
Employee[] staff=new Employee[3];
staff[0]=boss;
staff[1]=new Employee(“Harry Hacker”,50000,1989,10,1);
staff[2]=new Employee(“Tommy Tester”,40000,1990,3,15);
for(int i=0; i<=staff.length; i++)
{ Employee e=staff[i];
e.raiseSalery(5); System.out.println(“name”=+e.getName()+”,salary=”+e.getSalary());
}
}
}
class Employee
{
Public Employee(String n, double s, int year, int month, int dat)
{
name=n;
salary=s;
GregorianCalendar calendar=new GregorianCaledar(year, month-1, day);
hireDay=calendar.getTime();
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public Date get HireDay()
{
Return hireDay;
}
public void raiseSalary(double byPercent)
{
double raise=salary*byPercent/100;
salary+=raise;
}
private String name;
private double salary;
private Date hireDay;
}
class Manager estends Employee
{
public Manager(String n, double s, int year, int month, int day)
{
super(n, s, year, month, day);
bonus=0 ;
}
public double getSalary()
{
double baseSalary=super.getSalary();
return baseSalary+hours;
}
public void setBonus(double b)
{
bonus=b;
}
private double bonus;
}
程序運(yùn)行結(jié)果為:
D:\code>java ManagerTest1
name=Car1 Cracker,salary=89000.0
name=Harry Hacker,salary=52500.0
name=Tommy Tester,salary=42000.0
[NextPage]
8.定義一個(gè)含有主方法的類,定義Manager對(duì)象,定義Employee對(duì)象數(shù)組。為對(duì)象數(shù)組賦值。用計(jì)數(shù)循環(huán)加薪并打印姓名和年薪。
程序代碼:
import java.util.*;
public class ManagerTest2
{ public static void main(String) args)
{ Manager boss=new Manager(“Carl Cracker”, 80000, 1987,12,15);
boss.setBonus(5000);
Employee[] staff=new Employee[3];
staff[0]=boss;
staff[1]=new Employee(“Harry Hacker”, 50000, 1989,10, 1);
staff[2]=new Employee(“Tommy Tester”, 40000, 1990,3,15);
Array.sort(staff);
For(int i=0; i<=staff.length; i++)
{ Employee e= staff[i];
e.raiseSalary(5);
System.out.println(“name=”+e.getName()+”,salary=”+e.getSalary());
}
}
}
class Employee implements Comparable
{ public Employee(String n, double s, int year, int month, int day)
{ name =n;
salary=s;
GregorianCalendar calendar=new GregorianCalendar(year, month-1, day);
hireDay=calendar.getTime();
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public Date get HireDay()
{
Return hireDay;
}
public void raiseSalary(double byPercent)
{ double raise=salary*byPercent/100;
salary+=raise;
}
public int compareTo(Object otherobject)
{ Employee other=(Enployee)otherObject;
if(salary if(salary>other.salary) return 1; return 0; } private String name; private double salary; private Date hireDay; } class Manager estends Employee { public Manager(String n, double s, int year, int month, int day) { super(n, s, year, month, day); bonus=0 ; } public double getSalary() { double baseSalary=super.getSalary(); return baseSalary+hours; } public void setBonus(double b) { bonus=b; } private double bonus; } 程序運(yùn)行結(jié)果為: D:\code>java ManagerTest2 name= Tommy Tester,salary =42000.0 name=Harry Hacker,salary=52500.0 name= Car1 Cracker,salary =89000.0 排序方法可用 java.util.Arrays.sort(staff)給對(duì)象數(shù)組排序,其中staff是雇員對(duì)象數(shù)組名。 Employee類要實(shí)現(xiàn)java.lang.Comparable接口,要實(shí)現(xiàn)接口內(nèi)的抽象方法comparaTo()方法。 9.主方法的命令行參數(shù)是一個(gè)字符數(shù)組,即:String[] args 程序代碼: public class ConLinePara { public static void main(String[] args) ( if(args.length<1) System.exit(0); else System.out.println(“The conmand line parameters:”); for(int i=0; i { System.out.println(“ “); System.out.println(args[i]); } } } 程序運(yùn)行結(jié)果為: D:\code\java CommLinePara How are you! The command line parameters: How are you! D:\code\
北京 | 天津 | 上海 | 江蘇 | 山東 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
廣東 | 河北 | 湖南 | 廣西 | 河南 |
海南 | 湖北 | 四川 | 重慶 | 云南 |
貴州 | 西藏 | 新疆 | 陜西 | 山西 |
寧夏 | 甘肅 | 青海 | 遼寧 | 吉林 |
黑龍江 | 內(nèi)蒙古 |