JAVA program. I\'ve highlighted the instructions in the histogram class public c
ID: 3911969 • Letter: J
Question
JAVA program. I've highlighted the instructions in the histogram class
public class Main {
public static void main(String[] args) {
Histogram grade = new Histogram(5, 7, 12, 3, 2);
System.out.println("Histogram by bar: -----------------");
System.out.printf( "%s: %d", grade.barFor(Histogram.Bar.A), grade.barAHeight );
System.out.printf( "%n%s: %d", grade.barFor(Histogram.Bar.B), grade.barBHeight );
System.out.printf( "%n%s: %d", grade.barFor(Histogram.Bar.C), grade.barCHeight );
System.out.printf( "%n%s: %d", grade.barFor(Histogram.Bar.D), grade.barDHeight );
System.out.printf( "%n%s: %d", grade.barFor(Histogram.Bar.F), grade.barFHeight );
System.out.println(" Histogram with labels: ----------------------");
System.out.println( grade.barHistogram(true) );
System.out.println(" Histogram without labels: -------------------------");
System.out.println( grade.barHistogram(false) );
public class Histogram {
/* Enum representing the available histrogram bars. */
enum Bar { A, B, C, D, F }
char barASymbol = 'a';
char barBSymbol = 'b';
char barCSymbol = 'c';
char barDSymbol = 'd';
char barFSymbol = 'f';
int barAHeight;
int barBHeight;
int barCHeight;
int barDHeight;
int barFHeight;
public Histogram() {
/* TODO (1):
* Assign the values 2,3,5,2,1 directly, to the bar heights a-f respectively.
*/
int barAHeight = 2;
int barBHeight = 3;
int barCHeight = 5;
int barDHeight = 2;
int barFHeight = 1;
}// end Histogram()
public Histogram(int barAHeight,
int barBHeight,
int barCHeight,
int barDHeight,
int barFHeight) {
/* TODO (2):
* Initialize bar heights directly, with parameter values.
*/
this.barAHeight = barAheight;
this.barBHeight = barBheight;
this.barCHeight = barCheight;
this.barDHeight = barDheight;
this.barFHeight = barFheight;
}// end Histogram()
public String barHistogram(boolean withLabel) {
/* TODO (3):
* Return a string representing the entire histogram.
* If withLabel is true, then add the bar height to the end
* of each bar, e.g. aaa: 3
*/
}// end barHistogram()
public String barFor(Bar bar) {
/* TODO (4):
* Return a string representing the bar height for the specified bar.
* Call the method createBar() to create the string bar.
*/
}// end barFor()
public String createBar(char barSymbol, int barHeight) {
/* TODO (5):
* Return a string representing the bar height for the specified bar.
*/
}// end createBar()
}// end Histogram
Explanation / Answer
Given below is the completed code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
public class Histogram {
/* Enum representing the available histrogram bars. */
enum Bar { A, B, C, D, F }
char barASymbol = 'a';
char barBSymbol = 'b';
char barCSymbol = 'c';
char barDSymbol = 'd';
char barFSymbol = 'f';
int barAHeight;
int barBHeight;
int barCHeight;
int barDHeight;
int barFHeight;
public Histogram() {
/* TODO (1):
* Assign the values 2,3,5,2,1 directly, to the bar heights a-f respectively.
*/
barAHeight = 2;
barBHeight = 3;
barCHeight = 5;
barDHeight = 2;
barFHeight = 1;
}// end Histogram()
public Histogram(int barAHeight,
int barBHeight,
int barCHeight,
int barDHeight,
int barFHeight) {
/* TODO (2):
* Initialize bar heights directly, with parameter values.
*/
this.barAHeight = barAHeight;
this.barBHeight = barBHeight;
this.barCHeight = barCHeight;
this.barDHeight = barDHeight;
this.barFHeight = barFHeight;
}// end Histogram()
public String barHistogram(boolean withLabel) {
/* TODO (3):
* Return a string representing the entire histogram.
* If withLabel is true, then add the bar height to the end
* of each bar, e.g. aaa: 3
*/
String s = "";
s += barFor(Bar.A);
if(withLabel)
s += ": " + barAHeight;
s += " ";
s += barFor(Bar.B);
if(withLabel)
s += ": " + barBHeight;
s += " ";
s += barFor(Bar.C);
if(withLabel)
s += ": " + barCHeight;
s += " ";
s += barFor(Bar.D);
if(withLabel)
s += ": " + barDHeight;
s += " ";
s += barFor(Bar.F);
if(withLabel)
s += ": " + barFHeight;
s += " ";
return s;
}// end barHistogram()
public String barFor(Bar bar) {
/* TODO (4):
* Return a string representing the bar height for the specified bar.
* Call the method createBar() to create the string bar.
*/
switch(bar){
case A:
return createBar(barASymbol, barAHeight);
case B:
return createBar(barBSymbol, barBHeight);
case C:
return createBar(barCSymbol, barCHeight);
case D:
return createBar(barDSymbol, barDHeight);
case F:
return createBar(barFSymbol, barFHeight);
default:
return "";
}
}// end barFor()
public String createBar(char barSymbol, int barHeight) {
/* TODO (5):
* Return a string representing the bar height for the specified bar.
*/
String s = "";
for(int i = 1; i <= barHeight; i++)
s += barSymbol;
return s;
}// end createBar()
}// end Histogram
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.