Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is the base project I have for the question package cs1410; import java.awt

ID: 672576 • Letter: T

Question

This is the base project I have for the question

package cs1410;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Scanner;

public class GraphingMethods
{
/**
* Constant used to request a max operation
*/
public final static int MAX = 0;

/**
* Constant used to request a min operation
*/
public final static int MIN = 1;

/**
* Constant used to request a sum operation
*/
public final static int SUM = 2;

/**
* Constant used to request an average operation
*/
public final static int AVG = 3;

/**
* If names and values have different lengths, throws an IllegalArgumentException.
*
* If values contains any non-positive numbers, throws an IllegalAgumentException.
*
* If categories is of length zero, throws an IllegalArgumentException.
*
* If categories contains any duplicates, throws an IllegalArgumentException.
*
* If operation is anything other than SUM, AVG, MAX, or MIN, throws an IllegalArgumentException.
*
* Put side-by-side, the lists "names" and "values" represent a table of values, where each row contains a name and
* a value. For example, here is the table of values that might give the number of people named Zebediah in
* different parts of four states.
*
* <pre>
* Utah 5
* Nevada 6
* California 12
* Oregon 8
* Utah 9
* California 10
* Nevada 4
* Nevada 4
* Oregon 17
* California 6
* </pre>
*
* We can make a table of summaries by pairing each string c from the list "categories" with either the sum,
* average, maximum, or minimum of the numbers paired with c in the table above. For example, if "categories" is
*
* <pre>
* Utah
* Nevada
* California
* </pre>
*
* then, assuming we are summing, the table of summaries would be
*
* <pre>
* Utah 14
* Nevada 14
* California 28
* </pre>
*
* The method should calculate and return, as an ArrayList<Double>, the second column of the table of summaries. It
* should use the value of the "operation" parameter to determine whether the values are combined by summing,
* averaging, max-ing, or min-ing. (If, for any category, there are no values to sum, average, min, or max, the
* method should throw an IllegalArgumentException.)
*
* The order of the numbers in the returned ArrayList<Double> should correspond to the order of the categories in
* the "categories" list.
*/
public static ArrayList<Double> summarizeData (ArrayList<String> names, ArrayList<Double> values,
ArrayList<String> categories, int operation)
{
// This is sample code that always returns the same summaries.
ArrayList<Double> summaries = new ArrayList<Double>();
summaries.add(14.0);
summaries.add(14.0);
summaries.add(28.0);
return summaries;
}

/**
* If categories, summaries, or colors is empty, throws an IllegalArgumentException.
*
* If categories, summaries, and colors don't have the same number of elements, throws an IllegalArgumentException.
*
* If any of the numbers in summaries is non-positive, throws an IllegalArgumentException.
*
* Otherwise, views the three lists as a table with a String, a double, and a Color in each column and displays the
* data with either a pie chart (if usePieChart is true) or a bar graph (otherwise). Let SUM be the sum of all the
* entries in summaries. The area of slice i (of a pie chart) and the length of bar i (in a bar graph) should be
* proportional to categories[i]/SUM. The slice/bar should be labeled with categories[i] and summaries[i], and it
* should be colored with colors[i].
*
* For example, suppose we are given this data:
*
* <pre>
* Utah 14 Color.RED
* Nevada 14 Color.BLUE
* California 28 Color.GREEN
* </pre>
*
* In a pie chart Utah and Nevada should each have a quarter of the pie and California should have half. In a bar
* graph, California's line should be twice as long as Utah's and Nevada's.
*
* The method should display the pie chart or bar graph by drawing on the g parameter. The example code below draws
* both a pie chart and a bar graph for the situation described above.
*/
public static void drawGraph (Graphics g, ArrayList<String> categories, ArrayList<Double> summaries,
ArrayList<Color> colors, boolean usePieChart)
{
// This is sample code that always draws the same charts and graphs
if (usePieChart)
{
g.setColor(Color.RED);
g.fillArc(10, 10, 300, 300, 0, 360);
g.fillRect(330, 10, 10, 10);
g.setColor(Color.black);
g.drawString("Utah 14.00", 350, 20);
  
g.setColor(Color.BLUE);
g.fillArc(10, 10, 300, 300, 90, 90);
g.fillRect(330, 25, 10, 10);
g.setColor(Color.black);
g.drawString("Nevada 14.00", 350, 35);
  
g.setColor(Color.GREEN);
g.fillArc(10, 10, 300, 300, 180, 180);
g.fillRect(330, 40, 10, 10);
g.setColor(Color.black);
g.drawString("California 28.00", 350, 50);
}
else
{
g.setColor(Color.RED);
g.fillRect(235, 10, 75, 20);
g.setColor(Color.black);
g.drawString("Utah 14.00", 330, 25);
  
g.setColor(Color.BLUE);
g.fillRect(235, 40, 75, 20);
g.setColor(Color.black);
g.drawString("Nevada 14.00", 330, 55);
  
g.setColor(Color.GREEN);
g.fillRect(160, 70, 150, 20);
g.setColor(Color.black);
g.drawString("Califonia 28.00", 330, 85);
}
}

/**
* The dataSource should consist of zero or more lines. Each line should consist of some text, followed by a tab
* character, followed by a double literal, followed by a newline.
*
* If any lines are encountered that don't meet this criteria, the method throws an IllegalArgumentException whose
* message explains what is wrong.
*
* Otherwise, for each line, the text before the tab is added to names, and the parsed double literal is added to
* values.
*/
public static void readTable (Scanner dataSource, ArrayList<String> names, ArrayList<Double> values)
{
// This is sample code that always fills out names and values the same way.
ArrayList States = new ArrayList();
while (dataSource.hasNextLine())
{
String line = dataSource.nextLine();
String[] a = line.split(" ");
double d = Double.parseDouble(a[1]);
d = 0;
d++;
String[] a1 = line.split(" ");
}

}

}

The question is to do this

You are to implement the three methods that are specified in the GraphicsMethods class.  You do not need to change or even understand any other class, but you must implement the three methods in GraphicsMethod exactly as specified. Read their specifications carefully, because they are very detailed. The three methods have simple skeleton implementations that, while incorrect, allow the application to run. When you have correctly implemented the methods, the project as a whole will also work properly.

The purpose of this assignment is to give you practice with procedural design. The methods are complicated enough that you are going to almost certainly want to design and implement helper methods.

For each method that you implement---whenever it is one of my methods or one of your helpers---you are to create JUnit test cases that test it thoroughly. The only exception is for methods that draw with a Graphics object, as there is no easy way to make assertions about graphics.

Here's how you should proceed:

Create a set of JUnit test cases for the summarizeData and readTable methods. This will have the benefit of helping you understand what they are supposed to do. I have included one unit test that illustrates how to make assertions about ArrayLists.

Sit and think about you will go about implementing the three methods. Work through examples on paper. When you identify helper methods that would make your life easier, create stubs and test cases for them.

Once you have a good design, implement the methods. Use the test cases to gauge your progress. (If you're like me, you'll probably discover problems in the test cases as you go.)

Please tell me how to solve this question. This question has been troubling my whole week.

Explanation / Answer

Answer:

Program code to copy:

import java.awt.Color;

import java.awt.Graphics;

import java.util.*;

import java.io.*;

public class GraphingMethods

{

     /**

     * Constant used to request a max operation

     */

     public final static int MAX = 0;

     /**

     * Constant used to request a min operation

     */

     public final static int MIN = 1;

     /**

     * Constant used to request a sum operation

     */

     public final static int SUM = 2;

     /**

     * Constant used to request an average operation

     */

     public final static int AVG = 3;

     /**

     * If names and values have different lengths, throws an

     * IllegalArgumentException.

     *

     * If values contains any non-positive numbers, throws an

     * IllegalAgumentException.

     *

     * If categories is of length zero, throws an IllegalArgumentException.

     *

     * If categories contains any duplicates, throws an

     * IllegalArgumentException.

     *

     * If operation is anything other than SUM, AVG, MAX, or MIN, throws an

     * IllegalArgumentException.

     *

     * Put side-by-side, the lists "names" and "values" represent a table of

     * values, where each row contains a name and a value. For example, here is

     * the table of values that might give the number of people named Zebediah

     * in different parts of four states.

     *

     * <pre>

     * Utah          5

     * Nevada        6

     * California   12

     * Oregon        8

     * Utah          9

     * California   10

     * Nevada        4

     * Nevada        4

     * Oregon       17

     * California    6

     * </pre>

     *

     * We can make a table of summaries by pairing each string c from the list

     * "categories" with either the sum, average, maximum, or minimum of the

     * numbers paired with c in the table above. For example, if "categories"

     * is

     *

     * <pre>

     * Utah

     * Nevada

     * California

     * </pre>

     *

     * then, assuming we are summing, the table of summaries would be

     *

     * <pre>

     * Utah       14

     * Nevada     14

     * California 28

     * </pre>

     *

     * The method should calculate and return, as an ArrayList<Double>, the

     * second column of the table of summaries. It should use the value of the

     * "operation" parameter to determine whether the values are combined by

     * summing, averaging, max-ing, or min-ing. (If, for any category, there

     * are no values to sum, average, min, or max, the method should throw an

     * IllegalArgumentException.)

     *

     * The order of the numbers in the returned ArrayList<Double> should

     * correspond to the order of the categories in the "categories" list.

     */

