java 试题练习题(第10套)
Java语言程序设计第10章习题参考答案

习题十参考答案10.1什么是进程?什么是线程?二者有什么联系和区别?进程是程序的一次动态执行过程,它对应了从代码加载、执行到执行完毕的一个完整过程,这个过程也是进程从产生、发展到消亡的过程。
线程是比进程更小的执行单位。
一个进程在其执行过程中,可以产生多个线程,形成多条执行线索。
每条线索,即每个线程也有它自身的产生、存在和消亡的过程,是一个动态的概念。
在进程运行过程中,每个进程都有一段专用的内存区域,并以PCB作为它存在的标志;与进程不同的是,线程间可以共享相同的内存单元(包括代码与数据),并利用这些共享单位来实现数据交换、实时通信与必要的同步操作。
10.2线程有哪些基本状态?试描述它们的相互转换过程。
新建状态、就绪状态、运行状态、阻塞状态、死亡状态。
相互转换的过程如图:10.3创建线程的方式有哪几种?请举例说明。
1.继承自Thread类,代码逻辑写在子线程中,需要重写run()方法,主线程里start()就可以了。
public class ExtendsThread extends Thread{private final static int THREAD_NUM = 5;public static void main(String[] args){for (int i = 0; i <THREAD_NUM; i++) {new ExtendsThread("thread"+i).start();}}public ExtendsThread(String name){super(name);}@Overridepublic void run() {// TODO Auto-generated method stubfor (int i = 0; i < this.THREAD_NUM; i++) { System.out.println(this.getName()+i);}}}运行结果:thread00thread01thread02thread03thread04thread20thread21thread22thread23thread24thread40thread41thread42thread43thread44thread11thread12thread13thread14thread30thread31thread32thread33thread342.实现Runnable接口public class ImplRunnable implements Runnable {private static final int THREAD_NUM = 5;@Overridepublic void run() {for (int i = 0; i < THREAD_NUM; i++) {System.out.println(Thread.currentThread().getName()+i);}}public static void main(String[] args) {// TODO Auto-generated method stubfor (int j = 0; j < THREAD_NUM; j++) {ImplRunnable implRunnable= new ImplRunnable();new Thread(implRunnable,"thread"+j).start();}}}运行结果thread10thread12thread13thread14thread30thread31thread32thread33thread34thread00thread01thread02thread03thread04thread20thread21thread22thread23thread24thread40thread41thread42thread43thread4410.4试说明线程优先级及其调度的关系,并编写一个程序说明其关系。
java 试题练习题(第10套)

大学 —— 学年第 学期 《 Java 程序设计 》课程试题 课程号: √ 考试 □ A 卷 √ 闭卷 □ 考查 □ B 卷 □ 开卷一、单项选择题(20题;每题2分,共40分) 1、Java 语言具有许多的优点和特点,下列的选项___反映了Java 程序并行机制的特点。
A )安全性 B )多线程 C )跨平台 D )可移植 答案: B 知识点: java 基础 难度系数C 2、下列关于JAVA 语言特点的叙述中,错误的是____。
A )Java 是面向过程的编程语言 B )Java 支持分布式计算 C )Java 是跨平台的编程语言 D )Java 支持多线程 答案:A 知识点: java 基础 难度系数C 3、定义私有的成员函数或成员变量,正确的是____。
A )不需要定义,缺省的访问级就是私有级 B )在类的开头部分集中定义 C )成员函数需要定义,而成员变量不需要定义 D )利用private 关键字定义答案: D 知识点: 类成员 难度系数C4、下列语句正确的是________。
A )int a={1,2,3}B )int b=(1,2,3);C )int c[]={1,2,3}D )int []d={1 2 3}(难度系数C )答案:C 知识点:数组班级:姓名: 学号:试题共页加白纸张密封线5、在编写异常处理的Java程序中,每个catch语句块都应该与___语句块对应,使得用该语句块来启动Java的异常处理机制。
A)if – else B)switch C)try D)throw(难度系数B)答案:C知识点:异常6、下列表述中,不正确的是___。
A)标识符区分大小写B)改变变量的值不会改变其存储位置C)常量可以完全大写D)单精度变量赋值常数后面的字母“f”可以省略答案:D知识点:变量难度系数C7、下列陈述正确的是_________。
A)一个组件只能发生一种事件B)一个监听器处理一种事件C)多个事件监听器被注册到一个组件会引起编译错误D)如果多个事件监听器被注册到一个组件上,这些事件监听器一般都会起作用,但各个事件的处理顺序不确定(难度系数A)答案:D知识点:事件处理8、________属于容器的构件。
《Java语言程序设计(基础篇)》(第10版 梁勇 著)第九章练习题答案

