《Java面向对象程序设计》学习笔记——Java程序填空题

​笔记汇总:《Java面向对象程序设计》学习笔记

这些题其实都非常滴简单,相信大伙能够立刻就秒了吧😎

题目

  1. 以下程序要求从键盘输入一个整数, 判别该整数为几位数, 并且输出结果, 请将下面的程序填写完整。
import java.util.Scanner;
public class Blank1 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(____(1)____);
		int count = 0, t;
		int x = sc.nextInt();
		sc.close();
		t = x;
		while (t != 0) {
			count++;
			____(2)____;
		}
		System.out.println(x + "是" + count + "位数");
	}
}

答案:

(1) System.in
(2) t = t / 10
  1. 在下面的程序中使用方法重载分别实现了两个和三个整数的相加,请将下面的程序填写完整。
class AddOver {
	public ____(3)____ {
		return a + b;
	}
	
	public int add(int a, int b, int c) {
		return a + b + c;
	}
}

class Blank2 {
	public static void main(String[] args) {
		AddOver a = ____(4)____;
		System.out.println(a.add(1, 2));
		System.out.println(a.add(1, 2, 3));
	}
}

答案:

(3) int add(int a, int b)
(4) new AddOver()
  1. 构造一个类来描述一个点,该类的构成包括点的x和y两个坐标,以及一些对点进行的操作,包括:取得点的坐标值,利用另一个点对当前点的坐标进行赋值,请将下面的程序填写完整。
class Point {
	int x, y;

	public ____(5)____(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public Point getPoint() {
		Point temp = new Point(0, 0);
		temp.x = x;
		temp.y = y;
		return ____(6)____;
	}

	public void setPoint(____(7)____) {
		this.x = s.x;
		this.y = s.y;
	}
}

public class Blank3 {
	public static void main(String[] args) {
		Point a = new Point(3, 4);
		Point b = new Point(0, 0);
		b = a.getPoint();
		Point c = new Point(0, 0);
		c.setPoint(b);
	}
}

答案:

(5) Point
(6) temp
(7) Point s
  1. 下面的程序完成从 D:\Hello.txt 读取文本并显示在屏幕上,请将下面的程序填写完整。
public class Blank4 {
	public static void main(String[] args) {
		String fileName = "D:/Hello.txt", line;
		try {
			BufferedReader in = new BufferedReader(____(8)____);
			line = in.readLine();
			while (____(9)____) {
				System.out.println(line);
				line = ____(10)____;
			}
			in.close();
		} catch (Exception e) {
			System.out.println("Problem reading " + fileName);
		}
	}
}

答案:

(8) new FileReader(fileName)
(9) line != null
(10) in.readLine()
  1. 下面的程序通过方法调用从包含 7 个学号的数组中随机抽取一个学号并输出显示,请将下面的程序填写完整。
public class Ex1 {
	public ____(11)____ String getXh() {
		String[] xhs = {"1","2","3","4","5","6","7"};
		int index = ____(12)____; // 生成 0 ~ 6 之间的随机数
		return xhs[index];
	}
	
