Java Program: 1 Adam 85 2 Benjamin 90 3 Brian 91 4 Casey 98 5 Cathryn 88 6 David
ID: 3771455 • Letter: J
Question
Java Program:
1 Adam 85
2 Benjamin 90
3 Brian 91
4 Casey 98
5 Cathryn 88
6 David 77
7 Emily 85
8 Franchesca 95
9 Gloria 84
10 Hue 79
11 Isabelle 87
12 Janet 82
13 Katie 96
14 Lucy 89
15 Magaret 83
16 Nancy 71
17 Patrick 89
18 Roy 79
19 Scott 96
20 Zack 76
The program should find total sum, average, maximun, minimum scores and have the records displayed within histogram drawing panel as a legend.
Avg : 86.25
Max : 98
Min : 71
The histogram may be classified into certain number of score ranges divided into 6 levels from the minimum to maximum score. You can use array for tally.
71 ~ 75 : / (1)
76 ~ 80 : //// (4)
81~ 85 : ///// (5)
86 ~ 90 : ///// (5)
91 ~ 95 : // (2)
96 ~ 100 : /// (3)
The program output will be:
The above character-based histogram can be converted to graphical histogram (bar chart) chart.
Here are two template charts: horizontal or vertical histograms.
Please create a histogram generating tool which reads a file ("midterm.txt") containing students midterm scores associated with their id numbers and names. You are supposed to ask the score file fist from standard console input and try to read the names and scores from the file. The data are to be saved in the names and scores array. File "midterm.txt" includes the following:
Explanation / Answer
package assignment;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class HistogramStudentFileData {
public static void main(String[] args) {
System.out.println("Enter file name: ");
Scanner input = new Scanner(System.in);
String fileName = input.nextLine();
if(fileName != null && !"".equals(fileName.trim())) {
try {
ArrayList<String> names = new ArrayList<String>();
ArrayList<Integer> scores = new ArrayList<Integer>();
ArrayList<String> ids = new ArrayList<String>();
Scanner sc = new Scanner(new File(fileName));
String str = null;
String tokens[] = null;
while (sc.hasNext()) {
str = sc.nextLine();
tokens = str.split("\s+");
if(tokens != null && tokens.length == 3) {
ids.add(tokens[0].trim());
names.add(tokens[1].trim());
//for(String tk: tokens)
//System.out.println(tk);
scores.add(Integer.parseInt(tokens[2].trim()));
}
}
//calculate max, avg, min, histogram
/* historgram
- Index= 0)71 ~ 75 : / (1)
1) 76 ~ 80 : //// (4)
2) 81~ 85 : ///// (5)
3) 86 ~ 90 : ///// (5)
4) 91 ~ 95 : // (2)
5) 96 ~ 100 : /// (3)
*/
int historgram[] = new int[]{0,0,0,0,0,0};
int max = 0;
int min = 9999999;
float avg = 0;
int total = 0;
if(scores != null && scores.size() > 0) {
for(int marks: scores) {
if(marks < min)
min = marks;
if(marks > max)
max = marks;
total += marks;
if(marks >= 71 && marks <= 75)
historgram[0] += 1;
if(marks >= 76 && marks <= 80)
historgram[1] += 1;
if(marks >= 81 && marks <= 85)
historgram[2] += 1;
if(marks >= 86 && marks <= 90)
historgram[3] += 1;
if(marks >= 91 && marks <= 95)
historgram[4] += 1;
if(marks >= 96 && marks <= 100)
historgram[5] += 1;
}
for(int i= 0; i < historgram.length; i++) {
if(i == 0)
System.out.print("71 - 75 : ");
if(i == 1)
System.out.print("76 - 80 : ");
if(i == 2)
System.out.print("81 - 85 : ");
if(i == 3)
System.out.print("86 - 90 : ");
if(i == 4)
System.out.print("91 - 95: ");
if(i == 5)
System.out.print("96 - 100 : ");
for(int j= 0; j <historgram[i]; j++)
System.out.print("*");
System.out.println(" ("+historgram[i]+")");
}
avg = total/ scores.size();
System.out.println(" Avg : "+avg);
System.out.println(" MAx : "+max);
System.out.println(" Min : "+min);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
--output---
Enter file name:
D:/ravi/Cheg/scores.txt
71 - 75 : * (1)
76 - 80 : **** (4)
81 - 85 : ***** (5)
86 - 90 : ***** (5)
91 - 95: ** (2)
96 - 100 : *** (3)
Avg : 86.0
MAx : 98
Min : 71
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.