继承和多态性可以使我的代码减少冗余吗?

时间:2018-10-03 17:02:29

标签: java inheritance polymorphism

公共班学生

   private String name;
   private String id;
   private static double grade;
   private Midterm midtermScore;
   private FinalExam finalExamScore;

   public Student() {
      super();
   }

   public Student(String name, String id, Midterm midtermScore, FinalExam 
      finalExamScore) {
      this.name = name;
      this.id = id;
      this.midtermScore = midtermScore;
      this.finalExamScore = finalExamScore;
   }

   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 double getGrade() {
      grade = midtermScore.getMidtermScore() + 
      finalExamScore.getTotalScore();
      return grade;
   }

公共类期中考试

    private int numberOfQuestions;
    private int numberOfQuestionsCorrect;
    private final double WEIGHT = 0.4;
    private double midtermScore;

    public Midterm(int numberOfQuestions, int numberOfQuestionsCorrect) {
        super();
        this.numberOfQuestions = numberOfQuestions;
        this.numberOfQuestionsCorrect = numberOfQuestionsCorrect;
    }

    public int getNumberOfQuestions() {
        return numberOfQuestions;
    }
    public void setNumberOfQuestions(int numberOfQuestions) {
        this.numberOfQuestions = numberOfQuestions;
    }
    public int getNumberOfQuestionsCorrect() {
        return numberOfQuestionsCorrect;
    }
    public void setNumberOfQuestionsCorrect(int numberOfQuestionsCorrect) {
        this.numberOfQuestionsCorrect = numberOfQuestionsCorrect;
    }

    public double getMidtermScore() {
        this.midtermScore =(numberOfQuestionsCorrect*100)/numberOfQuestions;
        return midtermScore * WEIGHT;
    }

公共课程FinalExam

    private double grammarScore;
    private double spellingScore;
    private double lengthScore;
    private double contentScore;

    private final double GRAMMARWEIGHT = 0.3;
    private final double SPELLINGWEIGHT = 0.2;
    private final double LENGTHWEIGHT = 0.2;
    private final double CONTENTWEIGHT = 0.3;

    private final double WEIGHT = 0.6;
    private double totalScore;

    public FinalExam(double grammarScore, double spellingScore, double 
                     lengthScore, double contentScore) {
        super();
        this.grammarScore = grammarScore;
        this.spellingScore = spellingScore;
        this.lengthScore = lengthScore;
        this.contentScore = contentScore;
    }

    public FinalExam(FinalExam[] finalScores) {
        this.finalScores = finalScores;
    }
    public double getGrammarScore() {
        return grammarScore;
    }
    public void setGrammarScore(double grammarScore) {
        this.grammarScore = grammarScore;
    }
    public double getSpellingScore() {
        return spellingScore;
    }
    public void setSpellingScore(double spellingScore) {
        this.spellingScore = spellingScore;
    }
    public double getLengthScore() {
        return lengthScore;
    }
    public void setLengthScore(double lengthScore) {
        this.lengthScore = lengthScore;
    }
    public double getContentScore() {
        return contentScore;
    }
    public void setContentScore(double contentScore) {
        this.contentScore = contentScore;
    }

    public double getTotalScore() {
        this.totalScore = (grammarScore*GRAMMARWEIGHT) + 
        (spellingScore*SPELLINGWEIGHT) + (lengthScore*LENGTHWEIGHT) + 
        (contentScore*CONTENTWEIGHT);
        return totalScore*WEIGHT;
    }

主类

