学习网站建设有什么用,为食堂写个网站建设,百度推广怎么使用教程,龙岩网站设计 信任推商吧做词笔记汇总#xff1a;《Java面向对象程序设计》学习笔记
这些题其实都非常滴简单#xff0c;相信大伙能够立刻就秒了吧#x1f60e; 文章目录 题目答案 题目
以下程序要求从键盘输入一个整数#xff0c; 判别该整数为几位数#xff0c; 并且输出结果#xff0c; 请将下…笔记汇总《Java面向对象程序设计》学习笔记
这些题其实都非常滴简单相信大伙能够立刻就秒了吧 文章目录 题目答案 题目
以下程序要求从键盘输入一个整数 判别该整数为几位数 并且输出结果 请将下面的程序填写完整。
public class Blank1 {public static void main(String[] args) throws IOException {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 位数);}
}在下面的程序中使用方法重载分别实现了两个和三个整数的相加请将下面的程序填写完整。
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));}
} 构造一个类来描述一个点该类的构成包括点的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);}
}下面的程序完成从 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);}}
}下面的程序通过方法调用从包含 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)____);}
}下面的程序定义了一个线程 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)____; // 启动线程}}
}下面的程序对“百鸡百钱”问题进行了求解公鸡每只 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);}}}}
}下面的程序使用 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);}
}根据程序注释提示将下面的程序填写完整。
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}
}下面的程序是采用冒泡法对数组元素按小到大的顺序排序请将程序填写完整。
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] );}}
}“同构数”是指这样的整数它恰好出现在其平方数的右端例如 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-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);}
}完成求 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);}
}下面的程序定义了一个线程 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)____; // 启动线程}
}中国有句俗语“三天打鱼两天晒网”某人从 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); // 表示晒网}}
}调用函数 f 输出 n 的所有质数因子如 n13860 则输出 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);}
}下面的程序通过方法调用从包含 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)____);}
}宾馆里有 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 ); // 显示打开状态的房间编号}}}
}以下程序要求从键盘输入一整数判别该整数是否是素数并输出“是素数或“不是素数“请将程序填写完整。
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) tt/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%i0)
(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