Java语言程序设计(郑莉)第七章课后习题答案

合集下载

java语言程序设计课后答案

java语言程序设计课后答案

java语言程序设计课后答案作业参考答案习题一4、如何建立和运行Java程序,首先启动文本编辑器,如记事本、UltraEdit等,编辑程序代码,并以.Java作为文件扩展名保存程序源代码;然后进入dos环境利用javac编译源程序,生成扩展名为.class的字节码文件;再利用命令java运行字节码文件,得到程序的运行结果。

在集成开发环境Jbuilder、Eclipse下,可以完成程序的编辑、编译、调试及运行等所有任务。

5、public class LikeJava{public static void main(String [] args){System.out.println(“I Like Java Very much!”);}}习题二5、(1) 45 (2) false (3) 14 (4) 14 (5),6 (6) true(7) 129、public class Volume{public static void main(String [] args) {double r=0,v=0;r=double.parseDouble(args[0]);v=4*3.14159/3*r*r*r;System.out.println(“球体积为:”+v);}}习题三8、public class Factorials {public static void main(String args[]) {int i, j;long s=0, k;i=1;do //外循环开始{k = 1;j=1;do{//内循环开始k = k * j; //内循环体j++;}while(j<=i);//内循环结束System.out.println(i + "!=" + k);s = s + k;i++;}while(i<=20); //外循环结束System.out.println("Total sum=" + s); }}10、public class Num{public static void main(String[]args) {int i,j,k,n;for (n=100;n<1000;n++){i=n/100;j=(n-i*100)/10;k=n%10;if (i*i*i+j*j*j+k*k*k==n)System.out.print(n+" ");}}}习题四5、import java.util.Scanner;class Factor{long fac(int m){if(m==0||m==1)return 1;else return m*fac(m-1);}public static void main(String [] args){int i,n;long sum=0;String s="";Scanner input=new Scanner(System.in);System.out.print("Please input n: ");n=input.nextInt();Factor f=new Factor();for(i=1;i<=n;i++){ System.out.println(f.fac(i));sum=sum+f.fac(i);s=s+i+"!+";}System.out.println(s.substring(0,s.length()-1)+"="+sum); }}习题五2、import java.io.*;public class YangHuiOk{public static void main (String args[]) throws IOException {int max,a[][],i,j;char x;System.out.print("请输入杨辉三角要显示的行数: ");x=(char)System.in.read();max = Integer.parseInt(String.valueOf(x));a=new int[max][];for (i=0;i<max;i++){a[i]=new int[i+1];}a[0][0]=1;for (i=1;i<max;i++){a[i][0]=1;a[i][a[i].length-1]=1;for (j=1;j<a[i].length-1;j++){a[i][j]=a[i-1][j-1]+a[i-1][j];}}for(i=0;i<max;i++){//for(j=0;j<=max-i;j++) System.out.print(" ");for(j=0;j<=a[i].length-1;j++) System.out.print(a[i][j]+" "); System.out.println();}}}5、import java.util.Scanner;public class MatrixTurn {public static void main (String[] args) {int m,n;Scanner input=new Scanner(System.in);System.out.print("请输入矩阵的行数: ");m=input.nextInt();System.out.print("请输入矩阵的列数: ");n=input.nextInt();Matrix t=new Matrix(m,n);for(int i=1;i<=m;i++)//为矩阵各元素赋值for (int j=1;j<=n;j++)t.setElement(Math.random(),i,j);System.out.println("转置前的矩阵如下: ");for(int i=1;i<=m;i++){for (int j=1;j<=n;j++)//System.out.print(t.matrix[i][j]+" ");System.out.print(t.getElement(i,j)+" ");//访问矩阵元素方法1 System.out.println();}Matrix z;//声明转置矩阵z=t.turn(t);System.out.println("转置后的矩阵如下: ");for(int i=0;i<n;i++){for (int j=0;j<m;j++)System.out.print(z.matrix[i][j]+" ");//访问矩阵元素方法2,前提是matrix前无privateSystem.out.println();}}}习题六9、public class Vehicle,String color, kind;int speed;Vehicle(){color=”Red”;kind=”卡车”;speed=0;}public void setColor(String color1) { color=color1;}public void setSpeed(String speed1) { speed=speed1;}public void setKind(String kind1) { kind=kind1;}public String getColor( ) {return color;}public String getKind( ) {return kind;}public int getSpeed( ) {return speed;}public static void main(String [] args){Vehicle che=new Vehicle ();Che.setColor(“Blue”);Che.setSpeed(150);Che.setKind(“跑车”);System.out.p rintln(“有一辆”+che.getColor()+”的”+che.getKind()+”行驶在高速公路上”);System.out.println(“时速”+che.getSpeed()+”km/h”); }}习题七 7、public class Vehicle ,String color, kind;int speed;Vehicle(){color=” ”;kind=” ”;speed=0;}public void setColor(String color1){color=color1;}public void setSpeed(String speed1) {speed=speed1;}public void setKind(String kind1) {kind=kind1;}public String getColor( ) {return color;}public String getKind( ) {return kind;}public int getSpeed( ) {return speed;}}public class Car extends Vehicle {int passenger;public Car(){super();passenger=0;}public void setPassenger(int passenger){this. passenger = passenger; }public int getPassenger( ) {return passenger;}public static void main(String [] args){Car benz=new Car();benz.setColor(“Yellow”);benz.setKind(“roadster”);benz.setSpeed(120);benz.setPassenger(4);System.out.println(“benz: “);System.out.println(“Color “+benz.getColor());System.out.print(“Speed (km/h)“);System.out.println(benz.getSpeed()); System.out.println(“Kind: “+benz.getKind()); System.out.print(“Passenger: “);System.out.println(benz.getPassenger());}}习题九4、import java.io.*;public class UseException{public static void main(String [] args){System.out.println("请输入一个整数字符串");try{BufferedReader in=new BufferedReader(new InputStreamReader(System.in));int a=Integer.parseInt(in.readLine());System.out.println("您输入的整数是:"+a);}catch(IOException e){System.out.println("IO错误");}catch(NumberFormatException e1){System.out.println("您输入的不是一个整数字符串");}}}习题十 7、import java.io.*;public class SaveName {public static void main(String [] args){try{BufferedReader br=new BufferedReader(newInputStreamReader(System.in));BufferedWriter bw=new BufferedWriter(new FileWriter("name.txt"));String s;while(true){System.out.println("请输入姓名:");s=br.readLine();if(s.length()==0)break;bw.write(s);bw.newLine();}br.close();bw.close();}catch(FileNotFoundException e){System.out.println(e.toString());}catch(IOException e1){System.out.println(e1.toString());}}}8、import java.io.*;public class SaveGrade{public static void main(String [] args){try{BufferedReader br=new BufferedReader(newInputStreamReader(System.in));BufferedWriter bw=new BufferedWriter(new FileWriter("grade.txt"));String s,ss;while(true){System.out.println("请输入姓名:");s=br.readLine();if(s.length()==0)break;bw.write(s);bw.newLine();System.out.println("请输入学号:");s=br.readLine();bw.write(s);bw.newLine();System.out.println("请输入成绩:");s=br.readLine();bw.write(s);bw.newLine();}br.close();bw.close();int max=0,min=100,total=0,num=0;BufferedReader bf=new BufferedReader(new FileReader("grade.txt")); while(true){ss=bf.readLine();if(ss==null)break;ss=bf.readLine();ss=bf.readLine();int grade=Integer.parseInt(ss);total+=grade;num+=1;if(grade>max)max=grade;if(grade<min)min=grade;}System.out.println("学生成绩中最高为:"+max+",最低为:"+min+",平均分为:"+total*1.0/num);bf.close();}catch(FileNotFoundException e){System.out.println(e.toString());}catch(IOException e1){System.out.println(e1.toString());}}}习题十一6、import java.awt.*;import java.awt.event.*;public class ChangeColor extends Frame { private Button red=new Button("红");private Button green=new Button("绿"); private Button blue=new Button("蓝"); private TextField text=new TextField(); public ChangeColor(){super("改变颜色");this.setLayout(null);text.setBackground(Color.WHITE);red.setBounds(25,50,50,20);this.add(red);green.setBounds(125,50,50,20);this.add(green);blue.setBounds(225,50,50,20);this.add(blue);text.setBounds(25,100,250,30);this.add(text);red.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {text.setBackground(Color.RED);}});green.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {text.setBackground(Color.GREEN);}});blue.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {text.setBackground(Color.BLUE);}});addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});setSize(300,200);setVisible(true);}public static void main (String[] args){ChangeColor color=new ChangeColor(); }}习题十二5、import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Goods extends JFrame {private JComboBox list;private JTextArea info;private String names[]={"请选择你要查询的商品","A商品","B商品","C商品","D商品","E商品","F商品"};private String goods[][]={ {"","",""},{"A商品","北京",",300"},{"B商品","上海",",400"},{"C商品","广州",",500"},{"D商品","长沙",",600"},{"E商品","武汉",",700"},{"F商品","天津",",800"}};public Goods(){super("商品信息");Container pane=this.getContentPane();pane.setLayout(new BorderLayout());list=new JComboBox(names);info=new JTextArea(5,20);pane.add(list,BorderLayout.NORTH);pane.add(info,BorderLayout.CENTER);list.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent e) {int index=list.getSelectedIndex();info.setText("商品名:"+goods[index][0]+"\n"); info.append("产地:"+goods[index][1]+"\n"); info.append("价格:"+goods[index][2]+"\n"); }});this.setSize(250,300);this.setVisible(true);}public static void main (String[] args) {Goods ccc=new Goods();ccc.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) {System.exit(0);}});}}。

《Java语言程序设计-基础篇》答案-第07章

《Java语言程序设计-基础篇》答案-第07章
s1 >= s2
char c = s1(0);
char c = s1.charAt(s1.length()); 是错误的。
7.3 使用方法 equalsIgnoreCase(String).
7.4 使用方法toUpperCase () 将字符串的所有字母转换为大写;使用方法 toLowerCase () 将字符串的所有字母转换为小写;Use the methods toUpperCase and toLowerCase. The conversion methods (toLowerCase, toUpperCase, trim, replace) don’t change the contents of the string that invokes these methods. These methods create new strings.
(20)Welcome (21)WelcTme tT Java!

(22) WelcTme tT Java!
案 (23)WelcTme tT Java! 答 (24)返回一个包括字符W, e, l, c, o, m, e, , t, o, , J, a, v, a 的数组
7.2

课 String s3 = s1 - s2;
3. Number of strings is 0
4. Number of strings is 1
*
5. Number of strings is 当前目录下的文件之和,并会把当前目录下的文
件全部显示出来。
7.9 使用Character. isLowerCase(char) 判断是否小写字母;使用Character.
isUpperCase(char) 判断是否大写字母。

Java7课后习题

Java7课后习题
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
public class t71 {
public static void main(String[] args) {
InputStreamReader isr = new InputStreamReader(System.in);
FileReader fr2 = new FileReader(fin2);
FileWriter fw = new FileWriter(fout);
int ch;
while((ch=fr1.read())!=-1){
fw.write(ch);
C、RandomAccessFileD、FileWriter
9.在通常情况下,下列哪个类的对象可以作为BufferedReader类构造方法的参数(D)。
A、FileInputStreamB、FileReader
C、PrintStreamD、InputStreamReader
10.下列关于流类和File类的说法中错误的一项是(A)。
C、在Java中,用于处理字符流的两个基础的类是Reader和Writer。
D、按照流的方向分,可以分为输入流(Input Stream)和输出流(Output Stream)。
3.下面关于流的分类说法错误的一项是(B)。
A、为了处理Unicode字符串,定义了一系列的单独类,这些类是从抽象类Reader和Writer继承而来的。
11.Java语言的java.io包中的__File______类是专门用来管理磁盘文件和目录的。调用__File______类的方法则可以完成对文件或目录的常用管理操作,如创建文件或目录、删除文件或目录、查看文件的有关信息等。

Java程序设计课后练习答案

Java程序设计课后练习答案

《J a v a程序设计》课后练习答案第一章Java概述一、选择题1.( A )是在Dos命令提示符下编译Java程序的命令,( B )是运行Java程序的命令。

A.javacB.javaC.javadocD.javaw2.( D )不是Java程序中有效的注释符号。

A.//B.C.D.3.(A.B.C.D.4.JavaA.B.C.D.5.JavaA.1、JavaJava(JVM)Java以下图展示了Java程序从编译到最后运行的完整过程。

2、简述Java语言的特点Java具有以下特点:1)、简单性Java语言的语法规则和C语言非常相似,只有很少一部分不同于C语言,并且Java还舍弃了C语言中复杂的数据类型(如:指针和结构体),因此很容易入门和掌握。

2)、可靠性和安全性Java从源代码到最终运行经历了一次编译和一次解释,每次都有进行检查,比其它只进行一次编译检查的编程语言具有更高的可靠性和安全性。

3)、面向对象Java是一种完全面向的编程语言,因此它具有面向对象编程语言都拥有的封装、继承和多态三大特点。

4)、平台无关和解释执行Java语言的一个非常重要的特点就是平台无关性。

它是指用Java编写的应用程序编译后不用修改就可在不同的操作系统平台上运行。

Java之所以能平台无关,主要是依靠Java虚拟机(JVM)来实现的。

Java编译器将Java源代码文件编译后生成字节码文件(一种与操作系统无关的二进制文件)5)、6)、Java来。

1、/****/}}第二章Java语法基础一、选择题1.下面哪个单词是Java语言的关键字( B )?A. DoubleB. thisC. stringD. bool2.下面属于Java关键字的是( D )。

A. NULLB. IFC. DoD. goto3.在启动Java应用程序时可以通过main( )方法一次性地传递多个参数。

如果传递的参数有多个,可以用空格将这些参数分割;如果某一个参数本身包含空格,可以使用( B )把整个参数引起来。

java语言程序设计课后习题答案解析

java语言程序设计课后习题答案解析

习题23.使用“= =”对相同内容的字符串进行比较,看会产生什么样的结果。

答:首先创建一个字符串变量有两种方式:String str = new String("abc");String str = "abc";使用“= =”会因为创建的形式不同而产生不同的结果:String str1 = "abc";String str2 = "abc";System.out.println(str1= =str2); //trueString str1 = new String("abc"); String str2 = "abc";System.out.println(str1= =str2); //falseString str1 = new String("abc"); String str2 = new String("abc"); System.out.println(str1= =str2); //false因此自符串如果是对内容进行比较,使用equals方法比较可靠。

String str1 = "abc";String str2 = "abc";System.out.println(str1= =str2); //trueString str1 = new String("abc"); String str2 = "abc";System.out.println(str1.equals(str2)); //trueString str1 = new String("abc"); String str2 = new String("abc"); System.out.println(str1.equals(str2)); //true5.编写一个程序,把变量n的初始值设置为1678,然后利用除法运算和取余运算把变量的每位数字都提出来并打印,输出结果为:n=1678。

