2020-7-16 Java encapsulation exercise

2020-7-16 Java encapsulation exercise

1, Select

  1. When a member method in a class is modified by which of the following modifiers, the method can only be accessed in this class (B)
    A. public
    B. private
    C. final
    D. default

  2. Run the following Java code. The correct statement is (A)

    A. 15
    B. The program runs normally, but there is no output
    C. Compilation error
    D. Operation error

  3. Meaning of java encapsulation (multiple choice) (ABC)
    A. Prevent users from modifying system properties by mistake
    B. Improve system independence
    C. Improve software reusability
    D. Increase the risk of building large systems

  4. The running result of the following code is (A)

    A. I am a student.
    B. I am a student about to graduate from university.
    C. I am a student.
    I am a student about to graduate from university.
    D. Compilation error

  5. About package+ package name; What's not true is (A)
    A. A java source file can have multiple package statements
    B. It is suggested that all package names should be in English lowercase
    C. Suggested package name naming method: domain name flashback + module + function
    D. "package+ package name;" Must be placed in the first line of the java source file

  6. Which of the following is the correct declaration order for import, class and package (A)
    A. package,import,class
    B. class,import,package
    C. import,package,class
    D. package,class,import

  7. The following code runs as a result: (A)

    A. Compilation error
    B. Output: c
    C. The compilation is normal, and an error occurs when running
    D. The compilation is normal, and there is no output result when running

  8. The following statement about static is incorrect (A)
    A. Static methods can be defined, but static variables cannot be defined
    B. static cannot be used as modifier before class
    C. static can change a normal member method into a static method
    D. static can change a normal method into a class method

  9. For static and non static methods, the following description is correct (D)
    A. Non static methods can only access non static variables
    B. Static methods can directly access either static variables of this class or non static variables of this class
    C. Static methods can only be called by class name outside the class
    D. Non static methods can only be called through objects outside the class

  10. In the following code, the reason for the error is (multiple choices) (ACD)

    A. No parameters passed in
    B. Class method is not used to call
    C. Static references cannot be used to access non static methods
    D. No error will be reported as long as the object is instantiated

2, Programming

  1. Writing custom class to realize book information setting
    Task:
    Attribute: title, author, publishing house, price
    Method: information introduction
    Requirements:
    1. design constructor to assign value to attribute
    2. set private attributes, and use the get/set method to access attributes
    3. the limited book price must be greater than 10. If it is invalid, you need to prompt and force a value of 10
    4. restricted author and book title are read-only
    5. information introduction method describe all information of the book
 public class Book {
	//Private attribute: title, author, publishing house, price
	private String name;
	private String anthor;
	private String press;
	private double price;
	//Attribute assignment by construction method
	public Book(String press, double price) {
		this.getAnthor();
		this.getName();
		this.setPress(press);
		this.setPrice(price);
	}
	/*The public get/set method is used to access attributes, where:
	  1,The restricted book price must be greater than 10. If it is invalid, you need to prompt and force a value of 10
	  2,Restricted author and book title are read-only
	*/
	public String getName() {
		this.name = "Holy ruins";
		return name;
	}
	public String getAnthor() {
		this.anthor = "Chendong";
		return anthor;
	}
	public String getPress() {
		return press;
	}
	public void setPress(String press) {
		this.press = press;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		if(price < 10){
			System.out.println("The price of books cannot be less than 10!");
			this.price = 10;
		}else{
			this.price = price;
		}
	}
	//Information introduction method, describing all information of the book
	public String info(){
		String str = "Book information:\n Title:<" + this.name + ">";
		str += "\n Author:" + this.anthor;
		str += "\n Press:" + this.press;
		str += "\n Price:" + this.price;
		return str;	
	}
	public static void main(String[] args){
		Book book = new Book("Xinhua Publishing House",18.5);
		System.out.println(book.info());
	}
}
  1. Write a user-defined class to implement the user information class.
    Task:
    User class:
    Attribute: user name, password
    User management class:
    Method: user information verification
    Requirements:
    1. Designing constructors to assign values to attributes
    2. Set the private property, and use the get/set method to access the property
    3. User information verification determines whether the two user information are consistent. When the user name and password are consistent, return: the user name and password are consistent; otherwise, return: the user name or password are inconsistent

PS:
You can't use = = to judge the string. You can use the equals() method to judge the string content. If the content is equal, the return value is true, otherwise it is false. For example, when str represents the user's gender, you can use the following code to judge whether the gender is "male" or "female"

Code run result reference:

class People {
	//Set private properties
	private String name;
	private int password;
	
	//Set get/set method
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getPassword() {
		return password;
	}

	public void setPassword(int password) {
		this.password = password;
	}
	
	//Construction method
	public People(String name, int password) {
		this.setName(name);
		this.setPassword(password);
	}
	
	//User information introduction
	public void info1() {
		System.out.println("User name:" + this.name);
		System.out.println("Password:" + this.password);
	}
}

public class peop_manage {
	public static void main(String[] args) {
		//Assign values to user information
		People peo1 = new People("Lucy", 123456);
		People peo2 = new People("Mike", 123456);
		//Print user information
		peo1.info1();
		peo2.info1();
		
		System.out.println("=================");
		
		//Judge whether the user information is consistent
		//Get user name and password
		String peoName1 = peo1.getName();
		String peoName2 = peo2.getName();
		int perPass1 = peo1.getPassword();
		int prePass2 = peo2.getPassword();
		if(peoName1.equals(peoName2) && perPass1 == prePass2){
			System.out.println("User name and password are consistent");
		}else{
			System.out.println("Inconsistent user name or password");
		}
	}
}

Tags: Java

Posted by Jeff4507 on Mon, 30 May 2022 03:16:46 +0530