|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Java Program Advice
Ok, so I wrote a program to calculate a student's GPA. There are various options and such. I was just looking for some feedback for things that I did incorrectly or poorly. Thanks
I am not 100% sure how to put code into here, so if you would like to tell me how to enter code, it will help me for any of my future posts. This is my main: /* * Well, I made a programe in Visual Basic to calculate a Grade Point Average * by entering the grades received. Then you can chose what kind of grade point * scale to use, 4 or 5. */ package gpaconverter; import javax.swing.*; /** * * @author Mike */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new Calculator()); frame.pack(); frame.setVisible(true); } } This is my class: package gpaconverter; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * * @author Mike */ public class Calculator extends JPanel{ private int counter, hoursInput, totalHours; private String gradeInput1, gradeInput2; private float gradeTotal, gpa; private JPanel mainPanel, gradePanel, buttonPanel, resultsPanel, optionPanel, schoolPanel; private JLabel title, grade, scale, school, hours; private JTextField gradeEntry, hoursEntry; private JButton add, calculate, exit, reset, highSchool, college; private JTextArea resultsArea; private JRadioButton four, five; private boolean collegeScale; public Calculator(){ //Call the first method and start the program createWindow(); } public void createWindow(){ //initializes the panel and sets it's size and color. //Then calls next method. Also, initializes gradeTotal and counter. //Also, initializes my flag for the college chocie as false. mainPanel = new JPanel(); mainPanel.setPreferredSize(new Dimension(700,700)); mainPanel.setBackground(Color.red); gradeTotal = 0; counter = 0; collegeScale = false; addMainWindowComponents(); } public void addMainWindowComponents(){ //Create the main Label for the program. Give it it's text, set it's //dimension, set the text color, and set the alignment. Then add the //title to the main Panel and add the main Panel to the Frame. Then //call the next method. title = new JLabel("Grade Point Average Calculator."); title.setPreferredSize(new Dimension(700, 200)); title.setForeground(Color.white); title.setHorizontalAlignment(0); mainPanel.add(title); add(mainPanel); createSchoolPanel(); } public void createSchoolPanel(){ //Create the Panel that will have the options for if the user is in high //school or college. schoolPanel = new JPanel(); schoolPanel.setPreferredSize(new Dimension(700, 100)); schoolPanel.setBackground(Color.red); addSchoolPanelComponents(); } public void addSchoolPanelComponents(){ //Add the label and the two choices(radio buttons) to the Panel //then call the next method school = new JLabel("Do you need to calculate a GPA for high school" + " or college?"); school.setForeground(Color.white); highSchool = new JButton("High School"); college = new JButton("College"); highSchool.addActionListener(new ButtonListener()); college.addActionListener(new ButtonListener()); schoolPanel.add(school); schoolPanel.add(highSchool); schoolPanel.add(college); mainPanel.add(schoolPanel); } public void createOptionPanel(){ //Create the Panel that will let the user pick what GPA scale to use. optionPanel = new JPanel(); optionPanel.setPreferredSize(new Dimension(700, 100)); optionPanel.setBackground(Color.red); addOptionPanelComponents(); } public void addOptionPanelComponents(){ //Add two radio Buttons to the Panel, 4.0 and 5.0, and call the next //method scale = new JLabel("What GPA scale would you like to use?"); scale.setForeground(Color.white); four = new JRadioButton("4.0"); four.setForeground(Color.red); five = new JRadioButton("5.0"); five.setForeground(Color.red); ButtonGroup group = new ButtonGroup(); group.add(four); group.add(five); optionPanel.add(scale); optionPanel.add(four); optionPanel.add(five); mainPanel.add(optionPanel); createGradePanel(); } public void createGradePanel(){ //Create the grade Panel, or the panel on which the inputs for the user, //set it's size and color, and call the next method. gradePanel = new JPanel(); gradePanel.setPreferredSize(new Dimension(700, 100)); gradePanel.setBackground(Color.red); addGradeWindowComponents(); } public void addGradeWindowComponents(){ //Initialize the components that will be on the gradePanel and then //call the next method. grade = new JLabel("Enter the Grade You received: "); grade.setForeground(Color.white); grade.setHorizontalAlignment(2); gradeEntry = new JTextField(2); if (collegeScale == false){ gradePanel.add(grade); gradePanel.add(gradeEntry); mainPanel.add(gradePanel); } else { gradePanel.add(grade); gradePanel.add(gradeEntry); addCollegeOptions(); mainPanel.add(gradePanel); } createButtonPanel(); } public void createButtonPanel(){ //Create the button Panel, that will have all of the buttons that will //be used in the program on it. buttonPanel = new JPanel(); buttonPanel.setPreferredSize(new Dimension(700, 100)); buttonPanel.setBackground(Color.red); addButtonPanelComponents(); } public void addButtonPanelComponents(){ //Basically, adds all of the buttons to the Button Panel. Also, calls //next method. add = new JButton("Add Grade"); calculate = new JButton("Calculate GPA"); reset = new JButton("Reset Program"); exit = new JButton("Exit"); add.addActionListener(new ButtonListener()); calculate.addActionListener(new ButtonListener()); reset.addActionListener(new ButtonListener()); exit.addActionListener(new ButtonListener()); buttonPanel.add(add); buttonPanel.add(calculate); buttonPanel.add(reset); buttonPanel.add(exit); mainPanel.add(buttonPanel); createResultsPanel(); } public void createResultsPanel(){ //Creates the final panel where the results will eventually be displayed resultsPanel = new JPanel(); resultsPanel.setPreferredSize(new Dimension(700, 200)); resultsPanel.setBackground(Color.red); addResultsPanelComponents(); } public void addResultsPanelComponents(){ //Adds a text area in which the results are displayed. resultsArea = new JTextArea(); resultsArea.setPreferredSize(new Dimension(600,150)); resultsArea.setEditable(false); resultsPanel.add(resultsArea); mainPanel.add(resultsPanel); } public void addGradeHighSchool(){ gradeInput1 = gradeEntry.getText().toUpperCase(); if(gradeEntry.getText().equals("")){ JOptionPane.showMessageDialog(null,"Please Enter a Grade."); } else if((four.isSelected() == false) && (five.isSelected() == false)){ JOptionPane.showMessageDialog(null, "Please Select a Scale."); }else if(!((gradeInput1.equals("A")) || (gradeInput1.equals("B")) || (gradeInput1.equals("C")) || (gradeInput1.equals("D")) || (gradeInput1.equals("F")) )){ JOptionPane.showMessageDialog(null,"Please Eneter a Grade A, B, C, D or F."); } else { if(four.isSelected()==true){ if (gradeInput1.equals("A")){ gradeTotal = gradeTotal + 4; } else if (gradeInput1.equals("B")){ gradeTotal = gradeTotal + 3; } else if (gradeInput1.equals("C")){ gradeTotal = gradeTotal + 2; } else if (gradeInput1.equals("D")){ gradeTotal = gradeTotal + 1; } } else if(five.isSelected()==true){ if (gradeInput1.equals("A")){ gradeTotal = gradeTotal + 5; } else if (gradeInput1.equals("B")){ gradeTotal = gradeTotal + 4; } else if (gradeInput1.equals("C")){ gradeTotal = gradeTotal + 3; } else if (gradeInput1.equals("D")){ gradeTotal = gradeTotal + 2; } else if (gradeInput1.equals("F")){ gradeTotal = gradeTotal + 1; } } if (counter > 0){ resultsArea.setText("Grade Just Entered: " + gradeInput1 + "\n" + "Grade Before Last: " + gradeInput2 + "\n" + "Total Grades Added: " + (counter + 1)); } else if(counter == 0){ resultsArea.setText("Grade Just Entered: " + gradeInput1 + "\n"); } gradeInput2 = gradeInput1; counter++; gradeEntry.setText(""); gradeEntry.requestFocus(); } } public void addGradeCollege(){ gradeInput1 = gradeEntry.getText().toUpperCase(); if(gradeEntry.getText().equals("")){ JOptionPane.showMessageDialog(null,"Please Enter a Grade."); } else if((four.isSelected() == false) && (five.isSelected() == false)){ JOptionPane.showMessageDialog(null, "Please Select a Scale."); }else if(!((gradeInput1.equals("A")) || (gradeInput1.equals("B")) || (gradeInput1.equals("C")) || (gradeInput1.equals("D")) || (gradeInput1.equals("F")) )){ JOptionPane.showMessageDialog(null,"Please Eneter a Grade A, B, C, D or F."); } else if(hoursEntry.getText().equals("")) { JOptionPane.showMessageDialog(null,"Please Enter a number of hours"); }else if(!( (hoursEntry.getText().equals("1")) || (hoursEntry.getText().equals("2")) || (hoursEntry.getText().equals("3")) || (hoursEntry.getText().equals("4")))){ JOptionPane.showMessageDialog(null, "Please enter a number 1 through 4"); }else { if(hoursEntry.getText().equals("1")){ hoursInput= 1; totalHours = totalHours + 1; } else if (hoursEntry.getText().equals("@")){ hoursInput= 2; totalHours = totalHours + 2; } else if (hoursEntry.getText().equals("3")){ hoursInput= 3; totalHours = totalHours + 3; } else if (hoursEntry.getText().equals("4")){ hoursInput= 4; totalHours = totalHours + 4; } if(four.isSelected()==true){ if (gradeInput1.equals("A")){ gradeTotal = gradeTotal + (4 * hoursInput); } else if (gradeInput1.equals("B")){ gradeTotal = gradeTotal + (3 * hoursInput); } else if (gradeInput1.equals("C")){ gradeTotal = gradeTotal + (2 * hoursInput); } else if (gradeInput1.equals("D")){ gradeTotal = gradeTotal + (1 * hoursInput); } } else if(five.isSelected()==true){ if (gradeInput1.equals("A")){ gradeTotal = gradeTotal + (5 * hoursInput); } else if (gradeInput1.equals("B")){ gradeTotal = gradeTotal + (4 * hoursInput); } else if (gradeInput1.equals("C")){ gradeTotal = gradeTotal + (3 * hoursInput); } else if (gradeInput1.equals("D")){ gradeTotal = gradeTotal + (2 * hoursInput); } else if (gradeInput1.equals("F")){ gradeTotal = gradeTotal + (1 * hoursInput); } } if (counter > 0){ resultsArea.setText("Grade Just Entered: " + gradeInput1 + "\n" + "Grade Before Last: " + gradeInput2 + "\n" + "Total Grades Added: " + (counter + 1) + "\n" + "Total Hours" + totalHours); } else if(counter == 0){ resultsArea.setText("Grade Just Entered: " + gradeInput1 + "\n" + "Total Hours" + totalHours); } gradeInput2 = gradeInput1; counter++; hoursEntry.setText(""); gradeEntry.setText(""); gradeEntry.requestFocus(); } } public void calculateGPA(){ //Calculate and display GPA gpa = gradeTotal/counter; resultsArea.setText("Grades Total: " + counter + "\n" + "Overall GPA " + gpa); } public void calculateCollegeGPA(){ //Calculate and Display the GPA while incorporating the hours gpa = gradeTotal/totalHours; resultsArea.setText("Hours Total: " + totalHours + "\n" + "Overall GPA "+ gpa); } public void addCollegeOptions(){ //Add the hours label and textField hours = new JLabel("How many hours was this grade worth?"); hours.setForeground(Color.white); hoursEntry = new JTextField(2); gradePanel.add(hours); gradePanel.add(hoursEntry); } private class ButtonListener implements ActionListener{ public void actionPerformed(ActionEvent event){ if (event.getSource() == exit){ System.exit(0); } else if(event.getSource() == reset){ counter = 0; gradeTotal = 0; resultsArea.setText(""); gradeEntry.setText(""); hoursInput=0; totalHours=0; hoursEntry.setText(""); } else if(event.getSource()== add){ if(collegeScale==false){ addGradeHighSchool(); } else{ addGradeCollege(); } } else if(event.getSource()==calculate){ if(collegeScale == false){ calculateGPA(); } else{ calculateCollegeGPA(); } } else if(event.getSource()==highSchool){ schoolPanel.setVisible(false); createOptionPanel(); } else if(event.getSource()==college){ schoolPanel.setVisible(false); collegeScale = true; createOptionPanel(); } } } } Thanks for your help |
|
#2
|
||||
|
||||
|
[ highlight=java ] code [ /highlight ]
__________________
Chat Server Project & Tutorial | P2P Network Planned (Java) | Graphing/Analysis Program (Writing - non-tutorial form) | Joke Thread “Rational thinkers deplore the excesses of democracy; it abuses the individual and elevates the mob. The death of Socrates was its finest fruit.” |
|
#3
|
|||
|
|||
![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > Java Program Advice |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|