1.完成程序填空,使程序可以正常运行。
程序的功能是对两个内容相同的串采用两种方式进行比较,会得到不同的结果。该程序运行时的输出结果如下:true false
public class Program
{ //*********Found**********
public static void 下划线 ( 下划线 []args)
{String s1=new String("你正在考试");
String s2=new String("你正在考试");
System.out.println(s1==s2);
//*********Found**********
System.out.println(
下划线 (s2));
}
}
参考答案:第1空: main第2空: String第3空: s1.equals
2.完成程序填空,使程序可以正常运行。
class Juxing {
private double chang;
private double kuan;
public double getChang() {
return chang;
}
public void setChang(double chang) {
下划线 .chang = chang;
}
public double getKuan() {
return kuan;
}
public void setKuan(double kuan) {
this.kuan = kuan;
}
public double area(){
return chang*kuan;
}
}
class ChangFangTi 下划线 下划线
{
private double gao ;
public double getGao() {
return gao;
}
public void setGao(double gao) {
this.gao = gao;
}
public double tiji(){
return 下划线 ()*gao;
}
}
public class Program {
public static void main(String[] args){
ChangFangTi a=new ChangFangTi();
a.setChang(12);
a. 下划线 (10);
a.setGao(5);
System.out.println("长方体的底面积是:"+a.area());
System.out.println("长方体的体积是:"+a.tiji());
}
}
参考答案:
第1空: this
第2空: extends
第3空: Juxing
第4空: area
第5空: setKuan
.
3该程序的功能是统计素数个数:当用户在"N值"后面输入一个正整数(大于等于5)时,点击按钮"计算"后,计算出小于等于该值的所有素数,并在下方的区域中显示出素数的个数(注意该区域不能进行编辑)。下图所示的是可能的运行的界面显示:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Program extends JFrame implements ActionListener{
JTextField nval = new JTextField(10);
//**********Found**********
JButton calcBtn = 下划线 ;
JTextArea result = new JTextArea(10,20);
void initFrame(){
Container content = getContentPane();
JPanel calcPanel = new JPanel();
calcPanel.add(new JLabel("N值"));
//**********Found**********
calcPanel. 下划线 ;
calcPanel.add(calcBtn);
content.add(calcPanel,"North");
//**********Found**********
calcBtn. 下划线 ;
content.add(result,"Center");
result. setEditable(false);
}
public Program(){
super("计算素数");
setSize(500,200);
initFrame();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==calcBtn){
int N=Integer.parseInt(nval.getText());
int [] prime=new int[N/3+2];
prime[0]=2;prime[1]=3;
int k=2;
for(int m=5;m<=N;m+=2){
int j=1,isprime=1;
int kk=Math.round((float)Math.sqrt(m));
while(prime[j]<=kk){
if(m%prime[j]==0){
//**********Found**********
下划线 =0;
break;
}else
//**********Found**********
下划线 ;
}
if(isprime==1) prime[k++]=m;
}
//**********Found**********
String str="Total prime number: "+ 下划线 ;
result.setText("");
result.append(str );
}
}
public static void main(String[] args){
new Program();
}
}
参考答案:
第1空: new JButton(“计算”)第2空: add(nval)第3空: addActionListener(this)
第4空: isprime 第5空: j++第6空: k
4.鸟类和昆虫类都具有飞行的功能,这个功能是相同的,但是其它功能是不同的,在程序实现的过程中,就可以定义一个接口,专门描述飞行。
请在下划线处填写正确的内容,使程序实现描述飞行的功能。
程序运行结果:
Ant can fly
Ant’slegs are 6
pigeon can fly
pigeon can lay eggs
下划线 Flyanimal{
void fly();}
class Insect {
int legnum=6;}
class Bird {
int legnum=2;
void egg(){};}
class Ant 下划线 Insect implements Flyanimal {
public void fly(){
System.out.println("Ant can fly");
}}
class Pigeon extends Bird implements Flyanimal {
public void fly(){
System.out.println("pigeon can fly");
}
public void egg(){
System.out.println("pigeon can lay eggs ");
}}
public class Program{
public static void main(String args[]){
Ant a=new Ant();
a. 下划线 ();
System.out.println("Ant's legs are"+ a.legnum);
Pigeon p= new Pigeon();
p.fly();
p.egg();
}
}
参考答案:
第1空: interface
第2空: extends
第3空: fly
5.填写下面空白,使得程序输出 42。
import java.util. 下划线 ;
public class Program {
public static void main( String [ ] args) {
HashMap < String , Integer> points = new HashMap < String , Integer>();
points. put ("Amy", 154) ;
points. put ( 下划线 , 下划线 );
points. put ("Rob", 733);
System.out.println(points. 下划线 ("Dave"));
} }
参考答案:
第1空: HashMap第2空: "Dave"第3空: 42第4空: get
6.程序的功能是:打开和关闭数据库,填写空白,完成此功能
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DBHelper {
public static final String url = "jdbc:mysql://127.0.0.1/student";
public static final String name = "com.mysql.jdbc.Driver";
public static final String user = "root";
public static final String password = "root";
public 下划线 conn = null;
public PreparedStatement pst = null;
public DBHelper(String sql) {
try {
Class.forName(name);//指定连接类型
conn = DriverManager.getConnection(url, user, password);//获取连接
pst = conn. 下划线 (sql);//准备执行语句
} catch (Exception e) {
e. 下划线 ();
}
}
public void close() {
try {
this.conn.close();
this.pst. 下划线 ();
} catch (SQLException e) {
e.printStackTrace();
}}}
答案:第1空: Connection第2空: prepareStatement
第3空: printStackTrace第4空: close
7.完善程序,使程序输出abc
public class Program
{
public static void 下划线 (String []args)
{
char ch='d';
switch( 下划线 )
{
case 'a': System.out.print("a");break;
case 'b': System.out.print("b");
case 'c': System.out.print("c");break;
下划线 : System.out.print("abc");
}}}
参考答案:第1空: main第2空: ch 第3空: default
8代码段的功能是查询所有学生记录,请填写空白,完成该代码段。
public LIst<Student>getAllStudent() throws Exception{
List<Student> list=new ArrayList<Student>();
String 下划线 ="SELECT* 下划线 Student,Grade WHERE Student.GradeId=Grade.GradeId AND GradeName=?"
ResultSet rs = 下划线 (sql, 下划线 );
if(rs!=null){
while(rs.next()){
//获取学生值
Student student=new Student();
student.setGradeId(rs.getInt("gradeId"));
student.setStudentName(rs.getString("StudentName"));
student.setStudentNo(rs.getInt("studentNo"));
//获取的就是当前学生的年级信息
Grade grade=new Grade();
grade.setGradeName(rs.getString("gradeName"));
student.setGrade(grade);
list.add(student);
}}
closeResource();
return list;}
参考答案:
第1空: sql第2空: FROM第3空: executeQuery第4空: gradeName
9完成程序,使程序输出结果如下:
Name: Zhao Age: 20
Name: Wang Age: 18 School: Cambridge
public class Program {
public static void main(String[] args) {
Person p = new Person("Zhao",20);
Student s = new Student("Wang",18,"Cambridge");
System.out.println(p);
System.out.println(s);
}}
class Person {
private String name;
private int age;
public Person(String name, int age) {
//*********Found********
下划线 = name;
//*********Found********
下划线 = age;
}
public void setName(String n) {
//*********Found********
下划线 = n;
}
public String toString() {
return "Name: " + name + "Age: " + age;
}}
class Student extends Person {
private String school;
public Student(String name, int age, String s) {
//*********Found********
下划线 ;
school = s;
}
public String toString() {
//*********Found********
return 下划线 .toString() + " School: " + school;
}
}
参考答案:
第1空: this.name
第2空: this.age
第3空: name
第4空: super(name,age)
第5空: super
10下面的程序实现,向ArrayList集合中添加5个对象,然后使用迭代器输出集合中的对象。
请填写空白,完成程序功能。
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
public class Program{
public static void main(String[] args) {
List<Object> list = new 下划线 <Object>();
list.add("zhangsan");
list.add("lisi");
list.add("wangwu");
list.add("zhaoliu");
Iterator it = list.iterator();
while (it. 下划线 ) {
Object object = it. 下划线 ();
System.out.println(object);
}}}
参考答案:
第1空: ArrayList第2空: hasNext()第3空: next
11.请在下划线处填写正确的内容,使程序实现对数据库表中数据进行查询
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Program {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//连接数据库
Class. 下划线 ("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/mybatis_test";
String 下划线 = "root";
String password = "123456";
//建立数据库连接
Connection conn = 下划线 .getConnection(url, user, password);
String sql = "select * from user";
Statement stmt = conn.createStatement(); //创建一个statement对象
ResultSet rs = stmt.executeQuery(sql); //执行查询
int id, sex;
String username, address;
System.out.println("id\t姓名\t性别\t地址\t");
while(rs.next()) { //遍历结果集
id = rs. 下划线 ("id");
username = rs.getString("username");
sex = rs.getInt("sex");
address = rs.getString("address");
System.out.println(id + "\t" + username + "\t" + sex + "\t" + address);
}}}
参考答案:第1空: forName第2空: user第3空: DriverManager第4空: getInt
12填写空白以声明一个名为 hello() 的方法
public class Program
{
void 下划线 (){
System.out.print("Hello,World!");
}
public static void main(String args[])
{
}
}
参考答案:第1空: hello
13.完成程序填空,使程序可以正常运行。
class Parent {
public void printMe() {
System.out.println("parent");
}
}
class Child 下划线 Parent {
public void printMe() {
System.out.println("child");
}
public void printAll() {
下划线 .printMe();
this. 下划线 ();
printMe();
}}
public class Program {
public static void main(String[] args) {
Child 下划线 = new Child();
myC.printAll();
}}
参考答案:第1空: extends第2空: super第3空: printMe第4空: myC
14完成填空,是计算器程序可以正常运行
下划线 ICompute{
//接口
int computer(int a,int b);
}//四个类实现接口
class Add 下划线 ICompute{
public int computer(int a,int b){
return a+b;
}
}
class Minus implements ICompute{
public int computer(int a,int b){
return 下划线 ;
}}
class Multiply implements ICompute{
public int computer(int a,int b){
return a*b;
}
}
class Divide implements ICompute{
public int computer(int a,int b){
return a/b;
}
}
class UseComputer{
public void useCom(ICompute com,int one,int two){
System.out.println(com.computer(one,two));
}
}
public class Program{
public static void main(String[] args) {
UseComputer user = new UseComputer();
Add s1= new 下划线 ();
System.out.print("和为:");
user.useCom(s1,12,4);
Minus s2= new Minus();
System.out.print("差为:");
user.useCom(s2,12,4);
Multiply s3 = new Multiply();
System.out.print("乘积为:");
user.useCom(s3,12,4);
Divide s4 = new Divide();
System.out.print("商为:");
user.useCom(s4,12,4);
}
}
参考答案:第1空: interface第2空: implements第3空: a-b第4空: Add
15下列程序段的功能是:实现数据库的连接。请补全题目中的空白,以完成该功能。
Class. 下划线 ("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/database_name";
String user = "root";
Strign password = "root"
//建立连接
下划线 conn = DriverManager. 下划线 (url, user, password);
参考答案:第1空: forName第2空: Connection第3空: getConnection
16完善程序,使程序输出结果如下:
ii= 207 gg= 774
ff=25.132
dd= 2. 910410630643368
public class Program
{
public static void main( 下划线 args[])
{
byte b = 8;
//*********Found**********
下划线 g = 567L;
float f = 3.1415f;
double d = 2.789;
int ii = 207;
//*********Found**********
下划线 gg = g+ii;
float ff = b*f;
double dd = ff/ii+d;
//*********Found**********
System.out. 下划线 ("ii= "+ii+" ");
System.out.println("gg= "+gg);
System.out.println("ff= "+ff);
System.out.println("dd= "+dd);
}}
参考答案:第1空: String第2空: long第3空: long第4空: print
17请根据代码中注释提示,在横线处填写正确的内容,使程序实现生成10个1到20之间的不重复的随机数
import java.util. 下划线 ;
import java.util.Random;
public class Program {
public static void main(String[] args) {
//1.创建Random对象
Random r = new Random();
//2.创建HashSet对象
HashSet<Integer> hs = 下划线 HashSet<>(); //注意是包装类不是int
//3.判断并添加元素
while(hs.size()< 10) {
//4.通过Random中随机方法nextInt()获取1到20范围内随机数
hs.add(r.nextInt(20) + 1); //如果不加1,随机范围是0到19
}
//5.遍历HashSet
for (Integer integer : 下划线 ) {
System.out.println(integer);
} }}
参考答案:第1空: HashSet第2空: new第3空: hs
18一个对象写入一个数据文件,再读出该对象并显示出来。
程序的运行结果是:小王
import java.io.*;
public class Program {
public static void main(String[] args) {
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
File f = new File("Person.dat");
//*********Found**********
oos = new ObjectOutputStream(new 下划线 (f));
oos.writeObject(new Person("小王"));
oos.close();
ois = new ObjectInputStream(new FileInputStream(f));
//*********Found**********
Person d = (Person) ois. 下划线 ();
System.out.println(d);
ois.close();
} catch (Exception e) {
e.printStackTrace();
}}}
//*********Found**********
class Person implements 下划线 {
String name = null;
public Person(String s) {
name = s;
}
//*********Found**********
public String 下划线 () {
return name;
}
}
参考答案:第1空: FileOutputStream第2空: readObject
第3空: Serializable第4空: toString
19下面代码实现将不同颜色的字符串从集合中添加或删除,填写空白使得程序输出 [Red, Blue , Orange]。
import java.util.ArrayList;
public class Program{
public static void main(String[ ] args) {
ArrayList<String> colors = new 下划线 <String>();
colors.add("Red");
colors. 下划线 ("Blue");
colors.add("Green");
colors.add("Orange");
colors. 下划线 ("Green");
System.out.println(colors);
}}
参考答案:第1空: ArrayList第2空: add第3空: remove
20完善程序,使b的结果是5
public 下划线 Program{
public static void main(String args[]) {
byte b = 10; // 二进制表示00001010
//*********Found**********
byte c = 下划线 ;
b = (byte)(b ^ c);
//*********Found**********
System.out.println("b的结果是:" + 下划线 );}}
参考答案:第1空: class第2空: 0X000f第3空: b
21程序的功能是:实现乐器类的继承,请填写空白,完成程序。
Class 下划线 {
public void play(){
System.out.println("弹奏乐器!");
}}
class Brass extends Instrument {
public void 下划线 (){
System.out.println("弹奏brass!");
}
public void play2(){
System.out.println("调用brass的play2!");
}}
class 下划线 下划线 Instrument{
//复写play方法
public void play(){
System.out.println("弹奏Wind!");
}
public void play2(){
System.out.println("调用wind的play2");
}}
public class Program {
public static void tune(Instrument i){
i.play();}
public static void main(String[] args){
Wind w=new Wind();
Brass b=new Brass();
tune(w);
tune(b);
}}
参考答案:第1空: Instrument第2空: play第3空: Wind第4空: extends
22填写空白,使得程序输出 B。
import java.util.ArrayList;
public class Program{
public static void main(String[ ] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("A");
list. 下划线 ("B");
list.add("C");
System.out.println(list. 下划线 );
}}
参考答案:第1空: add第2空: get(1)
23完成下列填空,使程序可以正常运行
下划线 Interface1{
//接口Interface1
String S = "GOOD";//常量字符串S
void f();//f的抽象方法
}
下划线 class ClassA{
abstract void g();//g的抽象方法
}
class ClassB 下划线 ClassA 下划线 Interface1{
void g(){
//覆写ClassA的抽象方法
System.out.print(S);
}
public void f(){
//覆写f()的抽象方法
System.out.print(" "+S);
}}
public class Program{
public static void main(String[] args) {
ClassA a = new ClassB();//向上转型
Interface1 b = new ClassB();
a.g();//调用g()方法
b.f();//调用f()方法
}}
参考答案:
第1空: interface第2空: abstract第3空: extends第4空: implements
24程序的功能室:定义一个接口Program,完成程序填空。
public 下划线 Program {
public static final double PI = Math.PI;
public 下划线 double area(double a, double b);
}
参考答案:第1空: interface第2空: abstract
25 请填写下面空白,使得程序输出 Second。
import java.util.HashMap;
class Program{
public static void main(String[ ] args) {
HashMap<String, String> m = new HashMap<String, String>();
m.put ("A", "First");
m. 下划线 ("B", "Second");
m.put("C", "Third");
System.out.println(m. 下划线 ( 下划线 ) );
}
}
参考答案:第1空: put第2空: get第3空: “B”
26完成程序填空,使程序可以正常运行。
下划线 A{
double PI = 3.14;
double area();
}
interface B{
void setColor(String c);
}
interface C 下划线 A,B {
void volume();
}
class Cylinder 下划线 C {
private double radius;
private double height;
private String color;
public Cylinder(double radius, double height, String color){
this.radius = radius;
this.height = height;
this.color = color;
}
public void setColor(String c){
this.color = c;
}
public String getColor(){
return this.color;
}
public double area(){
return 下划线 *radius*radius;
}
public void volume(){
System.out.println("圆柱体的体积为:"+this. 下划线 ()*this.height);
}
}
public class Program{
public static void main(String[] agrs){
System.out.println("test c1:");
A c1 = new Cylinder(1,1,"red");
System.out.println("面积:"+c1.area());
System.out.println(((Cylinder)c1).getColor());
System.out.println("test c2:");
B c2 = new Cylinder(2,2,"blue");
System.out.println("面积:"+((Cylinder)c2).area());
System.out.println(((Cylinder)c2).getColor());
c2.setColor("green");
System.out.println(((Cylinder)c2).getColor());
System.out.println("test c3:");
C c3 = new Cylinder(1,2,"blank");
System.out.println("面积:"+c3.area());
c3.volume();
System.out.println(((Cylinder)c3).getColor());
}
}
参考答案:
第1空: interface
第2空: extends
第3空: implements
第4空: PI
第5空: area