Write a program that reads all the information. If a student has more than 100 p
ID: 3555288 • Letter: W
Question
Write a program that reads all the information.
If a student has more than 100 points in a homework then set the value to 100 (max possible is only 100).
If the field is blank then set it to 0.
For each student calculate the average homework grade (sum off all / 10).
Based on the average grade assign the letter grade (A,B,C,D,E,F)
Design a user interface which has a drop down box with A,B,C,D,E,F
Based on what letter grade I choose I want it to display the list of students who made that grade.
The below came out of the .csv file
Name Homework 1 Homework 2 Homework - 3 Homework 4 Homework 6 Homework 7 Homework 8 Bonus Homework Homework 9 Homework 10 Student 1 92 92 81 92 91 152 61 50 70 100 Student 2 85 94 86 90 79 182 100 50 85 82 Student 3 96 90 93 92 85 182 91 50 97 93 Student 4 85 96 94 92 82 76 27 50 91 85 Student 5 96 96 87 98 94 91 94 50 97 191 Student 6 86 92 256 86 79 50 64 85 Student 7 90 98 91 96 179 94 79 91 94 Student 8 90 96 90 98 87 185 50 88 85 Student 9 90 96 90 92 82 88 76 150 91 94 Student 10 96 96 94 94 94 94 194 150 91 97 Student 11 77 96 90 92 88 88 91 94 Student 12 96 94 90 100 94 94 179 50 91 69 Student 13 91 94 90 96 182 91 90 150 100 100 Student 14 82 76 79 88 79 67 72 50 75 90 Student 15 81 84 84 86 82 82 79 50 82 67 Student 16 87 90 92 94 88 88 267 91 82 Student 17 66 79 54 100 79 70 50 81 82 Student 18 92 92 94 90 94 88 50 94 97 Student 19 76 96 94 96 88 91 97 94 94 Student 20 74 86 76 98 76 167 73 50 52 52 Student 21 88 77 322 94 85 85 85 97 91 Student 22 80 90 96 294 50 91 97 Student 23 92 78 50 73 55 100 76 85 Student 24 100 96 96 98 94 97 97 50 100 100 Student 25 96 92 182 94 94 85 85 50 97 97 Student 26 80 84 76 98 76 164 85 50 82 97 Student 27 100 86 50 90 97 73 79 73 91 Student 28 46 100 100 Student 29 90 43 80 96 82 70 185 150 90 88 Student 30 83 90 90 96 70 88 167 50 79 85 Student 31 92 94 94 96 194 50 97 94 Student 32 92 36.5 76 86 100 50 100 79Explanation / Answer
Answer:
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Student {
List studentsList = new ArrayList();
public static void main(String[] args) {
Student obj = new Student();
obj.init();
obj.invokeUI();
}
/**
* this method will initiate the process of reading the csv file
*/
public void init() {
//give the path of the csv file
String csvFile = "C:\WorkSpace_R16\Testing\src\test.csv";
BufferedReader br = null;
String line = "";
String splitBySemiColon = ";";
String[] studentsAllSubjectMarks = null;
ArrayList<Object> allStudentsMarks = new ArrayList<Object>();
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
// use semiColon as separator
studentsAllSubjectMarks = line.split(splitBySemiColon);
System.out.println(studentsAllSubjectMarks.toString());
}
List listOfmarksWithComma = getMarksOfEachStudent(studentsAllSubjectMarks);
assignMarksToStudentsAndAvg(listOfmarksWithComma);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("Done");
}
/**
* this method will get add the marks into a list
* @param inputString
* @return
*/
public List getMarksOfEachStudent(String[] inputString){
List listOfMarks = new ArrayList();
String marksOfEachStudentWithComma = null;
for(int i=0; i<inputString.length;i++){
marksOfEachStudentWithComma = inputString[i];
listOfMarks.add(marksOfEachStudentWithComma);
}
return listOfMarks;
}
/**
* This method will assign marks to students
* @param listOfmarksWithComma
*/
public void assignMarksToStudentsAndAvg(List<String> listOfmarksWithComma){
for(int i=0;i<listOfmarksWithComma.size();i++){
StudentBean studentBean = new StudentBean();
setListOfMarksAndAvg(listOfmarksWithComma.get(i), i);
}
}
/**
* this method will assign marks calculate grade and
* avg for student and add student to student list
* @param Marks
* @param studentNumber
*/
public void setListOfMarksAndAvg(String Marks,int studentNumber) {
String[] parts = Marks.split(",");
List listOfMarksWithOutComma = new ArrayList();
double total = 0;
double avg = 0;
int i = 0;
for ( i = 0; i < parts.length; i++) {
if (parts[i].equalsIgnoreCase("")) {
listOfMarksWithOutComma.add(0);
total += 0;
} else if (null != parts[i] && (Double.parseDouble(parts[i])) > 100) {
listOfMarksWithOutComma.add(100);
total += 100;
} else {
listOfMarksWithOutComma.add(parts[i]);
total += Double.parseDouble(parts[i]);
}
}
if (total != 0) {
avg = total / 10;
}
System.out.println("List of marks For Student "
+ listOfMarksWithOutComma.toString());
StudentBean studentBean = new StudentBean();
System.out.println("Student"+studentNumber);
studentBean.setStudentName("Student"+studentNumber);
studentBean.setListOfMarks(listOfMarksWithOutComma);
System.out.println("avg of student " + avg);
studentBean.setAvg(avg);
studentBean.setGrade(getGrade(avg));
System.out.println(studentBean.getGrade());
studentsList.add(studentBean);
}
/**
* This methos will return grade depending upon the marks
* @param avg
* @return
*/
public String getGrade(double avg){
String grade = null;
if(avg>90){
grade = "A";
}
else if(avg>80){
grade = "B";
}
else if(avg>70){
grade = "C";
}
else if(avg>60){
grade = "D";
}
else {
grade = "E";
}
return grade;
}
/**
* This method will invoke the UI.
*/
public void invokeUI() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
JPanel panel = new JPanel();
panel.add(new JLabel("Please make a selection:"));
DefaultComboBoxModel model = new DefaultComboBoxModel();
model.addElement("A");
model.addElement("B");
model.addElement("C");
model.addElement("D");
model.addElement("E");
JComboBox comboBox = new JComboBox(model);
panel.add(comboBox);
int result = JOptionPane.showConfirmDialog(null, panel, "Grade", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
switch (result) {
case JOptionPane.OK_OPTION:
System.out.println("You selected " + comboBox.getSelectedItem());
showListOfStudentWithSelectedGrade(comboBox);
break;
}
}
/**
* This method will display the list of students with the selected grade
* @param comboBox
*/
public void showListOfStudentWithSelectedGrade(JComboBox comboBox){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
JPanel panel = new JPanel();
panel.add(new JLabel("List of Students with Grade: "+ comboBox.getSelectedItem()));
for(int itr=0;itr<studentsList.size();itr++){
if(((StudentBean)studentsList.get(itr)).getGrade().equalsIgnoreCase((String)comboBox.getSelectedItem())){
JLabel label = new JLabel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
label.setText("Name of Student "+ ((StudentBean)studentsList.get(itr)).getStudentName());
panel.add(label);
}
}
JOptionPane.showConfirmDialog(null, panel, "Grade", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
invokeUI();
}
}
//Part 2
import java.util.ArrayList;
import java.util.List;
public class StudentBean {
List listOfMarks = null;
String grade = null;
String studentName = null;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
double avg = 0;
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public List getListOfMarks() {
return listOfMarks;
}
public void setListOfMarks(List listOfMarks) {
this.listOfMarks = listOfMarks;
}
public double getAvg() {
return avg;
}
public void setAvg(double avg) {
this.avg = avg;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.