Question

answer the following question in the java programming language thank you!Definition: The Strategy pattern defines a...

answer the following question in the java programming language thank you!Definition: The Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use itYour assignment is to implement the Strategy pattern for the problem of computing an average for a list of submitted grades. The algorithms are as follows:Calculate the average grade for a list of integer numbersCalculate the average grade for a list of integer numbers, dropping the two lowest scoresIn addition, you should develop a set of Junit test cases that sufficiently show that your implementations execute. Finally, your code should be uploaded to repl.itTo get you started, your Strategy interface should look like:public interface AverageStrategy { public int compute(List grades) throws EmptyListException;}Note that your implementations for compute should throw EmptyListException when the method is passed an empty list. The aforementioned exception is a user defined exception and you will need to do a little research on how to create it. You should test the EmptyListException in your JUnit test cases.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.Test;

class EmptyListException extends Exception {
        public EmptyListException() {
                super("The list can not be empty.");
        }
}

interface AverageStrategy {
    public int compute(List<Integer> grades) throws EmptyListException;
}


class Strategy1 implements AverageStrategy {

        @Override
        public int compute(List<Integer> grades) throws EmptyListException {
                if(grades == null || grades.size() < 1) {
                        throw new EmptyListException();
                }

                int count = 0;
                int sum = 0;
                Collections.sort(grades);
                for(int i=0; i<grades.size(); i++) {
                        sum += grades.get(i);
                        count++;
                }
                
                return sum/count;
        }
        
}
class Strategy2 implements AverageStrategy {

        @Override
        public int compute(List<Integer> grades) throws EmptyListException {
                if(grades == null || grades.size() <= 2) {
                        throw new EmptyListException();
                }
                
                int count = 0;
                int sum = 0;
                Collections.sort(grades);
                for(int i=2; i<grades.size(); i++) {
                        sum += grades.get(i);
                        count++;
                }
                
                return sum/count;
        }
        
}

public class AvgTest {

        @Test
        public void testStrategy1() {
                AverageStrategy as = new Strategy1();
                try {
                        assertEquals(2, as.compute(Arrays.asList(1, 2, 3)));
                } catch (EmptyListException e) {
                        fail();
                }
        }


        @Test
        public void testStrategy2() {
                AverageStrategy as = new Strategy2();
                try {
                        assertEquals(3, as.compute(Arrays.asList(1, 2, 3)));
                } catch (EmptyListException e) {
                        fail();
                }
        }


        @Test
        public void testStrategy1Exce() {
                AverageStrategy as = new Strategy1();
                try {
                        as.compute(Arrays.asList());
                        fail();
                } catch (EmptyListException e) {
                }
        }


        @Test
        public void testStrategy2Exce() {
                AverageStrategy as = new Strategy2();
                try {
                        as.compute(Arrays.asList());
                        fail();
                } catch (EmptyListException e) {
                }
        }


}

please upvote.

Add a comment
Know the answer?
Add Answer to:
answer the following question in the java programming language thank you!Definition: The Strategy pattern defines a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C programming For this project, you have been tasked to read a text file with student grade...

    Using C programming For this project, you have been tasked to read a text file with student grades and perform several operations with them. First, you must read the file, loading the student records. Each record (line) contains the student’s identification number and then four of the student’s numerical test grades. Your application should find the average of the four grades and insert them into the same array as the id number and four grades. I suggest using a 5th...

  • write the following code in java thank you and if you can, could you please write...

    write the following code in java thank you and if you can, could you please write the code in replit and sent me a link to it replit is basically like a online compiler and you can write java code on it thank you The first part is to implement one of the questions from your examination. /* A Range objects represents an integer range, such as 1-10 or 50701-50799. The lower and upper bounds of a Range are given...

  • Needs Help with Java programming language For this assignment, you need to write a simulation program...

    Needs Help with Java programming language For this assignment, you need to write a simulation program to determine the average waiting time at a grocery store checkout while varying the number of customers and the number of checkout lanes. Classes needed: SortedLinked List: Implement a generic sorted singly-linked list which contains all of the elements included in the unsorted linked list developed in class, but modifies it in the following way: • delete the addfirst, addlast, and add(index) methods and...

  • This lab serves as an intro to Java Interfaces and an Object-Oriented design strategy towards implementing...

    This lab serves as an intro to Java Interfaces and an Object-Oriented design strategy towards implementing data structures. We will be using Entry.java objects which are key,value pairs, with key being an integer and value being a generic parameter. We will be sorting these Entries according to their key value. The file Entry.java is given to you and does not require any modification. As you should know by now, bucket sort works by placing items into buckets and each bucket...

  • !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random...

    !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random text with my generateText method, you can move on to the second step. Create a third class called Generator within the cs1410 package. Make class. This class should have a main method that provides a user interface for random text generation. Your interface should work as follows: Main should bring up an input dialog with which the user can enter the desired analysis level...

  • Write a C++ program named, gradeProcessor.cpp, that will do the following tasks: -Print welcome message -Generate...

    Write a C++ program named, gradeProcessor.cpp, that will do the following tasks: -Print welcome message -Generate the number of test scores the user enters; have scores fall into a normal distribution for grades -Display all of the generated scores - no more than 10 per line -Calculate and display the average of all scores -Find and display the number of scores above the overall average (previous output) -Find and display the letter grade that corresponds to the average above (overall...

  • Please answer Question 4 WITH A C# PROGRAM!!!!!! THANK YOU! File Edit View History Bookmarks Tools...

    Please answer Question 4 WITH A C# PROGRAM!!!!!! THANK YOU! File Edit View History Bookmarks Tools Help Share GEP IIIA Moorhead, MN 10-Day Weathe X N NDSU Faculty and Staff | North x D C#.NET - CSCI 213: Modern Sol X VitalSource Bookshelf: Starting x + + → OO - https://bookshelf.vitalsource.com/#/books/9780134400433/cf1/6 90% *** * Cambie Meble alle... S N 10 Crosby Derek Lam ... Alterna Caviar Anti-Ag... U C# Tutorial For Beginn... BE Celsius Fahrenheit con... Charter Club Sweater. Folklorama...

  • Please help me write in C++ language for Xcode. Thank you. In this lab you are...

    Please help me write in C++ language for Xcode. Thank you. In this lab you are going to write a time card processor program. Your program will read in a file called salary.txt. This file will include a department name at the top and then a list of names (string) with a set of hours following them. The file I test with could have a different number of employees and a different number of hours. There can be more than...

  • You need not run Python programs on a computer in solving the following problems. Place your...

    You need not run Python programs on a computer in solving the following problems. Place your answers into separate "text" files using the names indicated on each problem. Please create your text files using the same text editor that you use for your .py files. Answer submitted in another file format such as .doc, .pages, .rtf, or.pdf will lose least one point per problem! [1] 3 points Use file math.txt What is the precise output from the following code? bar...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT