Share on Google+Share on Google+

apiz
How to convert this Java code into GUI?
2 Answer(s)      5 years and 5 months ago
Posted in : Java Beginners
 import java.util.Scanner;

public class StudentMarks {

    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) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter number of students: ");
        int num = input.nextInt();
        StudentMarks data[] = new StudentMarks[num];
        for (int i = 0; i < data.length; i++) {
            System.out.println("Enter marks");
            double marks = input.nextDouble();
            data[i] = new StudentMarks();
            data[i].setTotalMarks(marks);
            if (marks < 40) {
                data[i].setGrade("E");
            }
            if (marks >= 40 && marks <= 44) {
                data[i].setGrade("D");
            }
            if (marks >= 45 && marks <= 49) {
                data[i].setGrade("D+");
            }
            if (marks >= 50 && marks <= 54) {
                data[i].setGrade("C-");
            }
            if (marks >= 55 && marks <= 59) {
                data[i].setGrade("C");
            }
            if (marks >= 60 && marks <= 64) {
                data[i].setGrade("C+");
            }
            if (marks >= 65 && marks <= 69) {
                data[i].setGrade("B-");
            }
            if (marks >= 70 && marks <= 74) {
                data[i].setGrade("B");
            }
            if (marks >= 75 && marks <= 79) {
                data[i].setGrade("B+");
            }
            if (marks >= 80 && marks <= 84) {
                data[i].setGrade("A");
            }
            if (marks >= 85 && marks <= 100)
                data[i].setGrade("A+");
        }

        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        int count4 = 0;
        int count5 = 0;
        int count6 = 0;
        int count7 = 0;
        int count8 = 0;
        int count9 = 0;
        int count10= 0;
        int count11= 0;
        for (int i = 0; i < num; i++) {
            StudentMarks show = data[i];
            String g = show.getGrade();
            if (g.equals("A+")) {
                count1++;
            }
            if (g.equals("A")) {
                count2++;
            }
            if (g.equals("B+")) {
                count3++;
            }
            if (g.equals("B")) {
                count4++;
            }
            if (g.equals("B-")){
                count5++;
            }
            if (g.equals("C+")){
                count6++;
            }
            if (g.equals("C")){
                count7++;
            }
            if (g.equals("C-")){
                count8++;
            }
            if (g.equals("D+")){
                count9++;
            }
            if (g.equals("D")){
                count10++;
            }
            if (g.equals("E")){
                count11++;
            }
        }
        System.out.println("Number of student getting A+ and A grade: "
                + (count1 + count2));
        System.out.println("Number of student getting B+ and B grade: "
                + (count3 + count4));
        System.out.println("Number of student getting A and B+ grade: "
                + (count2 + count3));
        System.out.println("Number of student getting B and B- grade: "
                + (count4 + count5));
        System.out.println("Number of student getting B- and C+ grade: "
                + (count5 + count6));
        System.out.println("Number of student getting C+ and C grade: "
                + (count6 + count7));
        System.out.println("Number of student getting C and C- grade: "
                + (count7 + count8));
        System.out.println("Number of student getting C- and D grade: "
                + (count8 + count9));
        System.out.println("Number of student getting D and D- grade: "
                + (count9 + count10));
        System.out.println("Number of student getting D- and E grade: "
                + (count10 + count11));

    }
}

Ads

View Answers

November 9, 2011 at 3:08 PM


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+");
        }


November 9, 2011 at 3:10 PM


continue..

            int count1 = 0;
            int count2 = 0;
            int count3 = 0;
            int count4 = 0;
            int count5 = 0;
            int count6 = 0;
            int count7 = 0;
            int count8 = 0;
            int count9 = 0;
            int count10 = 0;
            int count11 = 0;

            for (int i = 0; i < num; i++) {
                StudentGrade show = data[i];
                String g = show.getGrade();
                if (g.equals("A+")) {
                    count1++;
                }
                if (g.equals("A")) {
                    count2++;
                }
                if (g.equals("B+")) {
                    count3++;
                }
                if (g.equals("B")) {
                    count4++;
                }
                if (g.equals("B-")){
                    count5++;
                }
                if (g.equals("C+")){
                    count6++;
                }
                if (g.equals("C")){
                    count7++;
                }
                if (g.equals("C-")){
                    count8++;
                }
                if (g.equals("D+")){
                    count9++;
                }
                if (g.equals("D")){
                    count10++;
                }
                if (g.equals("E")){
                    count11++;
                }

            }
            JOptionPane.showMessageDialog(null,"Number of student getting A+ and A grade: "
                    + (count1 + count2));
            JOptionPane.showMessageDialog(null,"Number of student getting B+ and B grade: "
                    + (count3 + count4));
            JOptionPane.showMessageDialog(null,"Number of student getting A and B+ grade: "
                    + (count2 + count3));
            JOptionPane.showMessageDialog(null,"Number of student getting B and B- grade: "
                    + (count4 + count5));
            JOptionPane.showMessageDialog(null,"Number of student getting B- and C+ grade: "
                    + (count5 + count6));
            JOptionPane.showMessageDialog(null,"Number of student getting C+ and C grade: "
                    + (count6 + count7));
            JOptionPane.showMessageDialog(null,"Number of student getting C and C- grade: "
                    + (count7 + count8));
            JOptionPane.showMessageDialog(null,"Number of student getting C- and D grade: "
                    + (count8 + count9));
            JOptionPane.showMessageDialog(null,"Number of student getting D and D- grade: "
                    + (count9 + count10));
            JOptionPane.showMessageDialog(null,"Number of student getting D- and E grade: "
                    + (count10 + count11));

                }
            });
        }
    }









Related Tutorials/Questions & Answers:
Advertisements
Tutorials   
Java Spring Hibernate Struts Training What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized how to convert war file into .exe file using java code

Ads

 
Advertisement null

Ads