import java.util.Scanner; public class StudentGrade { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks = totalMarks; //"this" keyword refer to current instance of the method on which it is use } public double getTotalMarks() { return totalMarks; } public void setGrade(String grade) { this.grade = grade;//"this" keyword refer to current instance of the method on which it is use } public String getGrade() { return grade; } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter number of students in class: "); int num = input.nextInt(); StudentGrade data[] = new StudentGrade[num]; for (int a = 0; a < data.length; a++) { System.out.print("Enter student programming marks: "); double marks = input.nextDouble(); data[a] = new StudentGrade(); data[a].setTotalMarks(marks); if (marks < 40) { data[a].setGrade("E"); } if (marks >= 40 && marks <= 44) { data[a].setGrade("D"); } if (marks >= 45 && marks <= 49) { data[a].setGrade("D+"); } if (marks >= 50 && marks <= 54) { data[a].setGrade("C-"); } if (marks >= 55 && marks <= 59) { data[a].setGrade("C"); } if (marks >= 60 && marks <= 64) { data[a].setGrade("C+"); } if (marks >= 65 && marks <= 69) { data[a].setGrade("B-"); } if (marks >= 70 && marks <= 74) { data[a].setGrade("B"); } if (marks >= 75 && marks <= 79) { data[a].setGrade("B+"); } if (marks >= 80 && marks <= 84) { data[a].setGrade("A"); } if (marks >= 85 && marks <= 100) data[a].setGrade("A+"); } int grade1 = 0; int grade2 = 0; int grade3 = 0; int grade4 = 0; for (int a = 0; a < num; a++) { StudentGrade show = data[a]; String i = show.getGrade(); if (i.equals("A+")) { grade1++; } if (i.equals("A")) { grade2++; } if (i.equals("D")) { grade3++; } if (i.equals("E")) { grade4++; } } System.out.println("Student that getting A and A+ grade: " + (grade1 + grade2)); System.out.println("Student that getting D and E grade: " + (grade3 + grade4)); } }
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class StudentGrade { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks = totalMarks; } public double getTotalMarks() { return totalMarks; } public void setGrade(String grade) { this.grade = grade; } public String getGrade() { return grade; } public static void main(String[] args) { JLabel lab=new JLabel("Enter number of students in class: "); final JTextField text=new JTextField(20); JButton b=new JButton("Find"); lab.setBounds(10,10,150,20); text.setBounds(180,10,100,20); b.setBounds(180,40,100,20); JFrame f=new JFrame(); f.setLayout(null); f.add(lab); f.add(text); f.add(b); f.setSize(300,100); f.setVisible(true); b.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ int num = Integer.parseInt(text.getText()); StudentGrade data[] = new StudentGrade[num]; for (int a = 0; a < data.length; a++) { String input=JOptionPane.showInputDialog(null,"Enter student programming marks: "); double marks = Double.parseDouble(input); data[a] = new StudentGrade(); data[a].setTotalMarks(marks); if (marks < 40){ data[a].setGrade("E"); } if (marks >= 40 && marks <= 44) { data[a].setGrade("D"); } if (marks >= 45 && marks <= 49) { data[a].setGrade("D+"); } if (marks >= 50 && marks <= 54) { data[a].setGrade("C-"); } if (marks >= 55 && marks <= 59) { data[a].setGrade("C"); } if (marks >= 60 && marks <= 64) { data[a].setGrade("C+"); } if (marks >= 65 && marks <= 69) { data[a].setGrade("B-"); } if (marks >= 70 && marks <= 74) { data[a].setGrade("B"); } if (marks >= 75 && marks <= 79) { data[a].setGrade("B+"); } if (marks >= 80 && marks <= 84) { data[a].setGrade("A"); } if (marks >= 85 && marks <= 100) data[a].setGrade("A+"); } int grade1 = 0; int grade2 = 0; int grade3 = 0; int grade4 = 0; for (int a = 0; a < num; a++) { StudentGrade show = data[a]; String i = show.getGrade(); if (i.equals("A+")) { grade1++; } if (i.equals("A")) { grade2++; } if (i.equals("D")) { grade3++; } if (i.equals("E")) { grade4++; } } JOptionPane.showMessageDialog(null,"Student that getting A and A+ grade: " + (grade1 + grade2)); JOptionPane.showMessageDialog(null,"Student that getting D and E grade: "+ (grade3 + grade4)); } }); } }
Ads