C语言程序设计(郑莉)课后习题答案

C语言程序设计(郑莉)课后习题答案

C语言程序设计(郑莉)课后习题答案C++语言程序设计(清华大学郑莉)课后习题答案第一章概述1-1 简述计算机程序设计语言的发展历程。

解:迄今为止计算机程序设计语言的发展经历了机器语言、汇编语言、高级语言等阶段,C++语言是一种面向对象的编程语言,也属于高级语言。

1-2 面向对象的编程语言有哪些特点?解:面向对象的编程语言与以往各种编程语言有根本的不同,它设计的出发点就是为了能更直接的描述客观世界中存在的事物以及它们之间的关系。

面向对象的编程语言将客观事物看作具有属性和行为的对象,通过抽象找出同一类对象的共同属性(静态特征)和行为(动态特征),形成类。

通过类的继承与多态可以很方便地实现代码重用,大大缩短了软件开发周期,并使得软件风格统一。

因此,面向对象的编程语言使程序能够比较直接地反问题域的本来面目,软件开发人员能够利用人类认识事物所采用的一般思维方法来进行软件开发。

C++语言是目前应用最广的面向对象的编程语言。

1-3 什么是结构化程序设计方法?这种方法有哪些优点和缺点?解:结构化程序设计的思路是:自顶向下、逐步求精;其程序结构是按功能划分为若干个基本模块;各模块之间的关系尽可能简单,在功能上相对独立;每一模块内部均是由顺序、选择和循环三种基本结构组成;其模块化实现的具体方法是使用子程序。

