首页 > 代码库 > JAVA语言程序设计基础课后习题第五章

JAVA语言程序设计基础课后习题第五章

//exercise 5.1package fivechapterexercise1;public class first {    public static void main(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(int i){        return i*(3*i-1)/2;    }}//exercise 5.2package fivechapterexercise1;import java.util.Scanner;public class second {    public static void main(String[] args) {        // TODO Auto-generated method stub        Scanner in=new Scanner(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(int integer){        int sum=0;        while(integer!=0){            sum += integer%10;            integer /=10;        }                return sum;    }} //exercise 5.3package fivechapterexercise1;import java.util.Scanner;public class third {    public static void main(String[] args) {        // TODO Auto-generated method stub        Scanner in=new Scanner(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(int integer){        if(integer==reverse(integer))            return true;        else            return false;    }        public static int reverse(int integer){        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;        }        return sum;    }}//exercise 5.4package fivechapterexercise1;import java.util.Scanner;public class fourth {    public static void main(String[] args) {        // TODO Auto-generated method stub        Scanner in=new Scanner(System.in);        System.out.print("please input integer:");        int integer=in.nextInt();                System.out.print("The palindrome is:"+reverse(integer));    }        public static int reverse(int integer){        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;        }        return sum;    }}//exercise 5.5package fivechapterexercise1;public class fifth {    public static void main(String[] args) {        // TODO Auto-generated method stub        displaySortedNumbers(4.4,2.4,9.8);    }        public static void displaySortedNumbers(double num1,double num2,double num3){        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.6package fivechapterexercise1;public class sixth {    public static void main(String[] args) {        // TODO Auto-generated method stub        displayPattern(15);    }        public static void displayPattern(int n){        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.7package fivechapterexercise1;import java.util.Scanner;public class seventh {    public static void main(String[] args) {        // TODO Auto-generated method stub        Scanner in=new Scanner(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(double investmentAmount,            double monthlyInterestRate,int years){        double futureinvestmentValue=http://www.mamicode.com/investmentAmount*                (Math.pow((1+monthlyInterestRate),years));        return futureinvestmentValue;    }}//exercise 5.8package fivechapterexercise1;public class eighth {    public static void main(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(double celsius){        return (9.0/5)*celsius+32;    }        public static double fahrenheittocelsius(double fahrenheit){        return (fahrenheit-32)/(9.0/5);    }}    //exercise 5.9package fivechapterexercise1;public class ninth {    public static void main(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(double celsius){        return celsius*0.305;    }        public static double fahrenheittocelsius(double fahrenheit){        return (fahrenheit/0.305);    }}//exercise 5.10package fivechapterexercise1;import fivechapter1.seventh;public class tenth {    public static void main(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.11package fivechapterexercise2;public class first {    public static void main(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(double salesamount){        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-12package fivechapterexercise2;public class second {    public static void main(String[] args) {        // TODO Auto-generated method stub        printChars(‘1‘,‘Z‘,10);    }        public static void printChars(char ch1,char ch2,int numberPerLine){        int number=ch2-ch1+1;        final int NUMBER_OF_CHARS_PER_LINE=numberPerLine;        for(int i=0;i<number;i++){            System.out.print((char)(ch1+i)+" ");            if((i+1)%NUMBER_OF_CHARS_PER_LINE==0)                System.out.println();        }    }}//exercise 5.13package fivechapterexercise2;public class third {    public static void main(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(double integer){        double sum=0;        for(int i=1;i<=integer;i++){            sum=sum+(double)i/(i+1);        }        return sum;    }}//exercise 5-14package fivechapterexercise2;public class fourth {    public static void main(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(int integer){        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.17package fivechapterexercise2;public class seventh {    public static void main(String[] args) {        // TODO Auto-generated method stub        printMatrix(3);    }        public static void printMatrix(int n){        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.18package fivechapterexercise2;public class eighth {    public static void main(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-19package fivechapterexercise2;public class ninth {    public static void main(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,double side3){        if(side1<side2){            double temp=side1;            side1=side2;            side2=temp;        }        if(side1<side3){            double temp=side1;            side1=side3;            side3=temp;        }        if(side1<side2+side3)            return true;        else             return false;    }        public static double area(double side1,double side2,double side3){        double s=(side1+side2+side3)/2;                //calculation area        double area=Math.pow(s*(s-side1)*(s-side2)*(s-side3),0.5);                return area;    }}//exercise 5-20package fivechapterexercise2;public class tenth {    public static void main(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-21package fivechapterexercise3;import java.util.Scanner;//not precisepublic class first {    public static void main(String[] args) {        // TODO Auto-generated method stub        Scanner in=new Scanner(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<average.length;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<number.length;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);        return calculation;    }}//exercise 5-22package fivechapterexercise3;public class second {    public static void main(String[] args){        System.out.println("sqrt of 4 is "+sqrt(4));    }        public static double sqrt(double number){        double lastguess=1;        double reduce=1;        while (reduce>0.00000001){            double nextguess=(lastguess+(number/lastguess))/2;            reduce=nextguess-lastguess;            lastguess=nextguess;        }        return lastguess;    }}//exercise 5.23package fivechapterexercise3;import chenqingyuan.RandomCharacter;public class third {    public static void main(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.26package fivechapterexercise3;import chenqingyuan.math;public class sixth {    public static void main(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-27package fivechapterexercise3;import chenqingyuan.math;public class seventh {    public static void main(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++;        }    }}package fivechapterexercise3;public class eifhth {    public static void main(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.29package fivechapterexercise3;public class ninth {    public static void main(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.30package fivechapterexercise3;import chenqingyuan.math;public class tenth {    public static void main(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.32package fivechapterexercise4;public class second {    public static void main(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.33package fivechapterexercise4;import chenqingyuan.math;//System.currentTimeMillis() display is USA timepublic class third {    public static void main(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;        int mark;        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;                else                    numberofmonth=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.35package fivechapterexercise4;import java.util.Scanner;public class fifth {    public static void main(String[] args) {        // TODO Auto-generated method stub        Scanner in=new Scanner(System.in);        System.out.print("please input side:");        int side=in.nextInt();        System.out.println("This area is "+area(side));    }    public static double area(int side){        double  molecular=5*side*side;        double  denominator=4*Math.tan(Math.PI/5);        return molecular/denominator;    }}//exercise 5.36package fivechapterexercise4;import java.util.Scanner;public class sixth {    public static void main(String[] args) {        // TODO Auto-generated method stub        Scanner in=new Scanner(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,int side){        double molecular=n*side*side;        double denominator=4*Math.tan(Math.PI/n);        return molecular/denominator;    }}

 

JAVA语言程序设计基础课后习题第五章