Question: Instructions To complete the program, you need to write a Bar class. Add a new file called Bar.java to your project. The Bar class should

Instructions

To complete the program, you need to write a Bar class. Add a new file called Bar.java to your project.

The Bar class should have the following instance variables:

  • name (String)
  • value (int)
  • category (String)

After adding the instance variables, write the following methods:

  • A constructor that takes in the name, value, and category as parameters (in that order).
  • getName()
  • getValue()
  • getCategory()

Great! There is one last step we need to do to complete this program. The main class is going to create multiple Bar objects. In order to display the Bar objects in the correct order on screen, it is going to need to sort them. To do this, the main class calls Arrays.sort, which relies on the Comparable interface.

Implement the Comparable interface in your Bar class:

  • Add "implements Comparable" after the class name at the top of the file (omit the quotes). This goes after the class name, but before the beginning curly brace.
  • Override the compareTo method. Your compareTo method should compare Bar objects by comparing the "value" data members.

Run the program - does it work? One thing to watch out for: the bar chart should display with the longest bar on top. If you see the shortest bar on top and the longest bar on the bottom, your compareTo method is sorting objects in reverse order. Reverse your logic and try it again.

Tips on running the program: if you open up the BarChartRacer class, you'll notice that there are several options you can change at the top of the file. You can change the data used and the number of bars displayed, among other things. You may want to fine-tune these constants based on your preferences and your computer.Instructions To complete the program, you need to write a Bar class.Add a new file called Bar.java to your project. The Bar classshould have the following instance variables: name (String) value (int) category (String)After adding the instance variables, write the following methods: A constructor that

// draw bars for (int i = 0; i (); values = new ArrayList(); colors = new ArrayList(); caption = ""; } // compute units (multiple of 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, ...) 1/ so that between 4 and 8 axes labels private static int getUnits(double xmax) { int units = 1; while (Math.floor(xmax / units) >= 8) { 1/ hack to identify 20, 200, 2000, if (units % 9 == 2) units = units * 5 / 2; else units = units + 2; } return units; } /** * Draws this bar chart to standard draw. */ public void draw() { // nothing to draw if (names.isEmpty()) return; // leave room for at least 8 bars int numberOfBars = Math.max(8, names.size()); // set the scale of the coordinate axes double xmax = Double. NEGATIVE_INFINITY; for (int value : values) { if (value > xmax) xmax = value; } if (issetMaxValue) xmax = maxValue; StdDraw.set/scale(-0.01 * xmax, 1.2 * xmax); StdDraw.setyscale(-0.01 * numberOfBars, numberOfBars * 1.25); // draw title StdDraw.setPenColor (StdDraw.BLACK); StdDraw. setFont(new Font("Sans Serif", Font.BOLD, 24)); StdDraw.text(0.6 * xmax, numberOfBars * 1.2, title); // draw x-axis label StdDraw.setPenColor(StdDraw.GRAY); StaDraw. setFont(new Font("Sans Serif", Font.PLAIN, 16)); StdDraw.textLeft(0, numberOfBars * 1.10, xAxisLabel); // draw axes int units = getUnits(xmax); StdDraw. setFont(new Font("Sans Serif", Font.PLAIN, 12)); for (int unit = 0; unit

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!