	public static void main(String[] args) {
		System.out.println("随机抽取的学号为: " + ____(13)____);
	}
}

答案:

(11) static
(12) (int)(Math.random()*7)
(13) getXh()
  1. 下面的程序定义了一个线程 TimeThread ,该线程每隔 1 秒钟输出显示一次当前系统时间,在 main 方法中使用 TimeThread 类创建 3 个新线程,并启动这些线程,请将下面的程序填写完整。
import java.util.Date;

class TimeThread implements ____(14)____ {
	public void run() {
		while (true) {
			Date currentTime = new Date();
			try {
				____(15)____; // 休眠1秒钟
			} catch (Exception e) {
				System.out.println(e.toString());
			}
			System.out.println(Thread.currentThread().getName() + ":" + currentTime);
		}
	}
}

public class Ex2 {
	public static void main(String[] args) {
		String[] names = { "first", "second", "third" };
		TimeThread myThread = new TimeThread();
		for (int i = 0; i < 3; i++) {
			Thread thread = new Thread(myThread, names[i]);
			____(16)____; // 启动线程
		}
	}
}

答案:

(14) Runnable
(15) Thread.sleep(1000)
(16) thread.start()
  1. 下面的程序对“百鸡百钱”问题进行了求解,公鸡每只 3 元,母鸡每只 5 元,小鸡 3 只 1 元,用 100 元钱买 100 只鸡,公鸡、母鸡、小鸡应各买多少?请将程序填写完整。
public class Ex3 {
	public static void main(String[] args) {
		int a, b, c;
		for (a = 0; ____(17)____; a++) {
			for (b = 0; ____(18)____; b++) {
				c = 100 - a - b;
				if ((3 * a + 5 * b + c / 3 == 100) && (____(19)____)) {
					System.out.println("公鸡:" + a + " 母鸡:" + b + " 小鸡:" + c);
				}
			}
		}
	}
}

答案:

(17) a <= 33
(18) b <= 20
(19) c % 3 == 0
  1. 下面的程序使用 BufferedWriter 类在 D:\Hello.txt 文件中写入 10 万个数并输出所用的时间,请将程序填写完整。
public class Ex4 {
	public static void main(String[] args) throws IOException {
		long t = System.currentTimeMillis();
		BufferedWriter fw = new BufferedWriter(____(20)____);
		for (int i = 1; i <= 100000; i++) {
			____(21)____(i + "\n");
		}
		fw.close();
		t = System.currentTimeMillis() - t;
		System.out.println("Time elapsed: " + t + "ms");
	}
}

答案:

(20) new FileWriter("D:\\Hello.txt")
(21) fw.write
  1. 根据程序注释提示将下面的程序填写完整。
public class StringExample {
	public static void main(String[] args) {
		String s1 = new String("2012");
		String s2 = new String("100.50");
		int x = ____(21)____; // 将 s1 转换为 int 类型
		double y = ____(22)____; // 将 s2 转换为 double 类型
		double z = x + y;
		String s3 = ____(23)____; // 将 z 转换为字符串
		StringBuffer sbr = new StringBuffer("Thingking");
		String s4 = new String("in Java");
		____(24)____; // 将 s4 连接在 sbr 的后面
		System.out.println(sbr.toString()); // 显示为 Thingking in Java
	}
}

答案:

(22) Integer.parseInt(s1)
(23) Double.parseDouble(s2)
(24) String.valueOf(z) 或 z + ""
(25) sbr.append(s4)
  1. 下面的程序是采用冒泡法对数组元素按小到大的顺序排序,请将程序填写完整。
public class ArraySort {
	public static void main(String[] args) {
		int[] a = new int[] { 21, 34, 211, 15, 92, 68, 89, 794, 11, 863 };
		int temp;
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < ____(26)____; j++) {
				if(a[j] > a[j + 1]) {
					temp = a[j];
					____(27)____;
					____(28)____;
				}
			}
		}
		for (int i = 0; i < a.length; i++) {
			System.out.println(a[i] + " ");
		}
	}
}

答案:

(26) a.length - 1 - i 或 9 - i
(27) a[j] = a[j + 1]
(28) a[j + 1] = temp
  1. “同构数”是指这样的整数:它恰好出现在其平方数的右端,例如 5 和 6 就是同构数。请编写一程序找出 10 ~ 999 之间的同构数,并输出显示。
public class TGS {
	public static void main(String[] args) {
		for (int i = 10; i <= 999; i++) {
			if (____(29)____ || ____(30)____) {
				System.out.println(i);
			}
		}
	}
}

答案:

(29) i * i % 100 == i
(30) i * i % 1000 == i
  1. 编程求出 1-100 之间偶数的和。
public class Exam1 {
	public static void main(String[] args) {
		____(31)____; // 定义整型变量 sum
		for (int i = 2; i <=100;) {
			sum += i;
			____(32)____;
		}
		System.out.println("1-100 之间偶数的和是:" + sum);
	}
}

答案:

(31) int sum = 0
(32) i = i + 2 或 i += 2
  1. 完成求 n ! 的程序
public class Exam2 {
	static void factorial(int n) {
		long m = 1;
		for (int x = 1; x <= n; ____(33)____) {
			____(34)____;
			System.out.println(x + "!=" + m);
		}
	}
	
	public static void main(String[] args) {
		factorial(9);
	}
}

答案:

(33) x++
(34) m = m * x 或 m *= x
  1. 下面的程序定义了一个线程 PrintThread ,该线程打印输出 1~100 之间所有 3 的倍数,每输出一个数,休眠 1500 毫秒,在 main 方法中创建了该线程的一个实例,并启动该线程。请将下面的程序填写完整。
class PrintThread extends ____(35)____ {
	public PrintThread(String str) {
		____(36)____; // 调用父类的构造方法
	}

	public void run() {
		for (int i = 1; i <= 100; i++) {
			if (i % 3 == 0) {
				System.out.println(this.getName() + ": " + i);
			}
			try {
				____(37)____; // 休眠 1500 毫秒
			} catch (Exception e) {
				System.out.println(e.toString());
			}
		}
	}
}

public class Exam5 {
	public static void main(String[] args) {
		PrintThread myThread = new PrintThread("PrintThread");
		____(38)____; // 启动线程
	}
}

答案:

(35) Thread
(36) super(str)
(37) sleep(1500)
(38) myThread.start()
  1. 中国有句俗语“三天打鱼两天晒网”,某人从 2010 年 1 月 1 日起三天打鱼两天晒网,编程计算 2010年 5 月 1 日,他在打鱼还是在晒网。打鱼则输出 1 ,晒网则输出 0 。请将程序填写完整。
public class Exam4 {
	public static void main(String[] args) {
		int[] dpm = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		int month = 5;
		int day = 1;
		for (int i = 0; ____(39)____; i++) {
			day = day + dpm[i];
		}
		day = day % 5;
		if (____(40)____) {
			System.out.println("1"); // 表示打鱼
		} else {
			System.out.println("0"); // 表示晒网
		}
	}
}

