题目
●试题六
阅读以下说明和Java代码,将解答写入答题纸的对应栏内。
【说明】
下面是一个Applet程序,其功能是输出已定义好的两个变量x和chr。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。
注意:不改动程序的结构,不得增行或删行。
import javA.awt.*;
(1) import javA.Applet;
(2) public class MyApplet implements Applet
{
int x=10;
(3) char chr="R";
Label output1;
Label output2;
(4) private void init()
{
output1=new Label("定义int类型变量"+"x,的初值为"+x);
output2=new Label("定义char类型变量"+"chr,的初值为"+chr);
add(output1);
add(output2);
}
}
<HTML>
<HEAD>
<TITLE>ex34_3</TITLE>
</HEAD>
<BODY>
(5) <applet class="MyApplet.class"
width=400 height=400 >
</applet>
</BODY>
</HTML>
第1题
●试题六
阅读以下说明和Java代码,将解答写入答题纸的对应栏内。【说明】
下面程序的功能是显示已定义的一个3行3列的二维数组每行的元素,并求所有元素的和并输出。请在程序的每条横线处填写一个适当的语句,使程序的功能完整。
public class Array{
(1) static (2) main(String args[])
{
int sum=0;
int b[][]={{11,12,13},{21,22,23},{31,32,33}};
for(int i=0; (3) i++)
{
System.out.print("b["+i+"]: ");
for(int j=0; (4) j++)
{
System.out.print(b[i][j]+" ");
(5)
}
System.out.println();
}
System.out.println("sum="+sum);
}
}
第2题
●试题六
阅读以下说明和Java代码,将解答写入答题纸的对应栏内。
【说明】
请完成下列Java程序。程序的执行结果是生成一个具有一个TextField类型的对象in、Button类型的对象btn和Label类型的对象out图形用户界面,程序的功能是计算用户输入数的平方,如图3所示。
注意:请勿改动main()主方法和其他已有的语句内容,仅在下划线处填入适当的语句。
【程序】
import javA.awt.*;
import javA.awt.event.*;
public class square {
public static void main(String args[]){
(1)
}
}
class AppFrame. extends Frame{
TheAdapterTest listener=new TheAdapterTest();
TextField in=new TextField (5) ;
Button btn=new Button("计算");
Label ut=new Label("用于显示计算结果");
public AppFrame()
{
setLayout(new FlowLayout());
add(in);
add(btn);
add(out);
btn.addActionListener(new BtnActionAdapter());
addWindowListener(listener);
setSize(400,100);
show();
}
class BtnActionAdapter implements (2) {
public void actionPerformed((3) ){
String s=in.getText();
double d= (4)
double sq=d*d;
out.setText(d+"的平方是:"+sq);
}
}
class TheAdapterTest extends WindowAdapter
{
public void windowClosing((5) )
{
System.exit (1) ;
}
}
}
第3题
试题六(共15分)
阅读以下说明、图和Java代码,填补Java代码中的空缺(1)~(6),将解答写在答题纸的
对应栏内。
【说明】
已知对某几何图形绘制工具进行类建模的结果如图6.1所示,其中Shape为抽象(abstract)类,表示通用图形,Box(矩形)、Ellipse(椭圆)和Line(线条)继承(extends)了Shape类,其中,Circle表示圆(即特殊的椭圆)。
下面的Java代码用于实现图 6-1所给出的设计思路,将其空缺处填充完整并编译运行,输出结果为:
Ellipse
Circle
Ellipse
C
E
【Java代码】
(1) class Shape{
public Shape(String name){
this.name= name;
}
(2) void paint();
String getName(){
retum this.name;
}
final String name;
};
//Box 和Line类似下面 Ellipse,其代码略
class Ellipse (3) {
public Ellipse(String name){
super(name);
System.out.println("Ellipse");
}
Void paintO{∥绘制现状示意代码
System.out.println(getName0);
}
};
class Circle (4) {
public Circle(String name){
super(name);
System.out.println("Circle");
}
};
class Diagram{
private Shape shapes[]= new Shape[2];
public void drawAShape(Shape shape){
shape.paint();
}
void erase A Shape(Shape shape){
∥删除形状,代码略
}
void drawShapes(){
shapes*0+= new Circle("C”);
shapes[l]= new Ellipse("E");
for (int i=O; i<2;++i) {
drawAShap(shapes[i]);//绘制形状
}
}
void close(){
for (int i=0;i<2; ++1) { []关闭图,删除所绘制图形
(5) ;
}
}
public static void main(String[] args){
Diagram diagram= (6) ;
diagram.drawShapes();
diagram.close();
}
}
第4题
试题六(共 15分)
阅读以下说明和Java代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
【说明】
已知类 LinkedList 表示列表类,该类具有四个方法:addElement()、lastElement()、umberOfElement()以及removeLastElement()。四个方法的含义分别为:
void addElement(Object): 在列表尾部添加一个对象;
Object lastElement(): 返回列表尾部对象;
int numberOfElement(): 返回列表中对象个数;
void removeLastElement(): 删除列表尾部的对象。
现需要借助LinkedList来实现一个Stack栈类, Java代码1和Java代码2分别采用继承和组合的方式实现。
【Java代码1】
public class Stack extends LinkedList{
public void push(Object o){ //压栈
addElement(o);
}
public Object peek(){ //获取栈顶元素
return (1) ;
}
public boolean isEmpty(){ //判断栈是否为空
return numberOfElement() == 0;
}
public Object pop(){ //弹栈
Object o = lastElement();
(2) ;
return o;
}
}
【Java代码2】
public class Stack {
private (3) ;
public Stack(){
list = new LinkedList();
}
public void push(Object o){
list.addElement(o);
}
public Object peek(){//获取栈顶元素
return list. (4) ;
}
public boolean isEmpty(){//判断栈是否为空
return list.numberOfElement() == 0;
}
public Object pop(){ //弹栈
Object o = list.lastElement();
list.removeLastElement();
return o;
}
}
【问题】
若类LinkedList新增加了一个公有的方法removeElement(int index),用于删除列表中第index个元素,则在用继承和组合两种实现栈类Stack的方式中,哪种方式下Stack对象可访问方法removeElement(int index)? (5) (A. 继承 B. 组合)
第5题
试题六(共15分)
阅读下列说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
现欲开发一个软件系统,要求能够同时支持多种不同的数据库,为此采用抽象工厂模式设计该系统。以SQL Server和Access两种数据库以及系统中的数据库表Department为例,其类图如图6-1所示。
【Java代码】
import java.util.*;
class Department{/*代码省略*/}
interface IDepartment{
(1) ;
(2) ;
}
class SqlserverDepartment (3) {
public voidInsert(Department department){
System.out.println(”Insert a record into Department in SQL Server!");
∥其余代码省略
}
public Department GetDepartment(int id){
/*代码省略*/
}
}
classAccessDepartment(4) {
public void Insert(Department department){
System.out.println("Insert a record into Department in ACCESS!”);
∥其余代码省略
}
public Department GetDepartment(int id){
/*代码省略*/
}
}
(5) {
(6) ;
}
class SqlServerFactory implements IFactory{
public IDepartment CreateDepartment(){
retum new SqlserverDepartment();
}
∥其余代码省略
}
class AccessFactory implements IFactory{
public IDepartment CreateDepartment(){
return new AccessDepartment();
}
∥其余代码省略
}
第6题
试题六(共15分)
阅读下列说明和Java代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
【说明】
某发票(lnvoice)由抬头(Head)部分、正文部分和脚注(Foot)部分构成。现采用装饰(Decorator)模式实现打印发票的功能,得到如图6-1所示的类图。
【java代码】
class invoice{
public void printInvoice(){:
System.out.println ("This is the content of the invoice!");
}
}
class Decorator : extends Invoice {
protected Invoice ticket;
public Decorator(lnvoice t){
ticket = t;
}
public void printinvoice(){
if(ticket != NULL)
(1);
}
}
class FootDecorator extends Decorator{
public FootDecorator(lnvoice t){
super(t);
}
public void printinvoice (){
Systent.out.println("This is the header of the invoice! ");
(2) ;
}
}
class FootDecorator extends Decorator {
public FootDecorator(invoice t):{
super(t);
}
public void printlnvoice(){
(3) ;
Systent.out.println("This is the header of the invoice! ");
}
}
Class test {
public static void main(string[] args){
Invoice t =new invioce();
Invoice ticket;
Ticket= (4) ;
Ticket. Printinvoice();
Systent.out.println(“--------------“)
Ticket= (5) ;
Ticket. Printinvoice();
}
}
程序的输出结果为:
This is the header of the invoice!
This is the content of the invoice!
This is the footnote of the invoice!
----------------------------
This is the header of the invoice!
This is the footnote of the invoice!
第7题
试题六(共15分)
阅读以下说明和 Java 代码,将应填入 (n) 处的语句或语句成分写在答题纸的对
应栏内。
【说明】
某数据文件 students.txt的内容为100名学生的学号和成绩,下面的程序将文件中的
数据全部读入对象数组,按分数从高到低进行排序后选出排名前 30%的学生。
【Java代码】
import java.io.*;
class Student {
private String sNO; //学号
private int Credit; //分数
public int getCredit(){
return Credit;
}
public String toString() {
return "sNO = " + this.sNO + ", Credit = " + this.Credit;
}
Student(String sNO, int Credit){
(1) = sNO;
(2) = Credit;
}
}
public class SortStudent {
void sort(Student[] s) { //Sort the array s[] in decending order of Credit
for (int i = 0; i < s.length-1; i++) {
for (int j = i+1; j < s.length; j++) {
if (s[i]. (3) < s[j]. (4) ) {
Student tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
}
}
}
public static void main(String argv[]) {
Student[] testStudent = new Student[size];
try {
BufferedReader in = new BufferedReader(new FileReader("students.txt"));
boolean done = false;
int i = 0;
while (!done) {
String s = in.readLine(); //每次读取一个学生的学号和成绩
if (s != null) {
String tmp[] = s.split(",");
testStudent[i++] = (5) (tmp[0], Integer.parseInt(tmp[1]));
} else
done = true;
}
in.close();
(6) = new SortStudent();
ss.sort(testStudent);
System.out.println("top 30%:");
for (int j = 0; j < size * 0.3; j++)
System.out.println(testStudent[j]);
} catch (IOException e) {
System.out.println("io error!");
}catch (NumberFormatException e) {
System.out.println("not a number!");
}
}
(7) int size = 100; //学生总数
}
第8题
试题六(共15分)
阅读以下说明和Java代码,填补Java代码中的空缺(1)~(5),将解答写在答题纸的对应栏内。
【说明】
已知某公司主要有两大类耗电资产(Asset):计算机(ComputerAsset)和建筑物(Building Asset)。为了节约能源,通过控制各种电源,将可关闭的房灯、计算机显示器等在夜间关闭。
为了实现上述需求,设计了如图6-1所示的类图,并用下面的Java代码加以实现。
【Java代码】
abstract class Asset{ /*通用资产,基类*/}
interface PowerSwitchable{ /*可在夜间关闭电源的物体实现该接口*/
public void powerDown();
public void powerUp();
}
abstract class BuildingAsset extends Asset{/*建筑物资产*/
protected int room;
public BuildingAsset(int room){ this.room= room; }
}
abstract class BuildingLight extends BuildingAsset{
//灯的通用信息:flourescent/incandescent等,略
BuildingLight(int roomNumber){ super(roomNumber);}
}
classEmergencyLight (1) {/*应急灯,永不关闭*/
EmergencyLight(int roomNumber){
super(roomNumber);
}
}
class RoomLights (2) {
RoomLights(int roomNumber){ super(roomNumber); }
public void powerDown(){ /*关电源,代码略*/}
public void powerUp(){/*开电源,代码略*/}
}
/*ComputerAsset、 Computer CPU和Computer Monitor代码略*/
public class BuildingManagement{
Asset things[]= new Asset[24];
int numltems=0;
public void goodNight(){/*值班员定时“关闭”时调用,关闭可关闭的电源*/
for (int i=0; i<things.length; i++)
if(things[i] instanceof (3) )
((PowerSwitchable)things[i]).powerDown();
}
/*goodMorning()与goodNight()类似,依次调用powerUp(),其实现细节此处略*/
public void add(Asset thing){ /*为建筑添加资产*/
things[ (4) ]=thing;
}
public static void main(String[] args){
BuildingManagementbl= (5) BuildingManagement();
bl.add(new RoomLights(101)); //101房间的控制灯
bl.add(new EmergencyLight(101)); //101房间的应急灯
bl.add(new ComputerCPU(10104));//101房间4号桌上的计算机主机
bl.add(new ComputerMonitor(10104)); // 101房间4号桌上的计算机显示器
bl.goodNight();
}
}
第9题
试题六(共15分)
阅读以下说明和Java代码,填补Java代码中的空缺(1)~(6),将解答写在答题纸的对应栏内。
【说明】
己知某公司按周给员工发放工资,其工资系统需记录每名员工的员工号、姓名、工资等信息。其中一些员工是正式的,按年薪分周发放(每年按52周计算);另一些员工是计时工,以小时工资为基准,按每周工作小时数核算发放。
下面是实现该工资系统的Java代码,其中定义了四个类:工资系统类PayRoll,员工类Employee,正式工类Salaried和计时工类Hourly,Salaried和Hourly是Employee的子类。
【Java代码】
abstract class Employee{
protected String name; //员工姓名
protected int empCode; //员工号
protected double salary; //周发放工资
public Employee(int empCode, String name){
this.empCode= empCode;
this.name= name;
}
public double getSalary(){
return this.salary;
}
public abstract void pay();
}
class Salaried (1) Employee{
private double annualSalary;
Salaried(int empCode, String name, double payRate){
super(empCode, name);
this.annualSalary= payRate;
}
public void pay(){
salary= (2) ;//计算正式员工的周发放工资数
System.out.println(this.name+":"+this.salary);
}
}
class Hourly (3) Employee{
private double hourlyPayRate;
private int hours;
Hourly(int empCode, String name, int hours, double payRate){
super(empCode, name);
this.hourlyPayRate= payRate;
this.hows= hours,
}
public void pay(){
salary= (4) ;//计算计时工的周发放工资数
System.out.println(this.name+":"+this.salary);
}
}
public class PayRoll{
private (5) employees[]={
new Salaried(l001,"Zhang San", 58000.00),
//此处省略对其他职工对象的生成
new Hourly(1005,"Li", 12, 50.00)
};
public void pay(Employee e[]){
for (int i=0;i<e.length; i++){
e[i].pay();
}
}
public static void main(String[] args)
{
PayRoll payRoll= new PayRoll();
payRoll.pay((6) );
double total= 0.0;
for (int i=0;i<payRoll.employees.length; i++){//统计周发放工资总额
total+=payRoll.employees[i].getSalary();
}
System.out.println(total);
}
}
第10题
●试题七
阅读以下说明和Java代码,将解答写入答题纸的对应栏内。
【说明】
下面程序的功能是找出所有三位数中,个、十、百位数字的立方和等于该数本身的三位数并显示。在程序的每条横线处填写一个适当的词或语句,使程序的功能完整。
public (1) class cube_Root{
public static void (2) main(String args[]){
System.out.println("个、十、百位数字的立方和等于该数本身的三位数有:");
for(int n=100;n<1000;n++)
{
int a,b,c;
(3)
b=n/10%10;
(4)
if((5) )
System.out.println(n);
}
}
}
为了保护您的账号安全,请在“赏学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!