结构化程序设计由于采用了模块分解与功能抽象,自顶向下、分而治之的方法,从而有效地将一个较复杂的程序系统设计任务分解成许多易于控制和处理的子任务,便于开发和维护。

虽然结构化程序设计方法具有很多的优点,但它仍是一种面向过程的程序设计方法,它把数据和处理数据的过程分离为相互独立的实体。

当数据结构改变时,所有相关的处理过程都要进行相应的修改,每一种相对于老问题的新方法都要带来额外的开销,程序的可重用性差。

由于图形用户界面的应用,程序运行由顺序运行演变为事件驱动,使得软件使用起来越来越方便,但开发起来却越来越困难,对这种软件的功能很难用过程来描述和实现,使用面向过程的方法来开发和维护都将非常困难。

Java语言程序设计(郑莉)第七章课后习题答案

Java语言程序设计(郑莉)第七章课后习题答案

Java语言程序设计第七章课后习题答案1.数组的声明与数组元素的创建有什么关系?答:声明数组仅仅是代表试图创建数组,不分配任何存储空间,声明是为创建做“铺垫”。

2.Vector类的对象与数组有什么关系?什么时候适合使用数组,什么时候适合使用Vector?答:vector是一个能够存放任意对象类型的动态数组,容量能自动扩充,而数组存储固定且类型相同的对象;对于存储固定类型相同的对象使用数组,对于存储不同类型或者动态调整数组大小的情况使用Vector。

