-
February 5th, 2013, 05:01 PM
#1
GPA Program
i wrote the following code for a dialog box to enter grades and calculate gpa. is there any way to make it
shorter? like can i use object arrays and initialize it in a for loop? thank you
Code:
import javax.swing.*;
public class GPA
{
public static void main(String args[])
{
int Counter=0;
while(Counter!=1)
{
String Name=JOptionPane.showInputDialog("Enter Student Name");
JTextField[] Subj=new JTextField[6];
for(int i=0;i<Subj.length;i++)
{
Subj[i]=new JTextField();
}
double[] Credits={1.0, 1.0, 1.0, 1.0, 1.25, 0.75};
double[] Points=new double[6]; //Letter Grade into Points
double TotalCredits=0.0;
double GPA=0.0;
Object[] Message={
"Enter English Grade: ",Subj[0],
"Enter Math Grade",Subj[1],
"Enter Social Studies Grade",Subj[2],
"Enter Art Grade",Subj[3],
"Enter Science Grade",Subj[4],
"Enter P.E Grade",Subj[5]
};
int Value = JOptionPane.showConfirmDialog(null, Message,
"Enter Letter Grade", JOptionPane.OK_CANCEL_OPTION);
String[] Letter=new String[6];
for(int i=0;i<Subj.length;i++)
{
Letter[i]=Subj[i].getText();
}
for(int i=0;i<Letter.length;i++)
{
switch(Letter[i])
{
case "A":
Points[i]=4.00;
break;
case "A-":
Points[i]=3.67;
break;
case "B+":
Points[i]=3.33;
break;
case "B":
Points[i]=3.00;
break;
case "B-":
Points[i]=2.67;
break;
case "C+":
Points[i]=2.33;
break;
case "C":
Points[i]=2.00;
break;
case "C-":
Points[i]=1.67;
break;
case "D+":
Points[i]=1.33;
break;
case "D":
Points[i]=1.00;
break;
case "D-":
Points[i]=0.67;
break;
case "F":
Points[i]=0.00;
break;
default:
JOptionPane.showMessageDialog(null,"One of the"
+ " Inputted Grades is Not Valid");
System.exit(0);
break;
}
}
//calculate and display GPA
for(int j=0;j<Letter.length;j++)
{
TotalCredits+=Points[j]*Credits[j];
GPA=TotalCredits/6;
}
JOptionPane.showMessageDialog(null,String.format("Student Name: %s"
+ "\nGPA: %.2f",Name,GPA));
int value=JOptionPane.showConfirmDialog(null,"Would You Like To "
+ "Calculate Another GPA?","",JOptionPane.YES_NO_OPTION);
if(value==JOptionPane.NO_OPTION)
{
JOptionPane.showMessageDialog(null,"Thank You For Using This "
+ "GPA Calculator");
Counter++;
}
}
}
}
Last edited by DataMiser; February 5th, 2013 at 09:37 PM.
Reason: added code tags
-
February 6th, 2013, 04:51 AM
#2
Re: GPA Program
First thing I'd do is get rid of the big switch statement - firstly, switch(String) only works in Java 1.7 and secondly, it's only providing a map of grade (A, B+ etc) to a score (4, 3.67 etc). You can accomplish this just as easily with a map and a look up function. e.g. ...
Code:
public class GPA
{
private HashMap<String, Double> gradeMap = new HashMap<String, Double>();
GPA() {
gradeMap.put("A", 4.00);
gradeMap.put("A-", 3.67);
gradeMap.put("B+", 3.33);
gradeMap.put("B", 3.00);
gradeMap.put("B-", 2.67);
gradeMap.put("C+", 2.33);
gradeMap.put("C", 2.00);
gradeMap.put("C-", 1.67);
gradeMap.put("D+", 1.33);
gradeMap.put("D", 1.00);
gradeMap.put("D-", 0.67);
gradeMap.put("F", 0.00);
}
public double gradeToScore(String grade) {
double score = -1;
if (gradeMap.containsKey(grade.toUpperCase())) {
score = gradeMap.get(grade.toUpperCase());
}
return score;
}
public static void main(String args[])
{
GPA gpa = new GPA();
....
Don't worry about my mixing double (return value of gradeToScore) and Double (values held in the hash) - the compiler takes care of it. (Google autoboxing if interested).
The first line of main creates an instance of the GPA class. This invokes the constructor, which populates the map with the grade to score mapping. Then when doing the processing, you can replace the switch statement with
Code:
double grade = gpa.gradeToScore(Letter[i]);
if (grade >= 0) {
Points[i] = grade;
}
else {
JOptionPane.showMessageDialog(null,"One of the"
+ " Inputted Grades is Not Valid");
System.exit(0);
}
You test the returned score is greater than or equal to zero and use it if this is the case. Otherwise (it is -1) meaning that the grade is not found in the map - in other words one of the grades keyed is not valid.
Letters is largely irrelevant because you populate it from subj. You might as well directly access the subj array when ascertaining the scores... i.e.
Code:
for(int i=0;i<Subj.length;i++) {
double grade = gpa.gradeToScore(Subj[i].getText());
if (grade >= 0) {
...
and then use the subj array again to calculate the GPA
Code:
for(int j=0;j<Subj.length;j++) {
TotalCredits+=Points[j]*Credits[j];
GPA=TotalCredits/6;
You use the "magic number" 6 everywhere - to initialize array bounds and to calculate the GPA. It would make more sense to use a constant with a sensible name like SUBJECT_COUNT. This makes maintaining the program easier in the future because if you want to expand it to 8 subjects, you change the constant rather than the number 6 at several places in the code. You could even use it in all the for loops because you'd know that all the arrays they are iterating over are the right size. [The fly in the ointment here are the Message and Credits arrays which you must maintain by hand.]
Finally, when you choose to exit the program you do it by incrementing counter. It has no other function that this, so you might as well get rid of it. Make the main loop while(true) and replace the line where you increment the counter with a break statement.
-
February 6th, 2013, 11:04 AM
#3
Re: GPA Program
Originally Posted by AlexVV
First thing I'd do is get rid of the big switch statement - firstly, switch(String) only works in Java 1.7 and secondly, it's only providing a map of grade (A, B+ etc) to a score (4, 3.67 etc). You can accomplish this just as easily with a map and a look up function. e.g. ...
Or better still use an enum.
-
February 6th, 2013, 09:35 PM
#4
Re: GPA Program
Originally Posted by keang
Or better still use an enum.
can you show me how i would do this? i tried to make one but it wouldn't let me use decimal numbers. also how would you make one with multiple fields? thanks
-
February 7th, 2013, 07:31 AM
#5
Re: GPA Program
Originally Posted by yankeesfan
can you show me how i would do this? i tried to make one but it wouldn't let me use decimal numbers. also how would you make one with multiple fields? thanks
You need to save the values in variables in the enum using an appropriate constructor. For instance, if you wanted to have an enum for the grades with their corresponding points you could do something like this:
Code:
enum GradeEnum {
A(4.0),
A_MINUS(3.67),
B_PLUS(3.33),
// ...
F(0.0);
public final double points;
GradeEnum(double points) {
this.points = points;
}
}
You could also replace the JTextFields you use to enter the grades for each subject with combo boxes, so instead of each subj[i] in message you can use:
Code:
new JComboBox(GradeEnum.values());
The user will be presented with a combo box instead of a text box for the grades, and you will avoid the problem of invalid input.
Notice that the combo boxes will show the name of the enums (A_MINUS, as opposed to A-). To get them to show the grades in a more user-friendly format you will have to override the toString method in the enum with something like this:
Code:
@Override
public String toString() {
return name().replace("_PLUS", "+").replace("_MINUS", "-");
}
After the dialog asking for the grades you have to extract the selected grade for each subject (using the getSelectedItem() method of the combo boxes), cast it to a GradeEnum and get its points.
Obviously, you can also use enums for the subjects, where you can have the credits and the name of the subject, or alternatively the question used in the dialog (override toString to return "Enter " + subject name + " grade:").
Last edited by jcaccia; February 7th, 2013 at 07:47 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|