两道java程序题 新手请教要怎么写?

定义一个JavaCourse类,成员变量学生姓名、学号、测验成绩、平时成绩、期末成绩,总成绩=测验*30%+平时*30%+期末*40%。假设这门课(Java程序设计)有1... 定义一个JavaCourse 类,成员变量学生姓名、学号、测验成绩、平时成绩、期末成绩,总成绩 = 测验*30%+平时*30%+期末*40%。假设这门课(Java程序设计)有1个同学修,请在DOS控制台输出

1 这位学生详细情况(学号、姓名、测验······)
2 这门课程总成绩
要求主方法中直接给学生信息赋值
提示:
public final double TR= 0.3
public final double RR= 0.3
public final double ER= 0.4

定义一个JavaCourse 类,成员变量学生姓名、学号、测验成绩、平时成绩、期末成绩,总成绩 = 测验*30%+平时*30%+期末*40%。假设这门课(Java程序设计)有2个同学修,请在DOS控制台输出

1 这位学生详细情况(学号、姓名、测验······)
2 这门课程总成绩
要求学生信息的赋值通过构造方法实现!
展开
 我来答
匿名用户
2020-03-29
展开全部
import java.math.BigDecimal;

public class JavaCourse {

private String sName; //学生姓名
private String sId; //学号
private int testGrade; //测试成绩
private int regularGrade; //平时成绩
private int endOfTermGrade;//期末成绩
private int totalGrade; //总成绩
public final double TR= 0.3;
public final double RR= 0.3;
public final double ER= 0.4;

public JavaCourse() {
}

public JavaCourse(String sName, String sId, int testGrade, int regularGrade, int endOfTermGrade) {
this.sName = sName;
this.sId = sId;
this.testGrade = testGrade;
this.regularGrade = regularGrade;
this.endOfTermGrade = endOfTermGrade;
}

public String getsName() {
return sName;
}

public void setsName(String sName) {
this.sName = sName;
}

public String getsId() {
return sId;
}

public void setsId(String sId) {
this.sId = sId;
}

public int getTestGrade() {
return testGrade;
}

public void setTestGrade(int testGrade) {
this.testGrade = testGrade;
}

public int getRegularGrade() {
return regularGrade;
}

public void setRegularGrade(int regularGrade) {
this.regularGrade = regularGrade;
}

public int getEndOfTermGrade() {
return endOfTermGrade;
}

public void setEndOfTermGrade(int endOfTermGrade) {
this.endOfTermGrade = endOfTermGrade;
}

public int getTotalGrade() {
return totalGrade;
}

@Override
public String toString() {
return "JavaCourse{" +
"sName='" + sName + '\'' +
", sId='" + sId + '\'' +
", testGrade=" + testGrade +
", regularGrade=" + regularGrade +
", endOfTermGrade=" + endOfTermGrade +
'}';
}

public void showTotalGrad() {
//double四舍五入(0.5->1)转int类型
totalGrade = Integer.parseInt(new BigDecimal((testGrade * TR + regularGrade * RR + endOfTermGrade * ER)).setScale(0, BigDecimal.ROUND_HALF_UP).toString());
System.out.println("课程的总成绩:" + totalGrade);
}
}

public class DemoTest02 {

public static void main(String[] args) {
JavaCourse student1 = new JavaCourse();
student1.setsName("小张");
student1.setsId("10086");
student1.setTestGrade(85);
student1.setRegularGrade(90);
student1.setEndOfTermGrade(86);
System.out.println(student1.toString());
student1.showTotalGrad();

JavaCourse student2 = new JavaCourse("老王","10010",60,80,45);
System.out.println(student2.toString());
student2.showTotalGrad();
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式