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

//exercise 5.1
package 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.2
package 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.3
package 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.4
package 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.5
package 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.6
package 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.7
package 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=investmentAmount*
                (Math.pow((1+monthlyInterestRate),years));
        return futureinvestmentValue;
    }

}

//exercise 5.8
package 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.9
package 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.10
package 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.11
package 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-12
package 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.13
package 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-14
package 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.17
package 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.18
package 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-19
package 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-20
package 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-21
package fivechapterexercise3;
import java.util.Scanner;

//not precise
public 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-22
package 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.23
package 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.26
package 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-27
package 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.29
package 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.30
package 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.32
package 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.33
package fivechapterexercise4;
import chenqingyuan.math;
//System.currentTimeMillis() display is USA time
public 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.35
package 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.36
package 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;
    }
}
时间: 2024-12-06 21:47:53

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

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

//exercise 4.1 package fourthchapterexercise1; import java.util.Scanner; public class first { public static void main(String[] args) { // TODO Auto-generated method stub int integer=0,sum=0,positive=0,negative=0; Scanner in=new Scanner(System.in); Sy

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

//exercise 3.1 package thirdchapterexercise1; import java.util.Scanner; public class first { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in =new Scanner(System.in); System.out.print("Enter a,b,c:"); double

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

//exercise 6.1 package second; import java.util.Scanner; public class first { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("Enter the number of st

java语言程序设计基础课后习题第二章

1 //exercise 2.2 2 package secondchapterexercise1; 3 4 public class first01 { 5 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 double miles=100; 9 double KILOMETERS_PER_MILE=1.609; 10 double kilometers=miles*KILOMET

Java语言程序设计基础篇 方法(五)

生成随机字符 生成随机字符就是生成0到65535之间的一个随机整数,因为0<=Math.random()<1.0,必须在65535+1 (int) (Math.random() * (65535+1)) 随机生成小写字母 public class RandomCharacter { public static char getRandomCharacter(char ch1,char ch2){ return (char)(ch1 +Math.random() * (ch2 - ch1 + 1

Java语言程序设计基础篇 循环(四)

①打印:***** **** *** ** * for(int x=1; x<=5; x++) { for(int y=x; y<=5; y++) { System.out.print("*"); //向下一般的格式for(int y=x; y<=5; y++) } System.out.println(); } ②打印:* ** *** **** ***** for (int x=1; x<=5 ;x++ ) { for (int y=1;y<=x ;y

Java语言程序设计基础篇 循环(四)练习

*4.21(计算不同利率下的贷款)编写程序,让用户输入贷款总额及以年为单位的贷款期限,以1/8为递增量,显示从5%到8%的利率下每月支付额和总偿还额.假设输入贷款总量为10 000,还贷期限为5年,所显示的输出如下: 贷款总额:to 000 年数:5 利率月支付额总偿还额 5%188 .71   11322.74 5 .125%189.28   11357.13 5 .25%189.85   11391.59 ... //Exercise3_26.java: displays the month

Java语言程序设计基础篇 数组(六)

Java语法之数组 数组的定义 数组是对象. 如:int [ ]  x = new int[100];或 :int x [ ]  = new int[100];(这种方式主要是为了适应C/C++程序员) 声明一个数组变量:int [ ] x;并不会在内存中给数组分配任何空间,仅创建一个引用数组的存储地址. 数组创建后,其元素赋予默认值,数值型基本数据类型默认值为0,char类型为'\u0000',boolean类型为false. 数组的静态初始化 如:int [ ] x = new int [

Java语言程序设计基础篇第10版第5章习题答案

1 public class Demo { 2 public static void main(String[] args) { 3 java.util.Scanner input = new java.util.Scanner(System.in); 4 int num = input.nextInt(); 5 int count=0; 6 int a=0,b=0; 7 float sum=0; 8 while(num!=0){ 9 if(num>0) 10 a++; 11 else 12 b