2、设计一个学生类Student,要求: (1)Student类的成员变量有 no:int型,代表学号 name:String型,代表姓名 grade:float型,代表成绩 sum:float型,静态成员变量,代表总分num:int型,静态成员变量,代表人数(2)Student类的成员方法有: Student (int n, String na,float d):构造方法。分别用n、na、d对成员变量no、name、grade进行初始化,实现总分sum累加、人数增加的功能 average():返回平均分。 display():显示学号、姓名、成绩。
时间: 2023-05-21 19:05:22 浏览: 88
好的,以下是代码实现:
```
public class Student {
private int no;
private String name;
private float grade;
private static float sum = 0;
private static int num = 0;
public Student(int no, String name, float grade) {
this.no = no;
this.name = name;
this.grade = grade;
sum += grade;
num++;
}
public float average() {
return sum / num;
}
public void display() {
System.out.println("学号:" + no);
System.out.println("姓名:" + name);
System.out.println("成绩:" + grade);
}
}
```
希望我的回答能够满足你的要求,请问你还有什么问题需要我来回答吗?
相关问题
2.请按照以下要求设计一个学生类student,并进行测试。 要求如下: student类
学生类Student应该包含以下成员变量:
1. 姓名(name):用于存储学生的姓名。
2. 年龄(age):用于存储学生的年龄。
3. 学号(studentID):用于存储学生的学号。
同时,学生类应该具有以下方法:
1. 构造方法(constructor):用于创建学生对象,并初始化姓名、年龄和学号。
2. 获取姓名(getName):用于返回学生的姓名。
3. 设置姓名(setName):用于设置学生的姓名。
4. 获取年龄(getAge):用于返回学生的年龄。
5. 设置年龄(setAge):用于设置学生的年龄。
6. 获取学号(getStudentID):用于返回学生的学号。
7. 设置学号(setStudentID):用于设置学生的学号。
以下是一个示例实现:
public class Student {
private String name;
private int age;
private String studentID;
public Student(String name, int age, String studentID) {
this.name = name;
this.age = age;
this.studentID = studentID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getStudentID() {
return studentID;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
public static void main(String[] args) {
Student student = new Student("张三", 18, "20210001");
System.out.println("姓名:" + student.getName());
System.out.println("年龄:" + student.getAge());
System.out.println("学号:" + student.getStudentID());
student.setAge(19);
student.setStudentID("20210002");
System.out.println("修改后的年龄:" + student.getAge());
System.out.println("修改后的学号:" + student.getStudentID());
}
}
输出结果:
姓名:张三
年龄:18
学号:20210001
修改后的年龄:19
修改后的学号:20210002
java编程设计一个学生类Student,要求:(1) Student类的成员变量有: no:int型,代表学号 name:String型,代表姓名。 grade:float型,代表成绩 sum:float型,静态成员变量,代表总分 num:int型,静态成员变量,代表人数(2) Student类的成员方法有: Student (int n,String na,float d):构造方法。分别用n、na、d对成员变量no、name、grade进行初始化,实现总分sum累加、人数增加的功能 average( ):返回平均分。 display():显示学号、姓名、成绩。然后编写一个含有main方法的类SomeStudents,创建Student类的对象,并调用上面定义的方法。
好的,以下是Java代码设计一个学生类Student及使用SomeStudents类创建对象和调用方法的示例:
```java
public class Student {
private int no;
private String name;
private float grade;
private static float sum;
private static int num;
public Student(int n, String na, float d) {
no = n;
name = na;
grade = d;
sum += d;
num++;
}
public float average() {
return sum / num;
}
public void display() {
System.out.println("学号:" + no + ",姓名:" + name + ",成绩:" + grade);
}
}
public class SomeStudents {
public static void main(String[] args) {
Student s1 = new Student(1, "张三", 90.5f);
Student s2 = new Student(2, "李四", 88.0f);
Student s3 = new Student(3, "王五", 95.5f);
System.out.println("平均分:" + s1.average());
s1.display();
s2.display();
s3.display();
}
}
```
这段代码中,我们定义了一个Student类和一个SomeStudents类。首先在Student类中定义了所需的成员变量和方法,其中构造方法用于初始化成员变量,并实现累加总分和人数的功能。average方法返回平均分,display方法输出学生的基本信息。
在SomeStudents类中,我们创建3个Student对象,并使用这些对象调用上面定义的方法。运行程序,输出结果如下:
```
平均分:91.333336
学号:1,姓名:张三,成绩:90.5
学号:2,姓名:李四,成绩:88.0
学号:3,姓名:王五,成绩:95.5
```
以上就是我们使用Java编程设计学生类并创建对象调用方法的示例,希望能对您有所帮助。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)