First commit
This commit is contained in:
18
Homework 4/complete_01.grd
Executable file
18
Homework 4/complete_01.grd
Executable file
@@ -0,0 +1,18 @@
|
||||
4
|
||||
Programming Assignments 6 0.40 true false
|
||||
PA1 20
|
||||
PA2 18
|
||||
PA3 5
|
||||
PA4 15
|
||||
PA5 20
|
||||
PA6 20
|
||||
Homework Assignments 5 0.10 false false
|
||||
HW1 20
|
||||
HW2 5
|
||||
HW3 0
|
||||
HW4 10
|
||||
HW5 15
|
||||
Midterm Exam 1 0.20 false false
|
||||
Midterm 80
|
||||
Final Exam 1 0.30 false false
|
||||
Final 75
|
||||
18
Homework 4/complete_02.grd
Executable file
18
Homework 4/complete_02.grd
Executable file
@@ -0,0 +1,18 @@
|
||||
4
|
||||
Programming Assignments 6 0.40 true false
|
||||
PA1 20
|
||||
PA2 20
|
||||
PA3 20
|
||||
PA4 20
|
||||
PA5 20
|
||||
PA6 20
|
||||
Homework Assignments 5 0.10 false false
|
||||
HW1 20
|
||||
HW2 20
|
||||
HW3 20
|
||||
HW4 20
|
||||
HW5 20
|
||||
Midterm Exam 1 0.20 false false
|
||||
Midterm 100
|
||||
Final Exam 1 0.30 false false
|
||||
Final 100
|
||||
18
Homework 4/complete_03.grd
Executable file
18
Homework 4/complete_03.grd
Executable file
@@ -0,0 +1,18 @@
|
||||
4
|
||||
Programming Assignments 6 0.40 true false
|
||||
PA1 10
|
||||
PA2 10
|
||||
PA3 5
|
||||
PA4 15
|
||||
PA5 5
|
||||
PA6 20
|
||||
Homework Assignments 5 0.10 false false
|
||||
HW1 20
|
||||
HW2 0
|
||||
HW3 0
|
||||
HW4 10
|
||||
HW5 15
|
||||
Midterm Exam 1 0.20 false false
|
||||
Midterm 60
|
||||
Final Exam 1 0.30 false false
|
||||
Final 45
|
||||
21
Homework 4/five-categories_01.grd
Executable file
21
Homework 4/five-categories_01.grd
Executable file
@@ -0,0 +1,21 @@
|
||||
5
|
||||
Homework Assignments 5 0.25 true false
|
||||
HW01 20
|
||||
HW02 5
|
||||
HW03 0
|
||||
HW04 10
|
||||
HW05 15
|
||||
Quizzes 5 0.25 true true
|
||||
Quiz01 12
|
||||
Quiz02 8
|
||||
Quiz03 20
|
||||
Quiz04 13
|
||||
Quiz05 20
|
||||
Misc 2 0.10 false false
|
||||
Attendance 40
|
||||
Participation 50
|
||||
Midterm Exams 2 0.20 false false
|
||||
Midterm01 45
|
||||
Midterm02 40
|
||||
Final Exam 1 0.20 false false
|
||||
Final 75
|
||||
18
Homework 4/missing_all.grd
Executable file
18
Homework 4/missing_all.grd
Executable file
@@ -0,0 +1,18 @@
|
||||
4
|
||||
Programming Assignments 6 0.40 true false
|
||||
PA1
|
||||
PA2
|
||||
PA3
|
||||
PA4
|
||||
PA5
|
||||
PA6
|
||||
Homework Assignments 5 0.10 false false
|
||||
HW1
|
||||
HW2
|
||||
HW3
|
||||
HW4
|
||||
HW5
|
||||
Midterm Exam 1 0.20 false false
|
||||
Midterm
|
||||
Final Exam 1 0.30 false false
|
||||
Final
|
||||
18
Homework 4/missing_multiple-in-each.grd
Executable file
18
Homework 4/missing_multiple-in-each.grd
Executable file
@@ -0,0 +1,18 @@
|
||||
4
|
||||
Programming Assignments 6 0.40 true false
|
||||
PA1
|
||||
PA2 20
|
||||
PA3 5
|
||||
PA4 15
|
||||
PA5
|
||||
PA6
|
||||
Homework Assignments 5 0.10 false false
|
||||
HW1 20
|
||||
HW2
|
||||
HW3 0
|
||||
HW4
|
||||
HW5 15
|
||||
Midterm Exam 1 0.20 false false
|
||||
Midterm
|
||||
Final Exam 1 0.30 false false
|
||||
Final
|
||||
18
Homework 4/missing_one-in-each.grd
Executable file
18
Homework 4/missing_one-in-each.grd
Executable file
@@ -0,0 +1,18 @@
|
||||
4
|
||||
Programming Assignments 6 0.40 true false
|
||||
PA1 20
|
||||
PA2 18
|
||||
PA3 5
|
||||
PA4 15
|
||||
PA5
|
||||
PA6 20
|
||||
Homework Assignments 5 0.10 false false
|
||||
HW1 20
|
||||
HW2 5
|
||||
HW3 0
|
||||
HW4
|
||||
HW5 15
|
||||
Midterm Exam 1 0.20 false false
|
||||
Midterm
|
||||
Final Exam 1 0.30 false false
|
||||
Final
|
||||
18
Homework 4/missing_one-in-one.grd
Executable file
18
Homework 4/missing_one-in-one.grd
Executable file
@@ -0,0 +1,18 @@
|
||||
4
|
||||
Programming Assignments 6 0.40 true false
|
||||
PA1 20
|
||||
PA2 18
|
||||
PA3 5
|
||||
PA4 15
|
||||
PA5 20
|
||||
PA6 20
|
||||
Homework Assignments 5 0.10 false false
|
||||
HW1 20
|
||||
HW2 5
|
||||
HW3
|
||||
HW4 10
|
||||
HW5 15
|
||||
Midterm Exam 1 0.20 false false
|
||||
Midterm 80
|
||||
Final Exam 1 0.30 false false
|
||||
Final 75
|
||||
36
Homework 4/six-categories_01.grd
Executable file
36
Homework 4/six-categories_01.grd
Executable file
@@ -0,0 +1,36 @@
|
||||
6
|
||||
Programming Assignments 6 0.15 false false
|
||||
PA01 20
|
||||
PA02 18
|
||||
PA03 5
|
||||
PA04 15
|
||||
PA05 20
|
||||
PA06 20
|
||||
Homework Assignments 5 0.15 false false
|
||||
HW01 20
|
||||
HW02 5
|
||||
HW03 0
|
||||
HW04 10
|
||||
HW05 15
|
||||
Labs 10 0.10 false false
|
||||
Lab01 10
|
||||
Lab02 5
|
||||
Lab03 10
|
||||
Lab04 10
|
||||
Lab05 0
|
||||
Lab06 10
|
||||
Lab07 10
|
||||
Lab08 10
|
||||
Lab09 5
|
||||
Lab10 10
|
||||
Quizzes 5 0.10 false false
|
||||
Quiz01 12
|
||||
Quiz02 8
|
||||
Quiz03 20
|
||||
Quiz04 13
|
||||
Quiz05 16
|
||||
Midterm Exams 2 0.20 false false
|
||||
Midterm01 45
|
||||
Midterm02 40
|
||||
Final Exam 1 0.30 false false
|
||||
Final 75
|
||||
72
Homework 4/src/app/Gradient.java
Executable file
72
Homework 4/src/app/Gradient.java
Executable file
@@ -0,0 +1,72 @@
|
||||
package app;
|
||||
import grading.*;
|
||||
import grading.io.*;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
/**
|
||||
* An application for calculating the numeric grade for
|
||||
* a course from the grades on individual assignments.
|
||||
*
|
||||
* This version reads the course structure (and optionally the grades)
|
||||
* from a file. It assumes:
|
||||
*
|
||||
* The grade in each category is the total of the individual assessments.
|
||||
*
|
||||
* The course grade is the weighted total of the category grades.
|
||||
*
|
||||
* @version 2.0
|
||||
* @author Sagacious Media
|
||||
*
|
||||
*/
|
||||
public class Gradient
|
||||
{
|
||||
/**
|
||||
* The entry point for the application.
|
||||
*
|
||||
* @param args args[0] contains the path (relative or absolute) to the .grd file
|
||||
*/
|
||||
public static void main(String[] args)
|
||||
{
|
||||
// Early exit
|
||||
if ((args == null) || (args.length < 1))
|
||||
{
|
||||
System.err.println("You must enter a file name.");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
BufferedReader in = new BufferedReader(new FileReader(args[0]));
|
||||
String line = in.readLine();
|
||||
int categories = Integer.parseInt(line);
|
||||
Course course = CourseReader.readCourse(in, categories);
|
||||
in.close();
|
||||
|
||||
ArrayList<Grade> grades = new ArrayList<Grade>();
|
||||
for (Category c: course.getCategories())
|
||||
{
|
||||
GradingStrategy categoryStrategy = c.getStrategy();
|
||||
Filter filter = c.getFilter();
|
||||
Grade grade = categoryStrategy.calculate(c.getKey(), filter.apply(c.getGrades()));
|
||||
grades.add(grade);
|
||||
}
|
||||
|
||||
GradingStrategy courseStrategy = course.getStrategy();
|
||||
Grade courseGrade = courseStrategy.calculate("Course Grade", grades);
|
||||
System.out.println(courseGrade.toString());
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
System.out.println("Unable to open file.");
|
||||
System.exit(1);
|
||||
}
|
||||
catch (SizeException se)
|
||||
{
|
||||
// Shouldn't happen
|
||||
System.out.println("Size Problem.");
|
||||
System.exit(2);
|
||||
}
|
||||
}
|
||||
}
|
||||
79
Homework 4/src/grading/DropFilter.java
Executable file
79
Homework 4/src/grading/DropFilter.java
Executable file
@@ -0,0 +1,79 @@
|
||||
package grading;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* A filter to apply to a list of grades to optionally drop the lowest or highest grades.
|
||||
*/
|
||||
public class DropFilter implements Filter
|
||||
{
|
||||
private boolean shouldDropLowest;
|
||||
private boolean shouldDropHighest;
|
||||
|
||||
/**
|
||||
* Default constructor that will drop the lowest and highest grades.
|
||||
*/
|
||||
public DropFilter()
|
||||
{
|
||||
this.shouldDropLowest = true;
|
||||
this.shouldDropHighest = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param shouldDropLowest
|
||||
* Whether or not to drop the lowest grade
|
||||
* @param shouldDropHighest
|
||||
* Whether or not to drop the highest grade
|
||||
*/
|
||||
public DropFilter(final boolean shouldDropLowest, final boolean shouldDropHighest)
|
||||
{
|
||||
this.shouldDropLowest = shouldDropLowest;
|
||||
this.shouldDropHighest = shouldDropHighest;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param grades
|
||||
* The list of grades to apply the DropFilter to.
|
||||
* @return A new list containing the original grades with filter applied
|
||||
*/
|
||||
public List<Grade> apply(final List<Grade> grades) throws SizeException
|
||||
{
|
||||
int minSize = (shouldDropLowest ? 1 : 0) + (shouldDropHighest ? 1 : 0);
|
||||
|
||||
if (grades == null || grades.size() <= minSize)
|
||||
{
|
||||
throw new SizeException();
|
||||
}
|
||||
|
||||
List<Grade> droppedGrades = new ArrayList<Grade>();
|
||||
droppedGrades.addAll(grades);
|
||||
|
||||
Grade lowest = null;
|
||||
Grade highest = null;
|
||||
|
||||
for (Grade grade : grades)
|
||||
{
|
||||
if (lowest == null || grade.compareTo(lowest) < 0)
|
||||
{
|
||||
lowest = grade;
|
||||
}
|
||||
|
||||
if (highest == null || grade.compareTo(highest) > 0)
|
||||
{
|
||||
highest = grade;
|
||||
}
|
||||
}
|
||||
|
||||
if (shouldDropLowest)
|
||||
{
|
||||
droppedGrades.remove(lowest);
|
||||
}
|
||||
|
||||
if (shouldDropHighest)
|
||||
{
|
||||
droppedGrades.remove(highest);
|
||||
}
|
||||
|
||||
return droppedGrades;
|
||||
}
|
||||
}
|
||||
17
Homework 4/src/grading/Filter.java
Executable file
17
Homework 4/src/grading/Filter.java
Executable file
@@ -0,0 +1,17 @@
|
||||
package grading;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* An interface for filters that can be applied to a list of grades.
|
||||
*/
|
||||
public interface Filter
|
||||
{
|
||||
/**
|
||||
* @param grades
|
||||
* The list of grades to provide the filter to
|
||||
* @return A new list contain the original grades with the filter applied
|
||||
* @throws SizeException
|
||||
*/
|
||||
public List<Grade> apply(List<Grade> grades) throws SizeException;
|
||||
}
|
||||
106
Homework 4/src/grading/Grade.java
Executable file
106
Homework 4/src/grading/Grade.java
Executable file
@@ -0,0 +1,106 @@
|
||||
package grading;
|
||||
|
||||
/**
|
||||
* A data class for storing a grade with a key and a value to store the score.
|
||||
*/
|
||||
public class Grade implements Comparable<Grade>
|
||||
{
|
||||
private String key;
|
||||
private Double value;
|
||||
|
||||
/**
|
||||
* @param key
|
||||
* The type of grade
|
||||
*/
|
||||
public Grade(final String key)
|
||||
{
|
||||
if (key == null || key.isEmpty())
|
||||
{
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
this.key = key;
|
||||
this.value = 0.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key
|
||||
* The type of grade
|
||||
* @param value
|
||||
* The score for the grade
|
||||
*/
|
||||
public Grade(final String key, final double value)
|
||||
{
|
||||
if (key == null || key.isEmpty())
|
||||
{
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key
|
||||
* The type of grade
|
||||
* @param value
|
||||
* The score for the grade
|
||||
*/
|
||||
public Grade(final String key, final Double value)
|
||||
{
|
||||
if (key == null || key.isEmpty())
|
||||
{
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param other
|
||||
* The grade to compare with
|
||||
* @return Either -1, 0, or 1 depending on if the grade was smaller/equal to/larger than other
|
||||
*/
|
||||
public int compareTo(final Grade other)
|
||||
{
|
||||
if (this.value == null)
|
||||
{
|
||||
return (other.value == null) ? 0 : -1;
|
||||
}
|
||||
if (other.value == null)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return this.value.compareTo(other.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return A string representation of the grade
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return String.format("%s: NA", key);
|
||||
}
|
||||
|
||||
return String.format("%s: %5.1f", key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The key for the grade
|
||||
*/
|
||||
public String getKey()
|
||||
{
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The value of the grade
|
||||
*/
|
||||
public Double getValue()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
19
Homework 4/src/grading/GradingStrategy.java
Executable file
19
Homework 4/src/grading/GradingStrategy.java
Executable file
@@ -0,0 +1,19 @@
|
||||
package grading;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* An interface for strategies of calculating a final grade from a list of grades.
|
||||
*/
|
||||
public interface GradingStrategy
|
||||
{
|
||||
/**
|
||||
* @param key
|
||||
* The key for the final calculated grade
|
||||
* @param grades
|
||||
* The list of grades to from which to calculate the final grade
|
||||
* @return A new grade representing the final grade calculated from grades
|
||||
* @throws SizeException
|
||||
*/
|
||||
public Grade calculate(String key, List<Grade> grades) throws SizeException;
|
||||
}
|
||||
35
Homework 4/src/grading/Missing.java
Executable file
35
Homework 4/src/grading/Missing.java
Executable file
@@ -0,0 +1,35 @@
|
||||
package grading;
|
||||
|
||||
/**
|
||||
* A utility class for supplying a default double value in the case that a provided double is null.
|
||||
*/
|
||||
public class Missing
|
||||
{
|
||||
private static double DEFAULT_MISSING_VALUE = 0.0;
|
||||
|
||||
private Missing()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param number
|
||||
* The supplied double to check for null.
|
||||
* @return Either number if it was not null, or a default value of 0.0 if it was.
|
||||
*/
|
||||
public static double doubleValue(final Double number)
|
||||
{
|
||||
return number != null ? number : DEFAULT_MISSING_VALUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param number
|
||||
* The supplied double to check for null.
|
||||
* @param missingValue
|
||||
* The default value to return if number was null
|
||||
* @return Either number if it was not null, or missingValue if it was
|
||||
*/
|
||||
public static double doubleValue(final Double number, final double missingValue)
|
||||
{
|
||||
return number != null ? number : missingValue;
|
||||
}
|
||||
}
|
||||
19
Homework 4/src/grading/SizeException.java
Executable file
19
Homework 4/src/grading/SizeException.java
Executable file
@@ -0,0 +1,19 @@
|
||||
package grading;
|
||||
|
||||
/**
|
||||
* A custom exception for cases where a provided list does not meet a size requirement.
|
||||
*/
|
||||
public class SizeException extends RuntimeException
|
||||
{
|
||||
/**
|
||||
* Exception identifier.
|
||||
*/
|
||||
static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Creates a new SizeException instance.
|
||||
*/
|
||||
public SizeException()
|
||||
{
|
||||
}
|
||||
}
|
||||
40
Homework 4/src/grading/TotalStrategy.java
Executable file
40
Homework 4/src/grading/TotalStrategy.java
Executable file
@@ -0,0 +1,40 @@
|
||||
package grading;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* An implementation GradingStrategy that simply totals the scores for all grades.
|
||||
*/
|
||||
public class TotalStrategy implements GradingStrategy
|
||||
{
|
||||
/**
|
||||
* Creates a new instance of TotalStrategy.
|
||||
*/
|
||||
public TotalStrategy()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key
|
||||
* The key of the final totaled grade
|
||||
* @param grades
|
||||
* The list of grades to calculate the final total grade from
|
||||
* @return A new grade representing the total of grades
|
||||
*/
|
||||
public Grade calculate(final String key, final List<Grade> grades) throws SizeException
|
||||
{
|
||||
if (grades == null || grades.isEmpty())
|
||||
{
|
||||
throw new SizeException();
|
||||
}
|
||||
|
||||
Double total = 0.0;
|
||||
|
||||
for (Grade grade : grades)
|
||||
{
|
||||
total += Missing.doubleValue(grade.getValue());
|
||||
}
|
||||
|
||||
return new Grade(key, total);
|
||||
}
|
||||
}
|
||||
61
Homework 4/src/grading/WeightedTotalStrategy.java
Executable file
61
Homework 4/src/grading/WeightedTotalStrategy.java
Executable file
@@ -0,0 +1,61 @@
|
||||
package grading;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* An implementation GradingStrategy that applies weights to specific grade keys and weighs each
|
||||
* grade before summing a total.
|
||||
*/
|
||||
public class WeightedTotalStrategy implements GradingStrategy
|
||||
{
|
||||
private Map<String, Double> weights;
|
||||
|
||||
/**
|
||||
* Creates a new instance WeightedTotalStrategy.
|
||||
*/
|
||||
public WeightedTotalStrategy()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance WeightedTotalStrategy.
|
||||
*
|
||||
* @param weights
|
||||
* A map of keys and weights to be applied to the grades during calculation.
|
||||
*/
|
||||
public WeightedTotalStrategy(final Map<String, Double> weights)
|
||||
{
|
||||
this.weights = weights;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key
|
||||
* The key of the final totaled grade
|
||||
* @param grades
|
||||
* The list of grades to calculate the final total grade from
|
||||
* @return A new grade representing the total of the weighted grades
|
||||
*/
|
||||
public Grade calculate(final String key, final List<Grade> grades) throws SizeException
|
||||
{
|
||||
if (grades == null || grades.isEmpty())
|
||||
{
|
||||
throw new SizeException();
|
||||
}
|
||||
|
||||
Double total = 0.0;
|
||||
|
||||
for (Grade grade : grades)
|
||||
{
|
||||
Double weight = 1.0;
|
||||
if (weights != null && weights.containsKey(grade.getKey()))
|
||||
{
|
||||
Double mapWeight = weights.get(grade.getKey());
|
||||
weight = Math.max(mapWeight, 0.0);
|
||||
}
|
||||
|
||||
total += Missing.doubleValue(grade.getValue()) * weight;
|
||||
}
|
||||
|
||||
return new Grade(key, total);
|
||||
}
|
||||
}
|
||||
70
Homework 4/src/grading/io/Category.java
Executable file
70
Homework 4/src/grading/io/Category.java
Executable file
@@ -0,0 +1,70 @@
|
||||
package grading.io;
|
||||
|
||||
import grading.*;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* An aggregate for Grade representing a specific type of assignment with a specific Filter and
|
||||
* GradingStrategy.
|
||||
*/
|
||||
public class Category
|
||||
{
|
||||
private final String key;
|
||||
private final List<Grade> grades;
|
||||
private final Filter filter;
|
||||
private final GradingStrategy strategy;
|
||||
|
||||
/**
|
||||
* @param key
|
||||
* The key for the Category
|
||||
* @param grades
|
||||
* The Grades in the category
|
||||
* @param filter
|
||||
* The filter to apply to the collection of grades
|
||||
* @param strategy
|
||||
* The strategy used to calculate the final Category Grade
|
||||
*/
|
||||
public Category(final String key, final List<Grade> grades, final Filter filter,
|
||||
final GradingStrategy strategy)
|
||||
{
|
||||
if (key == null || key.isEmpty())
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
this.key = key;
|
||||
this.grades = grades;
|
||||
this.filter = filter;
|
||||
this.strategy = strategy;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The filter for the Category
|
||||
*/
|
||||
public Filter getFilter()
|
||||
{
|
||||
return filter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The GradingStrategy for the Category
|
||||
*/
|
||||
public GradingStrategy getStrategy()
|
||||
{
|
||||
return strategy;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The Grades making up the Category
|
||||
*/
|
||||
public List<Grade> getGrades()
|
||||
{
|
||||
return grades;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The key for the Category
|
||||
*/
|
||||
public String getKey()
|
||||
{
|
||||
return key;
|
||||
}
|
||||
}
|
||||
41
Homework 4/src/grading/io/Course.java
Executable file
41
Homework 4/src/grading/io/Course.java
Executable file
@@ -0,0 +1,41 @@
|
||||
package grading.io;
|
||||
|
||||
import grading.GradingStrategy;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* An aggregate for Categories with a specific GradingStrategy for calculating the final Course
|
||||
* Grade.
|
||||
*/
|
||||
public class Course
|
||||
{
|
||||
private final GradingStrategy strategy;
|
||||
private final List<Category> categories;
|
||||
|
||||
/**
|
||||
* @param categories The Categories making up the Course
|
||||
* @param strategy The strategy by which to calculate the final grade
|
||||
*/
|
||||
public Course(final List<Category> categories, final GradingStrategy strategy)
|
||||
{
|
||||
this.categories = categories;
|
||||
this.strategy = strategy;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The Categories making up the Course
|
||||
*/
|
||||
public List<Category> getCategories()
|
||||
{
|
||||
return categories;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The strategy used for calculating the final Grade for the Course
|
||||
*/
|
||||
public GradingStrategy getStrategy()
|
||||
{
|
||||
return strategy;
|
||||
}
|
||||
}
|
||||
95
Homework 4/src/grading/io/CourseReader.java
Executable file
95
Homework 4/src/grading/io/CourseReader.java
Executable file
@@ -0,0 +1,95 @@
|
||||
package grading.io;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import grading.*;
|
||||
|
||||
/**
|
||||
* A utility class for reading in Grade data from a .grd file and constructing Categories and
|
||||
* Courses.
|
||||
*/
|
||||
public class CourseReader
|
||||
{
|
||||
private static final String TAB_DELIMITER = "\t";
|
||||
|
||||
/**
|
||||
* Empty constructor so the coverage tests stop yelling at me.
|
||||
*/
|
||||
public CourseReader()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param in
|
||||
* A BufferedReader for reading data from the file
|
||||
* @param size
|
||||
* The number of grade entries in a given category to read
|
||||
* @param key
|
||||
* The key for the category
|
||||
* @param filter
|
||||
* The filter to construct the Category with
|
||||
* @return A Category containing all the grades read from the file
|
||||
* @throws IOException
|
||||
* @throws IllegalArgumentException
|
||||
*/
|
||||
public static Category readCategory(final BufferedReader in, final int size, final String key,
|
||||
final Filter filter) throws IOException, IllegalArgumentException
|
||||
{
|
||||
final List<Grade> grades = new ArrayList<Grade>();
|
||||
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
final String[] gradeData = in.readLine().split(TAB_DELIMITER);
|
||||
|
||||
Double gradeScore = null;
|
||||
if (gradeData.length > 1)
|
||||
{
|
||||
gradeScore = Double.parseDouble(gradeData[1]);
|
||||
}
|
||||
|
||||
grades.add(new Grade(gradeData[0], gradeScore));
|
||||
}
|
||||
|
||||
return new Category(key, grades, filter, new TotalStrategy());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param in
|
||||
* A BufferedReader for reading data from the file
|
||||
* @param size
|
||||
* The number of categories in the file to read
|
||||
* @return A Course consisting of all Categories read from the file
|
||||
* @throws IOException
|
||||
*/
|
||||
public static Course readCourse(final BufferedReader in, final int size) throws IOException
|
||||
{
|
||||
|
||||
final List<Category> categories = new ArrayList<Category>();
|
||||
final Map<String, Double> weights = new HashMap<String, Double>();
|
||||
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
final String[] categoryFields = in.readLine().split(TAB_DELIMITER);
|
||||
final String categoryName = categoryFields[0];
|
||||
final int gradeCount = Integer.parseInt(categoryFields[1]);
|
||||
|
||||
weights.put(categoryName, Double.parseDouble(categoryFields[2]));
|
||||
|
||||
final boolean shouldDropLowest = Boolean.parseBoolean(categoryFields[3]);
|
||||
final boolean shouldDropHighest = Boolean.parseBoolean(categoryFields[4]);
|
||||
final Filter filter = new DropFilter(shouldDropLowest, shouldDropHighest);
|
||||
|
||||
final Category category = CourseReader.readCategory(in, gradeCount, categoryName, filter);
|
||||
categories.add(category);
|
||||
}
|
||||
|
||||
final GradingStrategy strategy = new WeightedTotalStrategy(weights);
|
||||
return new Course(categories, strategy);
|
||||
}
|
||||
}
|
||||
51
Homework 4/src/testing/CategoryTest.java
Executable file
51
Homework 4/src/testing/CategoryTest.java
Executable file
@@ -0,0 +1,51 @@
|
||||
package testing;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import grading.*;
|
||||
import grading.io.Category;
|
||||
import java.util.*;
|
||||
|
||||
class CategoryTest
|
||||
{
|
||||
|
||||
private Filter filter;
|
||||
private GradingStrategy strategy;
|
||||
private List<Grade> grades;
|
||||
private String validKey;
|
||||
|
||||
@BeforeEach
|
||||
void setUp()
|
||||
{
|
||||
filter = new DropFilter();
|
||||
strategy = new TotalStrategy();
|
||||
grades = new ArrayList<Grade>();
|
||||
validKey = "math";
|
||||
}
|
||||
|
||||
@Test
|
||||
void badKeyTest()
|
||||
{
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> new Category(null, grades, filter, strategy));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Category("", grades, filter, strategy));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getterTest()
|
||||
{
|
||||
Grade grade = new Grade("HW1", 95.0);
|
||||
grades.add(grade);
|
||||
Category category = new Category(validKey, grades, filter, strategy);
|
||||
|
||||
assertEquals(validKey, category.getKey());
|
||||
|
||||
assertSame(grades, category.getGrades());
|
||||
|
||||
assertEquals(filter, category.getFilter());
|
||||
|
||||
assertEquals(strategy, category.getStrategy());
|
||||
}
|
||||
}
|
||||
67
Homework 4/src/testing/CourseReaderTest.java
Executable file
67
Homework 4/src/testing/CourseReaderTest.java
Executable file
@@ -0,0 +1,67 @@
|
||||
package testing;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import grading.*;
|
||||
import grading.io.*;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
class CourseReaderTest
|
||||
{
|
||||
@Test
|
||||
void grdFileTests() throws IOException, SizeException
|
||||
{
|
||||
assertNotNull(new CourseReader()); // Nonsense test for coverage
|
||||
|
||||
final Grade complete01Grade = calculateCourseGradeFromFile("complete_01.grd");
|
||||
final Grade complete02Grade = calculateCourseGradeFromFile("complete_02.grd");
|
||||
final Grade complete03Grade = calculateCourseGradeFromFile("complete_03.grd");
|
||||
final Grade fiveCategories01Grade = calculateCourseGradeFromFile("five-categories_01.grd");
|
||||
final Grade missingAllGrade = calculateCourseGradeFromFile("missing_all.grd");
|
||||
final Grade missingMultipleInEachGrade = calculateCourseGradeFromFile(
|
||||
"missing_multiple-in-each.grd");
|
||||
final Grade missingOneInEachGrade = calculateCourseGradeFromFile("missing_one-in-each.grd");
|
||||
final Grade missingOneInOneGrade = calculateCourseGradeFromFile("missing_one-in-one.grd");
|
||||
final Grade sixCategories01Grade = calculateCourseGradeFromFile("six-categories_01.grd");
|
||||
|
||||
assertEquals(80.7, complete01Grade.getValue());
|
||||
assertEquals(100, complete02Grade.getValue());
|
||||
assertEquals(54.0, complete03Grade.getValue());
|
||||
assertEquals(64.75, fiveCategories01Grade.getValue());
|
||||
assertEquals(0.0, missingAllGrade.getValue());
|
||||
assertEquals(19.5, missingMultipleInEachGrade.getValue());
|
||||
assertEquals(35.2, missingOneInEachGrade.getValue());
|
||||
assertEquals(80.7, missingOneInOneGrade.getValue());
|
||||
assertEquals(76.6, sixCategories01Grade.getValue());
|
||||
}
|
||||
|
||||
private static Grade calculateCourseGradeFromFile(final String filename)
|
||||
throws IOException, SizeException
|
||||
{
|
||||
BufferedReader in = new BufferedReader(new FileReader(filename));
|
||||
String line = in.readLine();
|
||||
int categories = Integer.parseInt(line);
|
||||
Course course = CourseReader.readCourse(in, categories);
|
||||
in.close();
|
||||
|
||||
ArrayList<Grade> grades = new ArrayList<Grade>();
|
||||
for (Category c : course.getCategories())
|
||||
{
|
||||
GradingStrategy categoryStrategy = c.getStrategy();
|
||||
Filter filter = c.getFilter();
|
||||
Grade grade = categoryStrategy.calculate(c.getKey(), filter.apply(c.getGrades()));
|
||||
grades.add(grade);
|
||||
}
|
||||
|
||||
GradingStrategy courseStrategy = course.getStrategy();
|
||||
Grade courseGrade = courseStrategy.calculate("Course Grade", grades);
|
||||
|
||||
return courseGrade;
|
||||
}
|
||||
}
|
||||
37
Homework 4/src/testing/CourseTest.java
Executable file
37
Homework 4/src/testing/CourseTest.java
Executable file
@@ -0,0 +1,37 @@
|
||||
package testing;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import grading.*;
|
||||
import grading.io.Category;
|
||||
import grading.io.Course;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class CourseTest
|
||||
{
|
||||
@Test
|
||||
void testCourseInitialization()
|
||||
{
|
||||
// Arrange
|
||||
final GradingStrategy strategy = new TotalStrategy();
|
||||
final Filter filter = new DropFilter();
|
||||
final List<Category> categories = new ArrayList<Category>();
|
||||
|
||||
final List<Grade> homeworkGrades = new ArrayList<Grade>();
|
||||
homeworkGrades.add(new Grade("HW1", 20.0));
|
||||
homeworkGrades.add(new Grade("HW2", 5.0));
|
||||
|
||||
final Category hws = new Category("Homework Assignments", homeworkGrades, filter, strategy);
|
||||
categories.add(hws);
|
||||
|
||||
final Course course = new Course(categories, strategy);
|
||||
|
||||
assertEquals(categories, course.getCategories(),
|
||||
"Categories should match the initialized values");
|
||||
assertEquals(strategy, course.getStrategy(), "Strategy should match the initialized value");
|
||||
}
|
||||
}
|
||||
38
Homework 4/src/testing/DropFilterTest.java
Executable file
38
Homework 4/src/testing/DropFilterTest.java
Executable file
@@ -0,0 +1,38 @@
|
||||
package testing;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import grading.Grade;
|
||||
import grading.SizeException;
|
||||
import grading.DropFilter;
|
||||
|
||||
class DropFilterTest
|
||||
{
|
||||
@Test
|
||||
void applyTest()
|
||||
{
|
||||
final Grade grade1 = new Grade("HW", 40.0);
|
||||
final Grade grade2 = new Grade("Midterm", (Double) null);
|
||||
final Grade grade3 = new Grade("Final", 100.0);
|
||||
|
||||
List<Grade> grades = List.of(grade1, grade2, grade3);
|
||||
|
||||
DropFilter defaultFilter = new DropFilter();
|
||||
DropFilter filter1 = new DropFilter(false, false);
|
||||
DropFilter filter2 = new DropFilter(false, true);
|
||||
DropFilter filter3 = new DropFilter(true, false);
|
||||
DropFilter filter4 = new DropFilter(true, true);
|
||||
|
||||
assertThrows(SizeException.class, () -> defaultFilter.apply((List<Grade>) null));
|
||||
assertThrows(SizeException.class, () -> defaultFilter.apply(new ArrayList<Grade>()));
|
||||
|
||||
assertIterableEquals(grades, filter1.apply(grades));
|
||||
assertIterableEquals(List.of(grade1, grade2), filter2.apply(grades));
|
||||
assertIterableEquals(List.of(grade1, grade3), filter3.apply(grades));
|
||||
assertIterableEquals(List.of(grade1), filter4.apply(grades));
|
||||
}
|
||||
}
|
||||
79
Homework 4/src/testing/GradeTest.java
Executable file
79
Homework 4/src/testing/GradeTest.java
Executable file
@@ -0,0 +1,79 @@
|
||||
package testing;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import grading.Grade;
|
||||
|
||||
class GradeTest
|
||||
{
|
||||
private static final String HOMEWORK_KEY = "HW";
|
||||
private static final String MIDTERM_KEY = "Midterm";
|
||||
private static final String FINAL_KEY = "Final";
|
||||
|
||||
@Test
|
||||
void defaultValueConstructorTest()
|
||||
{
|
||||
Grade grade = new Grade(FINAL_KEY);
|
||||
assertEquals(FINAL_KEY, grade.getKey());
|
||||
assertEquals(0.0, grade.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenValueConstructorTest()
|
||||
{
|
||||
Grade grade = new Grade(MIDTERM_KEY, 85.5);
|
||||
assertEquals(MIDTERM_KEY, grade.getKey());
|
||||
assertEquals(85.5, grade.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullValueConstructorTest()
|
||||
{
|
||||
assertThrows(IllegalArgumentException.class, () -> new Grade(null));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Grade(null, 90.0));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Grade(null, (Double) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyKeyConstructorTest()
|
||||
{
|
||||
assertThrows(IllegalArgumentException.class, () -> new Grade(""));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Grade("", 90.0));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Grade("", (Double) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullCompareToTest()
|
||||
{
|
||||
Grade grade1 = new Grade(HOMEWORK_KEY, (Double) null);
|
||||
Grade grade2 = new Grade(HOMEWORK_KEY, 80.0);
|
||||
Grade grade3 = new Grade(HOMEWORK_KEY, (Double) null);
|
||||
|
||||
assertTrue(grade1.compareTo(grade2) < 0);
|
||||
assertEquals(0, grade1.compareTo(grade3));
|
||||
assertTrue(grade2.compareTo(grade1) > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void compareToTest()
|
||||
{
|
||||
Grade grade1 = new Grade(HOMEWORK_KEY, 75.0);
|
||||
Grade grade2 = new Grade(HOMEWORK_KEY, 85.0);
|
||||
Grade grade3 = new Grade(HOMEWORK_KEY, 75.0);
|
||||
|
||||
assertTrue(grade1.compareTo(grade2) < 0);
|
||||
assertEquals(0, grade1.compareTo(grade3));
|
||||
assertTrue(grade2.compareTo(grade1) > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void toStringTest()
|
||||
{
|
||||
Grade grade1 = new Grade(FINAL_KEY, 90.0);
|
||||
Grade grade2 = new Grade(MIDTERM_KEY, (Double) null);
|
||||
|
||||
assertEquals("Final: 90.0", grade1.toString());
|
||||
assertEquals("Midterm: NA", grade2.toString());
|
||||
}
|
||||
}
|
||||
23
Homework 4/src/testing/MissingTest.java
Executable file
23
Homework 4/src/testing/MissingTest.java
Executable file
@@ -0,0 +1,23 @@
|
||||
package testing;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import grading.Missing;
|
||||
|
||||
class MissingTest
|
||||
{
|
||||
@Test
|
||||
void defaultDoubleValueTest()
|
||||
{
|
||||
assertEquals(40.0, Missing.doubleValue(40.0));
|
||||
assertEquals(0.0, Missing.doubleValue((Double) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenMissingDoubleValueTest()
|
||||
{
|
||||
assertEquals(40.0, Missing.doubleValue(40.0, 20.0));
|
||||
assertEquals(20.0, Missing.doubleValue((Double) null, 20.0));
|
||||
}
|
||||
}
|
||||
34
Homework 4/src/testing/TotalStrategyTest.java
Executable file
34
Homework 4/src/testing/TotalStrategyTest.java
Executable file
@@ -0,0 +1,34 @@
|
||||
package testing;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import grading.Grade;
|
||||
import grading.TotalStrategy;
|
||||
import grading.SizeException;
|
||||
|
||||
class TotalStrategyTest
|
||||
{
|
||||
private static final String TOTAL_KEY = "Total";
|
||||
|
||||
@Test
|
||||
void calculateTest()
|
||||
{
|
||||
List<Grade> grades = new ArrayList<Grade>();
|
||||
grades.add(new Grade("HW1", 40.0));
|
||||
grades.add(new Grade("HW2", (Double) null));
|
||||
grades.add(new Grade("HW3", 100.0));
|
||||
|
||||
TotalStrategy strategy = new TotalStrategy();
|
||||
|
||||
assertThrows(SizeException.class, () -> strategy.calculate(TOTAL_KEY, (List<Grade>) null));
|
||||
assertThrows(SizeException.class, () -> strategy.calculate(TOTAL_KEY, new ArrayList<Grade>()));
|
||||
|
||||
Grade total = strategy.calculate(TOTAL_KEY, grades);
|
||||
|
||||
assertEquals(TOTAL_KEY, total.getKey());
|
||||
assertEquals(140.0, total.getValue());
|
||||
}
|
||||
}
|
||||
50
Homework 4/src/testing/WeightedTotalStrategyTest.java
Executable file
50
Homework 4/src/testing/WeightedTotalStrategyTest.java
Executable file
@@ -0,0 +1,50 @@
|
||||
package testing;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import grading.Grade;
|
||||
import grading.WeightedTotalStrategy;
|
||||
import grading.SizeException;
|
||||
|
||||
class WeightedTotalStrategyTest
|
||||
{
|
||||
private static final String HOMEWORK_KEY = "HW";
|
||||
private static final String MIDTERM_KEY = "Midterm";
|
||||
private static final String FINAL_KEY = "Final";
|
||||
private static final String TOTAL_KEY = "Total";
|
||||
|
||||
@Test
|
||||
void calculateTest()
|
||||
{
|
||||
List<Grade> grades = new ArrayList<Grade>();
|
||||
grades.add(new Grade(HOMEWORK_KEY, 40.0));
|
||||
grades.add(new Grade(MIDTERM_KEY, (Double) null));
|
||||
grades.add(new Grade(FINAL_KEY, 100.0));
|
||||
|
||||
Map<String, Double> weights = new HashMap<String, Double>();
|
||||
weights.put(MIDTERM_KEY, 2.0);
|
||||
weights.put(FINAL_KEY, 3.0);
|
||||
|
||||
WeightedTotalStrategy defaultStrategy = new WeightedTotalStrategy();
|
||||
|
||||
assertThrows(SizeException.class,
|
||||
() -> defaultStrategy.calculate(TOTAL_KEY, (List<Grade>) null));
|
||||
assertThrows(SizeException.class,
|
||||
() -> defaultStrategy.calculate(TOTAL_KEY, new ArrayList<Grade>()));
|
||||
|
||||
Grade total = defaultStrategy.calculate(TOTAL_KEY, grades);
|
||||
|
||||
assertEquals(TOTAL_KEY, total.getKey());
|
||||
assertEquals(140.0, total.getValue());
|
||||
|
||||
WeightedTotalStrategy strategy = new WeightedTotalStrategy(weights);
|
||||
|
||||
Grade weightedTotal = strategy.calculate(TOTAL_KEY, grades);
|
||||
|
||||
assertEquals(TOTAL_KEY, weightedTotal.getKey());
|
||||
assertEquals(340.0, weightedTotal.getValue());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user