The Mycircle class is fully encapsulated.
The diameter of a given MyCircle is guaranteed to be twice its radius.
Lines 6 and 7 should be in a synchronized block to ensure encapsulation.
The radius of a MyCircle object can be set without affecting its diameter.
第1题:
阅读以下说明和JAVA 2代码,填入(n)处的。
[说明]
以下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
}
}
第2题:
下列程序中,先声明一个圆类circle和一个桌子类table,另外声明一个圆桌类roundtable,它是由 circle和table两个类派生的,要求声明一个圆桌类对象,并输出圆桌的高度,面积和颜色。请填空完成程序
include<iostream.h>
include<string.h>
class circle{
double radius;
public:
circle(double r){radius=r;}
double get_area(){return 3.416*radius*radius;}
};
class table{
double height;
public:
table(double h)<height=h;}
double get_height(){return height;}
};
class roundtable:public table,public circle{
char *color;
public:
roundtable(double h,double r,char c[]): 【 】 {
color=new char[strlen(c) +1];
【 】;
};
char*get_color(){return color;}
}:
void main(){
roundtable rt(0.8,1.0,“白色”);
cout<<"圆桌的高:"<<rt. get_height()<<end1;
cout<<"圆桌面积:"<<rt.get_area()<<end1;
cout<<"圆桌颜色:"<<n.get color()<<end1;
}
第3题:
本题的功能是用文本框来设定表盘中指针的位置。窗口中有一个画板和两个文本框,画板中绘制了一个表盘和时针、分针,通过文本框分别设定“时”和“分”,表盘中的时针和分针就会指到对应的位置上。
import java.awt.*;
import java.awt.event*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.event.*;
public class java3
{
public static void main(String[]args)
{
TextTestFrame. frame=new TextTestFrame():
frame.setDefauhCloseOperation(JFrame.EXIT_
0N_CLOSE);
frame.show();
}
}
class TextTestFrame. extends JFrame
{
public TextTestFrame()
{
setTitle("java3"):
setSize(DEFAULT_WIDTH,DEFAULT_
HElGHT);
Container contentPane=getContentPane();
DocumentListener listener=new DoeumentListen-
er();
JPanel panel=new JPanel();
hourField=new JTextField("12",3);
panel.add(hourField);
hourField.getDocument().addDocumentListener
(this);
minuteField=new JTextField("00",3):
panel.add(minuteField);
minuteField.getDocument().addDocumentListener
(listener);
contentPane.add(panel,BorderLayout.S()UTH);
clock=new ClockPanel();
contentPane.add(clock,BorderLayout.CEN-
TER);
}
public void setClock()
{
try
{
int hours
=Integer.parseInt(hourField.getText().trim
()):
int minutes
=Integer.parseInt(minuteField.getText().trim
());
clock.setTime(hours,minutes);
}
catch(NumberFormatExcepfion e){}
}
public static final int DEFAULT_WIDTH=300;
public static final int DEFAULT_HEIGHT
=300;
private J TextField hourField;
private JTextField minuteField;
private ClockPanel clock;
private class clockFieldListener extends Docu-
mentListener
{
public void insertUpdate(DocumentEvent e){ set-
Clock();}
public void removeUpdate(DocumentEvent e){
setClock();}
public void changedUpdate(DocumentEvent e){}
}
}
class ClockPanel extends JPanel
{
pubhc void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphies2D g2=(Graphics2D)g;
Ellipse2D circle
=new Ellipse2D.Double(0,0,2* RADIUS,2
*RADIUS);
g2.draw(circle);
double hourAngle
=Math.toRadians(90-360*minutes/(12
*60));
drawHand(92,hourAngle,HOUR_HAND_
LENGTH);
double minuteAngle
=Math.toRadians(90-360*minutes/60);
drawHand(g2,minuteAngle,MINUTE_HAND_
LENGTH):
}
punic void drawHand(Graphics2D g2,
double angle,double handLength)
{
Point2D end=new Point2D.Double(
RADIUS+handLength*Math.cos(angle),
RADIUS-handLength*Math.sin(angle));
Point2D center=new Point2D.Double(RADIUS,
RADIUS):
g2.draw(new Line2D.Double(center,end));
}
public void setTime(int h,int m)
{
minutes=h*60+m;
repaint();
}
private double minutes=0;
private double RADIUS=100;
private double MINUTE_HAND_LENGTH=
0.8*RADIUS;
private double HOUR_HAND_LENGTH=0.6
*RADIUS:
}
第4题:
Which will declare a method that is available to all members of the same package and be referenced without an instance of the class?()
第5题:
class super { public float getNum() {return 3.0f;} } public class Sub extends Super { } Which method, placed at line 6, will cause a compiler error?()
第6题:
public class Mycircle { public double radius; public double diameter; public void setRadius(double radius) this.radius = radius; this.diameter= radius * 2; } public double getRadius() { return radius; } Which statement is true?()
第7题:
1. class super { 2. public float getNum() {return 3.0f;} 3. } 4. 5. public class Sub extends Super { 6. 7. } Which method, placed at line 6, will cause a compiler error?()
第8题:
public abstract class Shape { private int x; private int y; public abstract void draw(); public void setAnchor(int x, int y) { this.x = x; this.y = y; } } Which two classes use the Shape class correctly?()
第9题:
Public float getNum() {return 4.0f; }
Public void getNum (){}
Public void getNum (double d){}
Public double getNum (float d) {retrun 4.0f; }
第10题:
The Mycircle class is fully encapsulated.
The diameter of a given MyCircle is guaranteed to be twice its radius.
Lines 6 and 7 should be in a synchronized block to ensure encapsulation.
The radius of a MyCircle object can be set without affecting its diameter.
第11题:
public class Circle implements Shape { private int radius; }
public abstract class Circle extends Shape { private int radius; }
public class Circle extends Shape { private int radius; public void draw(); }
public abstract class Circle implements Shape { private int radius; public void draw(); }
public class Circle extends Shape { private int radius;public void draw() {/* code here */} }
public abstract class Circle implements Shape { private int radius;public void draw() { / code here */ } }
第12题:
Abstract public void methoda();
Public abstract double methoda();
Static void methoda(double d1){}
Public native double methoda(){}
Protected void methoda(double d1){}
第13题:
阅读以下说明和C++程序,将应填入(n)处的字句写在对应栏内。
【说明】
以下程序的功能是计算正方体、球体和圆柱体的表面积和体积并输出。
程序由4个类组成:类cube、sphere和cylinder分别表示正方体、球体和圆柱体;抽象类 container为抽象类,提供了两个纯虚拟函数surface_area()和volum(),作为通用接口。
【C++程序】
include<iostream.h>
define pi 3.1416
class container{
protected:
double radius;
public:
container(double radius) {container::radius=radius;}
virtual double surface_area()=0;
virtual double velum()=0;
};
class cube:(1){ //定义正方体类
public:
cube(double radius):container(radius){};
double surface_area () {return 6 * radius * radius;}
double volum() {return radius * radius * radius;}
};
class sphere:(2){ //定义球体类
public:
sphere(double radius): container(radius){};
double surface_area() { return (3);}
double volum() {return pi * radius * radius * radius * 4/3;}
};
class cylinder:(4){ //定义圆柱体类
double height;
public:
cylinder(double radius,double height):container(radius)
{
container::height=height;
}
double surface_are a () { return 2 * pi * radius * (height+radius); }
double volum () {return (5);}
};
void main()
{
container * p;
cube obj1 (5);
sphere obj2(5);
cylinder obj3(5,5);
p=&obj1;
cout<<“正方体表面积”(<<p->surface_area()<<end1;
cont<<“正方体体积”<<p->volume()<<end1;
p=&obj2;
cout<<“球体表面积”<<p->surface_area()<<end1;
cout<<“球体体积”<<p->volume()<<end1;
p=&obj3;
cout<<“球体表面积”<<p->surface_area()<<end1;
cout<<“球体体积”<<p->volume()<<end1;
}
第14题:
阅读以下说明和JAVA 2代码,将应填入(n)处的字句写在对应栏内。
[说明]
以下程序为类类型的变量应用实例,通过异常处理检验了类CCircle的变量的合法性,即参数半径应为非负值。仔细阅读代码和相关注释,将程序补充完整。
[JAVA代码]
//定义自己的异常类
class CCircleException extends Exception
{
}
// 定义类 CCircle
class CCircle
{
private double radius;
public void setRadius ( double r ) (1)
{
if ( r<0 ) {
(2)
}
else
(3)
}
Public void show ( ) {
System. out. println ( "area="+3.14*radius*radius );
}
}
public class ciusample
{
public static void main ( String args[] )
{
CCircle cir=new CCircle( );
(4) {
cir. setRadius ( -2.0 )
}
(5)
{
System. out. println ( e+" throwed" ) ;
}
cir. show( ) ;
}
}
第15题:
第16题:
1. class Super { 2. public float getNum() { return 3.0f; } 3. } 4. 5. public class Sub extends Super { 6. 7. } Which method, placed at line6, causes compilation to fail?()
第17题:
public class OuterClass { private double d1 1.0; //insert code here } You need to insert an inner class declaration at line2. Which two inner class declarations are valid?()
第18题:
1. public class OuterClass { 2. private double d1 = 1.0; 3. // insert code here 4. } Which two are valid if inserted at line 3?()
第19题:
Which will declare a method that is available to all members of the same package and can be referenced without an instance of the class?()
第20题:
static class InnerOne { public double methoda() {return d1;} }
static class InnerOne { static double methoda() {return d1;} }
private class InnerOne { public double methoda() {return d1;} }
protected class InnerOne { static double methoda() {return d1;} }
public abstract class InnerOne { public abstract double methoda(); }
第21题:
public void getNum(){}
public void getNum(double d){}
public float getNum() { return 4.0f; }
public double getNum(float d) { return 4.0d; }
第22题:
static class InnerOne { public double methoda() { return d1; } }
static class InnerOne { static double methoda() { return d1; } }
private class InnerOne { public double methoda() { return d1; } }
protected class InnerOne { static double methoda() { return d1; } }
public abstract class InnerOne { public abstract double methoda(); }
第23题:
abstract public void methoda ();
public abstract double inethoda ();
static void methoda (double dl) {}
public native double methoda () {}
protected void methoda (double dl) {}
第24题:
Public float getNum() {return 4.0f; }
Public void getNum () { }
Public void getNum (double d) { }
Public double getNum (float d) {retrun 4.0f; }