1 Replies - 336 Views - Last Post: 02 August 2016 - 07:01 PM Rate Topic: -----

#1 SIeviN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-August 16

Design Pattern Algorithm implementation

Posted 02 August 2016 - 06:18 PM

Hello,
I'm trying to implement a function to calculate the average of a few assignment grades and exam grades.
The algorithm must be able to be selected at runtime and it must be possible to add new algorithms to compute the average to the program without modifying the Student class.
A. Assignments contribute 40% of grade, the other 60% comes from tests.
B. same weights as above, but if there is two or more assignment scores, drop the lowest

I believe I should be using the strategy design pattern, but with how the classes are written, I'm lost.

In main
Student student = new Student("John Doe");
student.addAssignmentScore(98.5);
student.addAssignmentScore(90.1);
student.addAssignmentScore(78.3);
student.addExamScore(95);
student.addExamScore(87);

//use first algorithm
System.out.println(student.getName() + " " + student.getAverage());

//switch to second algorithm
//code to switch to algorithm 2 here
System.out.println(student.getName() + " " + student.getAverage());

//code to switch back to algorithm 1 here



in Student class
private String name;
private ArrayList<Double> assignments;
private ArrayList<Double> exams;

public String getName()
{ return name; }

public void addAssignmentScore(double as)
{ assignments.add(as); }

public void addExamScore(double es)
{ exams.add(es); }

public double getAverage()
{
//implement this
}



Is This A Good Question/Topic? 0
  • +

Replies To: Design Pattern Algorithm implementation

#2 SIeviN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-August 16

Re: Design Pattern Algorithm implementation

Posted 02 August 2016 - 07:01 PM

The solution output is this, notice it doesn't have anything to prompt the user to select which algorithm to use.

John Doe 90.2
John Doe 92.3
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1