《Java语言程序设计(基础篇)》(第10版梁勇著)第九章练习题答案9.1public class Exercise09_01 {public static void main(String[] args) {MyRectangle myRectangle = new MyRectangle(4, 40);System.out.println("The area of a rectangle with width " +myRectangle.width + " and height " +myRectangle.height + " is " +myRectangle.getArea());System.out.println("The perimeter of a rectangle is " +myRectangle.getPerimeter());MyRectangle yourRectangle = new MyRectangle(3.5, 35.9);System.out.println("The area of a rectangle with width " +yourRectangle.width + " and height " +yourRectangle.height + " is " +yourRectangle.getArea());System.out.println("The perimeter of a rectangle is " +yourRectangle.getPerimeter());}}class MyRectangle {// Data membersdouble width = 1, height = 1;// Constructorpublic MyRectangle() {}// Constructorpublic MyRectangle(double newWidth, double newHeight) {width = newWidth;height = newHeight;}public double getArea() {return width * height;}public double getPerimeter() {return 2 * (width + height);}}9.2public class Exercise09_02 {public static void main(String[] args) {Stock stock = new Stock("SUNW", "Sun MicroSystems Inc."); stock.setPreviousClosingPrice(100);// Set current pricestock.setCurrentPrice(90);// Display stock infoSystem.out.println("Previous Closing Price: " +stock.getPreviousClosingPrice());System.out.println("Current Price: " +stock.getCurrentPrice());System.out.println("Price Change: " +stock.getChangePercent() * 100 + "%");}}class Stock {String symbol;String name;double previousClosingPrice;double currentPrice;public Stock() {}public Stock(String newSymbol, String newName) {symbol = newSymbol;name = newName;}public double getChangePercent() {return (currentPrice - previousClosingPrice) /previousClosingPrice;}public double getPreviousClosingPrice() {return previousClosingPrice;}public double getCurrentPrice() {return currentPrice;}public void setCurrentPrice(double newCurrentPrice) {currentPrice = newCurrentPrice;}public void setPreviousClosingPrice(double newPreviousClosingPrice) { previousClosingPrice = newPreviousClosingPrice;}}9.3public class Exercise09_03 {public static void main(String[] args) {Date date = new Date();int count = 1;long time = 10000;while (count <= 8) {date.setTime(time);System.out.println(date.toString());count++;time *= 10;}}}9.4public class Exercise09_04 {public static void main(String[] args) {Random random = new Random(1000);for (int i = 0; i < 50; i++)System.out.print(random.nextInt(100) + " ");}9.5public class Exercise09_05 {public static void main(String[] args) {GregorianCalendar calendar = new GregorianCalendar();System.out.println("Year is " + calendar.get(GregorianCalendar.YEAR)); System.out.println("Month is " + calendar.get(GregorianCalendar.MONTH)); System.out.println("Date is " + calendar.get(GregorianCalendar.DATE));calendar.setTimeInMillis(1234567898765L);System.out.println("Year is " + calendar.get(GregorianCalendar.YEAR)); System.out.println("Month is " + calendar.get(GregorianCalendar.MONTH)); System.out.println("Date is " + calendar.get(GregorianCalendar.DATE)); }}9.6public class Exercise09_06 {static String output = "";/** Main method */public static void main(String[] args) {Scanner input = new Scanner(System.in);// Prompt the user to enter yearSystem.out.print("Enter full year (i.e. 2001): ");int year = input.nextInt();// Prompt the user to enter monthSystem.out.print("Enter month in number between 1 and 12: ");int month = input.nextInt();// Print calendar for the month of the yearprintMonth(year, month);System.out.println(output);}/** Print the calendar for a month in a year */static void printMonth(int year, int month) {// Get start day of the week for the first date in the monthint startDay = getStartDay(year, month);// Get number of days in the monthint numOfDaysInMonth = getNumOfDaysInMonth(year, month);// Print headingsprintMonthTitle(year, month);// Print bodyprintMonthBody(startDay, numOfDaysInMonth);}/** Get the start day of the first day in a month */static int getStartDay(int year, int month) {// Get total number of days since 1/1/1800int startDay1800 = 3;long totalNumOfDays = getTotalNumOfDays(year, month);// Return the start dayreturn (int)((totalNumOfDays + startDay1800) % 7);}/** Get the total number of days since Jan 1, 1800 */static long getTotalNumOfDays(int year, int month) {long total = 0;// Get the total days from 1800 to year -1for (int i = 1800; i < year; i++)if (isLeapYear(i))total = total + 366;elsetotal = total + 365;// Add days from Jan to the month prior to the calendar month for (int i = 1; i < month; i++)total = total + getNumOfDaysInMonth(year, i);return total;}/** Get the number of days in a month */static int getNumOfDaysInMonth(int year, int month) {if (month == 1 || month==3 || month == 5 || month == 7 ||month == 8 || month == 10 || month == 12)return 31;if (month == 4 || month == 6 || month == 9 || month == 11)return 30;if (month == 2)if (isLeapYear(year))return 29;elsereturn 28;return 0; // If month is incorrect.}/** Determine if it is a leap year */static boolean isLeapYear(int year) {if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) return true;return false;}/** Print month body */static void printMonthBody(int startDay, int numOfDaysInMonth) { // Pad space before the first day of the monthint i = 0;for (i = 0; i < startDay; i++)output += " ";for (i = 1; i <= numOfDaysInMonth; i++) {if (i < 10)output += " " + i;elseoutput += " " + i;if ((i + startDay) % 7 == 0)output += "\n";}output += "\n";}/** Print the month title, i.e. May, 1999 */static void printMonthTitle(int year, int month) {output += " " + getMonthName(month)+ ", " + year + "\n";output += "-----------------------------\n";output += " Sun Mon Tue Wed Thu Fri Sat\n";}/** Get the English name for the month */static String getMonthName(int month) {String monthName = null;switch (month) {case 1: monthName = "January"; break;case 2: monthName = "February"; break;case 3: monthName = "March"; break;case 4: monthName = "April"; break;case 5: monthName = "May"; break;case 6: monthName = "June"; break;case 7: monthName = "July"; break;case 8: monthName = "August"; break;case 9: monthName = "September"; break;case 10: monthName = "October"; break;case 11: monthName = "November"; break;case 12: monthName = "December";}return monthName;}}9.7public class Exercise09_07 {public static void main (String[] args) {Account account = new Account(1122, 20000);Account.setAnnualInterestRate(4.5);account.withdraw(2500);account.deposit(3000);System.out.println("Balance is " + account.getBalance()); System.out.println("Monthly interest is " +account.getMonthlyInterest());System.out.println("This account was created at " +account.getDateCreated());}}class Account {private int id;private double balance;private static double annualInterestRate;private java.util.Date dateCreated;public Account() {dateCreated = new java.util.Date();}public Account(int newId, double newBalance) {id = newId;balance = newBalance;dateCreated = new java.util.Date();}public int getId() {return this.id;}public double getBalance() {return balance;}public static double getAnnualInterestRate() {return annualInterestRate;}public void setId(int newId) {id = newId;}public void setBalance(double newBalance) {balance = newBalance;}public static void setAnnualInterestRate(double newAnnualInterestRate) { annualInterestRate = newAnnualInterestRate;}public double getMonthlyInterest() {return balance * (annualInterestRate / 1200);}public java.util.Date getDateCreated() { return dateCreated;}public void withdraw(double amount) {balance -= amount;}public void deposit(double amount) {balance += amount;}}9.8public class Exercise09_08 {public static void main(String[] args) { Fan1 fan1 = new Fan1();fan1.setSpeed(Fan1.FAST);fan1.setRadius(10);fan1.setColor("yellow");fan1.setOn(true);System.out.println(fan1.toString());Fan1 fan2 = new Fan1();fan2.setSpeed(Fan1.MEDIUM);fan2.setRadius(5);fan2.setColor("blue");fan2.setOn(false);System.out.println(fan2.toString()); }}class Fan1 {public static int SLOW = 1;public static int MEDIUM = 2;public static int FAST = 3;private int speed = SLOW;private boolean on = false;private double radius = 5;private String color = "white";public Fan1() {}public int getSpeed() {return speed;}public void setSpeed(int newSpeed) {speed = newSpeed;}public boolean isOn() {return on;}public void setOn(boolean trueOrFalse) {this.on = trueOrFalse;}public double getRadius() {return radius;}public void setRadius(double newRadius) { radius = newRadius;}public String getColor() {return color;}public void setColor(String newColor) {color = newColor;}@Overridepublic String toString() {return"speed " + speed + "\n"+ "color " + color + "\n"+ "radius " + radius + "\n"+ ((on) ? "fan is on" : " fan is off"); }}public class Exercise09_09 {public static void main(String[] args) {RegularPolygon polygon1 = new RegularPolygon();RegularPolygon polygon2 = new RegularPolygon(6, 4);RegularPolygon polygon3 = new RegularPolygon(10, 4, 5.6, 7.8);System.out.println("Polygon 1 perimeter: " +polygon1.getPerimeter());System.out.println("Polygon 1 area: " + polygon1.getArea());System.out.println("Polygon 2 perimeter: " +polygon2.getPerimeter());System.out.println("Polygon 2 area: " + polygon2.getArea());System.out.println("Polygon 3 perimeter: " +polygon3.getPerimeter());System.out.println("Polygon 3 area: " + polygon3.getArea());}}class RegularPolygon {private int n = 3;private double side = 1;private double x;private double y;public RegularPolygon() {}public RegularPolygon(int number, double newSide) {n = number;side = newSide;}public RegularPolygon(int number, double newSide, double newX, double newY) {n = number;side = newSide;x = newX;y = newY;}public int getN() {return n;}public void setN(int number) {n = number;}public double getSide() {return side;}public void setSide(double newSide) {side = newSide;}public double getX() {return x;}public void setX(double newX) {x = newX;}public double getY() {return y;}public void setY(double newY) {y = newY;}public double getPerimeter() {return n * side;}public double getArea() {return n * side * side / (Math.tan(Math.PI / n) * 4); }}9.10public class Exercise09_10 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter a, b, c: ");double a = input.nextDouble();double b = input.nextDouble();double c = input.nextDouble();QuadraticEquation equation = new QuadraticEquation(a, b, c);double discriminant = equation.getDiscriminant();if (discriminant < 0) {System.out.println("The equation has no roots");}else if (discriminant == 0){System.out.println("The root is " + equation.getRoot1());}else// (discriminant >= 0){System.out.println("The roots are " + equation.getRoot1()+ " and " + equation.getRoot2());}}}class QuadraticEquation {private double a;private double b;private double c;public QuadraticEquation(double newA, double newB, double newC) {a = newA;b = newB;c = newC;}double getA() {return a;}double getB() {return b;}double getC() {return c;}double getDiscriminant() {return b * b - 4 * a * c;}double getRoot1() {if (getDiscriminant() < 0)return 0;else {return (-b + getDiscriminant()) / (2 * a);}}double getRoot2() {if (getDiscriminant() < 0)return 0;else {return (-b - getDiscriminant()) / (2 * a);}}}9.11public class Exercise09_11 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter a, b, c, d, e, f: ");double a = input.nextDouble();double b = input.nextDouble();double c = input.nextDouble();double d = input.nextDouble();double e = input.nextDouble();double f = input.nextDouble();LinearEquation equation = new LinearEquation(a, b, c, d, e, f);if (equation.isSolvable()) {System.out.println("x is " +equation.getX() + " and y is " + equation.getY());}else {System.out.println("The equation has no solution");}}}class LinearEquation {private double a;private double b;private double c;private double d;private double e;private double f;public LinearEquation(double newA, double newB, double newC, double newD, double newE, double newF) {a = newA;b = newB;c = newC;d = newD;e = newE;f = newF;}double getA() {return a;}double getB() {return b;}double getC() {return c;}double getD() {return d;}double getE() {return e;}double getF() {return f;}boolean isSolvable() {return a * d - b * c != 0;}double getX() {double x = (e * d - b * f) / (a * d - b * c);return x;}double getY() {double y = (a * f - e * c) / (a * d - b * c);return y;}}9.12public class Exercise09_12 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter the endpoints of the first line segment: ");double x1 = input.nextDouble();double y1 = input.nextDouble();double x2 = input.nextDouble();double y2 = input.nextDouble();System.out.print("Enter the endpoints of the second line segment: ");double x3 = input.nextDouble();double y3 = input.nextDouble();double x4 = input.nextDouble();double y4 = input.nextDouble();// Build a 2 by 2 linear equationdouble a = (y1 - y2);double b = (-x1 + x2);double c = (y3 - y4);double d = (-x3 + x4);double e = -y1 * (x1 - x2) + (y1 - y2) * x1;double f = -y3 * (x3 - x4) + (y3 - y4) * x3;LinearEquation equation = new LinearEquation(a, b, c, d, e, f);if (equation.isSolvable()) {System.out.println("The intersecting point is: (" +equation.getX() + ", " + equation.getY() + ")");}else {System.out.println("The two lines do not cross ");}}}9.13public class Exercise09_13 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter the number of rows and columns of the array: ");int numberOfRows = input.nextInt();int numberOfColumns = input.nextInt();double[][] a = new double[numberOfRows][numberOfColumns];System.out.println("Enter the array: ");for (int i = 0; i < a.length; i++)for (int j = 0; j < a[i].length; j++)a[i][j] = input.nextDouble();Location location = locateLargest(a);System.out.println("The location of the largest element is " + location.maxValue + " at ("+ location.row + ", " + location.column + ")");}public static Location locateLargest(double[][] a) {Location location = new Location();location.maxValue = a[0][0];for (int i = 0; i < a.length; i++)for (int j = 0; j < a[i].length; j++) {if (location.maxValue < a[i][j]) {location.maxValue = a[i][j];location.row = i;location.column = j;}}return location;}}class Location {int row, column;double maxValue;}9.14public class Exercise09_14 {public static void main(String[] args) {int size = 100000;double[] list = new double[size];for (int i = 0; i < list.length; i++) {list[i] = Math.random() * list.length;}StopWatch stopWatch = new StopWatch();selectionSort(list);stopWatch.stop();System.out.println("The sort time is " + stopWatch.getElapsedTime()); }/** The method for sorting the numbers */public static void selectionSort(double[] list) {for (int i = 0; i < list.length - 1; i++) {// Find the minimum in the list[i..list.length-1]double currentMin = list[i];int currentMinIndex = i;for (int j = i + 1; j < list.length; j++) {if (currentMin > list[j]) {currentMin = list[j];currentMinIndex = j;}}// Swap list[i] with list[currentMinIndex] if necessary;if (currentMinIndex != i) {list[currentMinIndex] = list[i];list[i] = currentMin;}}}}class StopWatch {private long startTime = System.currentTimeMillis(); private long endTime = startTime;public StopWatch() {}public void start() {startTime = System.currentTimeMillis();}public void stop() {endTime = System.currentTimeMillis();}public long getElapsedTime() {return endTime - startTime;}}。
【免费下载】java 试题练习题(第10套)

3、定义私有的成员函数或成员变量,正确的是____。
A)不需要定义,缺省的访问级就是私有级 B)在类的开头部分集中定义
C)成员函数需要定义,而成员变量不需要定义 D)利用private 关键字定
答案: D 知识点: 类成员
4、下列语句正确的是________。
A)int a={1,2,3}
C)int c[]={1,2,3}
(难度系数 A,重载) D
18、String s1 = new String(“Hello”);
String s2 = new String(“there”);
String s3 = new String();
D)static 方法中能处理非 static 的
知识点:关键字
上面是 Java 程序中的一些声明,选项中能通过编译的是______。
机制的特点。
义
A)安全性 B)多线程 C)跨平台 D)可移植
答案: B 知识点: java基础
2、下列关于 JAVA 语言特点的叙述中,错误的是____。
A)Java 是面向过程的编程语言 B)Java 支持分布式计算
C)Java 是跨平台的编程语言 D)Java 支持多线程
答案:A 知识点: java 基础
用,但各个事件的处理顺序不确定
(难度系数 A)答案:D 知识点:事件处理
8、________属于容器的构件。
A)JFrame B)JButton
C)JPnel
D)JApplet
答案:A;难度:C;知识点:窗体与控件。
9、如果希望所有的控件在界面上均匀排列,应使用_____布局管理器。
A)BoxLayout B)GridLayout
Java语言程序设计(基础篇)第10版习题答案Chapter9-1

