Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Java, implement the DropFilter class by following below instructions. Also, please make sure that it works with Gradient class and Grade class provided below.

Using Java, implement the DropFilter class by following below instructions. Also, please make sure that it works with Gradient class and Grade class provided below.image text in transcribedimage text in transcribed

package app;

import grading.*;

import java.util.*;

/**

* An application for calculating the numeric grade for

* a course from the grades on individual assignments.

*

* This version assumes that the course is structured as follows:

*

* 6 programming assignments (PAs) accounting for 40% of the course grade

* after the lowest grade is dropped.

*

* 5 homework assignments (HWs) accounting for 10% of the course grade.

*

* 1 mid-term exam (Midterm) accounting for 20% of the course grade.

*

* 1 final exam (Final) accounting for 30% of the course grade.

*

* @version 1.0

* @author Sagacious Media

*

*/

public class Gradient

{

/**

* The entry point for the application.

*

* @param args The 13 grades (ordered as above). Missing grades can be entered as "NA".

*/

public static void main(String[] args)

{

Filter paFilter;

Grade courseGrade, hwGrade, paGrade;

GradingStrategy courseStrategy, hwStrategy, paStrategy;

List grades, hws, pas;

Map courseWeights;

// Early exit

if ((args == null) || (args.length != 13))

{

System.err.println("You must enter all 13 grades. (Use NA for missing.)");

System.exit(1);

}

// Create the filter and strategy for PAs

paFilter = new DropFilter(true, false);

paStrategy = new TotalStrategy();

// Create the strategy for HWs

hwStrategy = new TotalStrategy();

// Create the weights and strategy for the course grade

courseWeights = new HashMap();

courseWeights.put("PAs", 0.4);

courseWeights.put("HWs", 0.1);

courseWeights.put("Midterm", 0.2);

courseWeights.put("Final", 0.3);

courseStrategy = new WeightedTotalStrategy(courseWeights);

try

{

// Put the PA grades in a List

pas = new ArrayList();

for (int i=0; i

{

pas.add(parseGrade("PA"+(i+1), args[i]));

}

// Calculate the PA grade (after filtering)

paGrade = paStrategy.calculate("PAs", paFilter.apply(pas));

// Put the HW grades in a List

hws = new ArrayList();

for (int i=0; i

{

hws.add(parseGrade("HW"+(i+1), args[i+6]));

}

// Calculate the HW grade

hwGrade = hwStrategy.calculate("HWs", hws);

// Put all of the grades in a List

grades = new ArrayList();

grades.add(paGrade);

grades.add(hwGrade);

grades.add(parseGrade("Midterm", args[11]));

grades.add(parseGrade("Final", args[12]));

// Calculate the final grade

courseGrade = courseStrategy.calculate("Course Grade", grades);

// Display the final grade

System.out.println(courseGrade.toString());

}

catch (SizeException se)

{

System.out.println("You entered too few valid grades.");

}

catch (IllegalArgumentException iae)

{

// Should never get here since all keys should be valid

}

}

/**

* Construct a Grade object from a key and a String representation of

* its value. If the String representation of the value is null or not a valid

* double then the resulting Grade will have a missing value.

*

* @param key The key for the Grade

* @param value The String representation of the value

* @return The corresponding Grade object

* @throws IllegalArgumentException if the key is invalid

*/

private static Grade parseGrade(String key, String value) throws IllegalArgumentException

{

Grade result;

try

{

Double v;

if (value == null) v = null;

else v = new Double(Double.parseDouble(value));

result = new Grade(key, v);

}

catch (NumberFormatException nfe)

{

result = new Grade(key, null);

}

return result;

}

}

Grade.java

package grading;

public class Grade implements Comparable { //data fields for this class private String key; private Double value; /** * compareTo() method to conform to Comparable type implementation * @param other * @return int : result of comparing values of two Grades */ @Override public int compareTo(Grade other) { if (this.value == null && other.value != null) { return -1; } else if (this.value == null && other.value == null) { return 0; } else if (this.value != null && other.value == null) { return 1; } else { //both values are non-null, continue with standard comparison return this.value.compareTo(other.value); } } /** * Constructor * @param key */ public Grade(String key) throws IllegalArgumentException { //error handling if (key == null || key == "") { throw new IllegalArgumentException("Key cannot be null or empty."); } this.key = key; this.value = 0.0; } /** * Constructor (java.lang.Double) * @param key * @param value */ public Grade(String key, Double value) throws IllegalArgumentException { //error handling if (key == null || key == "") { throw new IllegalArgumentException("Key cannot be null or empty."); } this.key = key; this.value = value; } /** * Constructor (double) * @param key * @param value */ public Grade(String key, double value) throws IllegalArgumentException { //error handling if (key == null || key == "") { throw new IllegalArgumentException("Key cannot be null or empty."); } this.key = key; this.value = value; } /** * Getter for key * @return String */ public String getKey() { return this.key; } /** * Getter for value * @return Double */ public Double getValue() { return this.value; } /** * toString() implementation * @return String : checks if null, then returns key/value as readable String */ public String toString() { if (value != null) { return String.format("%s: %5s", key, this.value); } else { return String.format("%s: %5.1f", key, "NA"); } } }

DropFilter -shouldDropLowest boolearn -shouldDropHighest: boolean +DropFilter0 DropFilter(shouldDropLowest: boolean, shouldDropHighest: boolean) DropFilter -shouldDropLowest boolearn -shouldDropHighest: boolean +DropFilter0 DropFilter(shouldDropLowest: boolean, shouldDropHighest: boolean)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 11G SQL

Authors: Joan Casteel

2nd Edition

1133947360, 978-1133947363

More Books

Students also viewed these Databases questions