Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 19th, 2008, 08:42 PM
mwebster mwebster is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 3 mwebster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 21 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old June 19th, 2008, 09:33 PM
gimp's Avatar
gimp gimp is offline
<?PHP user_title("gimp"); ?>
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2005
Location: Internet
Posts: 6,460 gimp User rank is General 25th Grade (Above 100000 Reputation Level)gimp User rank is General 25th Grade (Above 100000 Reputation Level)gimp User rank is General 25th Grade (Above 100000 Reputation Level)gimp User rank is General 25th Grade (Above 100000 Reputation Level)gimp User rank is General 25th Grade (Above 100000 Reputation Level)gimp User rank is General 25th Grade (Above 100000 Reputation Level)gimp User rank is General 25th Grade (Above 100000 Reputation Level)gimp User rank is General 25th Grade (Above 100000 Reputation Level)gimp User rank is General 25th Grade (Above 100000 Reputation Level)gimp User rank is General 25th Grade (Above 100000 Reputation Level)gimp User rank is General 25th Grade (Above 100000 Reputation Level)gimp User rank is General 25th Grade (Above 100000 Reputation Level)gimp User rank is General 25th Grade (Above 100000 Reputation Level)gimp User rank is General 25th Grade (Above 100000 Reputation Level)gimp User rank is General 25th Grade (Above 100000 Reputation Level)gimp User rank is General 25th Grade (Above 100000 Reputation Level)  Folding Points: 1555 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 22 h 7 m 28 sec
Reputation Power: 2642
Send a message via AIM to gimp
[ 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.”

Reply With Quote
  #3  
Old June 20th, 2008, 03:26 AM
king23 king23 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 23 king23 User rank is Corporal (100 - 500 Reputation Level)king23 User rank is Corporal (100 - 500 Reputation Level)king23 User rank is Corporal (100 - 500 Reputation Level)king23 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 10 h 54 m 29 sec
Reputation Power: 0

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Java Program Advice


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT