Java 2实用教程(第五版) 您所在的位置:网站首页 java使用教程第五版pdf Java 2实用教程(第五版)

Java 2实用教程(第五版)

2023-09-08 22:40| 来源: 网络整理| 查看: 265

习题解答 习题1(第1章)

一、问答题

1.James Gosling

2.需3个步骤:

用文本编辑器编写源文件。  使用javac编译源文件,得到字节码文件。 使用解释器运行程序。 3.由类所构成,应用程序必须有一个类含有public static void main(String args[])方法,含有该方法的类称为应用程序的主类。不一定,但最多有一个public类。

4.set classpath=D:\jdk\jre\lib\rt.jar;.;

5.  java和class

6.  java Bird  

7. 独行风格(大括号独占行)和行尾风格(左大扩号在上一行行尾,右大括号独占行)

二、选择题

1.B。2.D。

三、阅读程序

1.(a)Person.java。(b)两个字节码,分别是Person.class和Xiti.class。(c)在类Person中找不到main方法;找不到或无法加载主类;找不到或无法加载主类;您好,很高兴认识你nice to meet you

习题2(第2章)

一、问答题

1.用来标识类名、变量名、方法名、类型名、数组名、文件名的有效字符序列称为标识符。标识符由字母、下划线、美元符号和数字组成,第一个字符不能是数字。false不是标识符。

2.关键字就是Java语言中已经被赋予特定意义的一些单词,不可以把关键字作为名字来用。不是关键字。class implements interface enum extends abstract。

3.boolean,char,byte,short,int,long,float,double。

4.float常量必须用F或f为后缀。double常量用D或d为后缀,但允许省略后缀。

5.一维数组名.length。二维数组名.length。

二、选择题

1.C。2.ADF。3.B。4.BE。5.【代码2】【代码3】【代码4】【代码5】。6.B。

三、阅读或调试程序

4.【代码1】:4。【代码2】:b[0]=1。

5.【代码1】:40。【代码2】:7

四、编写程序

1.  public class E {

   public static void main(String args[]) {

     System.out.println((int)'你');

     System.out.println((int)'我');  

     System.out.println((int)'他');

   }

}

