Chapter 3 Homework

Question 1 Briefly describe the definition, steps and benefits of encapsulation

Answer: Encapsulation is to hide the state information of the class inside the class, and does not allow external programs to directly access it, and realize the operation and access to the hidden information through the methods provided by the class. Specific steps: 1. Modify the access rights of the class attributes .2, methods for creating setters (assignment) and getters (values). 3. Add access control statements to attributes in the getter. The advantage of encapsulation is that users can only access data through the methods specified by the program, hiding them. The implementation details of the class are convenient for adding access control statements and restricting unreasonable operations.

Question 2 Point out the error in the code and explain the reason for the error

The method in the class Teacher2 is not a constructor, so the instantiated object in the main() method below cannot pass parameters, and the modification method either removes the viod or removes the actual parameter.    

Question 3    points out the error in the code and explains the reason for the error

public int a(String s1,String s2) {} no return

public void a(int i, String s) {} has the same method name as private void a(int i, String mystring) {}, and the type and number of formal parameters are the same

Question 4 Write the class Student1 and the test class Student1Test

class Student1 {
private String name;
private int avg;
public void setAvg(String name,int avg) {
this.name = name;
this.avg = avg;
}
public void getMeg() {
if(avg>16) {
System. out.println("My name is:"+name+"Age is:"+avg);
}else {
System.out.println("Age is not up to standard");
}
}

}

import java.util.Scanner;
public class Student1Test{
public static void main(String[] args) {
Student1 s1 = new Student1();
Scanner in = new Scanner(System.in);
System.out.print("请输入姓名:");
String name = in.next();
System.out.print("请输入年龄:");
int avg = in.nextInt();
s1.setAvg(name, avg);
s1.getMeg();
}

}

Question 5  Write the class Student2 and the test class Student2Test

class Student2 {
private String name;
private int avg;
private String sex;
private String subject;
public Student2(String name,int avg) {
this.name = name;
this.avg = avg;
this.sex = "男";
this.subject = "Java";}public Student2(String name,int avg,String sex,String subject) {this.name = name;this.avg = avg;this.sex = sex;this.subject = subject;}public void show() {System.out.println("我的名字叫"+name+",今年"+avg+"岁,性别是"+sex+",学习的专业是:"+subject);}










}

import java.util.Scanner;
public class Student2Test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("请输入姓名:");
String name = in.next();
System.out.print("请输入年龄:");
int avg = in.nextInt();
Student2 s2 =new Student2(name, avg);
s2.show();
System.out.print("请输入姓名:");
String name1 = in.next();
System.out.print("请输入年龄:");
int avg1 = in.nextInt();
System.out.print("请输入性别:");
String sex = in.next();
System.out.print("请输入专业:");
String subject = in.next();
Student2 s3 =new Student2(name1, avg1, sex, subject);
s3.show();
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324838926&siteId=291194637