|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

09-09-2008, 08:20 PM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 4
|
|
|
Avoid/Eliminate Duplicate Code
I am trying to avoid duplicate code. How can I get this program to calculate one set of credit hours / letter grades, prompt the user (y/n) for more class info, keep calculating and then display the results when the user indicates they have no more class info to input. The first program is my most current attempt but I see myself having to duplicate the code with slight modifications to account fro the additional user data. If you look down below the first one you will see the mess I made before, but at least that one gives the desired results.
import javax.swing.*;
public class StudentGPA {
static String userInput, letterGrade, anotherClass;
static int creditHours;
static double totalCreditHours, gradeScore, gradePoints, totalGradePoints, gradePointAverage;
public static void main (String[] args) {
userInput = JOptionPane.showInputDialog("Enter credit hours for the class.");
creditHours = Integer.parseInt(userInput);
validateUserInput(userInput);
totalCreditHours = creditHours;
letterGrade = JOptionPane.showInputDialog("Enter letter grade for the class.");
letterGrade = letterGrade.toUpperCase();
validateLetterGrade(letterGrade);
calculateGradeScore(letterGrade);
gradePoints = creditHours * gradeScore;
totalGradePoints = gradePoints;
gradePointAverage = totalGradePoints / totalCreditHours;
anotherClass = JOptionPane.showInputDialog("Do you have more classes to input?");
anotherClass = anotherClass.toUpperCase();
validateAnotherClass(anotherClass);
displayGPA();
System.exit(0);
}
public static double calculateGradeScore(String letterGrade){
if (letterGrade.equals("A"))
gradeScore = 4.0;
else
if (letterGrade.equals("B"))
gradeScore = 3.0;
else
if (letterGrade.equals("C"))
gradeScore = 2.0;
else
if (letterGrade.equals("D"))
gradeScore = 1.0;
else
if (letterGrade.equals("F"))
gradeScore = 0.0;
return gradeScore;
}
public static void validateUserInput (String userInput) {
while (creditHours<1 || creditHours>6) {
JOptionPane.showMessageDialog(null,
"Credit hours must be between 1 & 6.");
userInput = JOptionPane.showInputDialog("Enter credit hours for the class.");
creditHours = Integer.parseInt(userInput);
}
}
public static void validateLetterGrade (String letterGrade) {
while (!letterGrade.equals("A") && !letterGrade.equals("B")
&& !letterGrade.equals("C") && !letterGrade.equals("D")
&& !letterGrade.equals("F")){
JOptionPane.showMessageDialog(null,
"You entered an invalid letter grade.");
letterGrade = JOptionPane.showInputDialog("Enter letter grade for the class.");
letterGrade = letterGrade.toUpperCase();
}
}
public static void validateAnotherClass (String anotherClass){
while (!anotherClass.equals("Y") && !letterGrade.equals("N")){
JOptionPane.showMessageDialog(null,
"Please enter y or n.");
anotherClass = JOptionPane.showInputDialog("Do you have more classes to input?");
anotherClass = anotherClass.toUpperCase();
}
}
public static void displayGPA() {
JOptionPane.showMessageDialog(null, "GPA = "+totalGradePoints+"/"+totalCreditHours+" = "+gradePointAverage);
}
}
import javax.swing.*;
public class StudentGPA {
//declare variables
static String userInput, letterGrade, anotherClass;
static int creditHours;
static double totalCreditHours, gradeScore, gradePoints, totalGradePoints, gradePointAverage;
//start main method
public static void main (String[] args) {
//get credit hours
userInput = JOptionPane.showInputDialog("Enter credit hours for the class.");
creditHours = Integer.parseInt(userInput);
//validate credit hours
validateUserInput(userInput);
totalCreditHours = creditHours;
//get letter grade
letterGrade = JOptionPane.showInputDialog("Enter letter grade for the class.");
letterGrade = letterGrade.toUpperCase();
//validate letter grade
validateLetterGrade(letterGrade);
calculateGradeScore(letterGrade);
gradePoints = creditHours * gradeScore;
totalGradePoints = gradePoints;
//prompt user for another class
anotherClass = JOptionPane.showInputDialog("Do you have other class information?");
anotherClass = anotherClass.toUpperCase();
while (anotherClass.equals("Y")) {
//get next class credit hours
userInput = JOptionPane.showInputDialog("Enter credit hours for the next class.");
creditHours = Integer.parseInt(userInput);
//validate next class credit hours
while (creditHours<0 || creditHours>6) {
//display error message
JOptionPane.showMessageDialog(null,
"Credit hours must be between 1 & 6.");
userInput = JOptionPane.showInputDialog("Enter credit hours for the next class.");
creditHours = Integer.parseInt(userInput);
}
totalCreditHours += creditHours;
//get letter grade for next class
letterGrade = JOptionPane.showInputDialog("Enter letter grade for the next class.");
letterGrade = letterGrade.toUpperCase();
//validate letter grade
while (!letterGrade.equals("A") && !letterGrade.equals("B")
&& !letterGrade.equals("C") && !letterGrade.equals("D")
&& !letterGrade.equals("F")){
//display error
JOptionPane.showMessageDialog(null,
"You entered an invalid letter grade.");
letterGrade = JOptionPane.showInputDialog("Enter letter grade for the next class.");
letterGrade = letterGrade.toUpperCase();
}
calculateGradeScore(letterGrade);
//more claculations
gradePoints = creditHours * gradeScore;
totalGradePoints += gradePoints;
//more classes
anotherClass = JOptionPane.showInputDialog("Do you have other class information?");
anotherClass = anotherClass.toUpperCase();
}
gradePointAverage = totalGradePoints / totalCreditHours;
displayGPA();
System.exit(0);
}
//letter grade calculations method
public static double calculateGradeScore(String letterGrade){
if (letterGrade.equals("A"))
gradeScore = 4.0;
else
if (letterGrade.equals("B"))
gradeScore = 3.0;
else
if (letterGrade.equals("C"))
gradeScore = 2.0;
else
if (letterGrade.equals("D"))
gradeScore = 1.0;
else
if (letterGrade.equals("F"))
gradeScore = 0.0;
return gradeScore;
}
public static void validateUserInput (String userInput) {
while (creditHours<0 || creditHours>6) {
//display error message
JOptionPane.showMessageDialog(null,
"Credit hours must be between 1 & 6.");
userInput = JOptionPane.showInputDialog("Enter credit hours for the class.");
creditHours = Integer.parseInt(userInput);
}
}
public static void validateLetterGrade (String letterGrade) {
while (!letterGrade.equals("A") && !letterGrade.equals("B")
&& !letterGrade.equals("C") && !letterGrade.equals("D")
&& !letterGrade.equals("F")){
//display error message
JOptionPane.showMessageDialog(null,
"You entered an invalid letter grade.");
letterGrade = JOptionPane.showInputDialog("Enter letter grade for the class.");
letterGrade = letterGrade.toUpperCase();
}
}
public static void validateAnotherClass (String anotherClass){
}
//display results method
public static void displayGPA() {
JOptionPane.showMessageDialog(null, "GPA = "+totalGradePoints+"/"
+totalCreditHours+" = "+gradePointAverage);
}
}
Last edited by kicker : 09-09-2008 at 08:25 PM.
|
|

