Java experiment 5 Student Marks
Code:
import [Link].*;
import [Link].*;
import [Link].*;
public class StudentResult extends JFrame implements ActionListener {
JLabel nameLabel;
JTextField nameField;
JLabel[] subjectLables = new JLabel[5];
JTextField[] markFields = new JTextField[5];
JButton submitButton;
public StudentResult() {
setTitle("Student Marks Entry");
setSize(400, 400);
setLayout(new GridLayout(8, 2, 10, 10));
setDefaultCloseOperation(EXIT_ON_CLOSE);
nameLabel = new JLabel("Student name: ");
nameField = new JTextField();
add(nameLabel);
add(nameField);
String[] subjects = { "Maths", "Science", "English", "History", "Computer"
};
for(int i = 0; i < 5; i++) {
subjectLables[i] = new JLabel(subjects[i] + "Marks: ");
markFields[i] = new JTextField();
add(subjectLables[i]);
add(markFields[i]);
}
submitButton = new JButton("Show Result");
[Link](this);
add(new JLabel());
add(submitButton);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
try {
String name = [Link]().trim();
if([Link]()) {
[Link](this, "Please Enter Student Name");
return;
}
int[] marks = new int[5];
int total = 0;
boolean fail = false;
for(int i = 0; i < 5; i++) {
marks[i] = [Link](markFields[i].getText().trim());
if(marks[i] < 0 || marks[i] > 100) {
[Link](this, "Marks Should Be Between 0
and 100");
return;
}
if(marks[i] < 35) {
fail = true;
}
total += marks[i];
}
double average = total / 5.0;
String result = fail ? "Fail" : "Pass";
JFrame resultFrame = new JFrame("Student Result");
[Link](300, 200);
[Link](new GridLayout(5, 1, 10, 10));
[Link](new JLabel("Name: " + name));
[Link](new JLabel("Total Marks: " + total));
[Link](new JLabel([Link]("Average: %.2f", average)));
[Link](new JLabel("Result: " + result));
[Link](true);
} catch(NumberFormatException ex) {
[Link](this, "Please Enter Valid Numeric Marks");
}
}
public static void main(String[] args) {
new StudentResult();
}
}
Commands to run the program:
C:\Users\ADMIN>cd Desktop
C:\Users\ADMIN\Desktop\aditya_java_exp>javac [Link]
C:\Users\ADMIN\Desktop\aditya_java_exp>javac --release 8 [Link]
C:\Users\ADMIN\Desktop\aditya_java_exp>java StudentResult