Java程序设计精编教程第版习题解答
发布时间:2020-06-18 13:03:28
发布时间:2020-06-18 13:03:28
1.James Gosling
2.需3个步骤:
1) 用文本编辑器编写源文件。
2) 使用javac编译源文件,得到字节码文件。
3) 使用解释器运行程序。
3.set classpath=D:\jdk\jre\lib\;.;
4. B
5. Java源文件的扩展名是.java,Java字节码的扩展名是.class。
6.D。
1.
2.
public class Teacher {
double add(double a,double b) {
return a+b;
}
double sub(double a,double b) {
return a-b;
}
}
public class Student {
public void speak() {
}
}
public class MainClass {
public static void main(String args[]) {
Teacher zhang=new Teacher();
Student jiang=new Student();
();
}
}
3. 如果源文件中有多个类,但没有public类,那么源文件的名字只要和某个类的名字相同,并且扩展名是.java就可以了,如果有一个类是public类,那么源文件的名字必须与这个类的名字完全相同,扩展名是.java。
4.行尾风格。
1.用来标识类名、变量名、方法名、类型名、数组名、文件名的有效字符序列称为标识符。标识符由字母、下划线、美元符号和数字组成,第一个字符不能是数字。true不是标识符。
2.关键字就是Java语言中已经被赋予特定意义的一些单词,不可以把关键字作为名字来用。不是关键字。class implements interface enum extends abstract。
3.boolean,char,byte,short,int,long,float,double。
4.属于操作题,解答略。
5.属于操作题,解答略。
6. public class E {
public static void main (String args[ ]) {
char cStart='A',cEnd='Z';
for(char c=cStart;c<=cEnd;c++)
}
}
7.不可以。
1.110。不规范。
2.新亲亲斤!!。
3.
public class JudgeAward {
void giveMess(int number) {
if(number==9||number==131||number==12) {
}
else if(number==209||number==596||number==27) {
}
else if(number==875||number==316||number==59) {
}
else{
}
}
}
4.
public class Computer {
public static void main(String args[]) {
Scanner reader=new Scanner;
double amount = 0; 2c2c27。
10.100和。
1.如果子类和父类在同一个包中,那么,子类自然地继承了其父类中不是private的成员变量作为自己的成员变量,并且也自然地继承了父类中不是private的方法作为自己的方法, 继承的成员或方法的访问权限保持不变。当子类和父类不在同一个包中时,父类中的private和友好访问权限的成员变量不会被子类继承,也就是说,子类只继承父类中的protected和public访问权限的成员变量作为子类的成员变量;同样,子类只继承父类中的protected和public访问权限的方法作为子类的方法。如果所声明的成员的变量的名字和从父类继承来的成员变量的名字相同(声明的类型可以不同),在这种情况下,子类就会隐藏掉所继承的成员变量。
2.不可以。
3.abstract类。
4.A类是B类的父类,当用子类创建一个对象b,并把这个对象b的引用放到父类的对象a中时,称a是b的上转型对象。
5.AD。
6.
。
7.
12。
1.不能。
2.不能。
3.可以把实现某一接口的类创建的对象的引用赋给该接口声明的接口变量中。那么该接口变量就可以调用被类实现的接口中的方法。
4.
8。
5. 18
15。
1.有效。
2.可以。
3.不可以。
4.大家好,祝工作顺利!
1.ABD。
2.Love:Game。
3. 15
abc我们。
4.13579。
5.9javaHello。
6.public class E {
public static void main (String args[ ]) {
String s1,s2,t1="ABCDabcd";
s1=();
s2=();
String s3=(s2);
}
}
7. public class E {
public static void main (String args[ ]) {
String s="ABCDabcd";
char cStart=(0);
char cEnd = ()-1);
}
}
8.
import .*;
public class E {
public static void main(String args[]) {
Scanner read=new Scanner;
CalendarBean cb=new CalendarBean();
int year=2000,month=1;
year=();
month=();
(year);
(month);
String [] a= ();oChar();
for(char c:str) {
3c",c);
}
for(int i=0;i<;i++) { import .*;
public class E {
public static void main (String args[ ]) {
Scanner read=new Scanner;
int year1,month1,day1,year2,month2,day2;
year1=();
month1=();
day1=();
year2=();
month2=();
day2=();
Calendar calendar=();
(year1,month1-1,day1);
long timeYear1=();
(year2,month2-1,day2);
long timeYear2=();
long 相隔天数=((timeYear1-timeYear2)/(1000*60*60*24));
year2+"年"+month2+"月"+day2+"日相隔"+相隔天数+"天");
}
}
10. import .*;
public class E {
public static void main (String args[ ]) {
double a=0,b=0,c=0;
a=12;
b=24;
c=;
c=;
c=(1);
c=(8);
}
}
11.public class E {
public static void main (String args[ ]) {
String str = "ab123you你是谁";
String regex = "\\D+";
str = (regex,"");
}
}
12. import .*;
public class E {
public static void main(String args[]) {
String cost = "数学87分,物理76分,英语96分";
Scanner scanner = new Scanner(cost);
double sum=0;
int count =0;
while()){
try{ double score = ();
count++;
sum = sum+score;
}
catch(InputMismatchException exp){
String t = ();
}
}
}
}
1.使用FileInputStream。
2.FileInputStream按字节读取文件,FileReader按字符读取文件。
3.不可以。
4.使用对象流写入或读入对象时,要保证对象是序列化的。
5.使用对象流很容易得获取一个序列化对象的克隆,只需将该对象写入到对象输出流,那么用对象输入流读回的对象一定是原对象的一个克隆。
6.
import .*;
public class E {
public static void main(String args[]) {
File f=new File("");;
try{ RandomAccessFile random=new RandomAccessFile(f,"rw");
(0);
long m=();
while(m>=0) {
m=m-1;
(m);
int c=();
if(c<=255&&c>=0)
else {
m=m-1;
(m);
byte cc[]=new byte[2];
(cc);
}
}
}
catch(Exception exp){}
}
}
7.
import .*;
public class E {
public static void main(String args[ ]) {
File file=new File("");
File tempFile=new File("");
try{ FileReader inOne=new FileReader(file);
BufferedReader inTwo= new BufferedReader(inOne);
FileWriter tofile=new FileWriter(tempFile);
BufferedWriter out= new BufferedWriter(tofile);
String s=null;
int i=0;
s=();
while(s!=null) {
i++;
(i+" "+s);
();
s=();
}
();
();
();
();
();
}
catch(IOException e){}
}
}
8. 属于上机操作题,解答略。
9. import .*;
import .*;
public class E {
public static void main(String args[]) {
File file = new File("");
Scanner sc = null;
double sum=0;
int count = 0;
try { sc = new Scanner(file);
while()){
try{ double price = ();
count++;
sum = sum+price;
}
catch(InputMismatchException exp){
String t = ();
}
}
}
catch(Exception exp){
}
}
}
1.Frame容器的默认布局是BorderLayout布局。
2.不可以。
3.
import .*;
import .*;
public class E {
public static void main(String args[]) {
Computer fr=new Computer();
}
}
class Computer extends JFrame implements DocumentListener {
JTextArea text1,text2;
int count=1;
double sum=0,aver=0;
Computer() {
setLayout(new FlowLayout());
text1=new JTextArea(6,20);
text2=new JTextArea(6,20);
add(new JScrollPane(text1));
add(new JScrollPane(text2));
(false);
()).addDocumentListener(this);
setSize(300,320);
setVisible(true);
validate();
setDefaultCloseOperation;
}
public void changedUpdate(DocumentEvent e) {
String s=();
sum=0;
aver=0;
for(int i=0;i<;i++) {
try { sum=sum+(a[i]);
}
catch(Exception ee) {}
}
aver=sum/count;
(null);
("\n和:"+sum);
("\n平均值:"+aver);
}
public void removeUpdate(DocumentEvent e){
changedUpdate(e);
}
public void insertUpdate(DocumentEvent e){
changedUpdate(e);
}
}
4.
import .*;
import .*;
public class E {
public static void main(String args[]) {
ComputerFrame fr=new ComputerFrame();
}
}
class ComputerFrame extends JFrame implements ActionListener {
JTextField text1,text2,text3;
JButton buttonAdd,buttonSub,buttonMul,buttonDiv;
JLabel label;
public ComputerFrame() {
setLayout(new FlowLayout());
text1=new JTextField(10);
text2=new JTextField(10);
text3=new JTextField(10);
label=new JLabel(" ",;
;
add(text1);
add(label);
add(text2);
add(text3);
buttonAdd=new JButton("加");
buttonSub=new JButton("减");
buttonMul=new JButton("乘");
buttonDiv=new JButton("除");
add(buttonAdd);
add(buttonSub);
add(buttonMul);
add(buttonDiv);
(this);
(this);
(this);
(this);
setSize(300,320);
setVisible(true);
validate();
setDefaultCloseOperation;
}
public void actionPerformed(ActionEvent e) {
double n;
if()==buttonAdd) {
double n1,n2;
try{ n1=());
n2=());
n=n1+n2;
(n));
("+");
}
catch(NumberFormatException ee)
{ ("请输入数字字符");
}
}
else if()==buttonSub) {
double n1,n2;
try{ n1=());
n2=());
n=n1-n2;
(n));
("-");
}
catch(NumberFormatException ee)
{ ("请输入数字字符");
}
}
else if()==buttonMul)
{double n1,n2;
try{ n1=());
n2=());
n=n1*n2;
(n));
("*");
}
catch(NumberFormatException ee)
{ ("请输入数字字符");
}
}
else if()==buttonDiv)
{double n1,n2;
try{ n1=());
n2=());
n=n1/n2;
(n));
("/");
}
catch(NumberFormatException ee)
{ ("请输入数字字符");
}
}
validate();
}
}
5. import .*;
import .*;
public class E {
public static void main(String args[]){
Window win = new Window();
("使用MVC结构");
(100,100,420,260);
}
}
class Window extends JFrame implements ActionListener {
Lader lader; rim());
double bottom = ().trim());
double height = ().trim());
(above) ;
(bottom);
(height);
double area = ();
("面积:"+area+"\n");
}
catch(Exception ex) {
("\n"+ex+"\n");
}
}
}
class Lader {
double above,bottom,height;
public double getArea() {
double area = (above+bottom)*height/;
return area;
}
public void setAbove(double a) {
above = a;
}
public void setBottom(double b) {
bottom = b;
}
public void setHeight(double c) {
height = c;
}
}
1.4种状态:新建、运行、中断和死亡。
2.有4种原因的中断:(1)JVM将CPU资源从当前线程切换给其他线程,使本线程让出CPU的使用权处于中断状态。(2)线程使用CPU资源期间,执行了sleep(int millsecond)方法,使当前线程进入休眠状态。(3)线程使用CPU资源期间,执行了wait()方法,使得当前线程进入等待状态。(4)线程使用CPU资源期间,执行某个操作进入阻塞状态,比如执行读/写操作引起阻塞。
3.死亡状态,不能再调用start()方法。
4.新建和死亡状态。
5.两种方法:用Thread类或其子类。
6.使用setPrority(int grade)方法。
7.Java使我们可以创建多个线程,在处理多线程问题时,我们必须注意这样一个问题:当两个或多个线程同时访问同一个变量,并且一个线程需要修改这个变量。我们应对这样的问题作出处理,否则可能发生混乱。
8.当一个线程使用的同步方法中用到某个变量,而此变量又需要其它线程修改后才能符合本线程的需要,那么可以在同步方法中使用wait()方法。使用wait方法可以中断方法的执行,使本线程等待,暂时让出CPU的使用权,并允许其它线程使用这个同步方法。其它线程如果在使用这个同步方法时不需要等待,那么它使用完这个同步方法的同时,应当用notifyAll()方法通知所有的由于使用这个同步方法而处于等待的线程结束等待。
9.不合理。
10.“吵醒”休眠的线程。一个占有CPU资源的线程可以让休眠的线程调用interrupt 方法“吵醒”自己,即导致休眠的线程发生InterruptedException异常,从而结束休眠,重新排队等待CPU资源。
11. public class E {
public static void main(String args[]) {
Cinema a=new Cinema();
}
}
class TicketSeller public class E {
public static void main(String args[]) {
ClassRoom room6501 = new ClassRoom();
try{ (1000);
}
catch(Exception exp){}
}
}
class ClassRoom implements Runnable {
Thread student1,student2,teacher;
ClassRoom() {
teacher = new Thread(this);
student1 = new Thread(this);
student2 = new Thread(this);
("王教授");
("张三");
("李四");
}
public void run(){
if()==student1) {
(1000*60*10);
}
catch(InterruptedException e) {
}
();
public class E {
public static void main(String args[]) {
JoinWork work=new JoinWork();
work.司机.start();
}
}
class JoinWork implements Runnable {
Thread 司机;
Thread 装运工;
Thread 管理员;
JoinWork() {
司机=new Thread(this);
装运工=new Thread(this);
管理员=new Thread(this);
}
public void run() {
if()==司机) {
try{
装运工.start();
装运工.join();
}
catch(InterruptedException e){}
}
else if()==装运工) {
try{
管理员.start();
管理员.join();
}
catch(InterruptedException e){}
}
else if()==管理员) {
}
}
}
14. BA
1.URL对象调用InputStream openStream() 方法可以返回一个输入流,该输入流指向URL对象所包含的资源。通过该输入流可以将服务器上的资源信息读入到客户端。
2.客户端的套接字和服务器端的套接字通过输入、输出流互相连接后进行通信。
3.使用方法accept(),accept()会返回一个和客户端Socket对象相连接的Socket对象。accept方法会堵塞线程的继续执行,直到接收到客户的呼叫。。
4.域名/IP。
5. (1)客户端
import .*;
import .*;
import .*;
import .*;
public class Client
{ public static void main(String args[])
{ new ComputerClient();
}
}
class ComputerClient extends Frame implements Runnable,ActionListener
{ Button connection,send;
TextField inputText,showResult;
Socket socket=null;
DataInputStream in=null;
DataOutputStream out=null;
Thread thread;
ComputerClient()
{ socket=new Socket();
setLayout(new FlowLayout());
Box box=();
connection=new Button("连接服务器");
send=new Button("发送");
(false);
inputText=new TextField(12);
showResult=new TextField(12);
(connection);
(new Label("输入三角形三边的长度,用逗号或空格分隔:"));
(inputText);
(send);
(new Label("收到的结果:"));
(showResult);
(this);
(this);
thread=new Thread(this);
add(box);
setBounds(10,30,300,400);
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ (0);
}
});
}
public void actionPerformed(ActionEvent e)
{ if()==connection)
{ try ;
import .*;
import .*;
public class Server
{ public static void main(String args[])
{ ServerSocket server=null;
Server_thread thread;
Socket you=null;
while(true)
{ try{ server=new ServerSocket(4331);
}
catch(IOException e1)
{
}
you=();
}
catch (IOException e)
}
if(you!=null)
{ new Server_thread(you).start(); oubleValue();i++;
}
catch(NumberFormatException e)
{ ("请输入数字字符");
quesion=true;
}
}
if(quesion==false)
{ double p=(a[0]+a[1]+a[2])/;
(" "+(p*(p-a[0])*(p-a[1])*(p-a[2])));
}
}
catch (IOException e)
return;
}
}
}
}
6. 解答略
1.(1)添加数据源,(2)选择驱动程序,(3)命名数据源名称。
2. import .*;
import .*;
public class E {
public static void main(String args[]) {
Query query=new Query();
String dataSource="myData";
String tableName="goods";
Scanner read=new Scanner;
dataSource = ();
tableName = ();
(dataSource);
(tableName);
("SELECT * FROM "+tableName);
();
}
}
class Query {
String datasourceName=""; import .*;
import .*;
public class E {
public static void main(String args[]) {
Query query = new Query();
String dataSource = "myData";
String tableName = "goods";
(dataSource);
(tableName);
String name = "";
Scanner read=new Scanner;
name = ();
String str="'%["+name+"]%'";
String SQL = "SELECT * FROM "+tableName+" WHERE name LIKE "+str;
(SQL);
();
}
}
class Query {
String datasourceName=""; //数据源名
String tableName=""; //表名
String SQL; //SQL语句
public Query() {
}
catch(ClassNotFoundException e) {
}
}
public void setDatasourceName(String s) {
datasourceName = ();
}
public void setTableName(String s) {
tableName = ();
}
public void setSQL(String SQL) {
= ();
}
public void inputQueryResult() {
Connection con;
Statement sql;
ResultSet rs;
try {
String uri = "jdbc:odbc:"+datasourceName;
String id = "";
String password = "";
con = (uri,id,password);
DatabaseMetaData metadata = ();
ResultSet rs1 = (null,null,tableName,null);
int 字段个数 = 0;
while()) {
字段个数++;
}
sql = ();
rs = (SQL);
while()) {
for(int k=1;k<=字段个数;k++) {
}
}
();
}
catch(SQLException e) {
}
}
}
4.将例子5中的代码:
String SQL = "SELECT * FROM "+tableName+" ORDER BY name";
更改为:
String SQL = "SELECT * FROM "+tableName+" ORDER BY madeTime";
可达题目要求。
5.使用预处理语句不仅减轻了数据库的负担,而且也提高了访问数据库的速度。
6.事务由一组SQL语句组成,所谓事务处理是指:应用程序保证事务中的SQL语句要么全部都执行,要么一个都不执行。步骤:
(1)使用setAutoCommit(boolean autoCommit)方法 con对象首先调用setAutoCommit(boolean autoCommit)方法,将参数autoCommit取值false来关闭默认设置:
(false);
(2) 使用commit()方法。con调用commit()方法就是让事务中的SQL语句全部生效。
(3) 使用rollback()方法。con调用rollback()方法撤消事务中成功执行过的SQL语句对数据库数据所做的更新、插入或删除操作,即撤消引起数据发生变化的SQL语句操作,将数据库中的数据恢复到commi()方法执行之前的状态。
7.解答略。