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

编程入门 行业动态 更新时间:2024-10-13 08:21:44

《Java面向对象<a href=https://www.elefans.com/category/jswz/34/1771020.html style=程序设计》学习笔记——Java程序填空题"/>

《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. 在下面的程序中使用方法重载分别实现了两个和三个整数的相加,请将下面的程序填写完整。
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));}
}

  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);}
}

  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);}}
}

  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)____);}
}

  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)____; // 启动线程}}
}

  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);}}}}
}

  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");}
}

  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}
}

  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] + " ");}}
}

  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);}}}
}

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

  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);}
}

  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)____; // 启动线程}
}

  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"); // 表示晒网}}
}

  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);}
}

  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)____);}
}

  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 + " "); // 显示打开状态的房间编号}}}
}

  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("是素数");}}
}

答案

(1) System.in
(2) t = t / 10(3) int add(int a, int b)
(4) new AddOver()(5) Point
(6) temp
(7) Point s(8) new FileReader(fileName)
(9) line != null
(10) in.readLine()(11) static
(12) (int)(Math.random()*7)
(13) getXh()(14) Runnable
(15) Thread.sleep(1000)
(16) thread.start()(17) a <= 33
(18) b <= 20
(19) c % 3 == 0(20) new FileWriter("D:\\Hello.txt")
(21) fw.write(22) Integer.parseInt(s1)
(23) Double.parseDouble(s2)
(24) String.valueOf(z) 或 z + ""
(25) sbr.append(s4)(26) a.length - 1 - i 或 9 - i
(27) a[j] = a[j + 1]
(28) a[j + 1] = temp(29) i * i % 100 == i
(30) i * i % 1000 == i(31) int sum = 0
(32) i = i + 2 或 i += 2(33) x++
(34) m = m * x 或 m *= x(35) Thread
(36) super(str)
(37) sleep(1500)
(38) myThread.start()(39) i < month 或 i < 5
(40) day > 0 && day <= 3(41) if(n % i == 0)
(42) i++(43) static
(44) (int)(Math.random()*4)
(45) getTel()(46) a[i] = true
(47) j = i
(48) a[j] = !a[j](49) System.in
(50) nextInt()
(51) x % i == 0
(52) flag == 0

更多推荐

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

本文发布于:2023-12-06 07:27:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1666868.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序设计   面向对象   学习笔记   填空题   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!