import sun.scjp.Color.*;
import static sun.scjp.Color.*;
import sun.scjp.Color; import static sun.scjp.Color.*;
import sun.scjp.*; import static sun.scjp.Color.*;
import sun.scjp.Color; import static sun.scjp.Color.GREEN;
第1题:
本题的功能是用按钮来控制文本框中文本的颜色。窗口中有两个带有文字标题的面板“Sample text”和“Text color control”,窗口的底部还有一个复选按钮“Disable changes”。在“Sample text”面板中有一个带有字符串的文本框,而在“Text color control”面板中有三个按钮:“Black”、“Red”和“Green”,并且每个按钮上都有一个对应颜色的圆。单击任意按钮,文本框中的文本变成对应的颜色,如果选中“Disable changes”复选项,则三个颜色按钮变为不可用,如果取消选中复选项,则三个按钮变为可用。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class java3 extends JFrame{
private JPanel upper,middle,lower;
private JTextField text;
private JButton black,red,green;
private JCheckBox disable;
public java3(String titleText){
super(titleText);
addWindowListener(new WindowAdapter(){
public void
windowClosing(WindowEvent e){
System.exit(0);
}
}
);
upper=new JPanel();
upper.setBorder(BorderFactory.ereateTitledBor-
der("Sample text"));
upper.setlayout(new BorderLayout());
text=new JTextField("Change the color of this
text"):
upper.add(text,BorderLayout.CENTER);
middle=new JPanel();
middle.setBorder(BorderFactory.createTitledBor-
der("Text color control"));
middle.setLayout(new FlowLayout(FlowLayout.
CENTER)):
black=new JButton("Black",new ColorIcon
(Color.black));
black.addActionListener( new ButtonListener
(Color.black));
middle.add(black);
red=new JButton("Red",new ColorIcon(Col-
or.red));
red.addActionListener(new ButtonListener(Col-
or.red));
middle.add(red);
green=new JButton("Green",new ColorIcon
(Color.green));
green.addActionListener(new ButtonListener
(Color.green));
middle.add(green);
lower=new JPanel();
lower.setLayout(new FlowLayout(FlowLayout.
RIGHT));
disable=new JCheckBox("Disable changes"):
disable.addItemListener(new ItemListener()(
public void itemStateChanged(ItemEvent e){
boolean enabled
=(e.getStateChange()
= =ItemEvent.DESELECTED):
black.setEnabled(enabled);
red.setEnabled(enabled);
green.setEnabled(enabled);
}
}
);
lower.add(disable);
Container cp=getContentPane();
cp.add(upper,BorderLayout.NORTH);
cp.add(middle,BorderLayout.CENTER);
cp.add(10wer,BorderLayout.SoUTH);
pack();
setVisible(true);
}
class ButtonListener extends ActionListener{
private Color c;
public ButtonListener(Color c){
this.c=c;
}
public void actionPerformed(ActionEvent e){
text.setForeground(c);
}
}
class ColorIcon implements Icon{
private Color c;
private static final int DIAMETER=10;
public ColorIcon(Color c){
c=c;
}
public void paintlcon(Component cp,Graphics g,
int x,int y){
g.setColor(c);
g.fillOval(X,y,DIAMETER,DIAMETER);
g.setColor(Color.black);
g.drawOval(x,y,DIAMETER,DIAMETER);
}
public int getlconHeight(){
return DIAMETER;
}
public int getlconWidth(){
return DIAMETER;
}
}
public static void main(String[]args){
new java3("advance");
}
}
第2题:
阅读以下说明和 Java 代码,填补代码中的空缺,将解答填入答题纸的对应栏内。 【说明】 设计 RGB 方式表示颜色的调色板,进行绘图。其类图如图 6-1 所示。该程序的 Java代码附后。图6-1 类图
【Java 代码】 //颜色类 class MyColor { private int red ,green, blue; public MyColor( ) { red = o; green = 0; blue = 0; } public MyColor(int red ,int green ,int blue) { this.red = red; this.green = green; this.blue = blue; } //其他方法略 public String toString( ) { return "Red: " + red + "\tGreen: " + green + "\tBlue " + blue; } } //调色板类 class Palette { public int number; / /颜色数 private (1)palette; //颜色表 public Palette( ) { number = 256; palette = new MyColor[number); } public Palette(MyColor[] palette ,int number) { (2)= number; (3)= palette; } //其他方法略 public String toString( ) { String str = ""; for (int i = 0; i < number; i++) { str +=i+ " : " + palette[i] + "\n"; } return str; } //绘图类 class Drawing { public (4) int COLORNUMBER = 16; public static void main(String[] args) { Palette palette; int red ,green ,blue; MyColor[] color = new MyColor[COLORNUMBER]; for (int i = 0; i < COLORNUMBER; i++) { red = (int) (Math.random( ) * 256); green = (int) (Math.random( ) * 256); blue = (int) (Math.random( ) * 256); color [i] = (5) (red ,green ,blue); } palette = new Palette(color ,COLORNUMBER); System.out.println(palette); } }
第3题:
public class Test { public static void main (String args) { string foo = “blue”; string bar = foo; foo = “green”; System.out.printIn(bar); } } What is the result?()
第4题:
现有: 1. abstract class Color2 { 2. //insert code here 3. } 4. 5. public class Blue2 extends Color2 { 6.public String getRGB() { return "blue"; } 7. } 和4个声明: public abstract String getRGB(); abstract String getRGB(); private abstract String getRGB(); protected abstract String getRGB(); 分别插入到第2行,有多少行可以编译?()
第5题:
class Sock { String size; String color; public boolean equals(Object o) { Sock s = (Sock) o; return color.equals(s.color); } // insert code here } 哪两个满足 hashCode 的约定?()
第6题:
package sun.scjp; public enum Color { RED, GREEN, BLUE } package sun.beta; // insert code here public class Beta { Color g = GREEN; public static void main( String[] argv) { System.out.println( GREEN); } } The class Beta and the enum Color are in different packages. Which two code fragments, inserted individually at line 2 of the Beta declaration, will allow this code to compile?()
第7题:
public class test( public static void main(stringargs){ string foo = args [1]; string foo = args ; string foo = args ; } ) And command line invocation: Java Test red green blue What is the result? ()
第8题:
interface Beta {} class Alpha implements Beta { String testIt() { return “Tested”; } } public class Main1 { static Beta getIt() { return new Alpha(); } public static void main( String[] args ) { Beta b = getIt(); System.out.println( b.testIt() ); } } What is the result?()
第9题:
Tested
Compilation fails.
The code runs with no output.
An exception is thrown at runtime.
第10题:
0
1
2
3
第11题:
import sun.scjp.Color.*;
import static sun.scjp.Color.*;
import sun.scjp.Color; import static sun.scjp.Color.*;
import sun.scjp.*; import static sun.scjp.Color.*;
import sun.scjp.Color; import static sun.scjp.Color.GREEN;
第12题:
An exception is thrown.
The code will not compile.
The program prints “null”
The program prints “blue”
The program prints “green”
第13题:
阅读下列说明和 C++代码,填补代码中的空缺,将解答填入答题纸的对应栏内。 【说明】 设计 RGB 方式表示颜色的调色板,进行绘图,其类图如图 5-1 所示。该程序的 C++代码附后。图5-1 类图
【C++代码】 include <iostream> include <stdlib.h> include <ctime> using namespace std; class MyColor{ private: int red; int green; int blue; public: MyColor() {red = 0; green = 0; blue = 0; } ~MyColor() { } MyColor(int red ,int green ,int blue) { this->red = red; this->green = green; this->blue = blue;} //其他方法略 void print() { cout<<"Red: " << red << "\tGreen: " << green << "\tBlue " << blue << endl; } }; class Palette ( private: int number; MyColor** palette; public: Palette() { number = 256; palette = (MyColor*)malloc (sizeof(MyColor ) *number); } ~Palette () { for (int i = 0; i < number; i++) { delete palette[i]; } (1) ; } Palette(MyColor** pale ,int number) { (2) = number; palette = (MyColor**)malloc(sizeof(MyColor*)*number) ; memcpy(palette ,pale ,sizeof(pale)*number); } //其他方法略 void print () { for (int i = 0; i < number; i++) { cout << i << " : " ; palette[i]->print(); } } }; class Drawing{ public: (3) int COLORNUMBER = 16; public: ~Drawing () { } void draw() ( Palette* palette; int red ,green ,blue; MyColor* color[COLORNUMBER); srand((unsigned)time(O)); for (int i = 0; i < COLORNUMBER; i++) red=rand ()% 256; green = rand() % 256; blue = rand ()% 256; color [i) = (4) (red ,green ,blue); } palette = new Palette(color ,COLORNUMBER); palette->print(); for (int i = 0; i < COLORNUMBER; i++) delete color[i]; } }; int main () { Drawing * d = (5) ; d->draw(); delete d; }
第14题:
public class test( public static void main(string[]args){ string foo = args [1]; string foo = args [2]; string foo = args [3]; } ) And command line invocation: Java Test red green blue What is the result?()
第15题:
Which declarations will allow a class to be started as a standalone program?()
第16题:
public class Test { public static void main( String[] args) { String foo = args[1]; String bar = args[2]; String baz = args[3]; System.out.println(“baz = “ + baz); } } And the command line invocation: java Test red green blue What is the result?()
第17题:
public class Ball { public enum Color { RED, GREEN, BLUE }; public void foo() { // insert code here { System.out.println(c); } } } Which code inserted at line 14 causes the foo method to print RED, GREEN, and BLUE?()
第18题:
现有: 1. abstract class Color { 2.protected abstract String getRGB(); 3. } 4. 5. public class Blue extends Color { 6. //insert code here 7. } 和四个声明: public String getRGB() { return "blue"; } String getRGB() { return "blue"; ) private String getRGB() { return "blue"; } protected String getRGB() { return "blue"; ) 分别插入到第6行,有几个可以通过编译?()
第19题:
10. interface Foo {} 11. class Alpha implements Foo {} 12. class Beta extends Alpha {} 13. class Delta extends Beta { 14. public static void main( String[] args) { 15. Beta x = new Beta(); 16. // insert code here 17. } 18. } Which code, inserted at line 16, will cause a java.lang.ClassCastException?()
第20题:
You work as a Web Developer at Certkiller.com. You make use of Microsoft ASP.NET 3.5 in orderto create a Web Site. In a separate code file you create the following class: public static class _Colors { public static Color NormalActivityColor = Color.Green; public static Color WarningActivityColor = Color.Yellow; public static Color ErrorActivityColor = Color.Red; public static Color GetRandomColor() { Random random = new Random((int)DateTime.Now.Ticks); int randomArgbValue = random.Next(); Color color = Color.FromArgb(255, Color.FromArgb(randomArgbValue)); return color; } } You need to ensure that the class is consumed by the Web Site. You have to configure the WebSite project. What should you do?()
第21题:
Baz has the value of “”
Baz has the value of null
Baz has the value of “red”
Baz has the value of “blue”
Bax has the value of “green”
The program throws an exception.
第22题:
baz =
baz = null
baz = blue
Compilation fails.
An exception is thrown at runtime.
第23题:
Baz has the value of “”
Baz has the value of null
Baz has the value of “red”
Baz has the value of “blue”
Bax has the value of “green”
The program throws an exception.
第24题:
0
1
2
3