Write a Java program that implements a class called ScoreCalculator
. The ScoreCalculator
is used to calculate the overall score of a student based on the scores obtained in different subjects. The class should have the following methods:
addScore(subject: String, score: double)
: This method adds the score obtained by the student in a particular subject. The subject
parameter represents the name of the subject, and the score
parameter represents the score obtained. The method should throw an IllegalArgumentException
if the subject already exists.
removeScore(subject: String)
: This method removes the score obtained in a particular subject. The subject
parameter represents the name of the subject. The method should throw an IllegalArgumentException
if the subject does not exist.
calculateOverallScore(): double
: This method calculates and returns the overall score of the student by summing up the scores obtained in all the subjects. The method should throw an IllegalStateException
if no scores have been added.
Implement the ScoreCalculator
class according to the above specifications and demonstrate its usage by creating an instance, adding/removing scores, and calculating the overall score.
public class ScoreCalculator {
private Map<String, Double> scores;
public ScoreCalculator() {
scores = new HashMap<>();
}
public void addScore(String subject, double score) {
if (scores.containsKey(subject)) {
throw new IllegalArgumentException("Subject already exists");
}
scores.put(subject, score);
}
public void removeScore(String subject) {
if (!scores.containsKey(subject)) {
throw new IllegalArgumentException("Subject does not exist");
}
scores.remove(subject);
}
public double calculateOverallScore() {
if (scores.isEmpty()) {
throw new IllegalStateException("No scores have been added");
}
double overallScore = 0;
for (double score : scores.values()) {
overallScore += score;
}
return overallScore;
}
}
Explanation:
The ScoreCalculator
class has a private instance variable scores
which is a Map
to store the scores obtained in different subjects.
In the constructor, we initialize the scores
map as an empty HashMap
.
The addScore
method checks if the subject
already exists in the scores
map using the containsKey
method. If it does, we throw an IllegalArgumentException
with an appropriate message. If not, we add the subject
and score
to the scores
map using the put
method.
The removeScore
method checks if the subject
exists in the scores
map using the containsKey
method. If it does not, we throw an IllegalArgumentException
with an appropriate message. If it exists, we remove the subject
from the scores
map using the remove
method.
The calculateOverallScore
method checks if the scores
map is empty using the isEmpty
method. If it is, we throw an IllegalStateException
with an appropriate message. If not, we iterate over the scores using a for
loop and calculate the overall score by summing up all the scores. Finally, we return the overall score.
Example usage:
ScoreCalculator scoreCalculator = new ScoreCalculator();
scoreCalculator.addScore("Math", 90.5);
scoreCalculator.addScore("Science", 85.0);
scoreCalculator.addScore("English", 92.3);
System.out.println("Overall Score: " + scoreCalculator.calculateOverallScore());
scoreCalculator.removeScore("Science");
System.out.println("Overall Score after removing Science: " + scoreCalculator.calculateOverallScore());
Output:
Overall Score: 267.8
Overall Score after removing Science: 182.8
In the example usage, we create an instance of ScoreCalculator
and add scores for three subjects. We then calculate the overall score and print it. Next, we remove the score for the "Science" subject and calculate the overall score again to observe the change.