JAVA PROGRAM The voter participation data we will work with is stored in a Word
ID: 3874658 • Letter: J
Question
JAVA PROGRAM
The voter participation data we will work with is stored in a Word document Presidential Elections.docx or you can find on http://www.sos.wa.gov/elections/voter_participation.aspx.
It’s expected that you hard code the bar data in the main() method, i.e., I expect to see the corresponding values typed into your program and passed to the functions as arguments. Outside of this, you should have no “magic numbers”; use values from the website instead.
Constant
Description
Sample
Histogram title
Text that will appear centered (over a 80-character space) before the histogram is displayed. One blank line follows it.
"Presidential Elections"
Histogram characters
The character to be repeated to create the data bar
T, R, V
Scale
Use a scale for the chart to create bars in the appropriate size
1000000
Java Program using:
Constants (at class level), using the recommended casing and word separator
Variables (but not class-level ones)
Assignment and calculations
Definite loops
Console output
But not:
Indefinite loops (no while)
Selection control structures (no if) unless you’re doing something way “above and beyond”
Arrays, lists, or other data structures or objects we haven't covered
Libraries or methods we haven't covered
Creating the following functions
• main() should consist of a series of function calls and nothing more
• printHeader() should accept one parameter – the name of the chart
• createBar() will be an overloaded method
the firsts variant should accept four parameters:
- an integer specifying the bar title (the year)
- an integer indicating the bar value
- the character to be repeated to create the data bar
- the scale
the second variant accepts three parameters, the same as the first but excluding the year.
• createAxis() should accept one parameter – a step
• emptyRow() should accept no parameters
None of the functions should return any value
You may wish to use the String length method; this one is allowed.
Example:
String userName = "Any Name";
int userNameLen = userName.length();
The output of the program is a character-based histogram showing some bars of data taking from the website. Use at least 5 different years. The header should be in the center of the first line. Don’t hardcode numbers, e.g., for the axis values. In other words, don’t have a print statement that prints a string literal “5000 10000 15000 …”; write a proper loop instead and use steps and scales. The same goes for tick marks.
Constant
Description
Sample
Histogram title
Text that will appear centered (over a 80-character space) before the histogram is displayed. One blank line follows it.
"Presidential Elections"
Histogram characters
The character to be repeated to create the data bar
T, R, V
Scale
Use a scale for the chart to create bars in the appropriate size
1000000
Presidential Elections 2000 I TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT 2004 ITTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT 2008 I TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT 2012 ITTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT 2016 ITTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT 0 1000000 2000000 3000000 4000000 5000000 6000000Explanation / Answer
//the JAVA program**
//save it as HistogramCreater.java
//run it from cmd or terminal using
//javac HistogramCreater.java
//java HistogramCreater
import java.util.*;
public class HistogramCreater {
//the total width of histogram
final static int totalCharactersInOneLine = 80;
final static int scale = 1000000;
final static int steps = 6;
public static void main(String[] args){
//data from websites
//Sorry but it was not specified in the questions what are the T R and V corresponds to
// im taking random values
String chartName = "Presidential Elections";
char[] histChar = {'T','R','V'};
YearVoterPair[] data = new YearVoterPair[5];
putData(data);
printHeader(chartName);
for(int i=0 ; i<data.length ; i++){
createBar(data[i].year,data[i].tValue,histChar[0],scale);
createBar(data[i].rValue,histChar[1],scale);
createBar(data[i].vValue,histChar[2],scale);
if(i != data.length-1){
//will not print empty row after the last row
emptyRow();
}
}
createAxis(steps);
}
private static void printHeader(String chartName){
int chartNameLength = chartName.length();
//assuming chartNameLength is not more than totalCharacterInOneLine
int leftoverCharacterSpace = totalCharactersInOneLine - chartNameLength;
//putting the name in the center
for(int i=0 ; i<leftoverCharacterSpace/2 ; i++){
System.out.print(" ");
}
System.out.println(chartName);
//additional one line as specified in question
System.out.println();
}
private static void createBar(int year, int value, char histChar, int scale){
int numberOfChars = ((totalCharactersInOneLine-6)*value)/(scale*steps);
System.out.print("" + year +" |");
for(int i=0 ; i<numberOfChars ; i++){
System.out.print("" + histChar);
}
System.out.println();
}
private static void createBar(int value, char histChar, int scale){
int numberOfChars = ((totalCharactersInOneLine-6)*value)/(scale*steps);
System.out.print(" |");
for(int i=0 ; i<numberOfChars ; i++){
System.out.print("" + histChar);
}
System.out.println();
}
private static void createAxis(int steps){
// -6 because this string "2016 |" takes 6 characters
int charsInOneUnitScale = (totalCharactersInOneLine)/steps;
System.out.print(" |");
for(int i=0 ; i < steps ; i++){
for(int j=0 ; j<charsInOneUnitScale-1 ; j++){
System.out.print("-");
}
System.out.print("+");
}
System.out.println();
//getting length of scale
int length = String.valueOf(scale).length();
int spacesBetween = charsInOneUnitScale-length;
System.out.print(" 0");
for(int i=0 ; i<charsInOneUnitScale-length/2 ; i++){
System.out.print(" ");
}
System.out.print("" + scale);
for(int i = 2 ; i<=steps ; i++){
for(int j=0 ; j<spacesBetween ; j++){
System.out.print(" ");
}
System.out.print("" + scale*i);
}
}
private static void emptyRow(){
System.out.println(" |");
}
private static void putData(YearVoterPair[] data){
data[0] = new YearVoterPair(2000,4368000,3335714,2517028);
data[1] = new YearVoterPair(2004,4646000,3508208,2884783);
data[2] = new YearVoterPair(2008,5010844,3630118,3071587);
data[3] = new YearVoterPair(2012,5221125,3904359,3172930);
data[4] = new YearVoterPair(2016,5557921,4270270,3363440);
}
//class for storing year and value data
static class YearVoterPair{
int year;
int tValue;
int rValue;
int vValue;
//constructer
public YearVoterPair(int year, int tValue, int rValue, int vValue){
this.year = year;
this.tValue = tValue;
this.rValue = rValue;
this.vValue = vValue;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.