2.   public class E {

   public static void main (String args[ ]) {

      char cStart='α',cEnd='ω';

      for(char c=cStart;c

double sum=0,a=1;

int i=1;

      while(i

  public static void main(String args[]) {

      int i,j;

      for(j=2;j

             if(j%i==0)

               break;

          }

          if(i>j/2) {

             System.out.print(" "+j);

          }

      }

   }

}

3.class Xiti3 {

  public static void main(String args[]) {

      double sum=0,a=1,i=1;

      do { sum=sum+a;

           i++;

           a=(1.0/i)*a;

       }

       while(i

  public static void main(String args[]) {

     int sum=0,i,j;

     for(i=1;i

           if(i%j==0)

               sum=sum+j;

        }

        if(sum==i)

           System.out.println("完数:"+i);

     }

  }

}

5.public class Xiti5 {

  public static void main(String args[]) {

     int m=8,item=m,i=1;

     long sum=0;

     for(i=1,sum=0,item=m;i

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

   }

}

习题4(第4章)

一、问答题

1. 封装、继承和多态。

2.当类名由几个单词复合而成时,每个单词的首字母使用大写。

3.名字的首单词的首字母使用小写,如果变量的名字由多个单词组成,从第2个单词开始的其它单词的首字母使用大写。

4.属性

5.行为

6.用类创建对象时。没有类型

7.用类创建对象时。

8.一个类中可以有多个方法具有相同的名字,但这些方法的参数必须不同,即或者是参数的个数不同,或者是参数的类型不同。可以。

9.可以。不可以。

10.不可以。

11.一个类通过使用new运算符可以创建多个不同的对象,不同的对象的实例变量将被分配不同的内存空间。所有对象的类变量都分配给相同的一处内存,对象共享类变量。

12.代表调用当前方法的对象。不可以。

二、选择题

1.B。2.D。3.D。4.D。5.CD。6.【代码1】【代码4】。7.【代码4】。

三、阅读程序

1.【代码1】:1,【代码2】:121,【代码3】:121。

2.sum=-100。

3. 27。

4.【代码1】:100,【代码2】:20.0。

四、编程题

CPU.java

public class CPU {

   int speed; 

   int getSpeed() {

      return speed;

   }

   public void setSpeed(int speed) {

      this.speed = speed;

   }

}

HardDisk.java

public class HardDisk {

   int amount; 

   int getAmount() {

      return amount;

   }

   public void setAmount(int amount) {

      this.amount = amount;

   }

}

PC.java

public class PC {

    CPU cpu;

    HardDisk HD;

    void setCPU(CPU cpu) {

        this.cpu = cpu;

    }

     void setHardDisk(HardDisk HD) {

        this.HD = HD;

    }

    void show(){

       System.out.println("CPU速度:"+cpu.getSpeed());

       System.out.println("硬盘容量:"+HD.getAmount());

    }

}

Test.java

public class Test {

   public static void main(String args[]) {

       CPU cpu = new CPU();

       HardDisk HD=new HardDisk();

       cpu.setSpeed(2200);

       HD.setAmount(200);

       PC pc =new PC();

       pc.setCPU(cpu);

       pc.setHardDisk(HD);

       pc.show();

    }

}

习题5(第5章)

一、问答题

1.不可以。

2.是。

3.不继承。

4.声明与父类同名的成员变量。

5.子类重写的方法类型和父类的方法的类型一致或者是父类的方法的类型的子类型,重写的方法的名字、参数个数、参数的类型和父类的方法完全相同。重写方法的目的是隐藏继承的方法,子类通过方法的重写可以把父类的状态和行为改变为自身的状态和行为。

6.不可以。

7.Abstract类。

8.上转型对象不能操作子类新增的成员变量,不能调用子类新增的方法。上转型对象可以访问子类继承或隐藏的成员变量,可以调用子类继承的方法或子类重写的实例方法。

9.通过重写方法。

10.面向抽象编程目的是为了应对用户需求的变化,核心是让类中每种可能的变化对应地交给抽象类的一个子类类去负责,从而让该类的设计者不去关心具体实现。

二、选择题

1.C。2.D。3.CD。4.D。5.B。6.B。7.D。8.B。9.A。

三、阅读程序

1.【代码1】:15.0。【代码2】:8.0。

2.【代码1】:11。【代码2】: 110。

3.【代码1】:98.0。【代码2】:12。【代码3】:98.0。【代码4】:9。

4.【代码1】:120。【代码2】:120。【代码3】:-100。

四、编程题

Animal.java

public abstract class Animal {

    public abstract void cry();

    public abstract String getAnimalName();

}

Simulator.java

public class Simulator {

   public void playSound(Animal animal) {

       System.out.print("现在播放"+animal.getAnimalName()+"类的声音:");

       animal.cry();

   }

}

Dog.java

public class Dog extends Animal {

   public void cry() {

      System.out.println("汪汪...汪汪");

   } 

   public String getAnimalName() {

      return "狗";

   }

}

Cat.java

public class Cat extends Animal {

   public void cry() {

      System.out.println("喵喵...喵喵");

   } 

   public String getAnimalName() {

      return "猫";

   }

}

Application.java

public class Example5_13 {

   public static void main(String args[]) {

      Simulator simulator = new Simulator();

      simulator.playSound(new Dog());

      simulator.playSound(new Cat());

   }

}

习题6(第6章)

一、问答题

1.不能。

2.不能。

3.可以把实现某一接口的类创建的对象的引用赋给该接口声明的接口变量中。那么该接口变量就可以调用被类实现的接口中的方法。

4.不可以。

5.可以。

二、选择题

1.D。2.AB。3. A。

三、阅读程序

1.【代码1】:15.0。【代码2】:8。

2.【代码1】:18。【代码2】:15。

四、编程题

Animal.java

public interface Animal {

    public abstract void cry();

    public abstract String getAnimalName();

}

Simulator.java

public class Simulator {

   public void playSound(Animal animal) {

       System.out.print("现在播放"+animal.getAnimalName()+"类的声音:");

       animal.cry();

   }

}

Dog.java

public class Dog implements Animal {

   public void cry() {

      System.out.println("汪汪...汪汪");

   } 

   public String getAnimalName() {

      return "狗";

   }

}

Cat.java

public class Cat implements Animal {

   public void cry() {

      System.out.println("喵喵...喵喵");

   } 

   public String getAnimalName() {

      return "猫";

   }

}

Application.java

public class Example5_13 {

   public static void main(String args[]) {

      Simulator simulator = new Simulator();

      simulator.playSound(new Dog());

      simulator.playSound(new Cat());

   }

}

习题7(第7章)

一、问答题

1.有效。

2.可以。

3.不可以。

4.一定是。

二、选择题

1.C。2.C。

三、阅读程序

1.大家好,祝工作顺利!

2.p是接口变量。

3.你好 fine thanks。

四、编程题

import java.util.*;

public class E {

    public static void main (String args[ ]){

      Scanner reader = new Scanner(System.in);

      double sum = 0;

       int m = 0;

       while(reader.hasNextDouble()){

           double x = reader.nextDouble();

           assert x< 100&&x>0:"数据不合理";

           m = m+1;

           sum = sum+x;

       }

       System.out.printf("%d个数的和为%f\n",m,sum);

       System.out.printf("%d个数的平均值是%f\n",m,sum/m);

    }

}

习题8(第8章)

一、问答题

1.不是。"\\hello"是。

2.4和3。

3.false和true。

4.负数。

5.是true。

6.3和-1。

7.会发生NumberFormatException异常。

二、选择题

1.A。2. B。 3.B。4.D。5.C。

三、阅读程序

1.【代码】:苹果。

2.【代码】:Love:Game。

3.【代码1】:15。【代码2】:abc我们。

4.【代码】:13579。

5.【代码】:9javaHello。

 

四、编程题

1.public class E {

  public static void main (String args[ ]) {

     String s1,s2,t1="ABCDabcd";

     s1=t1.toUpperCase();

     s2=t1.toLowerCase();

     System.out.println(s1);

     System.out.println(s2);

     String s3=s1.concat(s2);

      System.out.println(s3);

   }

}

2.   public class E {

  public static void main (String args[ ]) {

     String s="ABCDabcd";

     char cStart=s.charAt(0);

     char cEnd = s.charAt(s.length()-1);

     System.out.println(cStart);

     System.out.println(cEnd);

   }

}

3.   import java.util.*;

public class E {

  public static void main (String args[ ]) {

    int year1,month1,day1,year2,month2,day2;

      try{ year1=Integer.parseInt(args[0]);

           month1=Integer.parseInt(args[1]);

           day1=Integer.parseInt(args[2]);

           year2=Integer.parseInt(args[3]);

           month2=Integer.parseInt(args[4]);

           day2=Integer.parseInt(args[5]);

       }

       catch(NumberFormatException e)

         { year1=2012;

           month1=0;

           day1=1;

           year2=2018;

           month2=0;

           day2=1;

       }

       Calendar calendar=Calendar.getInstance();

       calendar.set(year1,month1-1,day1); 

       long timeYear1=calendar.getTimeInMillis();

       calendar.set(year2,month2-1,day2); 

       long timeYear2=calendar.getTimeInMillis();

       long 相隔天数=Math.abs((timeYear1-timeYear2)/(1000*60*60*24));

       System.out.println(""+year1+"年"+month1+"月"+day1+"日和"+

                            year2+"年"+month2+"月"+day2+"日相隔"+相隔天数+"天");

   }

}

4.   import java.util.*;

public class E {

  public static void main (String args[ ]) {

   double a=0,b=0,c=0;

      a=12;

      b=24;

      c=Math.asin(0.56);

      System.out.println(c);

      c=Math.cos(3.14);

      System.out.println(c);

      c=Math.exp(1);

      System.out.println(c);

      c=Math.log(8);

      System.out.println(c);

   }

}

5.public class E {

      public static void main (String args[ ]) {

        String str = "ab123you你是谁?";

        String regex = "\\D+";

        str = str.replaceAll(regex,"");

        System.out.println(str);

      }

}

6. import java.util.*;

public class E {

   public static void main(String args[]) {

      String cost = "数学87分,物理76分,英语96分";

      Scanner scanner = new Scanner(cost);

      scanner.useDelimiter("[^0123456789.]+");

      double sum=0;

      int count =0;

      while(scanner.hasNext()){

         try{  double score = scanner.nextDouble();

               count++;

               sum = sum+score;

               System.out.println(score);

         }

         catch(InputMismatchException exp){

              String t = scanner.next();

         }  

      }

      System.out.println("总分:"+sum+"分");

      System.out.println("平均分:"+sum/count+"分");

   }

}

习题9(第9章)

一、问答题

1.Frame容器的默认布局是BorderLayout布局。

2.不可以。

3.ActionEvent。

4.DocumentEvent。

5.5个。

6.MouseMotionListener。

二、选择题

1. A。2. A。3.D。

三、编程题

1. import java.awt.*;

import javax.swing.event.*;

import javax.swing.*;

import java.awt.event.*;

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

      text2.setEditable(false);

      (text1.getDocument()).addDocumentListener(this);

      setSize(300,320);

      setVisible(true);

      validate();

      setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

   }

   public void changedUpdate(DocumentEvent e) {

      String s=text1.getText();

      String []a =s.split("[^0123456789.]+");

      sum=0;

      aver=0; 

      for(int i=0;i

      changedUpdate(e); 

   }

   public void insertUpdate(DocumentEvent e){

      changedUpdate(e);

   }

}

2.   import java.awt.*;

import javax.swing.event.*;

import javax.swing.*;

import java.awt.event.*;

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(" ",JLabel.CENTER);

   label.setBackground(Color.green);

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

   buttonAdd.addActionListener(this);

   buttonSub.addActionListener(this);

   buttonMul.addActionListener(this); 

   buttonDiv.addActionListener(this);

   setSize(300,320);

   setVisible(true);

   validate();

   setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

  }  

  public void actionPerformed(ActionEvent e) {

    double n;

    if(e.getSource()==buttonAdd) {

       double n1,n2; 

       try{ n1=Double.parseDouble(text1.getText());

            n2=Double.parseDouble(text2.getText());

            n=n1+n2;

            text3.setText(String.valueOf(n));

            label.setText("+");

          }

       catch(NumberFormatException ee)

          { text3.setText("请输入数字字符");

          }

     }

    else if(e.getSource()==buttonSub) {

       double n1,n2; 

       try{  n1=Double.parseDouble(text1.getText());

            n2=Double.parseDouble(text2.getText());

            n=n1-n2;

            text3.setText(String.valueOf(n));

            label.setText("-");

          }

       catch(NumberFormatException ee)

          { text3.setText("请输入数字字符");

          }

     }

     else if(e.getSource()==buttonMul)

      {double n1,n2; 

       try{ n1=Double.parseDouble(text1.getText());

            n2=Double.parseDouble(text2.getText());

            n=n1*n2;

            text3.setText(String.valueOf(n));

            label.setText("*");

          }

       catch(NumberFormatException ee)

          { text3.setText("请输入数字字符");

          }

      }

      else if(e.getSource()==buttonDiv)

      {double n1,n2; 

       try{ n1=Double.parseDouble(text1.getText());

            n2=Double.parseDouble(text2.getText());

            n=n1/n2;

            text3.setText(String.valueOf(n));

            label.setText("/");

          }

       catch(NumberFormatException ee)

          { text3.setText("请输入数字字符");

          }

      }

     validate();

  }

}

习题10(第10章)

一、问答题

1.使用FileInputStream。

2.FileInputStream按字节读取文件,FileReader按字符读取文件。

3.不可以。

4.使用对象流写入或读入对象时,要保证对象是序列化的。

5.使用对象流很容易得获取一个序列化对象的克隆,只需将该对象写入到对象输出流,那么用对象输入流读回的对象一定是原对象的一个克隆。

二、选择题

1.C。2.B。

三、阅读程序

1.【代码1】:51。【代码2】:0。

2.【代码1】:3。【代码2】:abc。【代码3】:1。【代码4】:dbc。

四、编程题

1. import java.io.*;

public class E {

   public static void main(String args[]) {

       File f=new File("E.java");;

       try{   RandomAccessFile random=new RandomAccessFile(f,"rw");

              random.seek(0);

              long m=random.length();

              while(m>=0) {

                  m=m-1;

                  random.seek(m);

                  int c=random.readByte();

                  if(c=0)

                     System.out.print((char)c);

                  else {

                    m=m-1;

                    random.seek(m);

                    byte cc[]=new byte[2];

                    random.readFully(cc);

                    System.out.print(new String(cc));

                  }

              }

       }

       catch(Exception exp){}

    }

}

2.   import java.io.*;

public class E {

   public static void main(String args[ ]) {

      File file=new File("E.java");

      File tempFile=new File("temp.txt");

      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=inTwo.readLine();

           while(s!=null) {

               i++;

               out.write(i+" "+s);

               out.newLine();

               s=inTwo.readLine();

           }

           inOne.close();

           inTwo.close();

           out.flush();

           out.close();

           tofile.close();

      }

      catch(IOException e){}

   }

}

3.   import java.io.*;

import java.util.*;

public class E {

   public static void main(String args[]) {

      File file = new File("a.txt");

      Scanner sc = null;

      double sum=0;

      int count = 0;

      try { sc = new Scanner(file);

            sc.useDelimiter("[^0123456789.]+");

            while(sc.hasNext()){

               try{  double price = sc.nextDouble();

                    count++;

                    sum = sum+price;

                    System.out.println(price);

               }

               catch(InputMismatchException exp){

                    String t = sc.next();

               }  

            }

            System.out.println("平均价格:"+sum/count);

      }

      catch(Exception exp){

         System.out.println(exp);

      }

   }

}

文章部分内容参考自https://blog.csdn.net/lytwy123/article/details/83998175 特此声明。

如有错误,望您能留言告知,万分感谢。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有