Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 foodData; private int[] foodCounts;

/** * 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 favoriteFoodData;

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 getData() { return favoriteFoodData; } }

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

Step: 3

blur-text-image

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

Professional SQL Server 2000 Database Design

Authors: Louis Davidson

1st Edition

1861004761, 978-1861004765

More Books

Students also viewed these Databases questions

Question

=+ What topics are contained in the contracts?

Answered: 1 week ago