Question
Help with Java Histogram program. Any help is appreciated. I am trying to figure out the one missing piece to this program. I have everything
Help with Java Histogram program. Any help is appreciated.
I am trying to figure out the one missing piece to this program. I have everything but this:
your project should display the histogram using symbols for the frequency counts. (This is not shown in the sample projects.) For example, if the frequency count is 5 for a given category, display 5 asterisks: *****. If applied to the favorite color sample project, the histogram output might look like this:
Blue: 1 * Green: 2 ** Red: 4 **** Yellow: 0 Other: 2 **
Here is my code for the Display:
import java.util.ArrayList;
public class FavoriteFoodDisplay { private ArrayList
/** * Constructor for objects of class FavoriteDisplay */ public FavoriteFoodDisplay() { FavoriteFoodData data = new FavoriteFoodData(); foodData = data.getData(); analyzeData(); }
private void analyzeData() { foodCounts = new int[5]; for (String food : foodData) { if (food.equals("sushi")) { foodCounts [0]++; } else if (food.equals("pizza")) { foodCounts[1]++; } else if (food.equals("cotton candy")) { foodCounts[2]++; } else if (food.equals("steak and potato")) { foodCounts[3]++; } else { foodCounts[4]++; } } }
/** * Display the analyzed data as a histogram */ public void displayData() { System.out.println("How many times did you eat your favorite food?"); System.out.println("Sushi: " + foodCounts[0]); System.out.println("Pizza: " + foodCounts[1]); System.out.println("Cotton Candy: " + foodCounts[2]); System.out.println("Steak and Potato: " + foodCounts[3]); System.out.println("Another Favorite Food: " + foodCounts[4]); } }
And my code for data:
import java.util.ArrayList
public class FavoriteFoodData { private ArrayList
public FavoriteFoodData() { favoriteFoodData = new ArrayList<>(); populateData(); }
/** * Add types of favorite foods */ private void populateData() { favoriteFoodData.add("sushi"); favoriteFoodData.add("pizza"); favoriteFoodData.add("steak and potato"); favoriteFoodData.add("cotton candy"); favoriteFoodData.add("pizza"); favoriteFoodData.add("steak and potato"); favoriteFoodData.add("hamburger"); favoriteFoodData.add("pizza"); favoriteFoodData.add("sushi"); favoriteFoodData.add("pizza"); favoriteFoodData.add("sushi"); favoriteFoodData.add("salad");
}
/** * returns favorite food */ public ArrayList
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started