//exercise 5.1
packagefivechapterexercise1;public classfirst {public static voidmain(String[] args) {//TODO Auto-generated method stub
final int NUMBER_OF_PENTAGONAL_PER_LINE=10;for(int i=1;i<=100;i++){
System.out.print(getpentagonalnumber(i)+" ");if(i%NUMBER_OF_PENTAGONAL_PER_LINE==0)
System.out.println();
}
}public static int getpentagonalnumber(inti){return i*(3*i-1)/2;
}
}//exercise 5.2
packagefivechapterexercise1;importjava.util.Scanner;public classsecond {public static voidmain(String[] args) {//TODO Auto-generated method stub
Scanner in=newScanner(System.in);
System.out.print("input a integer :");int integer=in.nextInt();
System.out.println("The sum of digits is "+getsumdigits(integer));
}public static int getsumdigits(intinteger){int sum=0;while(integer!=0){
sum+= integer%10;
integer/=10;
}returnsum;
}
}//exercise 5.3
packagefivechapterexercise1;importjava.util.Scanner;public classthird {public static voidmain(String[] args) {//TODO Auto-generated method stub
Scanner in=newScanner(System.in);
System.out.print("please input a integer:");int integer=in.nextInt();
System.out.println("This integer is a palindrome?"+ispalindrome(integer));
}public static boolean ispalindrome(intinteger){if(integer==reverse(integer))return true;else
return false;
}public static int reverse(intinteger){int count=0;int temp=integer;while(integer!=0){
integer/=10;
count++;
}int sum=0;for(int i=count;i>=1;i--){
sum=sum+(int)(Math.pow(10, i-1))*(temp%10);
temp/= 10;
}returnsum;
}
}//exercise 5.4
packagefivechapterexercise1;importjava.util.Scanner;public classfourth {public static voidmain(String[] args) {//TODO Auto-generated method stub
Scanner in=newScanner(System.in);
System.out.print("please input integer:");int integer=in.nextInt();
System.out.print("The palindrome is:"+reverse(integer));
}public static int reverse(intinteger){int count=0;int temp=integer;while(integer!=0){
integer/=10;
count++;
}int sum=0;for(int i=count;i>=1;i--){
sum=sum+(int)(Math.pow(10, i-1))*(temp%10);
temp/= 10;
}returnsum;
}
}//exercise 5.5
packagefivechapterexercise1;public classfifth {public static voidmain(String[] args) {//TODO Auto-generated method stub
displaySortedNumbers(4.4,2.4,9.8);
}public static void displaySortedNumbers(double num1,double num2,doublenum3){if(num1>num2){double temp=num1;
num1=num2;
num2=temp;
}if(num1>num3){double temp=num1;
num1=num3;
num3=temp;
}if(num2>num3){double temp=num2;
num2=num3;
num3=temp;
}
System.out.println("Ascending order number three:"+num1+","+num2+","+num3+".");
}
}//exercise 5.6
packagefivechapterexercise1;public classsixth {public static voidmain(String[] args) {//TODO Auto-generated method stub
displayPattern(15);
}public static void displayPattern(intn){for(int i=1;i<=n;i++){for(int j=i+1;j<=n;j++){
System.out.print(" ");
}for(int j=i;j>=1;j--){
System.out.printf("%3d", j);
}
System.out.println();
}
}
}//exercise 5.7
packagefivechapterexercise1;importjava.util.Scanner;public classseventh {public static voidmain(String[] args) {//TODO Auto-generated method stub
Scanner in=newScanner(System.in);
System.out.print("input investment amount and annually interest rate:");int investmentAmount=in.nextInt();double annuallyInterestrate=in.nextDouble()/1200;
System.out.println("Years\tFuture Value");for(int i=1;i<=30;i++){
System.out.println(i+" \t"+futureInvestmentValue(investmentAmount,annuallyInterestrate,i));
}
}public static double futureInvestmentValue(doubleinvestmentAmount,double monthlyInterestRate,intyears){double futureinvestmentValue=investmentAmount*(Math.pow((1+monthlyInterestRate),years));returnfutureinvestmentValue;
}
}//exercise 5.8
packagefivechapterexercise1;public classeighth {public static voidmain(String[] args) {//TODO Auto-generated method stub
double celsius=40.0;double fahrenheit=120.0;
System.out.println("摄氏度\t华氏度\t 华氏度\t摄氏度");for(int i=1;i<=10;i++){
System.out.println(celsius+"\t"+((int)(celsiustofahrenheit(celsius)*10)/10.0)+"\t "+fahrenheit+" "+((int)(fahrenheittocelsius(fahrenheit)*10)/10.0));
celsius--;
fahrenheit-=10;
}
}public static double celsiustofahrenheit(doublecelsius){return (9.0/5)*celsius+32;
}public static double fahrenheittocelsius(doublefahrenheit){return (fahrenheit-32)/(9.0/5);
}
}//exercise 5.9
packagefivechapterexercise1;public classninth {public static voidmain(String[] args) {//TODO Auto-generated method stub
double celsius=1.0;double fahrenheit=20.0;
System.out.println("英尺\t米\t 米\t英尺");for(int i=1;i<=10;i++){
System.out.println(celsius+"\t"+((int)(celsiustofahrenheit(celsius)*10)/10.0)+"\t "+fahrenheit+" "+((int)(fahrenheittocelsius(fahrenheit)*10)/10.0));
celsius++;
fahrenheit+=5;
}
}public static double celsiustofahrenheit(doublecelsius){return celsius*0.305;
}public static double fahrenheittocelsius(doublefahrenheit){return (fahrenheit/0.305);
}
}//exercise 5.10
packagefivechapterexercise1;importfivechapter1.seventh;public classtenth {public static voidmain(String[] args) {//TODO Auto-generated method stub
int count=0;for(int i=1;i<=10000;i++){if(seventh.isPrime(i))
count++;
}
System.out.println("The number of prime is "+count);
}
}//exercise 5.11
packagefivechapterexercise2;public classfirst {public static voidmain(String[] args) {//TODO Auto-generated method stub
System.out.println("销售总额\t\t酬金");int salesamount=10000;for(int i=1;i<20;i++){
System.out.println(salesamount+"\t\t"+computecommission(salesamount));
salesamount+=5000;
}
}public static double computecommission(doublesalesamount){if(salesamount<5000)return salesamount*0.08;else if(salesamount<10000)return 5000*0.08+(salesamount-5000)*0.10;else
return 5000*0.08+5000*0.10+(salesamount-10000)*0.12;
}
}//exercise 5-12
packagefivechapterexercise2;public classsecond {public static voidmain(String[] args) {//TODO Auto-generated method stub
printChars('1','Z',10);
}public static void printChars(char ch1,char ch2,intnumberPerLine){int number=ch2-ch1+1;final int NUMBER_OF_CHARS_PER_LINE=numberPerLine;for(int i=0;i
System.out.print((char)(ch1+i)+" ");if((i+1)%NUMBER_OF_CHARS_PER_LINE==0)
System.out.println();
}
}
}//exercise 5.13
packagefivechapterexercise2;public classthird {public static voidmain(String[] args) {//TODO Auto-generated method stub
System.out.println("i\t\tm(i)");for(int i=1;i<=20;i++){
System.out.println(i+"\t\t"+m(i));
}
}public static double m(doubleinteger){double sum=0;for(int i=1;i<=integer;i++){
sum=sum+(double)i/(i+1);
}returnsum;
}
}//exercise 5-14
packagefivechapterexercise2;public classfourth {public static voidmain(String[] args) {//TODO Auto-generated method stub
System.out.println("i\t\tm(i)");int number=10;for(int i=1;i<=10;i++){
System.out.println(number+"\t\t"+m(number));
number+=10;
}
}public static double m(intinteger){double sum=0;for(int i=1;i<=integer+1;i++){
sum=sum+(Math.pow(-1,i-1)*(1.0/(2*i-1)));
}return 4*sum;
}
}//exercise 5.17
packagefivechapterexercise2;public classseventh {public static voidmain(String[] args) {//TODO Auto-generated method stub
printMatrix(3);
}public static void printMatrix(intn){for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){
System.out.print((int)(Math.random()*2)+" ");
}
System.out.println();
}
}
}//exercise 5.18
packagefivechapterexercise2;public classeighth {public static voidmain(String[] args) {//TODO Auto-generated method stub
System.out.println("number\t\tsqir");for(int i=0;i<=10;i++){
System.out.println(2*i+"\t\t"+Math.sqrt(2*i));
}
}
}//exercise 5-19
packagefivechapterexercise2;public classninth {public static voidmain(String[] args) {//TODO Auto-generated method stub
double side1=2,side2=3,side3=4;
System.out.println("side1=2,side2=3,side3=4 is a triangle?"+isvalid(side1,side2,side3));
}public static boolean isvalid(double side1,double side2,doubleside3){if(side1
side1=side2;
side2=temp;
}if(side1
side1=side3;
side3=temp;
}if(side1
return false;
}public static double area(double side1,double side2,doubleside3){double s=(side1+side2+side3)/2;//calculation area
double area=Math.pow(s*(s-side1)*(s-side2)*(s-side3),0.5);returnarea;
}
}//exercise 5-20
packagefivechapterexercise2;public classtenth {public static voidmain(String[] args) {//TODO Auto-generated method stub
final double RADIANS=Math.PI/180.0;int angle=0;
System.out.println("angle\tsine\tcosine");for(int i=0;i<=36;i++){
System.out.println(angle+"\t"+Math.sin(angle*RADIANS)+"\t"+Math.cos(angle*RADIANS));
angle+= 10;
}
}
}//exercise 5-21
packagefivechapterexercise3;importjava.util.Scanner;//not precise
public classfirst {public static voidmain(String[] args) {//TODO Auto-generated method stub
Scanner in=newScanner(System.in);
System.out.print("Enter ten numbers:");double []number=new double[10];for(int i=0;i<10;i++){
number[i]=in.nextDouble();
}
System.out.println("The mean is "+average(number));
System.out.println("The standard deviation is "+standarddeviation(number));
}public static double average(double...average){double sum=0;for(int i=0;i
sum=sum+average[i];
}return sum/average.length;
}public static double standarddeviation(double...number){double sum=0;double squaresum=0;for(int i=0;i
squaresum+=Math.sqrt(number[i]);
sum+=number[i];
}double num1=squaresum-Math.sqrt(sum)/(number.length);double num2=num1/(number.length-1);double calculation=Math.pow(num2,0.5);returncalculation;
}
}//exercise 5-22
packagefivechapterexercise3;public classsecond {public static voidmain(String[] args){
System.out.println("sqrt of 4 is "+sqrt(4));
}public static double sqrt(doublenumber){double lastguess=1;double reduce=1;while (reduce>0.00000001){double nextguess=(lastguess+(number/lastguess))/2;
reduce=nextguess-lastguess;
lastguess=nextguess;
}returnlastguess;
}
}//exercise 5.23
packagefivechapterexercise3;importchenqingyuan.RandomCharacter;public classthird {public static voidmain(String[] args) {//TODO Auto-generated method stub
for(int i=0;i<100;i++){if(i%10==0)
System.out.println();
System.out.print(RandomCharacter.getRandomUpperCaseLetter()+" ");
}
System.out.println("\n\n");for(int i=0;i<100;i++){if(i%10==0)
System.out.println();
System.out.print(RandomCharacter.getRandomDigitCharacter()+" ");
}
}
}//exercise 5.26
packagefivechapterexercise3;importchenqingyuan.math;public classsixth {public static voidmain(String[] args) {//TODO Auto-generated method stub
int count=0;int integer=1;while(count<100){if(math.isPrime(integer)&&math.isPalindrome(integer)){if(count%10==0)
System.out.println();
System.out.print(integer+" ");
count++;
}
integer++;
}
}
}//exercise 5-27
packagefivechapterexercise3;importchenqingyuan.math;public classseventh {public static voidmain(String[] args) {//TODO Auto-generated method stub
int count=0;int integer=1;while(count<100){if(math.isPrime(integer)&&math.isPrime(math.reverse(integer))&&!math.isPalindrome(integer)){if(count%10==0)
System.out.println();
System.out.print(integer+" ");
count++;
}
integer++;
}
}
}packagefivechapterexercise3;public classeifhth {public static voidmain(String[] args) {//TODO Auto-generated method stub
System.out.println("p\t\t2^p-1");for(int p=1;p<=31;p++){int number=(int)Math.pow(2,p)-1;if(chenqingyuan.math.isPrime(number))
System.out.println(p+"\t\t"+number);
}
}
}//exercise 5.29
packagefivechapterexercise3;public classninth {public static voidmain(String[] args) {//TODO Auto-generated method stub
int num1=(int)(Math.random()*6)+1;int num2=(int)(Math.random()*6)+1;int sum=num1+num2;
System.out.print("You rolled "+num1+" + "+num2+" = "+sum);
System.out.println();if(sum==3||sum==12){
System.out.println("You lose");
System.exit(0);
}else if(sum==7||sum==11){
System.out.println("You win");
System.exit(0);
}else{while(true){int num3=(int)(Math.random()*6)+1;int num4=(int)(Math.random()*6)+1;int add=num3+num4;if(sum==add){
System.out.println("point is "+sum);
System.out.print("You rolled "+num3+" + "+num4+" = "+add);
System.out.println();
System.out.println("You win");
System.exit(0);
}else if (add==7){
System.out.println("point is "+sum);
System.out.print("You rolled "+num3+" + "+num4+" = "+add);
System.out.println();
System.out.println("You lose");
System.exit(0);
}
}
}
}
}//exercise 5.30
packagefivechapterexercise3;importchenqingyuan.math;public classtenth {public static voidmain(String[] args) {//TODO Auto-generated method stub
for(int i=1;i<1000;i++){if(math.isPrime(i)&&math.isPrime(i+2))
System.out.println("("+i+","+(i+2)+")");
}
}
}//exercise 5.32
packagefivechapterexercise4;public classsecond {public static voidmain(String[] args) {//TODO Auto-generated method stub
int count=0;for(int i=1;i<=10000;i++){int num1=(int)(Math.random()*6)+1;int num2=(int)(Math.random()*6)+1;int sum=num1+num2;if(sum==3||sum==12){continue;
}else if(sum==7||sum==11){
count++;continue;
}else{while(true){int num3=(int)(Math.random()*6)+1;int num4=(int)(Math.random()*6)+1;int add=num3+num4;if(sum==add){
count++;break;
}else if (add==7){break;
}
}
}
}
System.out.println("The number if times you win is "+count);
}
}//exercise 5.33
packagefivechapterexercise4;importchenqingyuan.math;//System.currentTimeMillis() display is USA time
public classthird {public static voidmain(String[] args) {//TODO Auto-generated method stub
long millisecond=System.currentTimeMillis();long second=millisecond/1000%60;long minute=millisecond/1000/60%60;//+8 is to solve the time difference
long hour=(millisecond/3600/1000+8)%24;long day=(millisecond/3600/1000+8)/24;//judge year
long daya=day-730;long day1=daya%1461;long count=daya/1461;//judge year
long years=1970+2+4*count;if(day1>366){
years=years+1;
day1-= 366;
}while(day1>365){
years+=1;
day1-= 365;
}//resolve error !!To solve the time difference
day1 +=1;//judge month
int month=1;intmark;while(true){if(month==1||month==3||month==5||month==7||month==8
||month==10||month==12){
mark=(int)day1/32;if(mark!=0){
day1-= 31;
month++;
}else
break;
}else if(month==2){int numberofmonth=0;if(!math.isLeapYear((int)years))
numberofmonth=28;elsenumberofmonth=29;
mark=(int)day1/(numberofmonth+1);if(mark!=0){
day1-=numberofmonth;
month++;
}else
break;
}else{
mark=(int)day1/31;if(mark!=0){
day1-= 30;
month++;
}else
break;
}
}
System.out.println("Current date and time is "+math.getMonthName(month)+" "+day1+","+years+" "+hour+":"+minute+":"+second);
}
}//exercise 5.35
packagefivechapterexercise4;importjava.util.Scanner;public classfifth {public static voidmain(String[] args) {//TODO Auto-generated method stub
Scanner in=newScanner(System.in);
System.out.print("please input side:");int side=in.nextInt();
System.out.println("This area is "+area(side));
}public static double area(intside){double molecular=5*side*side;double denominator=4*Math.tan(Math.PI/5);return molecular/denominator;
}
}//exercise 5.36
packagefivechapterexercise4;importjava.util.Scanner;public classsixth {public static voidmain(String[] args) {//TODO Auto-generated method stub
Scanner in=newScanner(System.in);
System.out.print("input number of side and side:");int numberofside=in.nextInt();int side=in.nextInt();
System.out.println("This area is "+area(numberofside,side));
}public static double area(int n,intside){double molecular=n*side*side;double denominator=4*Math.tan(Math.PI/n);return molecular/denominator;
}
}