     public static ArrayList<Double> summarizeData(ArrayList<String> names,

              ArrayList<Double> values, ArrayList<String> categories,

              int operation)

     {

          ArrayList<Double> summaries = new ArrayList<Double>();

          try

          {

              if (names.size() != values.size())

                   throw new IllegalArgumentException();

              for (int i = 0; i < values.size(); i++)

              {

                   if (values.get(i) < 0)

                   {

                        throw new IllegalArgumentException();

                   }

              }

              if (categories.size() == 0)

                   throw new IllegalArgumentException();

              for (int i = 0; i < categories.size(); i++)

               {

                   String str = categories.get(i);

                   for (int j = i + 1; j < categories.size(); j++)

                   {

                        if (str.equalsIgnoreCase(categories.get(j)))

                             throw new IllegalArgumentException();

                   }

              }

              if (operation != SUM && operation != MAX && operation != MIN

                        && operation != AVG)

                   throw new IllegalArgumentException();

              for (int i = 0; i < names.size(); i++)

              {

                   System.out.println(names.get(i) + " " + values.get(i));

              }

          }

          catch (IllegalArgumentException iae)

          {

              System.out

                        .println("Names and colors arraylist sizes are different.");

              System.out.println("Categories arraylist is empty.");

              System.out.println("Categories contains duplicate values.");

              System.out.println("Values arraylist contains negative values.");

              System.out.println("Selection of operation is illegal.");

          }

          if (operation == SUM)

          {

              summaries = getSum(categories, names, values);

          }

          else if (operation == AVG)

          {

              summaries = getAverage(categories, names, values);

          }

          else if (operation == MAX)

          {

              summaries = getMaximums(categories, names, values);

          }

          else if (operation == MIN)

          {

              summaries = getMinimums(categories, names, values);

          }

          else

              throw new IllegalArgumentException();

          return summaries;

     }

     public static ArrayList<Double> getSum(ArrayList<String> categories,

              ArrayList<String> names, ArrayList<Double> values)

     {

          double val = 0;

          ArrayList<Double> result = new ArrayList<Double>();

          for (int i = 0; i < categories.size(); i++)

          {

              val = 0;

              for (int j = 0; j < values.size(); j++)

              {

                   if (categories.get(i).equalsIgnoreCase(names.get(j)))

                        val += values.get(j);

              }

              result.add(val);

          }

          return result;

     }

     public static ArrayList<Double> getAverage(ArrayList<String> categories,

              ArrayList<String> names, ArrayList<Double> values)

     {

          double val = 0, avg = 0;

          int count = 0;

          ArrayList<Double> result = new ArrayList<Double>();

          for (int i = 0; i < categories.size(); i++)

          {

              val = 0;

              count = 0;

              for (int j = 0; j < values.size(); j++)

              {

                   if (categories.get(i).equalsIgnoreCase(names.get(j)))

                   {

                        val += values.get(j);

                        count++;

                   }

              }

              avg = val / count;

              result.add(avg);

          }

          return result;

     }

     public static ArrayList<Double> getMaximums(ArrayList<String> categories,

              ArrayList<String> names, ArrayList<Double> values)

     {

          double max = 0;

          ArrayList<Double> result = new ArrayList<Double>();

          for (int i = 0; i < categories.size(); i++)

          {

              max = values.get(i);

              for (int j = 0; j < values.size(); j++)

              {

                   if (categories.get(i).equalsIgnoreCase(names.get(j)))

                   {

                        if (values.get(j) > max)

                             max = values.get(j);

                   }

              }

              result.add(max);

          }

          return result;

     }

     public static ArrayList<Double> getMinimums(ArrayList<String> categories,

              ArrayList<String> names, ArrayList<Double> values)

     {

          double min = 0;

          ArrayList<Double> result = new ArrayList<Double>();

          for (int i = 0; i < categories.size(); i++)

          {

               min = values.get(i);

              for (int j = 0; j < values.size(); j++)

              {

                   if (categories.get(i).equalsIgnoreCase(names.get(j)))

                   {

                        if (values.get(j) < min)

                             min = values.get(j);

                   }

              }

              result.add(min);

          }

          return result;

     }

