Design and Implement GPA Calculator

Ads
 

Design and Implement GPA Calculator

Once the user clicks on the calculate button the program should display on a dialog box first the info about the student like his name. ID, session and student type. Second the program should display the GPA.

Once the user clicks on the clear button all the fields in the form should be cleared. Once the user clicks on the cancel button the program should stop.

Assume that all the courses have the same credits number which is: 3

The points for the letter grades are as follows:

A 4 B+ 3.5 B 3 c+ 2.5 C 2 D+ 1.5 D 1 F 0

the user should be able to enter the letter grades in lower or upper cases like A or a.

View Answers

June 14, 2013 at 10:38 AM

Try the following code snippet, may this will be helpful for you.

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class GPACalculator {

    JFrame frame;   
    JInternalFrame jInternalFrame;
    JLabel headingLabel, stuNameLabel, stuIdLabel, sessionLabel, 
    stuType, courseLabel1, courseLabel2, courseLabel3, courseLabel4, 
    courseLabel5, courseLabel6;
    JCheckBox eveningCheckbox, morningCheckbox;
    JRadioButton regularRdbtn, nonDegreeRdbtn;
    JTextField course1, course2, course3, course4, course5, course6,
    stuNameTf, stuIdTf;
    JButton calculate, clear, cancel;
    JDesktopPane desktop = new JDesktopPane();

    public void createUI()
    {
        frame = new JFrame("GPA Calculator");
        //frame.setLayout(new BorderLayout());
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        headingLabel = new JLabel("Welcome To GPA Calculator");
        headingLabel.setBounds(10, 10, 200, 20);

        stuNameLabel = new JLabel("Student Name");
        stuNameLabel.setBounds(30, 30, 100, 20);

        stuNameTf = new JTextField();
        stuNameTf.setBounds(130, 30, 100, 20);

        stuIdLabel = new JLabel("ID");
        stuIdLabel.setBounds(270, 30, 20, 20);

        stuIdTf = new JTextField();
        stuIdTf.setBounds(290, 30, 100, 20);

        sessionLabel = new JLabel("Session");
        sessionLabel.setBounds(30, 60, 80, 20);

        eveningCheckbox = new JCheckBox("Evening");
        eveningCheckbox.setBounds(110, 60, 80, 20);

        morningCheckbox = new JCheckBox("Morning");
        morningCheckbox.setBounds(190, 60, 80, 20);

        stuType = new JLabel("Student Type");
        stuType.setBounds(30, 90, 80, 20);

        regularRdbtn = new JRadioButton("regular");
        regularRdbtn.setBounds(110, 90, 80, 20);

        nonDegreeRdbtn = new JRadioButton("non Degree");
        nonDegreeRdbtn.setBounds(190, 90, 100, 20);

        courseLabel1 = new JLabel("Course1");
        courseLabel1.setBounds(30, 120, 60, 20);

        course1 = new JTextField();
        course1.setBounds(90, 120, 100, 20);

        courseLabel2 = new JLabel("Course2");
        courseLabel2.setBounds(200, 120, 60, 20);

        course2 = new JTextField();
        course2.setBounds(260, 120, 100, 20);

        courseLabel3 = new JLabel("Course3");
        courseLabel3.setBounds(30, 150, 60, 20);

        course3 = new JTextField();
        course3.setBounds(90, 150, 100, 20);

        courseLabel4 = new JLabel("Course4");
        courseLabel4.setBounds(200, 150, 60, 20);

        course4 = new JTextField();
        course4.setBounds(260, 150, 100, 20);

        courseLabel5 = new JLabel("Course5");
        courseLabel5.setBounds(30, 180, 60, 20);

        course5 = new JTextField();
        course5.setBounds(90, 180, 100, 20);

        courseLabel6 = new JLabel("Course6");
        courseLabel6.setBounds(200, 180, 60, 20);

        course6 = new JTextField();
        course6.setBounds(260, 180, 100, 20);

Continue...


June 14, 2013 at 10:38 AM

calculate = new JButton("Calculate");
        calculate.setBounds(80, 230, 100, 20);
        calculate.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae)
            {
                displayStudentRecord();
            }
        });

        clear = new JButton("Clear");
        clear.setBounds(190, 230, 80, 20);
        clear.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae)
            {
                resetFields();
            }
        });

        cancel = new JButton("Cancel");
        cancel.setBounds(280, 230, 80, 20);
        cancel.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae)
            {
                closeFrame();
            }
        });     

        frame.add(headingLabel);
        frame.add(stuNameLabel);
        frame.add(stuNameTf);
        frame.add(stuIdLabel);
        frame.add(stuIdTf);
        frame.add(sessionLabel);
        frame.add(eveningCheckbox);
        frame.add(morningCheckbox);
        frame.add(stuType);
        frame.add(regularRdbtn);
        frame.add(nonDegreeRdbtn);
        frame.add(courseLabel1);
        frame.add(course1);
        frame.add(courseLabel2);
        frame.add(course2);
        frame.add(courseLabel3);
        frame.add(course3);
        frame.add(courseLabel4);
        frame.add(course4);
        frame.add(courseLabel5);
        frame.add(course5);
        frame.add(courseLabel6);
        frame.add(course6);
        frame.add(calculate);       
        frame.add(clear);
        frame.add(cancel);

        frame.pack();
        frame.setVisible(true);
        frame.setSize(500,400);
    }