09-09-2008, 11:18 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
do {
get user input
calculate one set of credit hours / letter grades,
prompt the user (y/n) for more class info
} while(userWantsToDoMore);
|
|

09-10-2008, 08:00 PM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 4
|
|
I have cleaned everything up the best I can. All the calculations, validations and outputs work. I have seen some other programs similar in nature but they use a lot of do while, else and if.
Would this program be said to contain duplicate code? Am I going about it all wrong?
The reason I ask is that I got this as a comment from my professor. It is kind of cryptic to me
BTW, avoid code duplication. by avoiding the same code of the loop body
being done before the loop too.
import javax.swing.*;
import java.text.*;
public class StudentGPA {
static String userInput, letterGrade, anotherClass;
static int creditHours;
static double totalCreditHours, gradeScore, gradePoints, totalGradePoints, gradePointAverage;
public static void main (String[] args) {
userInput = JOptionPane.showInputDialog("Enter credit hours for the class.");
creditHours = Integer.parseInt(userInput);
validateUserInput(userInput);
totalCreditHours = creditHours;
letterGrade = JOptionPane.showInputDialog("Enter letter grade for the class.");
letterGrade = letterGrade.toUpperCase();
validateLetterGrade(letterGrade);
calculateGradeScore(letterGrade);
gradePoints = creditHours * gradeScore;
totalGradePoints = gradePoints;
anotherClass = JOptionPane.showInputDialog("Do you have more classes to input?");
anotherClass = anotherClass.toUpperCase();
validateAnotherClass(anotherClass);
while (anotherClass.equals("Y")){
userInput = JOptionPane.showInputDialog("Enter credit hours for the next class.");
creditHours = Integer.parseInt(userInput);
validateUserInput(userInput);
totalCreditHours += creditHours;
letterGrade = JOptionPane.showInputDialog("Enter letter grade for the next class.");
letterGrade = letterGrade.toUpperCase();
validateLetterGrade(letterGrade);
calculateGradeScore(letterGrade);
gradePoints = creditHours * gradeScore;
totalGradePoints += gradePoints;
anotherClass = JOptionPane.showInputDialog("Do you have more classes to input?");
anotherClass = anotherClass.toUpperCase();
validateAnotherClass(anotherClass);
}
gradePointAverage = totalGradePoints / totalCreditHours;
displayGPA();
System.exit(0);
}
public static double calculateGradeScore(String letterGrade){
if (letterGrade.equals("A"))
gradeScore = 4.0;
else
if (letterGrade.equals("B"))
gradeScore = 3.0;
else
if (letterGrade.equals("C"))
gradeScore = 2.0;
else
if (letterGrade.equals("D"))
gradeScore = 1.0;
else
if (letterGrade.equals("F"))
gradeScore = 0.0;
return gradeScore;
}
public static void validateUserInput (String userInput) {
while (creditHours<1 || creditHours>6) {
JOptionPane.showMessageDialog(null,
"Credit hours must be between 1 & 6.");
userInput = JOptionPane.showInputDialog("Enter credit hours for the class.");
creditHours = Integer.parseInt(userInput);
}
}
public static void validateLetterGrade (String letterGrade) {
while (!letterGrade.equals("A") && !letterGrade.equals("B")
&& !letterGrade.equals("C") && !letterGrade.equals("D")
&& !letterGrade.equals("F")){
JOptionPane.showMessageDialog(null,
"You entered an invalid letter grade.");
letterGrade = JOptionPane.showInputDialog("Enter letter grade for the class.");
letterGrade = letterGrade.toUpperCase();
}
}
public static void validateAnotherClass (String anotherClass){
while (!anotherClass.equals("Y") && !anotherClass.equals("N")){
JOptionPane.showMessageDialog(null,
"Please enter y or n.");
anotherClass = JOptionPane.showInputDialog("Do you have more classes to input?");
anotherClass = anotherClass.toUpperCase();
}
}
public static void displayGPA() {
DecimalFormat df = new DecimalFormat("####.00");
JOptionPane.showMessageDialog(null, "GPA = "+(int)totalGradePoints+"/"+(int)totalCreditHours+" = "+(df.format(gradePointAverage)));
}
}
Last edited by kicker : 09-10-2008 at 08:06 PM.
|
|

09-10-2008, 08:20 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
|
Your code asks the same question outside and again inside the loop:
Enter ...A
Enter ...B
Do ...?
>>> begin loop <<
Enter ...A
Enter ...B
Do ...?
>>> end loop <<<
Can you think how to only ask a question in one place instead of two?
Have you looked at the do{}while loop?
|
|

09-10-2008, 08:58 PM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 4
|
|
|
I have tried to apply do{} while, but being a novice, when I start playing around all it does is break the existing program.
On a scale of 1 to 10 how close am I with the existing code?
1 being start over and 10 being just place the do{}while tags in the right spots and build it.
|
|

09-10-2008, 09:52 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
all it does is break the existing program.
Only thing to say is: keep trying.
|
|

09-11-2008, 06:14 AM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 4
|
|
|
I figured out why my previous attempts at using a do{}while didn't work!
while (anotherClass.equals("Y")); <-- This works.
while (anotherClass == "Y")); <-- This does not work.
Thank you for encouraging me and not giving it to me!
I still have an issue with one of my validations but I will figure it out.
Thanks again!
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|