     public static ArrayList<String> getCategories(ArrayList<String> names)

     {

          String str;

          boolean flag = false;

          ArrayList<String> newCatog = new ArrayList<String>();

          for (int i = 0; i < names.size(); i++)

          {

              flag = false;

              str = names.get(i);

               for (int j = i; j < names.size() - 1; j++)

              {

                   if (str.equalsIgnoreCase(names.get(j + 1)))

                        flag = true;

              }

              if (!flag)

                   newCatog.add(str);

          }

          return newCatog;

     }

     public ArrayList<Color> getColors(ArrayList<String> categories)

     {

          ArrayList<Color> color = new ArrayList<Color>();

          color.add(Color.RED);

          color.add(Color.BLUE);

          color.add(Color.MAGENTA);

          color.add(Color.GREEN);

          return color;

     }

     /**

     * If categories, summaries, or colors is empty, throws an

     * IllegalArgumentException.

     *

     * If categories, summaries, and colors don't have the same number of

     * elements, throws an IllegalArgumentException.

     *

     * If any of the numbers in summaries is non-positive, throws an

     * IllegalArgumentException.

     *

     * Otherwise, views the three lists as a table with a String, a double, and

     * a Color in each column and displays the data with either a pie chart (if

     * usePieChart is true) or a bar graph (otherwise). Let SUM be the sum of

     * all the entries in summaries. The area of slice i (of a pie chart) and

     * the length of bar i (in a bar graph) should be proportional to

     * categories[i]/SUM. The slice/bar should be labeled with categories[i]

     * and summaries[i], and it should be colored with colors[i].

     *

     * For example, suppose we are given this data:

     *

     * <pre>

     * Utah       14    Color.RED

     * Nevada     14    Color.BLUE

     * California 28    Color.GREEN

     * </pre>

     *

     * In a pie chart Utah and Nevada should each have a quarter of the pie and

     * California should have half. In a bar graph, California's line should be

     * twice as long as Utah's and Nevada's.

     *

     * The method should display the pie chart or bar graph by drawing on the g

     * parameter. The example code below draws both a pie chart and a bar graph

     * for the situation described above.

     */

     public static void drawGraph(Graphics g, ArrayList<String> categories,

              ArrayList<Double> summaries, ArrayList<Color> colors,

              boolean usePieChart)

     {

          try

          {

              if (categories == null && summaries == null && colors == null)

                   throw new IllegalArgumentException();

              if (categories.size() != summaries.size()

                        && summaries.size() != colors.size())

                   throw new IllegalArgumentException();

              for (int i = 0; i < summaries.size(); i++)

              {

                   if (summaries.get(i) < 0)

                   {

                        throw new IllegalArgumentException();

                   }

              }

              double total = 0;

              for (int i = 0; i < summaries.size(); i++)

              {

                   total += summaries.get(i);

              }

              // pie char code

              if (usePieChart)

              {

                   int start = 0;

                   double curve = 0;

                   for (int i = 0; i < categories.size(); i++)

                   {

                        start = (int) (curve * 360 / total);

                        int arc = (int) (summaries.get(i) * 360 / total);

                        g.setColor(colors.get(i));

                        g.fillArc(10, 10, 300, 300, start, arc);

                        curve += summaries.get(i);

                   }

                   g.setColor(colors.get(0));

                   g.fillRect(330, 10, 10, 10);

                   g.setColor(Color.black);

                   g.drawString(categories.get(0) + "" + summaries.get(0),

                             350, 20);

                   g.setColor(colors.get(1));

                   g.fillRect(330, 25, 10, 10);

                   g.setColor(Color.black);

                   g.drawString(categories.get(1) + "" + summaries.get(1),

                             350, 35);

                   g.setColor(colors.get(2));

                   g.fillRect(330, 40, 10, 10);

                   g.setColor(Color.black);

                   g.drawString(categories.get(2) + "" + summaries.get(2),

                             350, 50);

                   g.setColor(colors.get(3));

                   g.fillRect(330, 55, 10, 10);

                   g.setColor(Color.black);

                   g.drawString(categories.get(3) + "" + summaries.get(3),

                             350, 65);

              }

              else

              {

                   int xwidth = 300;

                   int yheight = 400;

                   int xleft = 0;

                   double max = 0;

                   for (Double maxW : summaries)

                   {

                        if (max < maxW)

                             max = maxW;

                   }

                   for (int i = 0; i < categories.size(); i++)

                   {

                        int x = (xwidth) * (i + 1) / categories.size();

                        int bwidth = xwidth / categories.size();

                        int bheight = (int) (Math.round(yheight

                                  * summaries.get(i)) / max);

                        g.setColor(colors.get(i));

                        g.fillRect(xleft, yheight - bheight, bwidth, bheight);

                        xleft = x;

                   }

                   g.setColor(colors.get(0));

                   g.fillRect(330, 10, 10, 10);

                   g.setColor(Color.black);

                   g.drawString(categories.get(0) + "" + summaries.get(0),

                             350, 20);

                   g.setColor(colors.get(1));

                   g.fillRect(330, 25, 10, 10);

                   g.setColor(Color.black);

                   g.drawString(categories.get(1) + "" + summaries.get(1),

                             350, 35);

                   g.setColor(colors.get(2));

                   g.fillRect(330, 40, 10, 10);

                   g.setColor(Color.black);

                   g.drawString(categories.get(2) + "" + summaries.get(2),

                             350, 50);

                   g.setColor(colors.get(3));

                   g.fillRect(330, 55, 10, 10);

                   g.setColor(Color.black);

                   g.drawString(categories.get(3) + "" + summaries.get(3),

                             350, 65);

              }

          }

          catch (IllegalArgumentException iae)

          {

              System.out

                        .println("Either of the summaries, categories or colors are empty.");

              System.out.println("The sizes of the arraylist are not equal.");

              System.out.println("Summaries contains negative values.");

          }

     }

