Question: Write in Java: Please define a Class, the requirements are as follows: Class name is Student Class is public class Has several private properties: (1)

Write in Java:

Please define a Class, the requirements are as follows: Class name is Student Class is public class

Has several private properties: (1) Grade attribute, type int, attribute name is grade (2) Name attribute, type is String, attribute name is name

Student has a public Constructor: (1) Have a Constructor without any parameters, this Constructor is public The function of the Constructor sets the grade to 1 and the name to "NoName". (2) Have a constructor that requires 2 parameters. The parameters are int, String, which represent the initial value of the grade and name respectively. This Constructor is public. The function of the Constructor initializes the attribute grade, name to the entered grade and name.

Student has a public method: (1) has a method that can return the grade value, the method is public, the method name is getGrade, the method has no input parameters, and the grade value of the int type can be returned.

(2) has a method that can set the grade value, the method is public, the method name is setGrade, the method needs an int type parameter, can return the value of the boolean type, indicating whether the setting is successful.

(3) has a method that can return the name value, the method is public, the method name is getName, the method has no input parameters, and the name value of the String type can be returned.

(4) has a method that can set the value of the name, the method is public, the method name is setName, the method needs a parameter of type String, can return the value of the boolean type, indicating whether the setting is successful.

P.S. Because of the scoring requirement, please add "student1 use getGrade and setGrade, student2 use getName and setName" and blank line when outputting the answer.

Here's the main code:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

Student student1=new Student();

Student student2=new Student();

System.out.println("student1 use getGrade and setGrade, student2 use getName and setName");

student1.setGrade(sc.nextInt());

student2.setName(sc.next());

System.out.println("Student1:"+"grade="+student1.getGrade()+" name="+student1.getName());

System.out.println("Student2:"+"grade="+student2.getGrade()+" name="+student2.getName());

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!