java设计课程类

package com.kidd.test.zhidao;import java.util.ArrayList;import java.util.List;/** * Hello world! * */public class Execute {public static void main(String[] args) {Lesson lesson1, lesson2, lesson3;lesson1 = new Lesson();lesson1.setName("语文");lesson1.setCredit(7);lesson2...
java设计课程类
仅供参考
package com.kidd.test.zhidao;import java.util.ArrayList;import java.util.List;/** * Hello world! * */public class Execute {public static void main(String[] args) {Lesson lesson1, lesson2, lesson3;lesson1 = new Lesson();lesson1.setName("语文");lesson1.setCredit(7);lesson2 = new Lesson();lesson2.setName("英语");lesson2.setCredit(8);lesson3 = new Lesson();lesson3.setName("数学");lesson3.setCredit(10);Student student = new Student();student.setName("陈云");student.setNum("2017060901");student.addLesson(lesson1);student.addLesson(lesson2);student.addLesson(lesson3);lesson1.setScore(60);try {student.exam(lesson1);} catch (ScoreException e) {System.out.println("输入的分数不合法.");}lesson2.setScore(70);try {student.exam(lesson2);} catch (ScoreException e) {System.out.println("输入的分数不合法.");}lesson3.setScore(90);try {student.exam(lesson3);} catch (ScoreException e) {System.out.println("输入的分数不合法.");}System.out.println(student);student.removeLesson(lesson2);System.out.println(student);lesson3.setScore(50);student.printLessons();student.printCredit();student.printAverage();}}class Lesson {private String name;private int credit;private float score;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getCredit() {return credit;}public void setCredit(int credit) {this.credit = credit;}public float getScore() {return score;}public void setScore(float score) {this.score = score;}}class Student {private String name;private String num;private List<Lesson> lessons;public Student() {lessons = new ArrayList<>();}public void addLesson(Lesson lesson) {int index = lessons.indexOf(lesson);if (index != -1) {System.out.println("该学生已经学习了该课程.");return;}lessons.add(lesson);}public boolean exam(Lesson lesson) throws ScoreException {int index = lessons.indexOf(lesson);if (index == -1) {System.out.println("该学生并未学习此课程.");return false;}if (lesson.getScore() <= 0 || lesson.getScore() >= 100) {throw new ScoreException();}lessons.get(index).setScore(lesson.getScore());return true;}public boolean removeLesson(Lesson lesson) {int index = lessons.indexOf(lesson);if (index == -1) {return false;}lessons.remove(index);return true;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getNum() {return num;}public void setNum(String num) {this.num = num;}public List<Lesson> getLessons() {return lessons;}public void setLessons(List<Lesson> lessons) {this.lessons = lessons;}public float getAverage() {float a = 0f;for (Lesson lesson : lessons) {a += lesson.getScore();}return a / lessons.size();}public int getTotal() {int a = 0;for (Lesson lesson : lessons) {if (lesson.getScore() >= 60) {a += lesson.getCredit();}}return a;}@Overridepublic String toString() {StringBuilder message = new StringBuilder();message.append("该学生总共修了" + lessons.size() + "门课程.分别是:");for (Lesson lesson : lessons) {message.append(lesson.getName() + ",");}message.append("\r\n");message.append("该学生的平均成绩是:" + getAverage() + "\r\n");message.append("该学生的总学分是:" + getTotal() + "\r\n");return message.toString();}public void printLessons() {System.out.println("该学生总共修了" + lessons.size() + "门课程.分别是:");for (int i = 0, k = lessons.size(); i < k; i++) {System.out.println(lessons.get(i).getName() + "成绩是:" + lessons.get(i).getScore());}System.out.println("");}public void printAverage() {System.out.print("该学生平均成绩是:");System.out.println(getAverage());}public void printCredit() {System.out.print("该学生总学分是:");System.out.println(getTotal());}}class ScoreException extends Exception {/** * */private static final long serialVersionUID = 1L;}2017-06-09
本视频主要从Java语言基础、JavaSE核心、WEB全栈及数据库、Servlet/Jsp核心、 Java框架五个阶段进行讲解。在本教程中,会让大家从零基础快速掌握Java知识。想要配套学习资料的小伙伴可以联系我哦!

2021-06-01
mengvlog 阅读 10 次 更新于 2025-07-19 20:38:32 我来答关注问题0
  •  信必鑫服务平台 JAVA编写一个课程类Cource的方法是什么?

    1.编写一个课程类Cource,包含:3个成员变量:课程编号(cNumber)、课程名(cName)和学分数(cUnit);1个方法:printCourceInfo:用于输出课程的相关信息;2.编写Cource类的测试程序,创建课程对象:编号为112101,课程Java 程序设计,学分为3。要求输出课程的相关信息。3.将上题创建的Cource的3个成员...

  • java扫雷游戏设计思路及课程设计概要 一、设计思路 游戏界面设计:使用AWT和Swing库来创建图形用户界面(GUI)。设计一个网格布局来表示雷区,每个网格单元可以是一个JButton组件。雷区初始化:创建一个二维数组来表示雷区,数组中的每个元素对应网格中的一个单元。使用Random类在雷区中随机放置指定数量的地雷。

  • java课程设计题目及代码分别是:1、题目:计算器。设计内容是设计一个图形界面(GUI)的计算器应用程序,完成简单的算术运算。设计要求是设计的计算器应用程序可以完成家法、减法、乘法、除法和取余运算。且有小数点、正负号、求倒数、退格和清零功能。2、代码:数字按钮NumberButton类如下:import java.awt...

  •  誉祥祥知识 java课程设计:画随机的圆报告_ 系统背景 系统模块 怎么写?

    在Java课程设计中,画随机的圆是一项有趣且实用的任务。首先,需要引入Random类以生成随机数。通过Random对象,可以控制圆的圆心坐标以及半径。例如,可以使用如下代码生成随机圆心坐标:x = new Random().nextInt(300); y = new Random().nextInt(300); 其中,300是画布的最大尺寸。同时,半径也可...

  •  圆神★cy Java课程设计的目录

    第1章 记忆测试系统1.1 设计内容1.2 设计要求1.3 总体设计1.4 具体设计1.4.1运行效果与程序发布1.4.2 主类Memory1.4.3 方块Block1.4.4 记忆测试板MemoryTestPane1.4.5 显示成绩Showrecord1.4.6 记录成绩Record1.4.7 随机排列图标RandomSetIcon1.4.8 测试者PeoPle1.5 课程设计作业第2...

檬味博客在线解答立即免费咨询

Java相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部