JAVA语言程序设计基础篇(Chapter 6)课后习题参考答案

发布于:2022-12-26 ⋅ 阅读:(571) ⋅ 点赞:(0)

1. (简答题)(Sum the digits in an integer) Write a method that computes the sum of the digits  in an integer. Use the following method header: public static int sumDigits(long n) For example, sumDigits(234) returns 9 (2 + 3 + 4). (Hint: Use the % operator to extract digits, and the / operator to remove the extracted digit. For instance,  to extract 4 from 234, use 234 % 10 (= 4). To remove 4 from 234, use 234 / 10 (= 23). Use a loop to repeatedly extract and remove the digit until all the digits  are extracted. Write a test program that prompts the user to enter an integer and  displays the sum of all its digits.

import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number:");
        int n = sc.nextInt();
        int sum=sumDigits(n);
        System.out.println("The sum is :"+sum);
    }
    public static int sumDigits(long n){
        int sum = 0;
        while ((int)(n%10)!=0){
            sum+=n%10;
            n=n/10;
        }
        return sum;
    }
}

 

2. (简答题)(Display an integer reversed) Write a method with the following header to display  an integer in reverse order: public static void reverse(int number) For example, reverse(3456) displays 6543. Write a test program that prompts  the user to enter an integer and displays its reversal.

import java.util.Scanner;

public class Demo2 {
    public static long reversal = 0;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number:");
        int number = sc.nextInt();
        reverse(number);
        System.out.println("The reversal is "+reversal);

    }
    public static void reverse(int number){
        while (true){
            reversal =reversal * 10 + number%10;
            number/=10;
            if (number==0){
                break;
            }
        }
    }
}

3. (简答题)(Palindrome integer) Write the methods with the following headers

// Return the reversal of an integer, i.e., reverse(456) returns 654 

public static int reverse(int number) 

// Return true if number is a palindrome 

public static boolean isPalindrome(int number) 

Use the reverse method to implement isPalindrome. A number is a palindrome if its reversal is the same as itself. Write a test program that prompts the  user to enter an integer and reports whether the integer is a palindrome.

import java.util.Scanner;

public class Demo3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number: ");
        int number =sc.nextInt();
        boolean flag = isPalindrome(number);
        System.out.println("The number is a palindrome :"+flag);
    }
    public static int reverse(int number){
        int result = 0;
        while (true){
            result=result*10+number%10;
            number/=10;
            if (number==0){
                break;
            }
        }
        return result;
    }
    public static boolean isPalindrome(int number){
        if(number==reverse(number)){
            return true;
        }else {
            return false;
        }
    }
}

4. (简答题)

(Palindromic prime) A palindromic prime is a prime number and also palindromic. For example, 131 is a prime and also a palindromic prime, as are 313 and  757. Write a program that displays the first 100 palindromic prime numbers. Display 10 numbers per line, separated by exactly one space, as follows: 

2 3 5 7 11 101 131 151 181 191 

313 353 373 383 727 757 787 797 919 929 ...

public class Demo4 {
    public static void main(String[] args) {
        boolean flag;
        int line = 0;
        int i = 2;
        while (line<=100){

            flag = true;
            for (int j = 2; j <=Math.sqrt(i); j++) {
                if (i % j == 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                if(isPalindrome(i)){
                System.out.print(i + "\t");
                line++;
                }else {
                    flag =false;
                }
            }
            if (line != 0 && flag && line % 10 == 0 ) {
                System.out.println();
            }
            i++;
        }
    }
    public static int reverse(int number){
        int result = 0;
        while (true){
            result=result*10+number%10;
            number/=10;
            if (number==0){
                break;
            }
        }
        return result;
    }
    public static boolean isPalindrome(int number){
        if(number==reverse(number)){
            return true;
        }else {
            return false;
        }
    }
}

 

5. (简答题)(Emirp) An emirp (prime spelled backward) is a nonpalindromic prime number  whose reversal is also a prime. For example, 17 is a prime and 71 is a prime, so 17  and 71 are emirps. Write a program that displays the first 100 emirps. Display 10  numbers per line, separated by exactly one space, as follows:

13 17 31 37 71 73 79 97 107 113  

149 157 167 179 199 311 337 347 359 389 ...

public class Demo5 {
    public static void main(String[] args) {
        isEmirp();
    }

