java编程实现学生类,功能如下:1. 属性:学号,姓名,大学英语和电子商务成绩;2. 能初始化学号,姓名的构造方法;能初始化所有学生属性的构造方法;无参构造方法 3. 其他方法包括:(1)计算学生的总分和平均分;(2)输出学生的学号、姓名、总分和平均分;(3)一个学生上课的方法,该方法有3个参数,一个参数表示上课时间,另一个参数表示课程名称,第三个参数表示上课地点,方法内容是输出这些参数信息。 测试要求如下:1、创建包括所有特征的2个学生对象;2. 输出这2个学生的总分和平均分;3. 第一个学生在2017年9月29日,在3223学习Java课程;第二个学生在2017年9月29日,在1402学习数据结构。
时间: 2024-03-20 10:39:42 浏览: 34
以下是Java代码实现:
```
public class Student {
private String studentId; // 学号
private String studentName; // 姓名
private double englishGrade; // 大学英语成绩
private double ecommerceGrade; // 电子商务成绩
// 构造方法:初始化学号和姓名
public Student(String studentId, String studentName) {
this.studentId = studentId;
this.studentName = studentName;
}
// 构造方法:初始化所有属性
public Student(String studentId, String studentName, double englishGrade, double ecommerceGrade) {
this.studentId = studentId;
this.studentName = studentName;
this.englishGrade = englishGrade;
this.ecommerceGrade = ecommerceGrade;
}
// 无参构造方法
public Student() {}
// 计算总分
public double getTotalGrade() {
return englishGrade + ecommerceGrade;
}
// 计算平均分
public double getAverageGrade() {
return getTotalGrade() / 2;
}
// 输出学生信息
public void printStudentInfo() {
System.out.println("学号:" + studentId + ",姓名:" + studentName + ",总分:" + getTotalGrade() + ",平均分:" + getAverageGrade());
}
// 上课方法
public void attendClass(String classTime, String className, String classLocation) {
System.out.println(studentName + "在" + classTime + "在" + classLocation + "上了" + className + "课程。");
}
}
```
测试代码如下:
```
public class Test {
public static void main(String[] args) {
// 创建两个学生对象
Student student1 = new Student("001", "张三", 80, 85);
Student student2 = new Student("002", "李四", 90, 95);
// 输出学生的总分和平均分
System.out.println("第一个学生的总分为:" + student1.getTotalGrade() + ",平均分为:" + student1.getAverageGrade());
System.out.println("第二个学生的总分为:" + student2.getTotalGrade() + ",平均分为:" + student2.getAverageGrade());
// 第一个学生上课
student1.attendClass("2017年9月29日", "Java", "3223");
// 第二个学生上课
student2.attendClass("2017年9月29日", "数据结构", "1402");
}
}
```
输出结果如下:
```
第一个学生的总分为:165.0,平均分为:82.5
第二个学生的总分为:185.0,平均分为:92.5
张三在2017年9月29日在3223上了Java课程。
李四在2017年9月29日在1402上了数据结构课程。
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)