3.与顺序查找相比,二分查找有什么优势?使用二分查找的条件?答:对于大数据量中进行查找时二分查找比顺序查找效率高得多;条件是已排序的数组。

4.试举出三种常见的排序算法,并简单说明其排序思路。

答:①选择排序:基本思想是站在未排序列中选一个最小元素,作为已排序子序列,然后再重复地从未排序子序列中选取一个最小元素,把它加到已经排序的序列中,作为已排序子序列的最后一个元素,直到把未排序列中的元素处理完为止。

②插入排序:是将待排序的数据按一定的规则逐一插入到已排序序列中的合适位置处,直到将全部数据都插入为止。

③二分查找:将表中间位置记录的关键字与查找关键字比较,如果两者相等,则查找成功;否则利用中间位置记录将表分成前、后两个子表,如果中间位置记录的关键字大于查找关键字,则进一步查找前一子表,否则进一步查找后一子表。

重复以上过程,直到找到满足条件的记录,使查找成功,或直到子表不存在为止,此时查找不成功。

5.声明一个类People,成员变量有姓名、出生日期、性别、身高、体重等;生成10个People 类对象,并放在一个以为数组中,编写方法按身高进行排序。

//People类public class People{private String name;private String birthdaydate;private String sex;private double height;private double weight;public People(){//默认构造函数}public People(People p){=;this.birthdaydate=p.birthdaydate;this.sex=p.sex;this.height=p.height;this.weight=p.weight;}public People(String name,String birthdaydate,String sex,double height,double weight){=name;this.birthdaydate=birthdaydate;this.sex=sex;this.height=height;this.weight=weight;}public String getName() {return name;}public void setName(String name) { = name;}public String getBirthdaydate() {return birthdaydate;}public void setBirthdaydate(String birthdaydate) {this.birthdaydate = birthdaydate;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}public double getWeight() {return weight;}public void setWeight(double weight) {this.weight = weight;}public String toString(){return"姓名:"+name+"\n出生年月:"+birthdaydate+"\n性别:"+sex+"\n 身高:"+height+"\n体重:"+weight;}}//test7_5类public class test7_5 {/***@param args*/public static void main(String[] args) {// TODO Auto-generated method stubPeople[] people={new People("林楚金","1989年8月13日","男",182,63.5),new People("诸葛亮","181年7月23日","男",184,76.6),new People("迈克杰克逊","1958年8月29日","男",180,60),new People("乔丹","1963年2月17日","男",198,98.1),new People("拿破仑","1769年8月15日","男",159.5,63),new People("苍井空","1983年11月11日","女",155,45),};People temp=new People();for(int i=0;i<people.length-1;i++)for(int j=i+1;j<people.length;j++){if(people[i].getHeight()<people[j].getHeight()){temp=people[j];people[j]=people[i];people[i]=temp;}}System.out.println("按身高从小到大排序后的结果如下:");for(int i=0;i<people.length;i++)System.out.println(people[i]+"\n");}}运行结果:6.声明一个类,此类使用私有的ArrayList来存储对象。

