[简答题][说明]
以下JAVA程序实现了在接口interface iShape2D的定义和应用,仔细阅读代码和相关注释,将程序补充完整。
[代码6-1]
interface iShape2D //定义接口
{
(1)
(2)
}
(3) //实现CRectangle类
{
int width, height;
(4) CRectangle (int w,int h) {
width=w;
height=h;
}
public void area ( ){ //定义area( )的处理方式
System. out.println ("area="+width*height);
}
}
(5) //实现CCircle类
{
double radius;
(6) CCircle (double r) {
radius=r;
}
public void area ( ) { //定义area( )的处理方式
System.out.println ("area="+pi*radius*radius);
}
}
[代码6-2]
public class app10_4
{
public static void main(String args[])
{
CRectangle rect=new CRectangle (5,10);
rect.area ( ); //调用CRectangle类里的area ( ) method
CCircle cir=new CCircle (2.0);
cir.area ( ); //调用CCircl类里的area ( ) method
}
}
[简答题]【说明】
Stack类是java. ntil包中专门用来实现栈的工具类。以下Java程序是一个不使用库函数而实现字符串反转的程序。例如,输入:123456,则输出:654321:输入:asdfeg,则输出:gefdsa。
【Java程序】
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class UseStack extends Applet implements ActionListener
{ Stack MyStack;
Label prompt=new Label("输入要反转字符:");
Button pushBtn=new Button("压栈");
Button popBtn=new Button("弹栈"); //反转
TextField input=new TextField(5);
int[] DrawStack =new int[10]; //记录堆栈中数据
int[] PoppedOut=new int[20]; //记录被弹出的数据
int StackCnt=0; //记录模拟堆栈的数组中的数据个数
String msg=" ";
public void init( )
{ MyStack=new Stack( );
add(prompt);
add(input);
add(pushBtn);
add(popBtn);
pushBtn.addActionListener(this);
(1)
}
public void paint(Graphics g)
{ for (int i=10; i<StackCnt; i++) //模拟显示堆栈内部的数据排列情况
{ g.drawRect(50,200-i*20,80,20);
g.drawString(Integer.toString(DrawStack[i]),80,215-i*20);
}
for
[填空题]阅读以下说明和Java程序,将应填入 (n) 处的字句写在对应栏内
[说明]
以下程序的功能时三角形、矩形和正方形的面积输出。
程序由5个类组成:areatest是主类,类Triangle,Rectangle和Square分别表示三角形、矩形和正方形,抽象类Figure提供了一个计算面积的抽象方法。
[Java程序]
public class areatest {
public static viod main(string args[]){
Figure[]Figures={
New triangle(2,3,3),new rectangle(5,8),new square(5)
};
for(int i=0; i<Figures.length;i++){
system.out.println(Figures+"area="+Figures.getarea( ));
}
}
}
public abstract class figure {
public abstract double getarea( );
}
public class rectangle extends (1) {
double height;
double width;
public rectangle (double height,double width){
this.height=height;
this.width=width;
}
public string tostring( ){
return"rectangle:height="+height+",width="+width+":";
}
public double getarea( ){
return (2)
}
}
public class square exends (3)
{
public square(double width
[简答题]
阅读以下说明和C++程序,将应填入(n)处的字句写在对应栏内。
【说明】以下程序实现了二叉树的结点删除算法,若树中存在要删除的结点,则删除它,否则返回。 FindNode
( )函数能够在二叉树中找到给定值的结点,并返回其地址和父结点。
【C++程序】
template < class T >
void
BinSTree < T >: :Delete( const T& item)
{
TreeNode < T >
* DelNodePtr, * ParNodePtr, * RepNodePtr;
if(( DelNodePtr
= FindNode (item,ParNodePtr)) = = NULL)
(1)
if(DelNodePtr→right = = NULL)
//被删除结点只有一个子结点的情况
RepNodePtr = DelNodePtr→left;
else if( DelNodePtr→left = = NULL)
(2) ;
else
//
被删除结点有两个子结点的情况
{
TreeNode < T >*
PofRNodePtr = DelNodePtr;
RepNodePtr =
DelNodePtr→left;
while(RepNodePtr→right ! =
NULL)
{
//定位左子树的最右结点
PofRNodePtr =RepNodePtr;
RepNodePtr = RepNodePtr→right;
}
if(PofRNodePtr = = DelNodePtr) //左子树没有右子结点
(3) ;
else
//用左子顷的最右结点替换删除的结点
{
(4)
[填空题][说明]
下面程序实现十进制向其他进制的转换。
[Java程序]
C1ass Node
int data;
Node next;
class Transform
private Node top;
publiC void print( )
Node P;
while(top !=null)
P=top;
if(P.data>9)
System.out.print((char)(p.data+55));
else
System.out.print(p.data);
top=P.next;
public void Trans(int d,int i)(//d为数字;i为进制
int m;
(1) n=false;
Node P;
while(d>0)
(2) ;
d=d/i;
P=flew Node( );
if( (3) )
P.data=m;
(4) ;
top=P;
n=true;
else
p.data=m;
(5) ;
toP=P;
[简答题][说明]
以下程序实现了在applet里移动图形文件,仔细阅读代码和相关注释,将程序补充完整。
[代码6-1]
import j ava. awt. *;
import j ava.awt.event.*;
import java.applet. Applet;
public class AppCIU extends Applet implements MouseMotionListener, MouseListener
{
Image IMG onClick=over(this) title=放大; // 声明 Image 类类型的变量 IMG onClick=over(this) title=放大
int x=70,y=60,posX=70,posY=60,dx,dy;
public void init ( )
{
IMG onClick=over(this) title=放大=getImage ( getCodeBase ( ) ,"mouse.gif" ); //载入影像
addMouseListener ( this );
addMouseMotionListener ( this );
}
public void mousePressed ( MouseEvent e )
{
dx=e.getX( )-posX; //取得按下之点与基准点X方向的距离
dy=e.getY( )-posY; //取得按下之点与基准点Y方向的距离
}
public void mouseDragged ( MouseEvent e )
{
(1)
(2)
if ( dx>0 && dx<120 && dy>0 && dy<60 ) //如果指针落在图形上方
{
Graphics g=getGraphics ( );
(3)
}
}
public void paint ( Graphics g )
{
(4)
(5
[多项选择][说明]
以下程序实现了在applet里移动图形文件,仔细阅读代码和相关注释,将程序补充完整。
[代码6-1]
import j ava. awt. *;
import j ava.awt.event.*;
import java.applet. Applet;
public class AppCIU extends Applet implements MouseMotionListener, MouseListener
Image img; // 声明 Image 类类型的变量 img
int x=70,y=60,posX=70,posY=60,dx,dy;
public void init ( )
img=getImage ( getCodeBase ( ) ,"mouse.gif" ); //载入影像
addMouseListener ( this );
addMouseMotionListener ( this );
public void mousePressed ( MouseEvent e )
dx=e.getX( )-posX; //取得按下之点与基准点X方向的距离
dy=e.getY( )-posY; //取得按下之点与基准点Y方向的距离
public void mouseDragged ( MouseEvent e )
(1)
(2)
if ( dx>0 && dx<120 && dy>0 && dy<60 ) //如果指针落在图形上方
Graphics g=getGraphics ( );
(3)
public void paint ( Graphics g )
(4)
[填空题]【说明】
以下程序实现了利用鼠标任意移动该圆形的位置,仔细阅读代码和相关注释,将程序补充完整。
【代码6】
import java.awt.*;
import java.awt.event.*;
public class CIUSAMPLE extends Frame implements MouseMotionListener, MouseListener
{
static CIUSAMPLE frm=new CIUSAMPLE ( );
int x=70,y=60,posX=70,posY=60,dx,dy;
public static void main ( String args[])
{
frm.setTitle ("Dragging a circle");
frm.setSize ( 200,150 );
(1)
frm.addMouseMotionListener ( frm );
frm. setVisible ( true );
}
public void mousePressed ( MouseEvent e )
{
(2)
dy=e.getY ( ) -posY;
}
public void mouseDragged ( MouseEvent e )
{
(3)
y=e.getY ( ) -dy;
if ( dx>0 && dx<50 && dy>0 && dy<50 ) //如果指我落在正方形区域内
{
Graphics g=getGraphics ( );
(4)
}
}
public void paint ( Graphics g )
{
g.setColor ( Color. pink ); // 设置绘图颜色为粉红
g.fillOval ( x,y,50,50 ); //以基准点为图形的左上角绘出圆形
(5)
pos
[简答题][说明]
以下程序实现了利用鼠标任意移动圆形的位置,仔细阅读代码和相关注释,将程序补充完整。
[代码6-1]
import java.awt.*;
import java.awt.event.*;
public class CIUSAMPLE extends Frame implements MouseMotionListener, MouseListener
static CIUSAMPLE frm=new CIUSAMPLE ( );
int x=70,y=60,posX=70,posY=60,dx,dy;
public static void main (String args[])
frm.setTitle ("Dragging a circle");
frm.setSize (200,150);
(1)
ffm.addMouseMotionListener (frm);
frm.setVisible (true);
public void mousePressed (MouseEvent e)
(2)
dy=e.getY ( ) -posY;
public void mouseDragged (MouseEvent e)
(3)
y=e.getY ( ) -dy;
if(dx>0&&dx<50&&dy>0&&dy<50) //如果指针落在正方形区域内
Graphicsg=getGraphics ( );
(4)
public void paint (Graphics g)
g.setColor (Color.pink); //设置绘图颜色为粉红
g.fillOval(x,y,50
[简答题]【说明】
以下程序实现数据的排序,将n个整数分别按照升序和降序进行排序,类SortInt_1实现升序排序,类SortInt_2实现降序排序。
【Java代码】
class SortInt_1{
int i,i,k,temp;
void SortInt(int a1,int a2[]){//升序排序
for(i=0;i<a1-1;i++){
k=i;
for(j=i+1;j<a1;j++){
if( (1) ) k=j;
if(k !=i){
temp=a2[i];a2[i]=a2[k];a2[k]=temp;
}
}
}
}
}
class SortInt_2 (2) {
int i,j,k,temp;
void SortInt(int a1, int a2[]){//降序排序
for(i=0; i<a1-1;i++){
k=i;
for(j=i+1;j<a1;j++){
if( (3) )k=j;
}
if(k !=i){
temp=a2[i];a2[i]=a2[k];a2[k]=temp;
}
}
}
}
public class test{
public static void main(String args[]){
int a[]={10,55,100,35,87,90,100,16};
SortInt_1 NewInt= (4) ;
NewInt.SortInt(a.lenvh,a);//调用SortInt_1类的方法
System.out.println("升序排列的数据: ");
for(int i=0;i<a.length;i++){
System.out.print(a[i]+
[多项选择]【说明】
以下程序实现数据的排序,将n个整数分别按照升序和降序进行排序,类SortInt_1实现升序排序,类SortInt_2实现降序排序。
【Java代码】
class SortInt_1
int i,i,k,temp;
void SortInt(int a1,int a2[])//升序排序
for(i=0;i<a1-1;i++)
k=i;
for(j=i+1;j<a1;j++)
if( (1) ) k=j;
if(k !=i)
temp=a2[i];a2[i]=a2[k];a2[k]=temp;
class SortInt_2 (2)
int i,j,k,temp;
void SortInt(int a1, int a2[])//降序排序
for(i=0; i<a1-1;i++)
k=i;
for(j=i+1;j<a1;j++)
if( (3) )k=j;
if(k !=i)
temp=a2[i];a2[i]=a2[k];a2[k]=temp;
public class test
public static void main(String args[])
int a[]=10,55,100,35,87,90,100,16;
SortInt_1 NewInt= (4) ;
NewInt.SortInt(a.lenvh,a);//调用SortInt_1类的方法
System.out.prin