面向对象程序设计——Java第二版习题答案
- 格式:pdf
- 大小:1.70 MB
- 文档页数:34


第一章1.public class BegintoLearn{public static void main(String args[]) {System.out.println("I’d like to study Java ! ");}}2.public class TraStar{public static void main(String args[]) {System.out.println("*");System.out.println("* *");System.out.println("* * *");System.out.println("* * * *");System.out.println("* * * * *");}}第二章1.public class ProNum{public static void main(String args[]) {float p;long q;int m=10,n=5;System.out.println("m="+m+" n="+n);m=m+3;n=n+5;System.out.println("m="+m+" n="+n);p=(float)(m+n)/2;System.out.println("p="+p);q=(m*m*n*n);System.out.println("q="+q);}}2.class SphereClass {public static void main(String args[]) {int r=3;double v;v=3.14*r*r*r*4/3;System.out.println("V="+v)}}第三章1. import java.io.*;public class InNum{public static void main(String args[]) throws IOException{ int num1,num2;String str;BufferedReader buf;buf=new BufferedReader(new InputStreamReader(System.in));System.out.print("Input first integer:");str=buf.readLine();num1=Integer.parseInt(str);System.out.print("Input second integer:");str=buf.readLine();num2=Integer.parseInt(str);System.out.println("The sum is "+(num1+num2));}}2.import java.io.*;public class Grade{public static void main(String args[]) throws IOException{ int score;char ch;String str;BufferedReader buf;buf=new BufferedReader(new InputStreamReader(System.in));System.out.print("Input the score(0-100):");str=buf.readLine();score=Integer.parseInt(str);if (score>=90)ch='A';else if (score>=75)ch='B';else if (score>=60)ch='C';elsech='D';System.out.print("The Grade is "+ch);}}3.public class ShuLie{public static void main(String args[]){int a ,q ,n;q=(150-135)/5;a=(135-20*q)/5;for (n=0;n<10;n++)System.out.print((a+n*q)+" ");}}4.public class Pyramid{public static void main(String args[]){int i,j,k;for(i=0;i<=4;i++){for ( j=0;j<20-i;j++)System.out.print(" ");for (k=0;k<=2*i;k++)if (k<=i)System.out.print(" "+(2*k+1));elseSystem.out.print(" "+(2*(2*i-k)+1));System.out.println();}}}5.import java.io.*;public class PrimeNumber{public static void main(String args[]) throws IOException{ int n=0;int m;String str;boolean mark=false;BufferedReader buf;buf=new BufferedReader(new InputStreamReader(System.in));System.out.print("Input the m:");str=buf.readLine();m=Integer.parseInt(str);for(int i=3;i<=m;i+=2){for(int j=2;j<i;j++){if(i%j==0){mark=true;break;}}if (!mark){System.out.print(" "+i);n++; //outputanewlineif(n%10==0) //after10numbersSystem.out.println();}mark=false;}System.out.println();}}6.import java.io.*;public class Factor12{public static void main(String args[]) throws IOException{ int m;String str;BufferedReader buf;buf=new BufferedReader(new InputStreamReader(System.in));System.out.print("Input the m:");str=buf.readLine();m=Integer.parseInt(str);System.out.print(m+"\'s factors are: ");System.out.println( );for(int i=1;i<=m;i++)if (m%i==0)System.out.print(" "+i);}}7.public class MSteel{public static void main(String args[]){int d=0;float m=2000;while (m>=5) {m=m/2;d++;System.out.print(d+": ");System.out.println(m);}System.out.print("You need "+d+" days");}}8.public class AlmostPi{public static void main(String args[]){int n;long m;double s,t;n=1;m=0;s=0;do{t=(double)n/(2*m+1);m++;n=-n;s=s+t;} while (4*s-3.14159> 0.0000001 || 4*s-3.14159< -0.0000001);System.out.println(m);}}9.public class LSRnd{public static void main(String args[]){int mun,n,max1,min1;max1=0;min1=100;for (n=1;n<=10;n++){mun=(int)(100*Math.random());System.out.print(mun+" ");if (mun>max1)max1=mun;if (mun<min1)min1=mun;}System.out.println();System.out.println("The largest number is: "+max1);System.out.println("The smallest number is: "+min1);}}10.import java.io.*;public class StrArry{public static void main(String args[]) throws IOException{int m;String str;Stringmonth[]={"January","February","March","April","May","June","July","August","september","October","November","December"};BufferedReader buf;buf=new BufferedReader(new InputStreamReader(System.in));System.out.print("Input the m:");str=buf.readLine();m=Integer.parseInt(str);if (m>=1 && m<=12)System.out.println(month[m-1]);elseSystem.out.print("Your Input is wrong");}}第四章1.import java.util.*;public class Person{private String name;private char sex;private int year,month;public Person( ){}public Person(String nm,char sx,int y,int m) {name=nm;sex=sx;year=y;month=m;}public void printPerson( ) {Calendar now=Calendar.getInstance();int age=now.get(Calendar.YEAR)-year;System.out.println("Name: "+name+",Sex: "+sex+", Age: "+age);}public static void main(String args[]){Person pe1=new Person("Tom",'m',1980,10);pe1.printPerson();}}2.public class Rectangle{double width,length,girth,area;public Rectangle(){};public Rectangle(double wd,double le) {width=wd;length=le;}public void setWidth(double wd) {width=wd;}public void setLength(double le) {length=le;}public double getWidth( ) {return width;}public double getLength( ) {return length;}public double girth(){return 2*(width+length);}public double area(){return width*length;}public void printRectangle(){System.out.println("Width="+width+" ,Length="+length);}public static void main(String args[]){Rectangle re1=new Rectangle(10,20);Rectangle re2=new Rectangle();re2.setWidth(3);re2.setLength(4);re1.printRectangle();System.out.println("Girth="+re1.girth()+",Area="+re1.area());re2.printRectangle();System.out.println("Girth="+re2.girth()+",Area="+re2.area());}}3.public class Matrix{ private int mx[][],m,n; public Matrix(int r,int c) { m=r;n=c;mx=new int[m][n];iniMatrix();}public Matrix(){m=3;n=3;mx=new int[3][3];iniMatrix();}public void iniMatrix(){ int i,j;for(i=0;i<=m-1;i++)for(j=0;j<=n-1;j++)mx[i][j]=(int)(Math.random()*100); }public void tranMatrix(){int i,j,t;int mt[][]=new int[m][n];for(i=0;i<=m-1;i++)for(j=0;j<=n-1;j++)mt[i][j]=mx[i][j];t=m;m=n;n=t;mx=new int[m][n];for(i=0;i<=m-1;i++)for(j=0;j<=n-1;j++)mx[i][j]=mt[j][i];}public void printMatrix(){int i,j;for(i=0;i<=m-1;i++){for(j=0;j<=n-1;j++)System.out.print(" "+mx[i][j]);System.out.println();}}public void addMatrix(Matrix b) {int i,j;for(i=0;i<=m-1;i++)for(j=0;j<=n-1;j++)mx[i][j]=mx[i][j]+b.mx[i][j];} public static void main(String args[]){Matrix ma=new Matrix(4,3);Matrix mb=new Matrix(4,3);System.out.println("The matrix_A:"); ma.printMatrix();System.out.println("The matrix_B:"); mb.printMatrix();System.out.println("Matrix_A +Matrix_B:");ma.addMatrix(mb);ma.printMatrix();System.out.println("Transpose Matrix_B:");mb.tranMatrix();mb.printMatrix();}}4.public class Substring{public static void main(String args[]){String str1=new String("addActionListener");String str2=new String("Action");int n;n=str1.indexOf(str2);if(n>=0){System.out.println("String_2 is in String_1");System.out.println("The substring before String_2 is "+str1.substring(0,n));System.out.println("The substring behind String_2 is "+str1.substring(n+str2.length( )));}}} 五、写出程序运行后的结果1. 2.。