Java语⾔程序设计(基础篇)第10版习题答案Chapter9-1(矩形类 Rectangle)遵照9.2节中 Circle 类的例⼦,设计⼀个名为 Rectangle 的类表⽰矩形。
这个类包括:两个名为 width 和 height 的 double 型数据域,它们分别表⽰矩形的宽和⾼。
width 和 height 的默认值都为1。
创建默认矩形的⽆参构造⽅法⼀个创建 width 和 height 为指定值的矩形的构造⽅法。
⼀个名为 getArea() 的⽅法返回这个矩形的⾯积。
⼀个名为 getPerimeter() 的⽅法返回周长。
画出该矩形的 UML 图并实现这个类。
编写⼀个测试程序,创建两个 Rectangle 对象——⼀个矩形的宽为 4 ⽽⾼为 40,另⼀个矩形的宽为3.5 ⽽⾼为 35.9 。
按照这个顺序显⽰每个矩形的宽、⾼、⾯积、周长。
程序代码:public class Rectangle {private double width;private double height;public Rectangle(){width=1.0;height=1.0;}public Rectangle(double width,double height) {this.width=width;this.height=height;}public double getWidth() {return width;}public double getHeight() {return height;}public double getArea() {return width*height;}public double getPerimeter() {return 2*(width+height);}public static void main(String[] args) {// TODO Auto-generated method stubRectangle t1=new Rectangle(4,40);Rectangle t2=new Rectangle(3.5,35.9);System.out.println("width:"+t1.getWidth()+" height:"+t1.getHeight()+" Area:"+t1.getArea()+" Per:"+t1.getPerimeter());System.out.println("width:"+t2.getWidth()+" height:"+t2.getHeight()+" Area:"+t2.getArea()+" Per:"+t2.getPerimeter());}}运⾏结果:width:4.0 height:40.0 Area:160.0 Per:88.0width:3.5 height:35.9 Area:125.64999999999999 Per:78.8。
java语言程序设计基础篇第十版练习答案精编

j a v a语言程序设计基础篇第十版练习答案精编Document number:WTT-LKK-GBB-08921-EIGG-2298601import class Exercise14_01 extendsApplication {@Override Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}02import class Exercise14_02 extends Application {@Override Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}03import class Exercise14_03 extends Application {@Override One is to use the hint in the book.ArrayList<Integer> list = new ArrayList<>(); for (int i = 1; i <= 52; i++) {(i);}HBox pane = new HBox(5);;().add(new ImageView("image/card/" + (0) +".png"));().add(new ImageView("image/card/" + (1) +".png"));().add(new ImageView("image/card/" + (2) +".png"));Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}04import class Exercise14_04 extends Application {@Override dd(txt);}Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}05import class Exercise14_05 extends Application {@Override dd(txt);}Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}05import class Exercise14_05 extends Application {@Override dd(txt);}Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}06import class Exercise14_06 extends Application {@Override dd(rectangle);}}Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}07import class Exercise14_07 extends Application {@Override Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}08import class Exercise14_08 extends Application {@Override ng"), j, i);}}Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}09import class Exercise14_09 extends Application {@Override Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}class FanPane extends Pane {double radius = 50;public FanPane() {Circle circle = new Circle(60, 60, radius);;;getChildren().add(circle);Arc arc1 = new Arc(60, 60, 40, 40, 30, 35);; ddAll(arc1, arc2, arc3, arc4);}}10import class Exercise14_10 extends Application {@Override ddAll, ;Arc arc2 = new Arc(100, 140, 50, 20, 180, 180); ;;().addAll(ellipse, arc1, arc2,new Line(50, 40, 50, 140), new Line(150, 40, 150, 140));Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}11import class Exercise14_11 extends Application {@Override ddAll(circle, ellipse1, ellipse2,circle1, circle2, line1, line2, line3, arc);Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}12import class Exercise14_12 extends Application {@Override ddAll(r1, text1, r2, text2, r3, text3, r4, text4);Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}13import class Exercise14_13 extends Application {@Override ddAll(arc1, text1, arc2, text2, arc3, text3, arc4, text4);Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}14import class Exercise14_14 extends Application {@Override ddAll(r1, r2, line1, line2, line3, line4);Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}15import class Exercise14_15 extends Application {@Override ddAll(polygon, text);Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}16import class Exercise14_16 extends Application {@Override ind().divide(3));().bind());().bind().divide(3));;Line line2 = new Line(0, 0, 0, 0);().bind().multiply(2).divide(3));().bind());().bind().multiply(2).divide(3));;Line line3 = new Line(0, 0, 0, 0);().bind().divide(3));().bind().divide(3));().bind());;Line line4 = new Line(0, 0, 0, 0);().bind().multiply(2).divide(3));().bind().multiply(2).divide(3));().bind());;().addAll(line1, line2, line3, line4);Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}17import class Exercise14_17 extends Application {@Override ddAll(arc, line1, line2, line3, circle, line4, line5, line6, line7, line8);Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}18import class Exercise14_18 extends Application {@Override ddAll(polyline, line1, line2,line3, line4, line5, line6, text1, text2);Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}19import class Exercise14_19 extends Application {@Override ddAll(polyline1, polyline2, line1,line2,line3, line4, line5, line6, text1, text2,text3,text4, text5, text6, text7);Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}20import class Exercise14_20 extends Application {@Override dd(new Line(x1, y1, x2, y2));dd(new Line(x2, y2, (x2 + (arctan + set45) * arrlen)),((y2)) + (arctan + set45) * arrlen)));().add(new Line(x2, y2, (x2 + (arctan - set45) * arrlen)),((y2)) + (arctan - set45) * arrlen)));}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}21import class Exercise14_21 extends Application {@Override istance(x2, y2) + "");().addAll(circle1, circle2, line, text);Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}22import class Exercise14_22 extends Application {@Override ddAll(circle1, circle2, line, text1, text2);Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}23import class Exercise14_23 extends Application {@Override ddAll(r1, r2, text);Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}24import class Exercise14_24 extends Application {@Override ddAll(polygon, new Circle(x5, y5, 10), text);Not needed for running from the command line. */public static void main(String[] args) {launch(args);}25import class Exercise14_25 extends Application {@Override ddAll(circle, polygon);Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}26import class Exercise14_26 extends Application {@Override ddAll(clock1, clock2);Not needed for running from the command line. */public static void main(String[] args) {launch(args);}27import class Exercise14_27 extends Application {@OverrideNot needed for running from the command line. */public static void main(String[] args) {launch(args);}}class DetailedClockPane extends Pane {private int hour;private int minute;private int second;lear();getChildren().addAll(circle, sLine, mLine, hLine);dd(new Line(xOuter, yOuter, xInner, yInner));}dd(text);}}}28import class Exercise14_28 extends Application {@OverrideNot needed for running from the command line.*/public static void main(String[] args) {launch(args);}}class ClockPaneWithBooleanProperties extends Pane { private int hour;private int minute;private int second;private boolean hourHandVisible = true;private boolean minuteHandVisible = true; private boolean secondHandVisible = true;public boolean isHourHandVisible() {return hourHandVisible;}public void setHourHandVisible(boolean hourHandVisible) {= hourHandVisible;paintClock();}public boolean isMinuteHandVisible() {return minuteHandVisible;}public void setMinuteHandVisible(boolean minuteHandVisible) {= minuteHandVisible;paintClock();}public boolean isSecondHandVisible() {return secondHandVisible;public void setSecondHandVisible(boolean secondHandVisible) {= secondHandVisible;paintClock();}lear();getChildren().addAll(circle, t1, t2, t3, t4);if (secondHandVisible) {getChildren().add(sLine);}if (minuteHandVisible) {getChildren().add(mLine);}if (hourHandVisible) {getChildren().add(hLine);}}}import class Exercise14_29 extends Application {final static double HGAP = 20;final static double VGAP = 20;final static double RADIUS = 5;final static double LENGTH_OF_SLOTS = 40;final static double LENGTH_OF_OPENNING = 15;final static double Y_FOR_FIRST_NAIL = 50;final static double NUMBER_OF_SLOTS = 9;final static double NUMBER_OF_ROWS =NUMBER_OF_SLOTS - 2;@Override dd(c);}}dd(new Line(x, y, x, y + LENGTH_OF_SLOTS)); }dd(new Line(centerX - (NUMBER_OF_ROWS - 1) * HGAP / 2 - HGAP,y + LENGTH_OF_SLOTS, centerX -(NUMBER_OF_ROWS - 1) * HGAP / 2 + NUMBER_OF_ROWS * HGAP,y + LENGTH_OF_SLOTS));dd(new Line(centerX + HGAP / 2,Y_FOR_FIRST_NAIL + RADIUS,centerX - (NUMBER_OF_ROWS - 1) * HGAP / 2 + NUMBER_OF_ROWS * HGAP, y));().add(new Line(centerX - HGAP / 2,Y_FOR_FIRST_NAIL + RADIUS,centerX - (NUMBER_OF_ROWS - 1) * HGAP / 2 - HGAP, y));dd(new Line(centerX - HGAP / 2,Y_FOR_FIRST_NAIL + RADIUS,centerX - HGAP / 2, Y_FOR_FIRST_NAIL - LENGTH_OF_OPENNING));().add(new Line(centerX + HGAP / 2,Y_FOR_FIRST_NAIL + RADIUS,centerX + HGAP / 2, Y_FOR_FIRST_NAIL - LENGTH_OF_OPENNING));Not needed for running from the command line.*/public static void main(String[] args) { launch(args);}}。
10级Java 试卷A

else if (s.substring(0,3).equals("abc")){
System.out.println(s.substring(3));
}
else {
System.out.println(s);
}
}
in.close();
}catch(FileNotFoundException e) {
1.以下程序的输出结果为_____。(5分)
public class mysort
{ public static void main(String args[ ]){
int i, j, k, temp;
int x[]= {1,5,3,4,2,7};
for( i= 0; i<x.length-1; i++ ) {
A. ResultsetB. ResultSet
C. ResultD.以上都不是
13.启动一个线程的方法是()。
A. run()B. start()
C. new()D. await()
14.加载某个数据库驱动程序的JDBC语句是()。
A.Class.forName()
B.DriverManager.getDrivers()
10.定义一个接口时,可以用以下哪个修饰符修饰成员?()。
A.publicB.protectedC.privateD.private或public
11.程序demo.java代码如下,其中正确的是()。
public person {}
class student extends person {}
public class demo {
()8.若一个类实现了某个接口,则一定实现了其超接口。
Java语言程序设计(基础篇)(第10版 梁勇著)第一章练习题答案

System.out.println("people of thirdYear = " + thirdYear); System.out.println("people of fourthYear = " + fourthYear); System.out.println("people of fifthYear = " + fifthYear); } }
// 第一章 P26 编程练习题1.1和1.2 (显示三条消息/显示五条消息) public class WelcomeWithThreeMessages { public static void main(String[] args) { System.out.println("Welcome to Java"); System.out.println("Welcome to Computer Science"); System.out.println("Programming is fun"); System.out.println("Welcome System.out.println("Welcome System.out.println("Welcome System.out.println("Welcome System.out.println("Welcome } } to to to to to Java"); Java"); Java"); Java"); Java");
// 第一章 P27 练习题1.10 (以英里计的平均速度) public class AverageSpeed { public static void main(String[] args) { // 45.5分钟等于45分钟30秒 double speedkm = 60 / (45.5 / 14); double speedm = speedkm / 1.6; // m/h代表英里/每小时 System.out.println("averagespeed = " + speedm + "m/h");
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
(难度系数 B)答案:对 知识点:内存回收 6.( ) abstract 和 final 不能同时修饰同一个类。
答案 对 难度 C 知识点:类的定义,修饰
7.( )访问类的成员,必须以类的对象为前缀。 答案 错 难度 C 知识点:类的封装,访问
8.( )诊断和改正程序中错误的工作通常称为程序执行。 答案:错;难度:C;知识点:编程基础。
1、Java 应用程序总是从主类的
方法开始执行。
知识点: 类概念
难度系数 C 答案
main
2、在 Java 中若定义抽象类则需要加关键字
来修饰。
答案:abstract ;难度:C;知识点:编程基础。
3、在子类中定义与父的相同的方法,若在多个子类中定义相同的方法,则可以调
用不同子类中的相同方法而实现不同的功能,这实现了程序运行时
知识点:异常处理
4. ( )Java 是区分大小写的语言,关键字的大小写不能搞错,如果把类 class 写成 Class 或者 CLASS,都是错误的。
(难度系数 B)答案:对 知识点:关键字
5.( )JAVA 语言对内存的释放是采用垃圾自动回收机制。JAVA 虚拟机自动判 断并收集“垃圾”,但一般不立即释放它们的存储空间。
答案:应用(Application)、小程序(Applet);难度:C;知识点:java 特性。
8、在 Java 程序中,通过类的定义只能实现
重继承,但通过接口的定
义可以实现
重继承关系。
答案:单、多;难度:B;知识点:java 特性。
9、字符串分为两大类,一类是字符串常量,使用
类的对象表示;
另一类是字符串变量,使用
大学 —— 学年第 学期
班 级
《 Java 程序设计 》课程试题
:
姓
名
密
:
课程号:
√考试 □考查
□A 卷 □B 卷
√闭卷 □开卷
题 号 一 二 三 四 五 六 七 八 九 十 总分 阅卷教师
各题分数 40 20 10 5 5 20
100
实得分数
学
号
:
封
一、单项选择题(20 题;每题 2 分,共 40 分)
答案: D 知识点: 类成员
难度系数C
4、下列语句正确的是________。
A)int a={1,2,3}
B)int b=(1,2,3);
C)int c[]={1,2,3}
D)int []d={1 2 3}
(难度系数 C)答案:C 知识点:数组
5、在编写异常处理的 Java 程序中,每个 catch 语句块都应该与___语句块对
B) 线程 D) 服务
20、在 Java Applet 程序用户自定义的 Applet 子类中,一般需要重载父类的_____
方法来完成一些画图操作。
A) start( )
B) stop( )
C) init( )
D) paint( )
(难度系数 A,APPLET) 答案:D
二、填空题(10 题;每题 2 分,共 20 分)
的
。
答案:多态;难度:C;知识点:面向对象基础。
4、用来定义一个类指定继承父类的关键字是
,用来指定接口的
继承的关键字是
。答案:extends 、implements;难度:C;知识点:
语法基础。
5、java 提供的两种多态机制是
和
。.
答案 重载 、覆盖 难度(B) 知识点:多态机制
6、在 Java 程序运行时,系统自动通过 System 类创建三个静态的 I/O 对象,它们
public static void main(String args[ ]){ int i , j ; int a[ ] = { 5,9,6,8,7}; for ( i = 0 ; i < a.length-1; i ++ ) { int k = i; for ( j = i ; j < a.length ; j++ )
}
}
难度系数 C
知识点:JAVA 类与对象
答案:(1)int chinese(2)int math(3)return chinese+math+English
五、读程序写结果(1 题;每题 5 分,共 5 分)
1.写出下列程序在控制台窗口中的输出结果。
public class TestArray {
页
C)Java 是跨平台的编程语言
D)Java 支持多线程
加
答案:A 知识点: java 基础
难度系数 C
白 纸
3、定义私有的成员函数或成员变量,正确的是____。
A)不需要定义,缺省的访问级就是私有级 B)在类的开头部分集中定义
张
C)成员函数需要定义,而成员变量不需要定义 D)利用private 关键字定义
13、在 Java 中,_______表示换行符的转义字符。
A)\n
B)\f
C)'n'
D)\dd
答案:A;难度:C;知识点:语法基础。
14、在 Java 中,所有类的根类是________。
A)ng.Object B)ng.Class
C)java.applet.Applet D)java.awt.Fram
A) abstract 不能与 final 并列修饰同一个类 B) abstract 类中不可以有 private
的成员
C)abstract 方法必须在 abstract 类中
D)static 方法中能处理非 static 的
属性
答案: D 难度系数: A
知识点:关键字
17、下面___函数是 public void aMethod(){...}的重载函数。 A) void aMethod( ){...} B) public int aMethod(){...} C) public void aMethod ( ){...} D) public int aMethod ( int m){...}
} }
答案:5 6
if ( a[j]<a[k] ) k = j; int temp =a[i]; a[i] = a[k]; a[k] = temp; } for ( i =0 ; i<a.length; i++ )
System.out.print(a[i]+" "); System.out.println( );
类的对象表示。
答案:String、StringBuffer;难度:B;知识点:变量类型。
10、在定义类时,指明类成员的权限修饰符有
、
和
。
答案 public protected private 难度 B 知识点:类的定义
三、判断题(10 题;每题 1 分,共 10 分)
1. ( ) Java语言属于高级程序设计语言。
9. ( ) 即使一个类中未显式定义构造函数,也会有一个缺省的构造函数,缺省
的构造函数是无参的,函数体为空。
答案:对
难度系数: C
知识点:构造函数
10. ( )用 Javac 编译 Java 源文件后得到代码叫字节码。 (难度系数 B)答案:错 知识点:JAVA 简介
四、程序填空题(1 题;每题 5 分,共 5 分)
答案:对 知识点: java 基础
难度系数 C
2. ( )字符串 "\'a\'" 的长度是 5。 答案:错;难度:B;知识点:语法基础
3.( ) 在异常处理中总是将可能产生异常的语句放在 try 块中,用 catch 子句
去处理异常,而且一个 try 块之后只能对应一个 catch 语句。
答案:错
难度系数: B
答案:A;难度:C;知识点:编程基础。
15、_________事件监听器可以处理在文本框中输入回车键的事件
A)ItemListener
B)ActionListener
C)MotionListener
D)AdjustListener
(难度系数 B)答案:B 知识点:事件处理
16、下列关于修饰符混用的说法,错误的是___。
1.下面程序是定义一个 Student 类,在空白处填入适当语句补充完整。
class student
{
String name;
int age;
(1)
;//定义一个整型属性 chinese
(2) ;//定义一个整型属性 math
int english;
int total()
{
(3)
;//返回 chinese、math 和 english3 个整型属性的总和。
答案: D 知识点: 变量
难度系数C
7、下列陈述正确的是_________。
A) 一个组件只能发生一种事件 B)一个监听器处理一种事件
C)多个事件监听器被注册到一个组件会引起编译错误
D) 如果多个事件监听器被注册到一个组件上,这些事件监听器一般都会起作
用,但各个事件的处理顺序不确定
(难度系数 A)答案:D 知识点:事件处理
答案:B;难度:B;知识点:窗体与控件。
10、下列关于构造方法的叙述中,错误的是____。
A)Java 语言规定构造方法名与类名必须相同
B)Java 语言规定构造方法没有返回值,但不用 void 声明
C)Java 语言规定构造方法不可以重载
D)Java 语言规定构造方法只能通过 new 自动调用
答案:C 知识点:类
应,使得用该语句块来启动 Java 的异常处理机制。
A) if –y
D) throw