public static void main(String[] args) {

    Student[] students = new Student[5];
    String[] names = new String[5];
    String[] ids = new String[5];
    String[] letterGrades = new String[5];

    Midterm m1 = new Midterm(50, 40);
    FinalExam f1 = new FinalExam(100, 90, 100, 80);
    Student s1 = new Student("Alan", "111", m1, f1);
    names[0]=s1.getName();
    ids[0]=s1.getId();

    Midterm m2 = new Midterm(40, 26);
    FinalExam f2 = new FinalExam(75, 80, 100, 60);
    Student s2 = new Student("Bill", "222", m2, f2);
    names[1]=s2.getName();
    ids[1]=s2.getId();

    Midterm m3 = new Midterm(45, 39);
    FinalExam f3 = new FinalExam(89, 94, 85, 89);
    Student s3 = new Student("Cate", "333", m3, f3);
    names[2]=s3.getName();
    ids[2]=s3.getId();

    Midterm m4 = new Midterm(35, 30);
    FinalExam f4 = new FinalExam(90, 45, 40, 68);
    Student s4 = new Student("Dave", "444", m4, f4);
    names[3]=s4.getName();
    ids[3]=s4.getId();

    Midterm m5 = new Midterm(100, 73);
    FinalExam f5 = new FinalExam(45, 56, 48, 62);
    Student s5 = new Student("Frank", "555", m5, f5);
    names[4]=s5.getName();
    ids[4]=s5.getId();

    students[0] = s1;
    students[1] = s2;
    students[2] = s3;
    students[3] = s4;
    students[4] = s5;

    String letterGrade;
    int i = 0;
    for(Student s : students) {
        if(s.getGrade() >= 90.0) {
            letterGrade = "A";
        }else if(s.getGrade() >= 80.0) {
            letterGrade = "B";
        }else if(s.getGrade() >= 70.0) {
            letterGrade = "C";
        }else if(s.getGrade() >= 60.0) {
            letterGrade = "D";
        }else
            letterGrade = "F";
        letterGrades[i] = letterGrade;
        i++;
    }

    int i = -1;
    for(++i; i<=4; i++) {
        if(i==4)
            System.out.print(String.format("%s%4s %5s%s %5s%s %11s", "|", 
                             names[i], "|", ids[i], "|", letterGrades[i], 
                             "|"));
        else
            System.out.print(String.format("%s%4s %6s%s %5s%s %11s", "|", 
                             names[i], "|", ids[i], "|", letterGrades[i], 
                             "|"));
        System.out.println();
    }

}

首先,对于我的代码格式不正确,我深表歉意,请随时编辑我的格式,我将尝试相应地进行更改。

我最近开始用Java编程,并完成了本周的编码任务。问题要求对5位学生的信息(姓名,身份证,期中成绩和期末成绩)进行硬编码。然后,程序必须输出学生的姓名和ID,并以代表他们的GPA的字母等级来表示。

我的教授强调了使用继承和多态性的重要性,但我真的不知道如何利用它。上周我们经历了合成,继承和多态性,但是对于此作业,我没有看到任何方法可以通过应用除合成以外的任何东西而使代码更容易,而没有故意使其变得更复杂。

我认为问题归结为实践,并且由于我的背景非常有限,因此有人可以提出任何有关如何改进代码的建议吗?是否使用继承,多态或我可以练习的任何其他方法?

通过更多的时间和实践,我可以更好地理解该语言,并且我想一种更好的方法是在我确信有改进空间的情况下,对我的代码提出反馈。

不幸的是,我的教授很忙,本周的电子邮件和辅导时间都不是很好,所以取消了本周的授课时间,因此,即使不是有关多态性和继承的信息,我也将感激不尽。

1 个答案:

答案 0 :(得分:0)

您确实有一些冗余代码。

我不会为您编写代码,但我会指出一些我马上看到的内容。

期中考试和期末考试都是考试的一种,也许考试或考试可能是很好的超级课程或接口?我敢肯定,您可以问问自己分数是多少,而不是在程序结束时计算分数,而是在每个对象本身中计算分数。

而不是仅在学生中存储期中考试和期末考试的分数。我建议实际将期中和期末考试对象存储在学生中。

然后您可以直接向学生询问他的最终成绩。然后,学生将使用期中和期末考试对象来计算最终分数。

我建议不要使用String[] names = new String[5];String[] ids = new String[5];String[] letterGrades = new String[5];数组。在打印出学生及其成绩时,直接转到学生对象并调用getName()getId()等...

您可以考虑使用CollectionArrayList之类的Hashset来代替学生使用的数组。