     /**

     * The dataSource should consist of zero or more lines. Each line should

     * consist of some text, followed by a tab character, followed by a double

     * literal, followed by a newline.

     *

     * If any lines are encountered that don't meet this criteria, the method

     * throws an IllegalArgumentException whose message explains what is wrong.

     *

     * Otherwise, for each line, the text before the tab is added to names, and

     * the parsed double literal is added to values.

     */

     public static void readTable(Scanner dataSource, ArrayList<String> names,

              ArrayList<Double> values)

     {

          try

          {

              while (dataSource.hasNextLine())

              {

                   String line = dataSource.nextLine();

                   String[] a = line.split(" ");

                   double d = Double.parseDouble(a[1]);

                   names.add(a[0]);

                   values.add(d);

              }

          }

          catch (IllegalArgumentException iae)

          {

              System.out

                        .println("The name and values are not separated by a tab space.");

              iae.printStackTrace();

          }

     }

}

import javax.swing.*;

import java.awt.*;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.Scanner;

public class GraphicsMethodsDemo extends JApplet

{

     static ArrayList<String> names = new ArrayList<String>();

     static ArrayList<Double> values = new ArrayList<Double>();

     static ArrayList<String> categories = new ArrayList<String>();

     static ArrayList<Double> summaries = new ArrayList<Double>();

     static ArrayList<Color> colors = new ArrayList<Color>();

     public void paint(Graphics g)

     {

          Scanner in = new Scanner(System.in);

          GraphingMethods gm = new GraphingMethods();

          Scanner input;

          boolean usePieChart;

          int operation;

          try

          {

              input = new Scanner(new File("NameValues.txt"));

              gm.readTable(input, names, values);

              for (int i = 0; i < names.size(); i++)

                   System.out.println(names.get(i) + " " + values.get(i));

              System.out

                        .println("Enter the operations to be performed for summarization");

              System.out.println("0. By Maximum values 1. By Minimum values");

              System.out.println("2. By Sum 3. By Average");

              System.out.print("Enter your choice: ");

              operation = in.nextInt();

              categories = gm.getCategories(names);

              summaries = gm.summarizeData(names, values, categories,

                        operation);

              System.out.println("Enter the type of graph to view");

              System.out.println("True - to draw Pie chart");

              System.out.println("False - to draw Bar chart");

              System.out.print("Enter your choice: ");

              usePieChart = in.nextBoolean();

              colors = gm.getColors(categories);

              gm.drawGraph(g, categories, summaries, colors, usePieChart);

          }

          catch (FileNotFoundException fnf)

          {

              fnf.printStackTrace();

          }

          catch (IllegalArgumentException ie)

          {

              ie.printStackTrace();

          }

     }

     public static void main(String args[])

     {

          JFrame jp1 = new JFrame();

          GraphicsMethodsDemo gmd = new GraphicsMethodsDemo();

          jp1.getContentPane().add(gmd, BorderLayout.CENTER);

          jp1.setSize(new Dimension(500, 500));

          jp1.setVisible(true);

     }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote