CGPA Calculator App In Java

CGPA Calculator App In Java

Introduction

In this article, we will design a CGPA Calculator App In Java. We can convert percentages of marks to CGPA and vice versa. There is also a help window to help users with the procedures- a simple yet helpful program.

Setting Up The Environment

Make sure JDK is installed in your system. If not, you can install it with the help of the IDE you are using.

We are using Eclipse IDE for the project. The first step in IDE is to create a project with your preferred name. Create a package named “cgpa” inside the project’s src folder. All the classes have to be created inside the package created earlier.

Coding CGPA Calculator App In Java

1. Main Module

File Name: “Main.java

This contains the function. Calls the first page to be displayed.

package cgpa;

public class Main {
	public static void main(String[] args) {
		Home home = new Home();
		home.homeView();
	}
}

2. Home Page Module

File Name: “Home.java

This module shows us the home page of the project. This is the first page opened when the project is started.

package cgpa;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Home {
	public void homeView() {
		Commons commons = new Commons();
		JFrame frame = commons.frames();
		
		//-----------PERCENTTOCGPA-------------------
		JButton ptoc = new JButton("PERCENTAGE TO CGPA");
		commons.bdes(ptoc, 175, 220);
		frame.add(ptoc);
		ptoc.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Conversions conv = new Conversions();
				conv.conversionView(1);
			}
		});
		//-------------------------------------------
		
		//-------------CGPATOPERCENT---------------------
		JButton ctop = new JButton("CGPA TO PERCENTAGE");
		commons.bdes(ctop, 175, 310);
		frame.add(ctop);
		ctop.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Conversions conv = new Conversions();
				conv.conversionView(2);
			}
		});
		//-----------------------------------------------
		
		//----------------HELP---------------------------
		JButton help = new JButton("HELP");
		commons.bdes(help, 175, 400);
		frame.add(help);
		help.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Help helpwindow = new Help();
				helpwindow.helpView();
			}
		});
		//-----------------------------------------------
		
		frame.setVisible(true);
	}
}

homepage

Here we have a logo, a closing button, and three buttons to operate. One to convert a percentage to CGPA, one to convert CGPA to a percentage, and the last one to display a help window.

3. Help window Module

File Name: “Help.java

This module simply shows the help instruction to work with the software.

package cgpa;

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class Help {
	public void helpView() {
		Commons commons = new Commons();
		JFrame frame= commons.frames();
		Font txt = new Font("", Font.BOLD, 18);
		
		//----------------BACK--------------------
		JLabel prev = commons.back(frame);
		frame.add(prev);
		//----------------------------------------
		
		//-------------INSTRUCTIONS-----------------
		JLabel i1 = new JLabel("-> SELECT THE CGPA GRADING SCALE.");
		i1.setBounds(50, 230, 650, 20);
		i1.setFont(txt);
		frame.add(i1);
		JLabel i2 = new JLabel("-> ENTER THE NUMBER THAT YOU WANT TO CONVERT.");
		i2.setBounds(50, 280, 650, 20);
		i2.setFont(txt);
		frame.add(i2);
		JLabel i3 = new JLabel("-> PRESS CALCULATE.");
		i3.setBounds(50, 330, 650, 20);
		i3.setFont(txt);
		frame.add(i3);
		JLabel i4 = new JLabel("-> RESULT WILL BE OBTAINED.");
		i4.setBounds(50, 380, 650, 20);
		i4.setFont(txt);
		frame.add(i4);
		//------------------------------------------
		
		frame.setVisible(true);
	}
}

help module

This module shows the help window.

4. Conversion Module

File Name: “Conversions.java

This module of the CGPA Calculator App In Java helps us with conversions. Both the view and operation are done in this module.

package cgpa;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Conversions {
	
	int x = 0;
	
	public void conversionView(int i) {
		Commons commons = new Commons();
		JFrame frame = commons.frames();
		JButton b10 = new JButton("10.0");
		JButton b5 = new JButton("5.0");
		JButton b4 = new JButton("4.0");
		
		//----------------BACK--------------------
		JLabel prev = commons.back(frame);
		frame.add(prev);
		//----------------------------------------
		
		//---------------LABEL------------------
		JLabel grade = new JLabel("SELECT THE GRADING SCALE : ");
		grade.setBounds(30, 180, 300, 50);
		grade.setFont(new Font("", Font.BOLD, 15));
		grade.setForeground(Color.decode("#949398"));
		frame.add(grade);
		//--------------------------------------
		
		//----------------4------------------------
		commons.bdes(b4, 0, 0);
		b4.setBounds(50, 230, 183, 40);
		frame.add(b4);
		b4.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				x=4;
				b4.setBackground(Color.decode("#763A12"));
				b5.setBackground(Color.decode("#949398"));
				b10.setBackground(Color.decode("#949398"));
			}
		});
		//------------------------------------------
		
		//----------------5------------------------
		commons.bdes(b5, 0, 0);
		b5.setBounds(283, 230, 183, 40);
		frame.add(b5);
		b5.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				x=5;
				b5.setBackground(Color.decode("#763A12"));
				b4.setBackground(Color.decode("#949398"));
				b10.setBackground(Color.decode("#949398"));
			}
		});
		//------------------------------------------
		
		//----------------10------------------------
		commons.bdes(b10, 0, 0);
		b10.setBounds(516, 230, 183, 40);
		frame.add(b10);
		b10.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				x=10;
				b10.setBackground(Color.decode("#763A12"));
				b4.setBackground(Color.decode("#949398"));
				b5.setBackground(Color.decode("#949398"));
			}
		});
		//------------------------------------------
		
		//-----------------INPUTLABEL-------------------
		JLabel enter = new JLabel("ENTER THE VALUE : ");
		enter.setBounds(30, 330, 200, 30);
		enter.setFont(new Font("", Font.BOLD, 15));
		enter.setForeground(Color.decode("#949398"));
		frame.add(enter);
		JTextField value = new JTextField();
		value.setBounds(50, 370, 183, 40);
		value.setBackground(Color.decode("#949398"));
		value.setForeground(Color.WHITE);
		value.setHorizontalAlignment(JLabel.CENTER);
		frame.add(value);
		//----------------------------------------------
		
		//----------------------RESULT--------------------
		JLabel result = new JLabel("RESULT : ");
		result.setBounds(496, 330, 200, 30);
		result.setFont(new Font("", Font.BOLD, 15));
		result.setForeground(Color.decode("#949398"));
		frame.add(result);
		JTextField res = new JTextField();
		res.setBounds(516, 370, 183, 40);
		res.setBackground(Color.decode("#949398"));
		res.setForeground(Color.WHITE);
		res.setFont(new Font("", Font.BOLD, 20));
		res.setEditable(false);
		res.setHorizontalAlignment(JLabel.CENTER);
		frame.add(res);
		//-------------------------------------------------
		
		//----------------WARNING----------------------
		JLabel warning = new JLabel();
		warning.setBounds(0, 430, 750, 30);
		warning.setHorizontalAlignment(JLabel.CENTER);
		warning.setForeground(Color.RED);
		warning.setFont(new Font("", Font.BOLD, 15));
		frame.add(warning);
		//---------------------------------------------
		
		//---------------CALCULATE----------------------
		JButton calc = new JButton("CALCULATE");
		commons.bdes(calc, 175, 340);
		calc.setBounds(283, 370, 183, 40);
		frame.add(calc);
		calc.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				float resValue;
				if(x==0)
					warning.setText("Select a grading scale!!!");
				else if(value.getText().equals(""))
					warning.setText("Enter Some Values!!!");
				else {
					warning.setText("");
					if(i==1) {
						if(Float.parseFloat(value.getText()) > 100 || Float.parseFloat(value.getText()) < 0)
							warning.setText("Value cannot be greater than 100% or less than 0%!!!");
						else {
							resValue = x*(Float.parseFloat(value.getText())/100);
							res.setText(resValue+"");
						}
					}
					else {
						if(Float.parseFloat(value.getText()) > x || Float.parseFloat(value.getText()) < 0)
							warning.setText("Value cannot be greater than "+ x +" or less than 0!!!");
						else {
							resValue = 100*(Float.parseFloat(value.getText())/x);
							res.setText(resValue+"%");
						}
					}
				}
			}
		});
		//----------------------------------------------
		
		frame.setVisible(true);
	}
}

conversion module

This module here has the option to select the grading scale. After selecting the grading scale, Enter the value and press “CALCULATE” to get the result. The result will be shown in the result area.

5. Common Module

File Name: “Commons.java

This module of the CGPA Calculator App In Java contains the function for the common frames, buttons, and labels in the project. This helps us in reducing the size of each module by avoiding the duplication of codes.

package cgpa;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Commons {
	public JFrame frames() {
		JFrame frame = new JFrame();
		frame.setSize(750, 500);
		frame.setLayout(null);
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
		frame.setUndecorated(true);
		frame.getContentPane().setBackground(Color.decode("#F4DF4E"));
		
		//---------------EXIT--------------------
		JLabel exit = new JLabel("X");
		exit.setBounds(710, 15, 25, 30);
		exit.setFont(new Font("", Font.BOLD, 25));
		exit.setForeground(Color.decode("#949398"));
		frame.add(exit);
		exit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				System.exit(0);
			}
		});
		//----------------------------------------
		
		//--------------LOGO----------------------
		JLabel cgpa = new JLabel("CGPA");
		cgpa.setBounds(0, 50, 750, 80);
		cgpa.setForeground(Color.decode("#949398"));
		cgpa.setFont(new Font("", Font.BOLD, 100));
		cgpa.setHorizontalAlignment(JLabel.CENTER);
		frame.add(cgpa);
		JLabel calc = new JLabel("CALCULATOR");
		calc.setBounds(0, 105, 750, 80);
		calc.setForeground(Color.decode("#949398"));
		calc.setFont(new Font("", Font.BOLD, 30));
		calc.setHorizontalAlignment(JLabel.CENTER);
		frame.add(calc);
		//------------------------------------------
		
		return frame;
	}
	
	//----------------BUTTON DESIGNS----------------------
	public JButton bdes(JButton button, int x, int y) {
		button.setBackground(Color.decode("#949398"));
		button.setForeground(Color.WHITE);
		button.setBounds(x, y, 400, 50);
		button.setFocusable(false);
		return button;
	}
	//------------------------------------------------------
	
	//-----------------BACK BUTTON---------------------------
	public JLabel back(JFrame frame) {
		JLabel prev = new JLabel("< BACK");
		prev.setBounds(25, 20, 90, 30);
		prev.setFont(new Font("", Font.BOLD, 25));
		prev.setForeground(Color.decode("#949398"));
		prev.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frame.dispose();
			}
		});
		return prev;
	}
	//--------------------------------------------------------
}

Output for CGPA Calculator In Java:

Thank you for reading CGPA Calculator App In Java and visiting our website.


Also Read:

Share:
Avatar of Muethesim P K

Author: Muethesim P K

Leave a Reply

Your email address will not be published. Required fields are marked *