    public static void isEmirp() {
        int i = 2;
        boolean flag;
        int line = 0;
        while (line <= 100) {

            flag = true;
            for (int j = 2; j <= Math.sqrt(i); j++) {
                if (i % j == 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                if (isPrime(reverse(i))&&isNonPalindrome(i)){
                    System.out.print(i + "\t");
                line++;
                }else {
                    flag=false;
                }
            }
            if (line != 0 && flag && line % 10 == 0) {
                System.out.println();
            }
            i++;
        }
    }

    public static boolean isPrime(int number) {
        int i = number;
        boolean flag = true;
        for (int j = 2; j <= Math.sqrt(i); j++) {
            if (i % j == 0) {
                flag = false;
                break;
            }
        }
        return flag;
    }



    public static int reverse(int number) {
        int result = 0;
        while (true) {
            result = result * 10 + number % 10;
            number /= 10;
            if (number == 0) {
                break;
            }
        }
        return result;
    }
    public static boolean isNonPalindrome(int number){
        if(number==reverse(number)){
            return false;
        }else {
            return true;
        }
    }
}

6. (简答题)(Check password ) Some websites impose certain rules for passwords. Write a  method that checks whether a string is a valid password. Suppose the password  rules are as follows: 

■ A password must have at least eight characters. 

■ A password consists of only letters and digits. 

■ A password must contain at least two digits. 

Write a program that prompts the user to enter a password and displays Valid Password if the rules are followed or Invalid Password otherwise.

public class Demo6 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a password :");
        String password = sc.next();
        switch (checkPassword(password)) {
            case 0:
                System.out.println("A password must have at least eight characters!");
                break;
            case 1:
                System.out.println("A password consists of only letters and digits!");
                break;
            case 2:
                System.out.println("A password must contain at least two digits!");
                break;
            default:
                System.out.println("Valid Password!");
        }
    }

    public static int checkPassword(String pass) {
        if(pass.length()>=8){
            int numDigit = 0;
            int numLetter = 0;
            for (int i = 0; i < pass.length(); i++) {
                if(Character.isDigit(pass.charAt(i))){
                    numDigit++;
                }
                if (Character.isLetter(pass.charAt(i))){
                    numLetter++;
                }
            }
            if (numDigit==0||numLetter==0){
                return 1;
            }else if (numDigit<2&&numDigit!=0){
                return 2;
            }else {
                return 3;
            }
        }else {
            return 0;
        }
    }
}

7. (简答题)(The MyTriangle class) Create a class named MyTriangle that contains the  following two methods: 

/** Return true if the sum of any two sides is   

   * greater than the third side. */ 

public static boolean isValid( double side1, double side2, double side3)

/** Return the area of the triangle. */  

public static double area( double side1, double side2, double side3) 

Write a test program that reads three sides for a triangle and computes the area if  the input is valid. Otherwise, it displays that the input is invalid.

import java.util.Scanner;

public class Demo7 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the value of side1:");
        double side1 = sc.nextDouble();
        System.out.println("Enter the value of side2:");
        double side2 = sc.nextDouble();
        System.out.println("Enter the value of side3:");
        double side3 = sc.nextDouble();
        if(isValid(side1,side2,side3)){
            System.out.println("The area of the Triangle is "+area(side1,side2,side3));
        }else {
            System.out.println("The input is invalid!");
        }
    }
    public static boolean isValid(double side1,double side2,double side3){
        double [] arr = new double[3];
        arr[0]=side1;
        arr[1]=side2;
        arr[2]=side3;
        for (int i = 0; i < arr.length-1; i++) {
            for(int j =0;j<arr.length-1-i;j++){
                if(arr[j]>arr[j+1]){
                    double temp = arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp;
                }
            }
        }
        if((arr[0]+arr[1])>arr[2]){
            return true;
        }else {
            return false;
        }
    }
    public static double area(double side1,double side2,double side3){
        double p = (side1 + side2 + side3)/2;
        double x1 = p-side1;
        double x2 = p-side2;
        double x3 = p-side3;
        double s1 = p*x1*x2*x3;
        double area = Math.sqrt(s1);
        return area;
    }
}


网站公告

今日签到

点亮在社区的每一天
去签到