Java语言程序设计课后习题答案全集

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程序,实现学生信息管理系统,要求包括以下功能:- 添加学生信息(姓名、年龄、性别、学号等);- 修改学生信息;- 删除学生信息;- 查询学生信息。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Java语言程序设计第七章课后习题答案1.数组的声明与数组元素的创建有什么关系?答:声明数组仅仅是代表试图创建数组,不分配任何存储空间,声明是为创建做“铺垫”。

2.Vector类的对象与数组有什么关系?什么时候适合使用数组,什么时候适合使用Vector?答:vector是一个能够存放任意对象类型的动态数组,容量能自动扩充,而数组存储固定且类型相同的对象;对于存储固定类型相同的对象使用数组,对于存储不同类型或者动态调整数组大小的情况使用Vector。

3.与顺序查找相比,二分查找有什么优势?使用二分查找的条件?答:对于大数据量中进行查找时二分查找比顺序查找效率高得多;条件是已排序的数组。

4.试举出三种常见的排序算法,并简单说明其排序思路。

答:①选择排序:基本思想是站在未排序列中选一个最小元素,作为已排序子序列,然后再重复地从未排序子序列中选取一个最小元素,把它加到已经排序的序列中,作为已排序子序列的最后一个元素,直到把未排序列中的元素处理完为止。

②插入排序:是将待排序的数据按一定的规则逐一插入到已排序序列中的合适位置处,直到将全部数据都插入为止。

③二分查找:将表中间位置记录的关键字与查找关键字比较,如果两者相等,则查找成功;否则利用中间位置记录将表分成前、后两个子表,如果中间位置记录的关键字大于查找关键字,则进一步查找前一子表,否则进一步查找后一子表。

重复以上过程,直到找到满足条件的记录,使查找成功,或直到子表不存在为止,此时查找不成功。

5.声明一个类People,成员变量有姓名、出生日期、性别、身高、体重等;生成10个People 类对象,并放在一个以为数组中,编写方法按身高进行排序。

