I wrote this code first as a class to get information of a student and calculate average
/** * This program shows a database of students in a school. * It gathers information as name, classes, grades, and calculates average. * * @author Luis Gerardo Fierro * */ import javax.swing.JOptionPane; public class StudentReport { public static void main(String[]args) { String name; //name of the student String schoolName; //name of the school String schoolGrade; //grade in school int numOfGrades; //number of grades or number of classes double grades; //grades received double average; //average of the grades double total=0; //total of the grades String trash; //to convert string to double //ask user for Name, grade, school name=JOptionPane.showInputDialog("Name:"); schoolGrade=JOptionPane.showInputDialog("Grade:"); schoolName=JOptionPane.showInputDialog("School:"); //ask user to enter number of grades trash=JOptionPane.showInputDialog("Number of grades:"); numOfGrades=Integer.parseInt(trash); // All in one //calculate average average=total/numOfGrades; //get the grades added together in order to calculate the average for (int i = 1; i <=numOfGrades; i++) { trash=JOptionPane.showInputDialog("Test " + i + " : " ); grades=Integer.parseInt(trash); total+=grades; } average=total/numOfGrades; //display window showing information JOptionPane.showMessageDialog(null, "School name: " + schoolName + "\n" + "Student: "+ name + " \n " + "School grade: " + schoolGrade + "\n" + "Average: " + average + "."); } }
Now I ma doing a program that tests that class. I will be using GUI, but because I know that I need to add something in
the first class in order to be able to call it in the demo program. The thing is that I don't know what to add the in the first code
in order to bring that information to calculate gpa, also I have no clue how to convert decimal average into gpa (0-4 scale).
import javax.swing.JOptionPane; import javax.swing.*; import java.awt.event.*; /** This program test the StudentReport class to display in a GUI format average and calculates the gpa based on that information. */ public class ReportGenerator extends JFrame{ private JLabel mesLab; //to present the title of the program private JButton start; //button to go to the next window, start the program private JButton calc; //calculate button private JPanel panel; //panel to hold components private final int WINDOW_WIDTH= ; // private final int WINDOW_HEIGHT= ; // private double getGpa; //calcualte gpa private double getAvg; //receive average results from the StudentReport class private StudentReport calculator; //to the class reference /** ReportGenerator constructor */ public ReportGenerator ( ) { //set title setTitle("Student Report"); //set the title size of window setSize(WINDOW_WIDTH, WINDOW_HEIGHT); //what happens when close button clicked setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //create the buttons start = new JButton("Start"); calc = new JButton("Calculate"); //event listener with the buttons start.addActionListener(new ButtonListener( ) ); calc.addActionListener(new ButtonListener( ) ); //create a panel to add the buttons panel = new JPanel( ); panel.add(next); panel.add(calc); //add panel to the content pane add(panel); //display window setVisible (true); } /** inner class that handles the reaction of the buttoons */ private class StartListener implements ActionListener { public void actionPerformed(ActionEvent e) { //get the action command String actionCommand = e.getActionCommand(); if(actionCommand.equals("Start") } {
The code is still unfinished and I know it is way out of working conditions but that is the main idea.
My idea is that the GUI should prompt a first window with the title of the program and a button saying start to use the program
Then it should prompt a new window to add enter the user's information, and finally at the bottom of the second window
the button calculate, it would display a third and last window showing the information entered by the user, average and gpa.
I kind of got an idea of what I should do but still I'm not completely in track because my first class works by itself.