在java中编写程序,定义一个学生类
代码如下:
package exam2;
import java.util.ArrayList;
import java.util.List;
/**
编写一个Java应用程序,该程序包括3个类:
定义一个学生类,Student有姓名,学号,选学的课程列表圆裤
定义一个课程类:课程名称,课程分数
(1)初始化一个学生,选择了三门课程,并添加到学生的属性中
(2)实现统计学生的总分功能
1.正确编写2个类(5分)
2.统计学生的总分功能(5分)
*/
public class Test {
public static void main(String[] args) {
List<Course> courses = new ArrayList<>();
// 初始化3门课程及分数
Course course1 = new Course("java", 80);
Course course2 = new Course("Math", 60);
Course course3 = new Course("English", 90);
// 课程对象添加到集合
courses.add(course1);
courses.add(course2);
courses.add(course3);
// 初始化学生对象
Student student = new Student("tom", "2015101", courses);
// student.setStuid("2015101");
// student.setStuname("tom");
// student.setCourses(courses);
// System.out.println(student);
// (2)实现统计 学生 的总分功能
int sum = sumScore(courses);
System.out.println(student.getStuname() + "学生总分:" + sum);
}
private static int sumScore(List<Course> courses) {
int sum = 0;
// 用课程对象,来获取课程的分数
for (Course course : courses) {
sum += course.getScore();
}
return sum;
}
}
package exam2;
import java.util.List;
public class Student {
// 定义一个学生类,Student有姓名,学号,选学的课程列表
private String stuname;
private String stuid;
private List<Course>如腔轮 courses;
public Student() {
}
public Student(String stuname, String stuid, List<Course> courses) {
this.stuname = stuname;
this.stuid = stuid;
this.courses = courses;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getStuid() {
return stuid;
}
public void setStuid(String stuid) {
this.stuid = stuid;
}
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
}
@Override
public String toString() {
return "学生姓名=" + stuname + ",学号=" + stuid + ", 课程=" + courses;
}
}
package exam2;
public class Course {
//定渣信义一个课程类:课程名称,课程分数
private String cname;
private int score;
public Course() {
}
public Course(String cname, int score) {
this.cname = cname;
this.score = score;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "[课程名称=" + cname + ", 课程分数=" + score + "]";
}
}
/*
运行:
tom学生总分:230
*/
扩展资料:
Public 语句说明
1.Public 语句声明的变量在所有应用程序的所有没有使用OptionPrivate Module 的模块的任何过程中都是可用的;若该模块使用了 OptionPrivate Module,则该变量只是在其所属工程中是公用的。
2.使用Public 语句可以声明变量的数据类型。例如,下面的语句声明了一个 Integer 类型的变量。
3.Public NumberOfEmployees As Integer 也可以使用Public 语句来声明变量的对象类型。下面的语句为工作表的新实例声明了一个变量。
参考资料:百度百科-Public 语句
public
class
Student{
private
String
name;
private
int
age;
private
double
score;
public
dd(String
name,
int
age,
double
score)
{
super();
this.name
=
name;
this.age
=
age;
this.score
=
score;
}
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
double
getScore()
{
return
score;
}
public
void
setScore(double
score)
{
this.score
=
score;
}
public
void
printInfo()
{
System.out.println("name:"
name);
System.out.println("age:"
age);
System.out.println("score:"
score);
}
}
main()
方法卖敬备:
public
class
TestStudent{
public
static
void
main(String[]
args)
{
Student
stu=new
Student("李四",20,89);
stu.printInfo();
}
}
友情提示稿宏:考试的题目还是应该自己做,尽管就是做错了,那对自己还是很有帮助的中毁
private String name;
private String stuNo;
private double score1;
private double score2;
private double score3;
public Student() {
}
public Student(String name, String stuNo, double score1, double score2,
double score3) {
this.name = name;
this.stuNo = stuNo;
this.score1 = score1;
this.score2 = score2;
this.score3 = score3;
}
public void GetAllScore() {
System.out.println("总成绩为:" + this.score1 + this.score2 + this.score3);
}
public void GetAvgScore() {
System.out.println("平均成绩碰瞎为:" + (this.score1 + this.score2 + this.score3)
/ 3);
}
}再到main方法里调用Student类里的带参构造函数为类中笑胡空的属性赋值,然后调用求总成绩和平均成绩做敏的两个方法就可以了
public class Student {
//属性包括姓名,学号,语文成绩,数学成绩,英语成绩
private String name;
private String id;
private int chn_score;
private int math_score;
private int en_score;
//方法包括输出数据,设置姓名和学号,设置老扮三门课程的成绩,求总成绩和平均成绩
public void diplay(){
System.out.println("the student name is: "侍磨灶 + this.name);
System.out.println("the student id is: " + this.id);
System.out.println("the student chinese score is: " + this.chn_score);
System.out.println("the student math score is: " + this.math_score);
System.out.println("the student english score is: " + this.en_score);
System.out.println("the student total score is: " + this.get_total_score());
System.out.println("the student average score is: " + this.get_avg_score());
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getChn_score() {
return chn_score;
}
public void setChn_score(int chnScore) {
chn_score = chnScore;
}
public int getMath_score() {
return math_score;
}
public void setMath_score(int mathScore) {
math_score = mathScore;
}
public int getEn_score() {
return en_score;
}
public void setEn_score(int enScore) {
en_score = enScore;
}
public int get_total_score(){
return this.chn_score + this.en_score + this.math_score;
}
public int get_avg_score(){
return this.get_total_score() / 3;
}
public static void main(String[] args) {
Student stu = new Student();
stu.setName("baidu"游笑);
stu.setId("123");
stu.setChn_score(89);
stu.setEn_score(98);
stu.setMath_score(88);
stu.diplay();
}
}
* 在java中编写程序,定义一个学生类,属性包括姓晌皮名,学历谨历号,语文成绩,数学成绩,英语成绩,方法包括输出数据,
* 设置姓名和学号,设置三门课程的成绩,求总成绩和平均成绩!
* @author Administrator
*/
public class Student {
private String name;//姓名
private String sNo;//学好
private double chineseScore;//语文成绩
private double englishScore;//英语成绩
private double mathScore;//数学成绩
//设置姓名
public void setName(String name) {
this.name = name;
}
//设置学号
public void setsNo(String sNo) {
this.sNo = sNo;
}
//设置语文成绩
public void setChineseScore(double chineseScore) {
this.chineseScore = chineseScore;
}
//设置英语成绩
public void setEnglishScore(double englishScore) {
this.englishScore = englishScore;
}
//设置数学成绩
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
}
//获取姓名
public String getName() {
return name;
}
//获取学肢搜号
public String getsNo() {
return sNo;
}
//获取语文成绩
public double getChineseScore() {
return chineseScore;
}
//获取英语成绩
public double getEnglishScore() {
return englishScore;
}
//获取数学成绩
public double getMathScore() {
return mathScore;
}
//输出数据
public void exportInfo(){
System.out.println("学生:"+name+",学号为:"+sNo+",语文成绩为:"+chineseScore+",数学成绩为:"+mathScore+",英语成绩为:"+englishScore);
}
//获取总成绩
public double getTotalScore(){
return chineseScore+englishScore+mathScore;
}
//获取平均分
public double getAverageScore(){
return (chineseScore+englishScore+mathScore)/3;
}
}