June 14, 2013 at 10:39 AM

public JCheckBox checkboxName()
    {
        JCheckBox ckbox = null;
        if(eveningCheckbox.isSelected())
        {
            ckbox = eveningCheckbox;
        }
        else if(morningCheckbox.isSelected())
        {
            ckbox = morningCheckbox;
        }

        return ckbox;
    }

    public JRadioButton radioButtonName()
    {
        JRadioButton radioBtn = null;
        if(regularRdbtn.isSelected())
        {
            radioBtn = regularRdbtn;
        }
        else if(nonDegreeRdbtn.isSelected())
        {
            radioBtn = nonDegreeRdbtn;
        }

        return radioBtn;
    }

June 14, 2013 at 10:40 AM

Continue...

public String getGPA()
    {
        double A = 0, BP = 0, B = 0, CP = 0, C = 0, DP = 0, D = 0, F = 0;
        double avg = 0, total=0;
        String gpa = "";
        String grade ="";
        String c1 = course1.getText();
        String c2 = course2.getText();
        String c3 = course3.getText();
        String c4 = course4.getText();
        String c5 = course5.getText();
        String c6 = course6.getText();      

        if(c1.equalsIgnoreCase("a") 
                || c2.equalsIgnoreCase("a")
                || c3.equalsIgnoreCase("a") 
                || c4.equalsIgnoreCase("a")
                || c5.equalsIgnoreCase("a")
                || c6.equalsIgnoreCase("a"))
        {
            A = 4;
        }
        else if(c1.equalsIgnoreCase("b+")
                || c2.equalsIgnoreCase("b+")
                || c3.equalsIgnoreCase("b+")
                || c4.equalsIgnoreCase("b+")
                || c5.equalsIgnoreCase("b+")
                || c6.equalsIgnoreCase("b+"))
        {
            BP = 3.5;
        }
        else if(c1.equalsIgnoreCase("b")
                || c2.equalsIgnoreCase("b")
                || c3.equalsIgnoreCase("b")
                || c4.equalsIgnoreCase("b")
                || c5.equalsIgnoreCase("b")
                || c6.equalsIgnoreCase("b"))
        {
            B = 3;
        }
        else if(c1.equalsIgnoreCase("c+")
                || c2.equalsIgnoreCase("c+")
                || c3.equalsIgnoreCase("c+")
                || c4.equalsIgnoreCase("c+")
                || c5.equalsIgnoreCase("c+")
                || c6.equalsIgnoreCase("c+"))
        {
            CP = 2.5;
        }
        else if(c1.equalsIgnoreCase("c")
                || c2.equalsIgnoreCase("c")
                || c3.equalsIgnoreCase("c")
                || c4.equalsIgnoreCase("c")
                || c5.equalsIgnoreCase("c")
                || c6.equalsIgnoreCase("c"))
        {
            C = 2;
        }
        else if(c1.equalsIgnoreCase("d+")
                || c2.equalsIgnoreCase("d+")
                || c3.equalsIgnoreCase("d+")
                || c4.equalsIgnoreCase("d+")
                || c5.equalsIgnoreCase("d+")
                || c6.equalsIgnoreCase("d+"))
        {
            DP = 1.5;
        }
        else if(c1.equalsIgnoreCase("d")
                || c2.equalsIgnoreCase("d")
                || c3.equalsIgnoreCase("d")
                || c4.equalsIgnoreCase("d")
                || c5.equalsIgnoreCase("d")
                || c6.equalsIgnoreCase("d"))
        {
            D = 1;
        }
        else if(c1.equalsIgnoreCase("f")
                || c2.equalsIgnoreCase("f")
                || c3.equalsIgnoreCase("f")
                || c4.equalsIgnoreCase("f")
                || c5.equalsIgnoreCase("f")
                || c6.equalsIgnoreCase("f"))
        {
            F = 0;
        }

        avg = A+BP+B+CP+C+DP+D+F;
        if(avg >= 4)
        {
            gpa = "A";
        }
        if(avg < 4 && avg >=3.5)
        {
            gpa = "B+";
        }
        if(avg < 3.5 && avg >= 3)
        {
            gpa = "B";
        }
        if(avg < 3 && avg >= 2.5)
        {
            gpa = "C+";
        }
        if(avg < 2.5 && avg >= 2)
        {
            gpa = "C";
        }
        if(avg < 2 && avg >= 1.5)
        {
            gpa = "D+";
        }
        if(avg < 1.5 && avg >=1)
        {
            gpa = "D";
        }
        if(avg < 1)
        {
            gpa = "F";
        }
        System.out.println(gpa);
        return gpa;         

    }

June 14, 2013 at 10:41 AM

Continue...

public void displayStudentRecord()
    {
        jInternalFrame = new JInternalFrame("Student Record", true, true, true, true);
        jInternalFrame.setBounds(50, 50, 400, 250);
        jInternalFrame.setVisible(true);

        String stuName = stuNameTf.getText();
        String stuId = stuIdTf.getText();
        String checkboxValue = checkboxName().getText();
        String radioBtnValue = radioButtonName().getText();
        String stuGpaValue = getGPA();

        JLabel stuNameLabel = new JLabel("Name");
        JLabel name = new JLabel();
        name.setText(stuName);
        stuNameLabel.setBounds(20, 20, 80, 20);
        name.setBounds(130, 20, 150, 20);

        JLabel stuIdLabel = new JLabel("ID");
        JLabel id = new JLabel();
        id.setText(stuId);
        stuIdLabel.setBounds(20, 40, 30, 20);
        id.setBounds(130, 40, 100, 20);

        JLabel ckLabel = new JLabel("Session");
        JLabel ckValue = new JLabel();
        ckValue.setText(checkboxValue);
        ckLabel.setBounds(20, 60, 80, 20);
        ckValue.setBounds(130, 60, 100, 20);

        JLabel rdLabel = new JLabel("Student Type");
        JLabel rdValue = new JLabel();
        rdValue.setText(radioBtnValue);
        rdLabel.setBounds(20, 80, 110, 20);
        rdValue.setBounds(130, 80, 100, 20);

        JLabel gpLabel = new JLabel("GPA");
        JLabel gpValue = new JLabel();
        gpValue.setText(stuGpaValue);
        gpLabel.setBounds(20, 100, 30, 20);
        gpValue.setBounds(130, 100, 20, 20);

        jInternalFrame.add(stuNameLabel);
        jInternalFrame.add(name);
        jInternalFrame.add(stuIdLabel);
        jInternalFrame.add(id);
        jInternalFrame.add(ckLabel);
        jInternalFrame.add(ckValue);
        jInternalFrame.add(rdLabel);
        jInternalFrame.add(rdValue);
        jInternalFrame.add(gpLabel);
        jInternalFrame.add(gpValue);

        desktop.add(jInternalFrame);
        frame.setContentPane(desktop);
        desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
    }

    public void resetFields()
    {
        stuNameTf.setText("");
        stuIdTf.setText("");
        course1.setText("");
        course2.setText("");
        course3.setText("");
        course4.setText("");
        course5.setText("");
        course6.setText("");        
    }

    public void closeFrame()
    {
        frame.dispose();
    }
    public static void main(String args[])
    {
        GPACalculator cal = new GPACalculator();
        cal.createUI();
    }
}

Ads









Related Tutorials/Questions & Answers:
Design and Implement GPA Calculator
Design and Implement GPA Calculator  Once the user clicks...); headingLabel = new JLabel("Welcome To GPA Calculator... should display the GPA. Once the user clicks on the clear button all the fields
How can i implement the calculator programe in jsp code
How can i implement the calculator programe in jsp code  Please send code for the calculator using jsp technologies please guidelines to me.   Here is a simple jsp calculator code that accepts two numbers from the user
Advertisements
Calculator
Calculator  need a simple java program to degin a CALCULATOR without using ADVANCED JAVA....   Calculator in Java Swing
ModuleNotFoundError: No module named 'gpa-api'
ModuleNotFoundError: No module named 'gpa-api'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'gpa-api' How to remove the ModuleNotFoundError: No module named 'gpa-api
ModuleNotFoundError: No module named 'nu-gpa'
ModuleNotFoundError: No module named 'nu-gpa'  Hi, My Python...-gpa' How to remove the ModuleNotFoundError: No module named 'nu-gpa'... to install padas library. You can install nu-gpa python with following command
ModuleNotFoundError: No module named 'nu-gpa'
ModuleNotFoundError: No module named 'nu-gpa'  Hi, My Python...-gpa' How to remove the ModuleNotFoundError: No module named 'nu-gpa'... to install padas library. You can install nu-gpa python with following command
simple calculator program in javascript
simple calculator program in javascript  strong textsimple calculator in javascript
Calculator class
Calculator class  I am a beginner in Eclipse. I have to do a program called calculator that adds numbers. This is my code so far: //Margaret //ICS... class Calculator extends JFrame implements ActionListener { JTextField text
Calculator
ModuleNotFoundError: No module named 'Calculator'
ModuleNotFoundError: No module named 'Calculator'  Hi, My Python... 'Calculator' How to remove the ModuleNotFoundError: No module named 'Calculator' error? Thanks   Hi, In your python environment you
Implement transaction serializability
Implement transaction serializability  I need to implement transaction serializability in Java using semaphores/monnitors. What is the code to do
thread program for calculator implementation
thread program for calculator implementation  Hi i'm prem i need calculator progrm in java that are implemented by Thread interface.....pls strong text
implement microsoft office
implement microsoft office   how implement microsoft office in my java swing project ,or when microsoft office is not install then file open in defualt text editar.please send me source code
Design pattern
Design pattern  explain factory pattern
Java Calculator Program
Java Calculator Program  Hi, so I need to make a program that "works like a calculator". I need to make two versions: 1) I'm given the Expression Class and need to implement the children classes, which are Number, Product, Sum
implement sale purchaes
implement sale purchaes  i want to implement a code in advance java with connectivity .. i have a table in database in which i have some stack of sms if customer puchases and enter value in text box the amount will be deduct
Calculator - JSP-Servlet
Calculator  Dear Deepak Sir, Calculator program is avilable in Jsp... calculator program in jsp function checkValue(){ var msg...; } Simple calculator program in jsp /> >
How to write calculator in J2ME program?
How to write calculator in J2ME program?  How to write calculator in J2ME program
The implement keyword
The implement keyword       In java programming language, the keyword implement specifies... for using the implement keyword in a class. public class DominoPizza
Design Patterns
Design Patterns  Explain the Factory Pattern, VO Pattern, DAO Pattern, and Singleton Pattern
DESIGN PATTERNS
DESIGN PATTERNS  How many instances will be created for single ton pattern in Cluster Environment
java: implement a class queue
java: implement a class queue  Implement a class Queue which supports the following operations: boolean isEmpty() Returns true if the queue is empty, and false otherwise. int peak() Returns the value at the front of a non
implement core data iphone sdk
implement core data iphone sdk  How to implement core data in iPhone SDK? Please Help
Can an Interface implement another Interface?
Can an Interface implement another Interface?   Hi, Can an Interface implement another Interface? Thanks
Program to implement array of Objects.
Program to implement array of Objects.  Create a class it has following data members:- -name -account id -balance initialize the data members through constructors & define a print function & also define a function
ModuleNotFoundError: No module named 'aerospace-calculator'
ModuleNotFoundError: No module named 'aerospace-calculator'  Hi...: No module named 'aerospace-calculator' How to remove the ModuleNotFoundError: No module named 'aerospace-calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'agnostic-calculator'
ModuleNotFoundError: No module named 'agnostic-calculator'  Hi, My... named 'agnostic-calculator' How to remove the ModuleNotFoundError: No module named 'agnostic-calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'arithmetic-calculator'
ModuleNotFoundError: No module named 'arithmetic-calculator'  Hi...: No module named 'arithmetic-calculator' How to remove the ModuleNotFoundError: No module named 'arithmetic-calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'arithmetic-calculator'
ModuleNotFoundError: No module named 'arithmetic-calculator'  Hi...: No module named 'arithmetic-calculator' How to remove the ModuleNotFoundError: No module named 'arithmetic-calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'calculator-test'
ModuleNotFoundError: No module named 'calculator-test'  Hi, My... named 'calculator-test' How to remove the ModuleNotFoundError: No module named 'calculator-test' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'ccc-calculator'
ModuleNotFoundError: No module named 'ccc-calculator'  Hi, My... named 'ccc-calculator' How to remove the ModuleNotFoundError: No module named 'ccc-calculator' error? Thanks   Hi, In your python

Ads