答案:

(39) i < month 或 i < 5
(40) day > 0 && day <= 3
  1. 调用函数 f 输出 n 的所有质数因子如 n=13860 则输出 2 2 3 3 5 7 11。请将下面的程序填写完整。
public class JModify {
	public static void f(int n) {
		int i = 2;
		while (n > 1) {
			____(41)____ {
				System.out.println(i);
				n /= i;
			} else {
				____(42)____;
			}
		}
	}

	public static void main(String[] args) {
		int n = 100;
		f(n);
	}
}

答案:

(41) if(n % i == 0)
(42) i++
  1. 下面的程序通过方法调用从包含 4 个手机号码的字符串数组中随机抽取一个幸运手机号并输出显示,请根据提示将程序填写完整。
public class RandomTel {
	public ____(43)____ String getTel() {
		String[] tels = {"138****8080","189****6666","133****1234","139****9999",};
		int index = ____(44)____; // 用Math类中的方法生成0 ~ 3 之间的随机数
		return tels[index];
	}
	
	public static void main(String[] args) {
		System.out.println("随机幸运手机号为:" + ____(45)____);
	}
}

答案:

(43) static
(44) (int)(Math.random()*4)
(45) getTel()
  1. 宾馆里有 100 个房间,从 1-100 进行编号,第一个服务员将所有的房间门都打开,第二个服务员把所有编号是 2 的倍数的房间“相反处理’',第三个服务员将所有编号是 3 的倍数的房间再作“相反处理",以后每个服务员都是如此操作,当第 100 个服务员来过后,请编程计算哪几个房间的门是打开的?(所谓“相反处理"是指原来开着的门关上,原来关上的门打开)请将程序填写完整。
public class HotelDoor {
	public static void main(String[] args) {
		boolean[] a = new boolean[101];
		final int N = 101;
		int i,j;
		for (i = 1; i < N; i++) {
			____(46)____; // 第 1 个服务员将所有房间设置为打开状态
		}
		for (i = 2; i < N; i++) {
			for (____(47)____; j < N; j++) {
				if(j % i == 0) {
					____(48)____; // 执行相反处理
				}
			}
		}
		for (i = 1; i < N; i++) {
			if(a[i] == true) {
				System.out.println(i + " "); // 显示打开状态的房间编号
			}
		}
	}
}

答案:

(46) a[i] = true
(47) j = i
(48) a[j] = !a[j]
  1. 以下程序要求从键盘输入一整数,判别该整数是否是素数,并输出“是素数"或“不是素数“,请将程序填写完整。
import java.util.Scanner;
public class PrimeExam {
	public static void main(String[] args) {
		Scanner sc = new Scanner(____(49)____);
		int flag = 0;
		int x = sc.____(50)____;
		int y = (int)Math.sqrt(x);
		for (int i = 2; i <= y; i++) {
			if(____(51)____) {
				System.out.println("不是素数");
				flag = 1;
				break;
			}
		}
		if (____(52)____) {
			System.out.println("是素数");
		}
	}
}

答案:

(49) System.in
(50) nextInt()
(51) x % i == 0
(52) flag == 0
  • 15
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java程序设计》课程的题库资料,由贺州学院整理,可供学生期末课程复习使用,也可以供相关任课教师出卷使用。 内容示例为: 40. __________包包含了Collection的接口的类的API。 答案:Java.util 41. Math.round(11.5)等于__________,Math.round(-11.5)等于__________。 答案:12; -11 [考点范围] 常用的系统类 42. ________对象可以使用read方法从标准的输入设备(通常键盘)读取数据;__________对象可以使用print方法向标准输出设备(屏幕)输出显示。 答案:System.in ;System.out [考点范围] JAVA输入输出系统 43. 框架(JFrame)和面板(JPanel)的默认布局管理器分别是______和_______。 答案:BorderLayout FlowLayout [考点范围] 图形用户界面 44. Swing的布局管理器主要包括_______。 答案:FlowLayout、BorderLayout、CardLayout、GridLayout、GridBogLayout、BoxLayout [考点范围] 图形用户界面 45. Java事件处理包括建立事件源、________和将事件源注册到监听器 。 答案:声明监听器 [考点范围] 图形用户界面 46. AWT的事件处理机制包括_______、事件和事件监听者。 答案:事件源 [考点范围] 图形用户界面 47. Swing的顶层容器有________、JApplet、JWwindow和JDialog。 答案:JFrame [考点范围] 图形用户界面 48. 线程的启动是通过调用其______________方法而实现的。 答案:start() [考点范围] 线程 49. Java虚拟机(JVM)中的线程调度器负责管理线程,调度器把线程的优先级分为10个级别,分别用Thread类中的类常量表示,每个Java线程的优先级都在常数________和_______之间,即Thread.MIN_PRIORIY和Thread.MAX_PRIORIY之间。 答案:1;10 [考点范围] 线程

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值