Review of Java final exam programming questions

Posted by geaser_geek on Fri, 04 Mar 2022 11:17:58 +0100

Turn:

Review of Java final exam programming questions

  1. Define the Person class in the program, and write the following fields, constructors, accessors, modifiers and other corresponding methods for this class. (20 points)

<1> Define two fields in the Person class: private access permission and name field of String type; Private access, age field of type int.
<2> Define a constructor in the Person class
The constructor has two parameters. The type of the first parameter is String and the name is name. The second type is int and the name is age. The value of the first parameter is assigned to the field name, and the value of the second parameter is assigned to the field age.
<3> Write accessor methods for the two fields of the Person class, and the method names are getName and getAge respectively. The getName method returns the value of the name field of type String; The getAge method returns the value of the age field of type int.
<4> Write modifier methods for the two fields of the Person class, with the names setName and setAge respectively. setName method has a parameter of String type, which is used to set the value of field name in the method; setAge method has an int type parameter, which is used to set the value of the field age, and ensure that the value of the field age cannot be negative. If you want to set age to negative, an error message will be given.
<5> Create an object of type Person in main() and call the above method.

class Person {
     
     
    /* Define string type name and int type age */
    private String name;
    private int age;

    /* constructor  */
    public Person(String _name, int _age) {
     
     
        age = _age;
        name = _name;
    }

    /* How to set name and age */
    public void setName(String _name) {
     
     
        name = _name;
    }

    public void setAge(int _age) {
     
     
        if (_age < 0) {
     
     
            /* If the set age is less than 0 years old, print error */
            System.out.println("error");
        } else {
     
     
            age = _age;
        }
    }

    /* How to get your name and age */
    public String getName() {
     
     
        return name;
    }

    public int getAge() {
     
     
        return age;
    }
}

public class App {
     
     
    public static void main(String[] args) {
     
     
        // Create the Person object (p1 uses the constructor method to create, p2 uses the class method to set the data)
        Person p1 = new Person("Xiao Zhang", 18);
        Person p2 = new Person(null, 0);
        // Call class method settings
        p2.setAge(21);
        p2.setName("petty thief");
        // Call get method to output information
        System.out.println(p1.getName() + "What is your age " + p1.getAge());
        System.out.println(p2.getName() + "What is your age " + p2.getAge());
    }
}

2. Given three decimals x, y and Z, please output these three numbers from large to small (20 points)

public class App {
     
     
    public static void main(String[] args) throws Exception {
     
     
        double Num1 = 10.2;
        double Num2 = 11.9;
        double Num3 = 7.8;

        double max = 0.0, min = 0.0;
        // If num1 is greater than num2 and num3, the maximum number is num1
        if (Num1 > Num2 && Num1 > Num3) {
     
     
            // If the above judgment is true, determine the minimum value from num2 and num3
            if (Num2 > Num3) {
     
     
                min = Num3;
            } else {
     
     
                min = Num2;
            }
            max = Num1;
        } else if (Num2 > Num1 && Num2 > Num3) {
     
     
            if (Num1 > Num3) {
     
     
                min = Num3;
            } else {
     
     
                min = Num1;
            }
            max = Num2;
        } else if (Num3 > Num2 && Num3 > Num1) {
     
     
            if (Num2 > Num1) {
     
     
                min = Num1;
            } else {
     
     
                min = Num2;
            }
            max = Num3;
        }
        // Print out the maximum and minimum values
        System.out.println("The maximum value is: " + max + " The minimum value is:" + min);

    }
}

3. Please n!, For example, when n=6, the output is 720 (15 points)

import java.util.Scanner;

public class App {
     
     
    public static void main(String[] args) throws Exception {
     
     
        /* Find the factorial of n */
        System.out.print("Please enter factorial value:");
        Scanner sc = new Scanner(System.in);
        // Enter a value for factoring
        int Num = sc.nextInt();
        int value = 1;
        // Factorial core program
        for (int i = 1; i < Num; i++) {
     
     
            value += i * value;
        }
        // Output factorial value
        System.out.println(Num + "factorial  = " + value);
        // Close sc class
        sc.close();
    }
}

4. Output an array in reverse order. For example, the original is {1,2,3,4,5}, and the output is {5,4,3,2,1} (15 points)

public class App {
     
     
    public static void main(String[] args) {
     
     
        int[] arrList = {
     
      1, 2, 3, 4, 5 };
        // Reverse order output
        for (int i = arrList.length - 1; i >= 0; i--) {
     
     
            System.out.print(arrList[i] + " ");
        }
    }
}

5. If the student's score is known, if the score is greater than or equal to 90, the output is excellent; If it is between 80 and 90, the output is good; If it is between 70 and 80, the output is medium; If it is between 60 and 70, the output is qualified; If it is between 0 and 60, the output will fail; If the score is less than 0 or more than 100, the output is wrong) (10 points)

import java.util.Scanner;

public class App {
     
     
    public static void main(String[] args) throws Exception {
     
     
        Scanner sc = new Scanner(System.in);
        System.out.print("Please enter student grade:");
        double scores = sc.nextDouble();
        String level;
        if (scores >= 90 && scores <= 100) {
     
     
            level = "excellent";
        } else if (scores >= 80) {
     
     
            level = "good";
        } else if (scores >= 70) {
     
     
            level = "secondary";
        } else if (scores >= 60) {
     
     
            level = "pass";
        } else if (scores >= 0) {
     
     
            level = "fail,";
        } else {
     
     
            level = "error";
        }

        System.out.println("The student's grade is:" + level);
        sc.close();
    }
}

2021-1-2 update factorial program correction (change factorial value type to double type):

Byte takes up 8 bits of one byte, and the value range is - 27 ~ 27-1

int takes up 4 bytes and 32 bits, and the value range is - 231 ~ 231-1

short takes up 2 bytes and 16 bits, and the value range is - 215 ~ 215-1

long takes up 8 bytes and 64 bits, and the value range is - 263 ~ 263-1

float occupies 4 bytes and 32 bits, and the value range is 3.402823e+38 ~ 1.401298e-45

double occupies 8 bytes and 64 bits, and the value range is 1.797693e+308~ 4.9000000e-324

char occupies 2 bytes and 16 bits, and the value range is 0 ~ 65535

boolean means true or false

import java.util.Scanner;

public class App {
     
     
    public static void main(String[] args) throws Exception {
     
     
        /* Find the factorial of n */
        System.out.print("Please enter factorial value:");
        Scanner sc = new Scanner(System.in);
        // Enter a value for factoring
        int Num = sc.nextInt();
        double value = 1;
        // Factorial core program
        for (int i = 1; i < Num; i++) {
     
     
            value += i * value;
        }
        // Output factorial value
        System.out.println(Num + "factorial  = " + value);
        // Close sc class
        sc.close();
    }
}

Turn:

Review of Java final exam programming questions


--Posted from Rpc