//People类public class People{private String name;private String birthdaydate;private String sex;private double height;private double weight;public People(){//默认构造函数}public People(People p){=;this.birthdaydate=p.birthdaydate;this.sex=p.sex;this.height=p.height;this.weight=p.weight;}public People(String name,String birthdaydate,String sex,double height,double weight){=name;this.birthdaydate=birthdaydate;this.sex=sex;this.height=height;this.weight=weight;}public String getName() {return name;}public void setName(String name) { = name;}public String getBirthdaydate() {return birthdaydate;}public void setBirthdaydate(String birthdaydate) {this.birthdaydate = birthdaydate;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}public double getWeight() {return weight;}public void setWeight(double weight) {this.weight = weight;}public String toString(){return"姓名:"+name+"\n出生年月:"+birthdaydate+"\n性别:"+sex+"\n 身高:"+height+"\n体重:"+weight;}}//test7_5类public class test7_5 {/***@param args*/public static void main(String[] args) {// TODO Auto-generated method stubPeople[] people={new People("林楚金","1989年8月13日","男",182,63.5),new People("诸葛亮","181年7月23日","男",184,76.6),new People("迈克杰克逊","1958年8月29日","男",180,60),new People("乔丹","1963年2月17日","男",198,98.1),new People("拿破仑","1769年8月15日","男",159.5,63),new People("苍井空","1983年11月11日","女",155,45),};People temp=new People();for(int i=0;i<people.length-1;i++)for(int j=i+1;j<people.length;j++){if(people[i].getHeight()<people[j].getHeight()){temp=people[j];people[j]=people[i];people[i]=temp;}}System.out.println("按身高从小到大排序后的结果如下:");for(int i=0;i<people.length;i++)System.out.println(people[i]+"\n");}}运行结果:6.声明一个类,此类使用私有的ArrayList来存储对象。

使用一个Class类的引用得到第一个对象的类型之后,只允许用户插入这种类型的对象。

// Fuck类import java.util.ArrayList;public class Fuck {private ArrayList man=new ArrayList();private Class classType=null;public void add(Object f){if(man.size()==0){classType=f.getClass();}if(classType.equals(f.getClass())){man.add(f);System.out.println("插入成功.");}else{System.out.println("只允许插入"+getClassType()+"类的对象.");}}public ArrayList getMan() {return man;}public Class getClassType() {return classType;}public Fuck(){}}//test7_6public class test7_6 {public static void main(String[] args) {Fuck fuckman=new Fuck();String s=new String("林楚金");fuckman.add(s);fuckman.add(10);//测试插入插入整数fuckman.add('f');//测试插入插入字符fuckman.add("希特勒");System.out.println(fuckman.getMan());}}运行结果:7.找出一个二维数组的鞍点,即该位置上的元素在所在行上最大,在所在列上最小。

(也可能没有鞍点)//test7_7import java.util.Scanner;public class test7_7 {public static void main(String[] args) {int row, series, max;boolean T=false;Scanner cin = new Scanner(System.in);System.out.println("请输入数组的行数");row = cin.nextInt();System.out.println("请输入数组的列数");series = cin.nextInt();int[][] Array = new int[row][series];int[] R = new int[row];// 记录每行最大的数的列标int[] S = new int[series];// 记录每列最小的数的行标System.out.println("请输入数组内容");for (int i = 0; i < Array.length; i++)for (int j = 0; j < Array[i].length; j++){ Array[i][j] = cin.nextInt();if(j==series-1) {max = Array[i][0];for (int z = 1; z < series; z++)if (Array[i][z] > max){max = Array[i][z];R[i] = z;}}}for (int j = 0; j < Array[0].length; j++) {max = Array[0][j];for (int z = 1; z < row ; z++)if (Array[z][j] < max){max = Array[z][j];S[j] = z;}}for(int i=0;i<Array.length;i++){if(S[R[i]]==i){System.out.println("鞍点:"+"Array["+i+"]["+R[i]+ "]:"+Array[i][R[i]]+"\n");T=true;}}if(T==false)System.out.println("没有鞍点");}}运行结果:8. 声明一个矩阵类Matrix,其成员变量是一个二维数组,数组元素类型为int,设计下面的方法,并声明测试类对这些方法进行测试。

相关文档
最新文档