java语言程序设计基础篇第十章第三题参看答案
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语言程序设计基础篇第十版课后答案
第一章1.1public class Test{public static void main(String[]args){System.out.println("Welcome to Java!"); System.out.println("Welcome to Computer Science!");System.out.println("Progr amming is fun.");}}1.2public class Test{public static void main(String[]args){for(int i=0;i<=4;i++){System.out.println("Welcome to Java!");}}}1.3public class Test{public static void main(String[]args){System.out.println("]");System.out.printl n("]");System.out.println("]]");System.out.println("]]");}}public class Test{public static void main(String[]args){System.out.println("A"); System.out.println("A A");System.out.println("AAAAA");System.out.println("A A");}}public class Test{public static void main(String[]args){System.out.println("V V");System.out.println("V V");System.out.println("V V");System.out.println(" V");}}1.4public class Test{public static void main(String[]args){System.out.println("a a^2a^3");System.out.println("111");System.out.println("248");System.out.println("3 927");System.out.println("41664");}}1.5public class Test{public static void main(String[]args){System.out.println((9.5*4.5-2.5*3)/(45.5-3.5) );}}1.6public class Test{public static void main(String[]args){int i=1,sum=0;for(;i<=9;i++)sum+ =i;System.out.println(sum);}1.7public class Test{public static void main(String[]args){System.out.println(4*(1.0-1.0/3+1.0/5-1.0/7+1.0/9-1.0/11));System.out.println(4*(1.0-1.0/3+1.0/5-1.0/7+1.0/9-1.0/11+1.0/13));}}1.8public class Test{public static void main(String[]args){final double PI=3.14; double radius=5.5;System.out.println(2*radius*PI);System.out.println(PI*radius*radius);}}1.9public class Test{public static void main(String[]args){System.out.println(7.9*4.5);System.out.p rintln(2*(7.9+4.5));}}1.10public class Test{public static void main(String[]args){double S=14/1.6;double T=45*60+30;double speed=S/T;System.out.println(speed);}1.11public class Test{public static void main(String[]args){int BN=312032486; //original person numbers double EveryYS,EveryYBP,EveryYDP,EveryYMP;EveryY S=365*24*60*60;EveryYBP=EveryYS/7;EveryYDP=EveryYS/13;Every YMP=EveryYS/45;int FirstYP,SecondYP,ThirdYP,FourthYP,FivthYP;FirstYP=(int)(BN+EveryYBP+EveryYMP-EveryYDP);SecondYP=(int)(FirstYP +EveryYBP+EveryYMP-EveryYDP);ThirdYP=(int)(SecondYP+EveryYBP+Ev eryYMP-EveryYDP);FourthYP=(int)(ThirdYP+EveryYBP+EveryYMP-EveryYD P);FivthYP=(int)(FourthYP+EveryYBP+EveryYMP-EveryYDP);System.out.pri ntln(FirstYP);System.out.println(SecondYP);System.out.println(ThirdYP);Syste m.out.println(FourthYP);System.out.println(FivthYP);}}1.12public class Test{public static void main(String[]args){double S=24*1.6; double T=(1*60+40)*60+35;double speed=S/T;System.out.println(sp eed);}}1.13import java.util.Scanner;public class Test{public static void main(String[]args){Scanner input=new Scan ner(System.in);System.out.println("input a,b,c,d,e,f value please:");double a=input.nextDouble();double b=input.nextDouble();double c=input.nextDouble();double d=input. nextDouble();double e=input.nextDouble();第二章package cn.Testcx;import java.util.Scanner;public class lesson2{public static void main(String[]args){@SuppressWarnings("resource")Scanner in put=new Scanner(System.in);System.out.print("请输入一个摄氏温度:");double Celsius=input.nextDouble();double Fahrenheit=(9.0/5)*Celsius+3 2;System.out.println("摄氏温度:"+Celsius+"度"+"转换成华氏温度为:"+Fahrenheit+"度");System.out.print("请输入圆柱的半径和高:");double radius=input.nextDouble();int higth=input.nextInt();double are as=radius*radius*Math.PI;double volume=areas*higth;System.out.println("圆柱体的面积为:"+areas);System.out.println("圆柱体的体积为:"+volume);System.out.print("输入英尺数:");double feet=input.nextDouble();double meters=feet*0.305;System.out.print ln(feet+"英尺转换成米:"+meters);System.out.print("输入一个磅数:");double pounds=input.nextDouble();double kilograms=pounds*0.454;Syste m.out.println(pounds+"磅转换成千克为:"+kilograms);System.out.println("输入分钟数:");long minutes=input.nextInt();long years=minutes/(24*60*365);long days=(minutes%(24*60*365))/(24*60);System.out.println(minutes+"分钟"+"有"+years+"年和"+days+"天");long totalCurrentTimeMillis=System.currentTimeMillis();long totalSeconds=t otalCurrentTimeMillis/1000;long currentSeconds=totalSeconds%60;long totalM inutes=totalSeconds/60;long currentMinutes=(totalSeconds%(60*60))/60;long currenthours=(totalMinutes/60)%24;System.out.print("输入时区偏移量:");byte zoneOffset=input.nextByte();long currentHour=(currenthours+(zoneOf fset*1))%24;System.out.println("当期时区的时间为:"+currentHour+"时"+currentMinutes+"分"+currentSeconds+"秒");System.out.print("请输入v0,v1,t:");double v0=input.nextDouble();double v1=input.nextDouble();doublet=input.nextDouble();float a=(float)((v1-v0)/t);System.out.println("平均加速度a="+a);System.out.println("输入水的重量、初始温度、最终温度:");double water=input.nextDouble();double initialTemperature=input.nextDou ble();double finalTemperature=input.nextDouble();double Q=water*(finalTemp erature-initialTemperature)*4184;System.out.println("所需热量为:"+Q);System.out.print("输入年数:");int numbers=input.nextInt();long oneYearsSecond=365*24*60*60;Longpop ulation=(long)((312032486+((oneYearsSecond/7.0)+(oneYearsSecond/45.0)-(oneYearsSecond/13.0))*numbers));System.out.println("第"+numbers+"年后人口总数为:"+population);System.out.print("输入速度单位m/s和加速度a单位m/s2:");double v=input.nextDouble();double a1=input.nextDouble();double l engthOfAirplane=(Math.pow(v,2))/(2*a1);System.out.println("最短长度为:"+lengthOfAirplane);System.out.print("输入存入的钱:");double money=input.nextInt();double monthRate=5.0/1200;for(int i=1;i<7; i++){double total=money*(Math.pow(1+monthRate,i));System.out.println("第"+i+"个月的钱为:"+total);//告诉我书上的银行在哪里,我要去存钱,半年本金直接翻6倍、、、}System.out.print("用户请输入身高(英寸)、体重(磅):");double height=input.nextDouble();double weight=input.nextDouble(); double BMI=(weight*0.45359237)/(Math.pow((height*0.0254),2));System.out.println("BMI的值为"+BMI);System.out.print("输入x1和y1:");System.out.print("输入x2和y2:");double x1=input.nextDouble();double y1=input.nextDouble();double x2 =input.nextDouble();double y2=input.nextDouble();double point1=Math.pow((x2-x1),2);double point2=Math.pow((y2-y1),2);double distance=Math.pow((point1+point2),(1.0/2));//也可以Math.pow((point1+point2),0.5)System.out.println("两点间的距离为:"+distance);System.out.print("输入六边形的边长:");double side=input.nextDouble();double area=(3*(Math.pow(3,0.5))*(Math.p ow(side,2)))/2;System.out.println("六边形的面积为:"+area);}}。
java各章习题答案
java各章习题答案Java各章习题答案在学习Java编程语言的过程中,习题是非常重要的一部分。
通过习题,我们可以巩固所学的知识,加深对语言特性和用法的理解。
在每一章的学习结束后,我们都会遇到一系列的习题,这些习题涵盖了本章的知识点,帮助我们检验自己的掌握程度。
下面我们来看一下Java各章习题的答案。
第一章:Java基础知识1.1 什么是Java编程语言?Java是一种面向对象的编程语言,由Sun Microsystems公司于1995年推出。
它具有跨平台性和安全性等特点。
1.2 Java程序的基本结构是什么?Java程序的基本结构包括包名、导入语句、类声明、主方法和其他方法。
1.3 Java中的数据类型有哪些?Java中的数据类型包括基本数据类型和引用数据类型。
基本数据类型包括整型、浮点型、字符型和布尔型。
1.4 Java中的控制语句有哪些?Java中的控制语句包括if语句、switch语句、while循环、for循环等。
第二章:面向对象编程2.1 什么是面向对象编程?面向对象编程是一种程序设计范式,它将数据和方法封装在对象中,通过对象之间的交互来实现程序的功能。
2.2 Java中的类和对象有什么区别?类是一种抽象的概念,它用来描述对象的属性和行为;对象是类的实例,它具体描述了类的属性和行为。
2.3 Java中的继承和多态是什么?继承是一种类与类之间的关系,它允许一个类继承另一个类的属性和方法;多态是一种对象的多种形态,它允许同一个方法在不同的对象上具有不同的行为。
2.4 Java中的封装和抽象是什么?封装是一种将数据和方法封装在对象中的机制,它可以保护数据的安全性;抽象是一种将对象的共性特征提取出来的机制,它可以提高代码的复用性。
第三章:异常处理3.1 什么是异常?异常是程序在运行过程中发生的错误或意外情况,它可能导致程序的中断或崩溃。
3.2 Java中的异常处理机制有哪些?Java中的异常处理机制包括try-catch语句、throw语句和throws关键字等。
Java程序设计基础与实战-习题答案(人邮) 第10章作业参考答案
第10章作业参考答案1 .填空题(1)多线程(2)阻塞Runnable(4)并行(5)同步2 .选择3 .简答题(1)回答要点线程可以彼此独立的执行,它是一种实现并发机制的有效手段,可以同时使用多个线程来完成不同的任务,并且一般用户在使用多线程时并不考虑底层处理的细节。
进程(process)是程序的一次执行过程,或是正在运行的一个程序。
线程是比进程更小的程序执行单位,一个进程可以启动多个线程同时运行,不同线程之间可以共享相同的内存区域和数据。
(2)回答要点有三种方式,如下所示:①继承Thread类:编写简单,如果需要访问当前线程,那么无需使用Thread.currentThread() 方法,直接使用this即可获得当前线程。
但线程类已经继承了Thread类,所以不能再继承其他父类。
②实现Runnable接口:防止由于Java单继承带来的局限性。
但编程稍微复杂,如果要访问当前线程,那么必须使用Thread.currentThread()方法。
③使用Callable接口和Future接口创立多线程:防止由于Java单继承带来的局限性,有返回值,可以抛出异常。
但编程稍微复杂,如果要访问当前线程,那么必须使用Thread. currentThread。
方法。
(3)回答要点线程从新建到死亡称为线程的生命周期。
(4)回答要点启动一个线程是调用start。
方法,使线程所代表的虚拟处理机处于可运行状态,这意味着它可以由JVM调度并执行。
这并不意味着线程就会立即运行。
4 .编程题(1)源代码:参考本章资料文件夹下“作业1”。
(2)源代码:参考本章资料文件夹下“作业2”。
JAVA程序设计(山东联盟-潍坊科技学院)知到章节答案智慧树2023年
JAVA程序设计(山东联盟-潍坊科技学院)知到章节测试答案智慧树2023年最新第一章测试1.以下对Java 语言不正确的描述是( )A. Java语言是一个完全面向对象的语言。
B. Java是结构中立与平台无关的语言。
C. Java是一种编译性语言。
D. Java是一种结构化语言。
参考答案:Java是一种结构化语言2.以下说法正确的是( )(多选)A.用javac命令运行编译过的java 程序。
B.字节码文件扩展名是.java。
C.Java源程序文件的扩展名必须是.java。
D.Java编译器将源文件编译为字节码文件。
参考答案:Java编译器将源文件编译为字节码文件;Java源程序文件的扩展名必须是.java3.在命令窗口下执行一个Java 程序“FirstApp” 的方法是()A.直接双击编译好的Java 目标码文件执行B.运行“javac FirstApp.java; java FirstAppC.运行“java FirstApp.java” ; java FirstAppD.运行“javac FirstApp.class”参考答案:运行“javac FirstApp.java; java FirstApp4.下列说法正确的是( )A.Java语言的标识符是区分大小写的B.源文件名与public类名可以不相同C.源文件扩展名为.jarD.源文件中public类的数目不限参考答案:Java语言的标识符是区分大小写的5.如果有一个类是public类,那么源文件的名字必须与这个类的名字完全相同()参考答案:对第二章测试1. Java中的char型数据占用()个字节参考答案:22.15/2的结果是()参考答案:73.能够终止循环和switch的语句是()参考答案:break4.若已定义 int[ ] a= {1,2,3,4} ; 则对a数组元素错误的引用是()参考答案:a[4]5.Java中没有无符号数()参考答案:对6. 3.14是float类型的()参考答案:错7.不同类型数据混合运算时,会自动转换成同一类型()参考答案:对8.^表示乘方运算()参考答案:错9.if后面的表达式可以为任何类型()参考答案:错10.do-while循环至少执行一次()参考答案:对11.数组是引用类型()参考答案:对12.Java声明数组时不能指定其长度()参考答案:对13.以下有关标识符说法不正确的是:()参考答案:Java的保留字也可作为标识符使用。
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);}}。
java基础教程第3版习题解答
Java基础教程第3版习题解答第一章习题1. JamesGoslin g2.需3个步骤:1)用文本编辑器编写源文件2)使用java c编译源文件,得到字节码文件3)应用程序使用解释器运行。
3. path d:\jdk\binclassp ath =d:\jdk\jre\lib\rt.jar;.;4. B5. java 和class6.D。
第二章习题1.用来标识类名、变量名、方法名、类型名、数组名、文件名的有效字符序列称为标识符。
标识符由字母、下划线、美元符号和数字组成,第一个字符不能是数字。
fal se不是标识符。
2.关键字就是Java语言中已经被赋予特定意义的一些单词,不可以把关键字作为名字来用。
不是关键字。
cl assi mplem entsi nterface enum extend s abstra ct。
3.float常量必须用F或f为后缀。
double常量用D或d为后缀,但允许省略后缀。
4.一维数组名.length。
二维数组名.l ength。
5. C6.ADF7. B8 【代码2】【代码3】【代码4】9.B。
10.属于操作题,解答略。
11.3,112.public classE {public static void main(String args[]) {System.out.printl n((int)'你');System.out.printl n((int)'我');System.out.printl n((int)'他');}}13.public classE {public static void main (String args[ ]) {char cStart='α',cEnd='ω';for(char c=cStart;c<=cEnd;c++)System.out.print(" "+c);}}第三章习题1. 1102.beep!!3.public classE {public static void main (String args[ ]) {for(charc='а';c<='я';c++){S ystem.out.print(" "+c);}}}4.public class Xiti3_4{ public s tatic void main(String args[]) { double sum=0,a=1;int i=1;while(i<=20){ sum=sum+a;i++;a=a*i;}System.out.println("sum="+sum);}}5.public classXiti5{ public static void main(String args[]){ int i,j;for(j=2;j<=100;j++){ for(i=2;i<=j/2;i++){ if(j%i==0)break;}if(i>j/2){ System.out.print(" "+j);}}}}6.class Xiti6{ public s tatic void main(String args[]){ double sum=0,a=1,i=1;do { sum=sum+a;i++;a=(1.0/i)*a;}while(i<=20);System.out.println("使用do-while循环计算的s um="+sum);for(sum=0,i=1,a=1;i<=20;i++){ a=a*(1.0/i);sum=sum+a;}System.out.println("使用for循环计算的s um="+sum);}}7.class Xiti7{ public s tatic void main(String args[]){ int sum=0,i,j;for(i=1;i<=1000;i++){ for(j=1,sum=0;j<i;j++){ if(i%j==0)sum=sum+j;}if(sum==i)System.out.println("完数:"+i);}}}8.import java.util.*;public classE {public static void main (String args[ ]) {int m,n;Scanne r scaner= new Scanne r(System.in);System.out.println("输入正数m回车确认");m = scaner.nextIn t();System.out.println("输入正数n回车确认");n = scaner.nextIn t();int p=m;int q= n;int r = m%n;while(r!=0) {m = n;n =r;r =m%n;}System.out.println(p+"和"+q+"的最大公约数"+n);System.out.println(p+"和"+q+"的最小公倍数"+(p*q)/n);}}9.public classE{ public static void main(String args[]){ int n=1;long sum=0;while(true){ sum=sum+n;n++;if(sum>=8888)break;}System.out.println("满足条件的最大整数:"+(n-1));}}第四章习题1.用该类创建对象时。
Java语言程序设计(郑莉)一到八章课后习题答案
第二章习题答案1.什么是对象、类,它们之间的联系?答:1)对象是包含现实世界物体特征的抽象实体,它反映系统为之保存信息和与它交互的能力。
对象是一些属性及服务的封装体,在程序设计领域,可以用“对象=数据+作用于这些数据上的操作”来表示。
现实生活中对象是指客观世界的实体;在程序中对象是指一组变量和相关方法的集合。
2)类是既有相同操作功能和相同的数据格式的对象的集合与抽象!两者的关系:对象是类的具体实例.。
2.什么是面向对象的程序设计方法?它有那些基本特征?答:面向对象程序设计从所处理的数据入手,以数据为中心而不是以服务为中心来描述系统。
它把编程问题视为一个数据集合,数据相对于功能而言,具有更强的稳定性。
它的特征:抽象,封装,继承,多态。
3.在下面的应用中,找出可能用到的对象,对每一个对象,列出可能的状态和行为。
1)模拟航空预订系统交易的程序2)模拟银行交易的程序答:1)航空预订交易:状态:旅客姓名,身份证号,联系号码,出发地址,抵达地址,出发日期。
行为:订票,领票,买票,退票。
2)银行交易:状态:客户姓名,账号,身份证号。
行为:存款,取款,汇款。
4.请解释类属性、实例属性及其区别。
答:实例属性,由一个个的实例用来存储所有实例都需要的属性信息,不同实例的属性值可能会不同。
5.请解释类方法、实例属性及其区别。
答:实例方法表示特定对象的行为,在声明时前面不加static修饰符,在使用时需要发送给一个类实例。
类方法也称为静态方法,在方法声明时前面需加static修饰符,类方法表示具体实例中类对象的共有行为。
区别:实例方法可以直接访问实例变量,调用实例方法,实例方法可以直接访问类变量,调用类方法;类方法可以直接调用类变量和类方法,类方法不能直接调用实例变量和实例方法;6.类的访问控制符有哪几种?具体含义及其区别。
答:类的访问控制符只有public(公共类)及无修饰符(默认类)两种。
区别:当使用public修饰符时表示所有其他的类都可以使用此类;当没有修饰符时,则只有与此类处于同一包中的其他类可以使用类。
JAVA第9章第10章课后习题答案
第9章多线程4、C5、D6、package cn.ntu.zhanbin;public class ThreadOutput implements Runnable {static ThreadOutput to = new ThreadOutput();public static void main(String[] args) {//创建两个线程Thread t1 = new Thread(new ThreadOutput());Thread t2 = new Thread(new ThreadOutput());t1.start();t2.start();}public void run() {synchronized (to) {to.print();}}void print() {for (int i = 0; i < 6; i++) {System.out.print(10 + i);if (i < 5) {System.out.print(",");}try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("");}}--------------------------------------------------------------------- 8、package cn.ntu.zhanbin;public class ThreadAlphabet extends Thread {public static void main(String[] args) {//创建3个线程ThreadAlphabet ta1 = new ThreadAlphabet("线程1:");ThreadAlphabet ta2 = new ThreadAlphabet("线程2:");ThreadAlphabet ta3 = new ThreadAlphabet("线程3:");ta1.start();ta2.start();ta3.start();}ThreadAlphabet(String name) {super(name);//调用父类的构造函数}public void run() {//获取当前线程名String name = Thread.currentThread().getName();//输出for (int i = 0; i < 26; i++) {System.out.println(name + (char) (i + 65));try {Thread.sleep(200);} catch (InterruptedException e) {e.printStackTrace();}}}}--------------------------------------------------------------------- 9、package cn.ntu.zhanbin;public class PrintOdds implements Runnable {private int bound;//创建一个静态PrintOdds对象,并设置边界范围为20static PrintOdds po = new PrintOdds(20);public PrintOdds(int b) {bound = b;}public void print() {for (int i = 1; i < bound; i+=2) {System.out.println(i);}}public void run() {po.print();}public static void main(String[] args) {Thread t = new Thread(po);t.start();}}第10章数据库编程说明:课本中连接数据库和关闭数据库的操作都是调用的P201的例10-1的ConnectionManager,而书中的类只写了getConnection()方法,各个关闭操作的方法均未给出。
Java语言程序设计课后习题答案全集
Java语言程序设计课后习题答案全集Java语言程序设计是一门广泛应用于软件开发领域的编程语言,随着其应用范围的不断扩大,对于掌握Java编程技巧的需求也逐渐增加。
为了帮助读者更好地掌握Java编程,本文将提供Java语言程序设计课后习题的全集答案,供读者参考。
一、基础知识题1. 代码中的注释是什么作用?如何使用注释.答:注释在代码中是用来解释或者说明代码的功能或用途的语句,编译器在编译代码时会自动忽略注释。
在Java中,有三种注释的方式:- 单行注释:使用"// " 可以在代码的一行中加入注释。
- 多行注释:使用"/* */" 可以在多行中添加注释。
- 文档注释:使用"/** */" 可以添加方法或类的文档注释。
2. 什么是Java的数据类型?请列举常见的数据类型。
答:Java的数据类型用来指定变量的类型,常见的数据类型有:- 基本数据类型:包括整型(byte、short、int、long)、浮点型(float、double)、字符型(char)、布尔型(boolean)。
- 引用数据类型:包括类(class)、接口(interface)、数组(array)等。
二、代码编写题1. 编写Java程序,输入两个整数,求和并输出结果。
答:```javaimport java.util.Scanner;public class SumCalculator {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.print("请输入第一个整数:");int num1 = scanner.nextInt();System.out.print("请输入第二个整数:");int num2 = scanner.nextInt();int sum = num1 + num2;System.out.println("两个整数的和为:" + sum);}}```三、综合应用题1. 编写Java程序,实现学生信息管理系统,要求包括以下功能:- 添加学生信息(姓名、年龄、性别、学号等);- 修改学生信息;- 删除学生信息;- 查询学生信息。
java程序设计基础 课后习题答案
java程序设计基础课后习题答案Java程序设计基础课后习题答案在学习Java程序设计基础课程的过程中,课后习题是非常重要的一部分。
通过完成课后习题,我们可以巩固课堂上所学的知识,提高编程能力,加深对Java 语言的理解。
下面我们来看一些常见的Java程序设计基础课后习题答案。
1. 编写一个Java程序,实现输入两个整数,然后输出它们的和。
```javaimport java.util.Scanner;public class AddTwoNumbers {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("请输入第一个整数:");int num1 = input.nextInt();System.out.println("请输入第二个整数:");int num2 = input.nextInt();int sum = num1 + num2;System.out.println("它们的和是:" + sum);}}```2. 编写一个Java程序,实现输入一个整数,然后判断它是奇数还是偶数。
```javaimport java.util.Scanner;public class OddOrEven {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("请输入一个整数:");int num = input.nextInt();if (num % 2 == 0) {System.out.println(num + "是偶数");} else {System.out.println(num + "是奇数");}}}```3. 编写一个Java程序,实现输入一个年份,然后判断它是否是闰年。
JAVA语言程序设计(基础篇)答案
3.4import javax.swing.*;public class AdditionTutor{public static void main(String[] args){int number1=(int)(System.currentTimeMillis()%100);int number2=(int)(System.currentTimeMillis()*5%100);String answerString=JOptionPane.showInputDialog("what is "+ number1 +"+ "+ number2+" ?");int answer=Integer.parseInt(answerString);JOptionPane.showMessageDialog(null,number1 +" + "+ number2 +" = "+answer+" is "+(number1+number2==answer));}}3.10import javax.swing.JOptionPane;public class ComputeTaxWithSelectionStatement{public static void main(String[] args){//Prompt the user to enter filing statusString statusString = JOptionPane.showInputDialog("Enter the filing status:\n"+"(0-single filer,1-married jointly,\n"+"2-married separately,3-head of household)");int status = Integer.parseInt(statusString);//Prompt the user to enter taxable incomeString incomeString = JOptionPane.showInputDialog("Enter the taxable income:");double income = Double.parseDouble(incomeString);//Comput taxdouble tax=0;if (status == 0){//Compute tax for single filersif (income <= 6000)tax = income * 0.10;else if (income <= 27950)tax = 6000 * 0.10 + (income - 6000) * 0.15;else if (income <= 67700)tax = 6000 * 0.10 + (27950 - 6000) * 0.15 +(income - 27950) * 0.27;else if (income <= 141250)tax = 6000 * 0.10 + (27950 - 6000) * 0.15 +(67700 - 27950) * 0.27 + (income - 67700) * 0.30;else if (income <=307050)tax = 6000 * 0.10 + (27950 - 6000) * 0.15 +(67700 - 27950) * 0.27 + (141250 - 67700) * 0.30 +(income - 141250) * 0.35;elsetax = 6000 * 0.10 + (27950 - 6000) * 0.15 +(67700 - 27950) * 0.27 + (141250 -67700) * 0.30 +(307050 - 141250) * 0.35 + (income - 307050) * 0.386;}else if (status == 1){//Compute tax for married file jointly if (income <= 12000)tax = income * 0.10;else if (income <= 46700)tax = 12000 * 0.10 + (income - 12000) * 0.15;else if (income <= 112850)tax = 12000 * 0.10 + (46700 - 12000) * 0.15 +(income - 46700) * 0.27;else if (income <= 171950)tax = 12000 * 0.10 + (46700 - 12000) * 0.15 +(112850 - 46700) * 0.27 + (income - 112850) * 0.30;else if (income <= 307050)tax = 12000 * 0.10 + (46700 - 12000) * 0.15 +(112850 - 46700) * 0.27 + (141250 - 112850) * 0.30 +(income - 307050) * 0.35;elsetax = 12000 * 0.10 + (46700 - 12000) * 0.15 +(112850 - 46700 ) * 0.27 + (171950 - 112850) * 0.30 +(307050 - 171950) * 0.35 + (income - 307050) * 0.386;}else if (status == 2){//Compute tax for married separately if (income <= 6000)tax = income * 0.10;else if (income <= 23350)tax = 6000 * 0.10 + (income - 6000) * 0.15;else if (income <= 56425)tax = 6000 * 0.10 + (23350 - 6000) * 0.15 +(income - 23350) * 0.27;else if (income <= 85975)tax = 6000 * 0.10 + (23350 - 6000) * 0.15 +(56425 - 23350) * 0.27 + (income - 56425) * 0.30;else if (income <= 153525)tax = 6000 * 0.10 + (23350 - 6000) * 0.15 +(56425 - 23350) * 0.27 + (85975 - 56425) * 0.30 +(income - 85975) * 0.35;elsetax = 6000 * 0.10 + (23350 - 6000) * 0.15 +(56425 - 23350) * 0.27 + (85975 - 56425) * 0.30 +(153525 - 85975) * 0.35 + (income - 153525) * 0.386;}else if (status == 3){//Compute tax for head of householdif (income <= 10000)tax = income * 0.10;else if (income <= 37450)tax = 10000 * 0.10 + (income - 10000) * 0.15;else if (income <= 96700)tax = 10000 * 0.10 + (37450 - 10000) * 0.15 +(income - 37450) * 0.27;else if (income <= 156600)tax = 10000 * 0.10 + (37450 - 10000) * 0.15 +(96700 - 37450) * 0.27 + (income - 96700) * 0.30;else if (income <= 307050)tax = 10000 * 0.10 + (37450 - 10000) * 0.15 +(96700 - 37450) * 0.27 + (156600 - 96700) * 0.30 +(income - 156600) * 0.35;elsetax = 10000 * 0.10 + (37450 - 10000) * 0.15 +(96700 - 37450) * 0.27 + (156600 - 96700) * 0.30 +(307050 - 156600) * 0.35 + (income - 307050) * 0.386;}else{System.out.println("Error: invalid status");System.exit(0);}//Display the resultJOptionPane.showMessageDialog(null,"Tax is " +(int)(tax * 100) / 100.0);}}4.11public class ZhengChu{public static void main(String[] args){int count=0;for(int i=100;i<=200;i++){if((i%5==0||i%6==0)&&i%30!=0){System.out.print(" "+ i);count++;if(count%10==0)System.out.println();}}}}4.14public class ASCII{public static void main(String[] args){int count = 0;for(int i = 33; i <= 126; i++){count++;char ch = (char)i;System.out.print(" " + ch);if(count % 10 == 0)System.out.println();}}}4.17import javax.swing.JOptionPane;public class FindSalesAmount{/**Main method*/public static void main(String[] args){//The commission soughtString COMMISSION_SOUGHTString = JOptionPane.showInputDialog("Enter the COMMISSION_SOUGHT :");double COMMISSION_SOUGHT = Double.parseDouble(COMMISSION_SOUGHTString);double commission = 0;double salesAmount;for (salesAmount = 0.01;commission <= COMMISSION_SOUGHT;){salesAmount += 0.01; //防止犯off-by-one错误,先判断在做自加!if (salesAmount >= 10000.01)commission =5000 * 0.08 + 5000 * 0.1 + (salesAmount - 10000) * 0.12;else if (salesAmount >= 5000.01)commission = 5000 * 0.08 + (salesAmount - 5000) * 0.10;elsecommission = salesAmount * 0.08;}String output ="The sales amount $" + (int)(salesAmount * 100) / 100.0 +"\n is needed to make a commission of $" + COMMISSION_SOUGHT;JOptionPane.showMessageDialog(null,output);}}5.6import javax.swing.JOptionPane;public class PrintPyramid{public static void main(String[] args){String input = JOptionPane.showInputDialog("Enter the number of lines:");int numberOfLines = Integer.parseInt(input);displayPattern(numberOfLines);}public static void displayPattern(int n){for (int row = 1;row <= n;row++){for (int column = 1;column <= n - row;column++)System.out.print(" ");for (int num = row;num >= 1;num--)System.out.print((num >= 10) ? " " + num : " " + num);System.out.println();}}}5.18public class MathSuanFa{public static void main(String[] args){double A = Math.sqrt(4);double B = (int)Math.sin(2 * Math.PI);double C = (int)Math.cos(2 * Math.PI);double D = (int)Math.pow(2, 2);double E = (int)Math.log(Math.E);double F = (int)(Math.exp(1)*100000)/100000.0;double G = (int)Math.max(2, Math.min(3, 4));double H = (int)Math.rint(-2.5);double I = (int)Math.ceil(-2.5);double J = (int)Math.floor(-2.5);int K = (int)Math.round(-2.5f);int L = (int)Math.round(-2.5);double M = (int)Math.rint(2.5);double N = (int)Math.ceil(2.5);double O = (int)Math.floor(2.5);int P = (int)Math.round(2.5f);int Q = (int)Math.round(2.5);int R = (int)Math.round(Math.abs(-2.5));System.out.println(A +" "+ B +" "+ C +" "+ D +" "+ E +" "+ F +" "+ G +" "+ H +" "+ I +" "+ J +" "+ K +" "+ L +" "+ M +" "+ N +" "+ O +" "+ P +" "+ Q +" "+ R);}}6.9import javax.swing.JOptionPane;public class Array{public static void main (String[] args){String incomeString = JOptionPane.showInputDialog("Enter the number of array size:");int income = Integer.parseInt(incomeString);int arraySize = income;int [] myList = new int [arraySize];int m;for ( m = 0; m < arraySize; m++){String elementString = JOptionPane.showInputDialog("Enter the " + (m + 1) + " element of the array");int element = Integer.parseInt(elementString);myList[m] = element;}int minElement = myList[0];for (int i=1; i < arraySize; i++){if (myList[i] < minElement)minElement = myList[i];}JOptionPane.showMessageDialog(null,"The min element is: " + minElement); }}。
《Java语言程序设计》(第3版)第10章
第10章Java数据库连接【1】试述JDBC提供了哪几种连接数据库的方法。
[解答]:JDBC连接数据库的方法取决于JDBC驱动程序类型,Java定义了4种JDBC驱动程序类型:(1)JDBC-ODBC桥驱动程序JDBC-ODBC桥接器负责将JDBC转换为ODBC,用JdbcOdbc.Class和一个用于访问ODBC驱动程序的本地库实现的。
这类驱动程序必须在服务器端安装好ODBC驱动程序,然后通过JDBC-ODBC的调用方法,进而通过ODBC来存取数据库。
(2)Java到本地API这种类型的驱动程序是部分使用Java语言编写和部分使用本机代码编写的驱动程序,这类驱动程序也必须在服务器端安装好特定的驱动程序,如ODBC驱动程序,然后通过桥接器的转换,把Java API调用转换成特定驱动程序的调用方法,进而操作数据库。
(3)网络协议搭配的Java驱动程序这种驱动程序将JDBC转换为与DBMS无关的网络协议,这种协议又被某个服务器转换为一种DBMS协议。
这种网络服务器中间件能够将它的纯Java客户机连接到多种不同的数据库上。
所用的具体协议取决于提供者。
(4)本地协议纯Java驱动程序这种类型的驱动程序将JDBC访问请求直接转换为特定数据库系统协议。
不但无须在使用者计算机上安装任何额外的驱动程序,也不需要在服务器端安装任何中间程序,所有对数据库的操作,都直接由驱动程序来完成。
【2】SQL语言包括哪几种基本语句来完成数据库的基本操作。
[解答]:SQL语言包括以下6种基本语句来完成数据库的基本操作:(1)select语句:用来对数据库进行查询并返回符合用户查询标准的结果数据。
(2)create table语句:用来建立新的数据表。
(3)insert 语句:向数据表中插入或添加新的数据行。
(4)update语句:更新或修改符合规定条件的记录。
(5)delete语句:删除数据表中的行或记录。
(6)drop table语句:删除某个数据表以及该表中的所有记录。
Java程序设计 第10章习题参考答案[1页]
第10章习题参考答案
一、简答题
1.什么是多态?类的多态是怎么实现的?
多态(Polymorphism)按字面的意思就是“多种状态”,在面向对象语言中,接口的多种不同的实现方式即为多态。
多态表示不同的对象可以执行相同的动作,但要通过它们自己的实现代码来执行。
多态通过重载和覆盖实现,重载发生在一个类中,覆盖发生在子类,子类重写父类相同名称的方法。
2.创建多态需要哪些条件?
在代码中实现Java的多态必须遵循的必要条件:存在子类继承父类关系(包括接口的实现)、子类覆盖父类中的方法,在实现多态,需有子类对父类方法的覆盖(重写)。
3.类是怎么向接口转型实现多态的?
定义一个父类类型的引用指向一个子类的对象既可以使用子类强大的功能,又可以抽取父类的共性。
所以,父类类型的引用可以调用父类中定义的所有属性和方法,而对于子类中定义而父类中没有的方法,它是无可奈何的;同时,父类中的一个方法只有在在父类中定义而在子类中没有重写的情况下,才可以被父类类型的引用调用;对于父类中定义的方法,如果子类中重写了该方法,那么父类类型的引用将会调用子类中的这个方法,这就是动态连接。
Java语言程序设计基础教程习题解答
《Java语言程序设计基础教程》练习思考题参考答案第1章Java程序设计概述1.9 练习思考题1、Java运行平台包括三个版本,请选择正确的三项:()A. J2EEB. J2MEC. J2SED. J2E解答:A,B,C2、Java JDK中反编译工具是:()A. javacB. javaC. jdbD.javap解答:D3、public static void main方法的参数描述是:()A. String args[]B. String[] argsC. Strings args[]D. String args解答:A,B4、在Java中,关于CLASSPATH环境变量的说法不正确的是:()A. CLASSPATH一旦设置之后不可修改,但可以将目录添加到该环境变量中。
B. 编译器用它来搜索各自的类文件。
C. CLASSPATH是一个目录列表。
D. 解释器用它来搜索各自的类文件。
解答:A5、编译Java Application源文件将产生相应的字节码文件,扩展名为()A. .javaB. .classC. .htmlD. .exe解答:B6、开发及运行Java程序需要经过的三个主要步骤为____________、____________和____________。
7、如果一个Java Applet源程序文件只定义有一个类,该类的类名为MyApplet,则类MyApplet必须是______类的子类并且存储该源程序文件的文件名为______。
8、如果一个Java Applet程序文件中定义有3个类,则使用Sun公司的JDK编译器编译该源程序文件将产生______个文件名及类名相同而扩展名为______的字节码文件。
9、开发及运行Java程序需要经过哪些主要步骤和过程?10、Java程序是由什么组成的?一个程序中必须要有public类吗?Java源文件的命名规则是怎么样的?11、编写一个简单的Java应用程序,该程序在命令行窗口输出两行文字:“你好,很高兴学习Java”和“We are students”。
《Java语言程序设计-基础篇》答案-第10章
10.6 答:正确。
网
案
答
后
课
1/3 = 0.3333333333333333 1/2 = 0.5
10.4 答:错误如下:
(1)找不到符号
符号: 方法 add(Rational)
位置: 类 ng.Number
System.out.println(r.add(new Rational())); (2)找不到符号
om 符号: 方法 add(Rational) .c 位置: 类 ng.Number w System.out.println((Rational)r.add(new Rational())); khda 10.5 答:错误原因:ng.Number 是抽象的;无法对其进行实例化 www. Number r = new Number();
网(继承)
(组合)
案 账户和存款账户 (继承)
答
后 10.3 答:输出结果如下:
课1/3 + 1/2 = 5/6
1/2 + 1/3 = 5/6
1/3 - 1/2 = -1/6
1/2 - 1/3 = 1/6
1/3 * 1/2 = 1/6
1/2 * 1/3 = 1/9
1/31/3 = 3/2
绘图略公司和雇员聚集课程和师资关联学生和人继承房子和窗户组合账户和存款账户继承103答
第 10 章 面向对象建模
复习题
10.1 答:
类之间的关系
图形符号
继承
关联 聚集 组合(包容) 依赖
10.2 答:绘图略 公司和雇员 课程和师资
((聚关集联))
学生和人 房子和窗户
《Java语言程序设计》考试练习题(带答案)三
《Java语言程序设计基础教程》练习思考题参考答案第1章 Java程序设计概述1.1单项选择题1.编译Java Application源文件将产生相应的字节码文件,扩展名是什么A. .javaB. .classC. .htmlD. .exe解答:B2.Java语言具有许多优点和特点,下列选项中,哪个反映了Java中“一次编译,随处运行”的特点。
A. 简单性B. 平台无关性C. 面向对象性D. 安全性解答:B3.Java语言具有许多优点和特点,下列选项中,哪个反映了Java中并行机制的特点。
A. 多线程B. 平台无关性C. 面向对象性D. 安全性解答:A4.在Java语言中,下列哪些语句关于内存回收的说明是正确的?A.程序员必须创建一个线程来释放内存;B.内存回收程序负责释放无用内存;C.内存回收程序允许程序员直接释放内存;D.内存回收程序可以在指定的时间释放内存对象解答:B5.一个Java源文件中最多只能有多少个public类。
A. 1个B. 2个C. 3个D. 任意多个解答:A6.下面哪一个不是Java语言所具有的特点。
A. 没有全局变量,在类的定义外部没有任何的变量定义;B. 不直接支持指针操作,而使用更安全的引用类型;C. 支持子类对父类的多继承;D. 具有自动垃圾收集功能;解答:C7.下面哪一个不是Java开发工具包JDK的组成部分。
A. Java的编译器;B. Java的解释器;C. Java的API继承类库;D. Eclipse开发工具;解答:C8.JDK提供的编译器是()。
A. java.exeB. javac.exeC. javap.exeD. javaw.exe解答:B9.作为Java应用程序入口的main方法,其声明格式可以是()A. public void main(String [] args)B. public static void main(String [] args)C. public static void Main(String * args)D. public int main(String [] args)解答:B10.下列说法正确的是(A )A、JAVA程序的main方法必须写在类里面B、JAVA程序中可以有多个main方法C、JAVA程序中类名必须与文件名一样D、JAVA程序的main方法中如果只有一条语句,可以不用{}(大括号)括起来11.下列哪些语句关于Java内存回收的说明是正确的? ( B )A、程序员必须创建一个线程来释放内存B、内存回收程序负责释放无用内存C、内存回收程序允许程序员直接释放内存D、内存回收程序可以在指定的时间释放内存对象1.2填空题1、开发与运行Java程序需要经过的三个主要步骤为____________、____________和____________。
《Java语言程序设计(基础篇)》(第10版梁勇著)第三章练习题答案
《Java语言程序设计(基础篇)》(第10版梁勇著)第三章练习题答案《Java语言程序设计(基础篇)》(第10版梁勇著)第三章练习题答案3.1public class Exercise03_01 {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();double discriminant = b * b - 4 * a * c;if (discriminant < 0) {System.out.println("The equation has no real roots");}else if (discriminant == 0) {double r1 = -b / (2 * a);System.out.println("The equation has one root " + r1);}else { // (discriminant > 0)double r1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a);double r2 = (-b - Math.pow(discriminant, 0.5)) / (2 * a);System.out.println("The equation has two roots " + r1 + " and " + r2);}}}3.1附加public class Exercise03_01Extra {public static void main(String args[]) {Scanner input = new Scanner(System.in);System.out.print("Enter a numerator: ");int numerator = input.nextInt();System.out.print("Enter a denominator: ");int denominator = input.nextInt();if (numerator < denominator) {System.out.println(numerator + " / " + denominator + " is a proper fraction");}else if (numerator % denominator == 0) {System.out.print(numerator + " / " + denominator + " is an improper fraction ");System.out.println("and it can be reduced to " + numerator / denominator);}else {System.out.print(numerator + " / " + denominator + " is an improper fraction ");System.out.println("and its mixed fraction is " + numerator / denominator + " + " +numerator % denominator + " / " + denominator);}}}3.2public class Exercise03_02 {public static void main(String[] args) {Scanner input = new Scanner(System.in);int number1 = (int)(System.currentTimeMillis() % 10);int number2 = (int)(System.currentTimeMillis() * 7 % 10);int number3 = (int)(System.currentTimeMillis() * 3 % 10);System.out.print("What is " + number1 + " + " + number2 + " + " +number3 + "? ");int answer = input.nextInt();System.out.println(number1 + " + " + number2 + " + " + number3 +" = " + answer + " is " +(number1 + number2 + number3 == answer));}}3.2附加public class Exercise03_02Extra {public static void main(String args[]) {Scanner input = new Scanner(System.in);System.out.print("Enter the coordinates for two points: ");double x1 = input.nextDouble();double y1 = input.nextDouble();double x2 = input.nextDouble();double y2 = input.nextDouble();double m = (y2 - y1) / (x2 - x1);double b = y1 - m * x1;System.out.print("The line equation for two points (" + x1 + ", " + y1 + ") and (" + x2 + ", " + y2 + ") is " + "y = ");if (m == -1)System.out.print("-x");else if (m == 1)System.out.print("x");elseSystem.out.print(m + "x");if (b > 0)System.out.println(" + " + b);else if (b < 0)System.out.println(" - " + (-1 * b));else// b is 0System.out.println();}}3.3public class Exercise03_03 {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();double detA = a * d - b * c;if (detA == 0) {System.out.println("The equation has no solution"); }else {double x = (e * d - b * f) / detA;double y = (a * f- e * c) / detA;System.out.println("x is " + x + " and y is " + y);}}}3.3附加public class Exercise03_03Extra {public static void main(String[] args) {final double RADIUS = 5;double angle = Math.random() * 2 * Math.PI;double x = RADIUS * Math.random() * Math.cos(angle);double y = RADIUS * Math.sin(angle);double distance = Math.pow(x * x + y * y, 0.5);System.out.println("The point is (" + x + ", " + y + ") and its distance to the center is " + distance);}}3.4public class Exercise03_04 {public static void main(String[] args) {int number = (int)(Math.random() * 12) + 1;// or int number = (int)(System.currentTimeMillis() % 12 + 1);// or int number = (int)(Math.random() * 12) + 1;if (number == 1)System.out.println("Month is Januaray");else if (number == 2)System.out.println("Month is Feburary");else if (number == 3)System.out.println("Month is March");else if (number == 4)System.out.println("Month is April");else if (number == 5)System.out.println("Month is May");else if (number == 6)System.out.println("Month is June");else if (number == 7)System.out.println("Month is July");else if (number == 8)System.out.println("Month is August");else if (number == 9)System.out.println("Month is September");else if (number == 10)System.out.println("Month is October");else if (number == 11)System.out.println("Month is November");else// if (number == 12)System.out.println("Month is December");}}3.5public class Exercise03_05 {public static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in);// Prompt the user to enter an integer for todaySystem.out.print("Enter today抯 day: ");int today = input.nextInt();System.out.print("Enter the number of days elapsed since today: ");int elapsedDays = input.nextInt();String nameForToday;if (today == 0)nameForToday = "Sunday";else if (today == 1)nameForToday = "Monday";else if (today == 2)nameForToday = "Tuesday";else if (today == 3)nameForToday = "Wednesday";else if (today == 4)nameForToday = "Thursday";else if (today == 5)nameForToday = "Friday";else// if (today == 6)nameForToday = "Saturday";int futureDay = (today + elapsedDays) % 7; String nameForFutureDay;if (futureDay == 0)nameForFutureDay = "Sunday";else if (futureDay == 1) nameForFutureDay = "Monday";else if (futureDay == 2) nameForFutureDay = "Tuesday";else if (futureDay == 3) nameForFutureDay = "Wednesday";else if (futureDay == 4) nameForFutureDay = "Thursday";else if (futureDay == 5) nameForFutureDay = "Friday";else// if (futureDay == 6) nameForFutureDay = "Saturday";System.out.println("Today is " + nameForToday+ " and the future day is " + nameForFutureDay); } }3.6public class Exercise03_06 {public static void main(String[] args) {Scanner input = new Scanner(System.in);// Prompt the user to enter weight in pounds System.out.print("Enter weight in pounds: "); double weight = input.nextDouble();// Prompt the user to enter heightSystem.out.print("Enter feet: ");double feet = input.nextDouble();System.out.print("Enter inches: ");double inches = input.nextDouble();double height = feet * 12 + inches;// Compute BMIdouble bmi = weight * 0.45359237 / ((height * 0.0254) * (height * 0.0254));// Display resultSystem.out.println("BMI is " + bmi);if (bmi < 18.5)System.out.println("Underweight");else if (bmi < 25)System.out.println("Normal");else if (bmi < 30)System.out.println("Overweight");elseSystem.out.println("Obese");}}3.7/** Break down an amount into smaller units* Display the non-zero denominations only, and display singular* words for single units like 1 dollars, 1 penny, and display plural * words for more than one unit like 2 dollars, 3 pennies.*/public class Exercise03_07 {// Main methodpublic static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in);// Receive the amount entered from the keyboardSystem.out.print("Enter an amount in double, for example 11.56: ");double amount = input.nextDouble();int remainingAmount = (int)(amount * 100);// Find the number of one dollarsint numberOfOneDollars = remainingAmount / 100;remainingAmount = remainingAmount % 100;// Find the number of quarters in the remaining amountint numberOfQuarters = remainingAmount / 25;remainingAmount = remainingAmount % 25;// Find the number of dimes in the remaining amountint numberOfDimes = remainingAmount / 10;remainingAmount = remainingAmount % 10;// Find the number of nickels in the remaining amountint numberOfNickels = remainingAmount / 5;remainingAmount = remainingAmount % 5;// Find the number of pennies in the remaining amountint numberOfPennies = remainingAmount;// Display resultsif (amount < 0) {System.out.println("Your amount is negative");System.exit(1);}else if (amount < 0) {System.out.println("Your amount is zero");System.exit(2);}System.out.println("Your amount " + amount + " consists of ");if (numberOfOneDollars > 1)System.out.println(numberOfOneDollars + "\ dollars");else if (numberOfOneDollars == 1)System.out.println(numberOfOneDollars + "\ dollar");if (numberOfQuarters > 1)System.out.println(numberOfQuarters + "\ quarters");else if (numberOfQuarters == 1)System.out.println(numberOfQuarters + "\ quarter");if (numberOfDimes > 1)System.out.println(numberOfDimes + "\ dimes");else if (numberOfDimes == 1)System.out.println(numberOfDimes + "\ dime");if (numberOfNickels > 1)System.out.println(numberOfNickels + "\ nickels");else if (numberOfNickels == 1)System.out.println(numberOfNickels + "\ nickel");if (numberOfPennies > 1)System.out.println(numberOfPennies + "\ pennies");else if (numberOfPennies == 1)System.out.println(numberOfPennies + "\ penny");}}3.8public class Exercise03_08 {public static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in); // Enter three numbersSystem.out.print("Enter three integers: ");int number1 = input.nextInt();int number2 = input.nextInt();int number3 = input.nextInt();if (number1 > number2) {int temp = number1;number1 = number2;number2 = temp;}if (number2 > number3) {int temp = number2;number2 = number3;number3 = temp;}if (number1 > number2) {int temp = number1;number1 = number2;number2 = temp;}System.out.println("The sorted numbers are "+ number1 + " " + number2 + " " + number3);}}public class Exercise03_09 {public static void main(String[] args) {Scanner input = new Scanner(System.in);// Prompt the user to enter an integerSystem.out.print("Enter the first 9 digits of an ISBN as integer: ");int number = input.nextInt();// Calculate checksum (You may write a loop to simplify it in Ch4int checksum =((number / 100000000 % 10) * 1 +(number / 10000000 % 10) * 2 +(number / 1000000 % 10) * 3 +(number / 100000 % 10) * 4 +(number / 10000 % 10) * 5 +(number / 1000 % 10) * 6 +(number / 100 % 10) * 7 +(number / 10 % 10) * 8 +(number % 10) * 9) % 11;System.out.print("The ISBN-10 number is ");// Display leading zeros, improve the solution using loops in the next chapterif (number / 100000000 == 0) {System.out.print("0");if (number / 10000000 == 0) {System.out.print("0");if (number / 1000000 == 0) {System.out.print("0");if (number / 100000 == 0) {System.out.print("0");if (number / 10000 == 0) { System.out.print("0");if (number / 1000 == 0) { System.out.print("0");if (number / 100 == 0) {System.out.print("0");if (number / 10 == 0) {System.out.print("0");if (number == 0) {System.out.print("0");}}}}}}}}}System.out.print(number);if (checksum == 10)System.out.print("X");elseSystem.out.print(checksum);}}3.10public class Exercise03_10 {public static void main(String[] args) {// 1. Generate two random single-digit integersint number1 = (int)(Math.random() * 10);int number2 = (int)(Math.random() * 10);// 2. Prompt the student to answer 搘hat is number1 + number2?? System.out.print("What is " + number1 + " + " + number2 + "? "); Scanner input = new Scanner(System.in);int answer = input.nextInt();// 4. Grade the answer and display the resultString replyString;if (number1 + number2 == answer)replyString = "You are correct!";elsereplyString = "Your answer is wrong.\" + number1 + " + "+ number2 + " should be " + (number1 + number2);System.out.println(replyString);}}3.11public class Exercise03_11 {public static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in);// Prompt the user to enter inputSystem.out.print("Enter a month in the year (e.g., 1 for Jan): ");int month = input.nextInt();System.out.print("Enter a year: ");int year = input.nextInt();int numberOfDaysInMonth = 0;switch (month) {case 1:System.out.print("January " + year);numberOfDaysInMonth = 31;break;case 2:System.out.print("February " + year);if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { numberOfDaysInMonth = 29;}else {numberOfDaysInMonth = 28;}break;case 3:System.out.print("March " + year);numberOfDaysInMonth = 31;break;case 4:System.out.print("April " + year);numberOfDaysInMonth = 30;break;case 5:System.out.print("May " + year);numberOfDaysInMonth = 31;break;case 6:System.out.print("June " + year);numberOfDaysInMonth = 30;break;case 7:System.out.print("July " + year);numberOfDaysInMonth = 31;break;case 8:System.out.print("August " + year);numberOfDaysInMonth = 31;break;case 9:System.out.print("September " + year);numberOfDaysInMonth = 30;break;case 10:System.out.print("October " + year);numberOfDaysInMonth = 31;break;case 11:System.out.print("November " + year);numberOfDaysInMonth = 30;break;case 12:System.out.print("December " + year);numberOfDaysInMonth = 31;break;}System.out.print(" has " + numberOfDaysInMonth + " days"); }}3.12public class Exercise03_12 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter a three-digit integer: ");int number = input.nextInt();if (number / 100 == number % 10)System.out.println(number + " is a palindrome");elseSystem.out.println(number + " is not a palindrome");}}3.13public class Exercise03_13 {public static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in);// Prompt the user to enter filing statusSystem.out.print("(0-single filer, 1-married jointly or qualifying widow(er),"+ "\2-married separately, 3-head of household)\" + "Enter the filing status: ");int status = input.nextInt();// Prompt the user to enter taxable incomeSystem.out.print("Enter the taxable income: ");double income = input.nextDouble();// Compute taxdouble tax = 0;if (status == 0) { // Compute tax for single filersif (income <= 8350) {tax = income * 0.10;} else if (income <= 33950) {tax = 8350 * 0.10 + (income - 8350) * 0.15;} else if (income <= 82250) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950)* 0.25; } else if (income <= 171550) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (income - 82250) * 0.28;} else if (income <= 372950) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (income - 171550) * 0.33;} else {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (372950 - 171550) * 0.33 + (income - 372950) * 0.35;}} else if (status == 1) { // Compute tax for married file jointly if (income <= 16700) {tax = income * 0.10;} else if (income <= 67900) {tax = 16700 * 0.10 + (income - 16700) * 0.15;} else if (income <= 137050) {tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (income - 67900) * 0.25; } else if (income <= 208850) {tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (income - 137050) * 0.28;} else if (income <= 372950) {tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (income - 208850) * 0.33;} else {tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (171950 - 137050) * 0.28 + (372950 - 208850) * 0.33+ (income - 372950) * 0.35;}} else if (status == 2) { // Compute tax for married separately if (income <= 8350) {tax = income * 0.10;} else if (income <= 33950) {tax = 8350 * 0.10 + (income - 8350) * 0.15;} else if (income <= 68525) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25; } else if (income <= 104425) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (income - 68525) * 0.28;} else if (income <= 186475) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (income - 104425) * 0.33;} else {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (186475 - 104425) * 0.33 + (income - 186475) * 0.35;}} else if (status == 3) { // Compute tax for head of household if (income <= 11950) {tax = income * 0.10;} else if (income <= 45500) {tax = 11950 * 0.10 + (income - 11950) * 0.15;} else if (income <= 117450) {tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (income - 45500) * 0.25; } else if (income <= 190200) {tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (income - 117450) * 0.28;} else if (income <= 372950) {tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (income - 190200) * 0.33;} else {tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (372950 - 190200) * 0.33+ (income - 372950) * 0.35;}} else {System.out.println("Error: Wrong filing status");System.exit(1);}// Display the resultSystem.out.println("Tax is " + (int) (tax * 100) / 100.0);}}3.14public class Exercise03_14 {public static void main(String[] args) {// Obtain the random number 0 or 1int number = (int)(Math.random() * 2);// Prompt the user to enter a guessjava.util.Scanner input = new java.util.Scanner(System.in);System.out.print("Guess head or tail? " +"Enter 0 for head and 1 for tail: ");int guess = input.nextInt();// Check the guessif (guess == number)System.out.println("Correct guess");else if (number == 0)System.out.println("Sorry, it is a head");elseSystem.out.println("Sorry, it is a tail");}}3.15public class Exercise03_15 {public static void main(String[] args) {// Generate a lotteryint lottery = (int)(Math.random() * 1000);// Prompt the user to enter a guessjava.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Enter your lottery pick (three digits): ");int guess = input.nextInt();// Get digitsint l1 = lottery / 100;int l2 = (lottery % 100) / 10; // l2 = (lottery / 10) % 10int l3 = lottery % 10;int g1 = guess / 100;int g2 = (guess % 100) / 10;int g3 = guess % 10;System.out.println("Lottery is " + lottery);// Check the guessif (guess == lottery)System.out.println("Exact match: you win $10,000");else if (g1 == l1 && g3 == l2 && g2 == l3 ||g2 == l1 && g1 == l2 && g3 == l3 ||g2 == l1 && g3 == l2 && g1 == l3 ||g3 == l1 && g1 == l2 && g2 == l3 ||g3 == l1 && g2 == l2 && g1 == l3)System.out.println("Match all digits: you win $3,000"); else if (g1 == l1 || g1 == l2 || g1 == l3 ||g2 == l1 || g2 == l2 || g2 == l3 ||g3 == l1 || g3 == l2 || g3 == l3)System.out.println("Match one digit: you win $1,000"); elseSystem.out.println("Sorry, no match");}}3.16public class Exercise03_16 {public static void main(String[] args) {double x = Math.random() * 100 - 50;double y = Math.random() * 200 - 100;System.out.println(x + ", " + y);}}3.17public class Exercise03_17 {public static void main(String[] args) {// Generate scissor, rock, paperint computerNumber = (int)(Math.random() * 3);// Prompt the user to enter scissor, rock, or paper java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("scissor (0), rock (1), paper (2): ");int userNumber = input.nextInt();// Check the guessswitch (computerNumber) {case 0:if (userNumber == 0)System.out.print("The computer is scissor. You are scissor too. It is a draw");else if (userNumber == 1)System.out.print("The computer is scissor. You are rock. You won");else if (userNumber == 2)System.out.print("The computer is scissor. You are paper. You lost");break;case 1:if (userNumber == 0)System.out.print("The computer is rock. You are scissor. You lost");else if (userNumber == 1)System.out.print("The computer is rock. You are rock too. It is a draw");else if (userNumber == 2)System.out.print("The computer is rock. You are paper. You won");break;case 2:if (userNumber == 0)System.out.print("The computer is paper. You are scissor. You won");else if (userNumber == 1)System.out.print("The computer is paper. You are rock. You lost");else if (userNumber == 2)System.out.print("The computer is paper. You are paper too.It is a draw");break;}}}3.18public class Exercise03_18 {public static void main(String args[]) {Scanner input = new Scanner(System.in);System.out.print("Enter package weight: ");double w = input.nextDouble();if (w <= 1) {System.out.println("The shipping cost is $3.5");}else if (w <= 3) {System.out.println("The shipping cost is $5.5");}else if (w <= 10) {System.out.println("The shipping cost is $8.5");}else if (w <= 20) {System.out.println("The shipping cost is $10.5");}else {System.out.println("The package cannot be shipped");}}}3.19public class Exercise03_19 {public static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in); // Enter three edgesSystem.out.print("Enter three edges (length in double): ");double edge1 = input.nextDouble();double edge2 = input.nextDouble();double edge3 = input.nextDouble();。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
}
public boolean equals(MyInteger value){
return value.equals(this.value);
}
public int parseInt(char[] cs){
String s=new String(cs);
}
int getValue(){
return value;
}
boolean isEven(){
return value%2==0?true:false;
}
boolean isOdd(){
return value%2!=0?true:false;
}
boolean isPrime(){
for(int i=2;i<value/2;i++)
if (value%i!=0)
return true;
return false;
}
boolean isEven(int value){
this.value=value;
return value%2==0?true:false;
}
boolean isOdd(int value){
this.value=value;
return value.isEven();
}
boolean isOdd(MyInteger value){
return value.isOdd();
}
boolean isPrime(MyInteger value){
return value.isPrime();
}
public boolean equals(int value){
System.out.println("\n是偶数吗?"+m1.isEven(13)+"\n是奇数吗?"+m1.isOdd(13)+"\n是素数吗?"+m1.isPrime(13));
System.out.println("\n是否相等:"+m1.equals(13)+" \n是否相等:"+m1.equals(13));
10.3 /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
return value%2!=0?true:false;
}
boolean isPrime(int value){
this.value=value;
for(int i=2;i<value/2;i++)
if (value%i==0)
return false;
return true;
}
boolean isEven(MyInteger value){
System.out.println("\n"+m1.parseInt("124"));
System.out.println("\n"+Integer.parseInt("sab"));
}
}
System.out.println("是偶数吗?"+m1.isEven()+"\n是奇数吗?"+m1.isOdd()+"\n是素数吗?"+m1.isPrime());
System.out.println("\n是偶数吗?"+m1.isEven(12)+"\n是奇数吗?"+m1.isOdd(12)+"\n是素数吗?"+m1.isPrime(12));
return parseInt(s);
}
public int parseInt(String s){
return Integer.valueOf(s);
}
}
void main(String[] args){
MyInteger m1=new MyInteger(11);
*/
/**
*
* @author Administrator
*/
class MyInteger{
int value;
String value1;
MyInteger(int value){
this.value=value;
}
MyInteger(String value1){
this.value1=value1;