Results 1 to 7 of 7
Thread: Avoid/Eliminate Duplicate Code
- 09-09-2008, 06:20 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 4
- Rep Power
- 0
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.
Java Code: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); } }Java Code: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 06:25 PM.
- 09-09-2008, 09:18 PM #2
Java Code: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, 06:00 PM #3
Member
- Join Date
- Sep 2008
- Posts
- 4
- Rep Power
- 0
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.
Java Code: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 06:06 PM.
- 09-10-2008, 06:20 PM #4
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, 06:58 PM #5
Member
- Join Date
- Sep 2008
- Posts
- 4
- Rep Power
- 0
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, 07:52 PM #6
Only thing to say is: keep trying.all it does is break the existing program.
- 09-11-2008, 04:14 AM #7
Member
- Join Date
- Sep 2008
- Posts
- 4
- Rep Power
- 0
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!
Similar Threads
-
Hibernate Duplicate Entry problem
By mc1392 in forum Advanced JavaReplies: 0Last Post: 09-02-2008, 10:03 PM -
trying to improve code to avoid java.lang.OutOfMemory error
By bdyarem in forum New To JavaReplies: 16Last Post: 08-05-2008, 11:34 AM -
random string are duplicate
By googgoo in forum New To JavaReplies: 3Last Post: 04-03-2008, 10:01 AM -
Duplicate entry in registration form!!!
By anki1234 in forum Advanced JavaReplies: 1Last Post: 01-04-2008, 08:15 PM


LinkBack URL
About LinkBacks

Bookmarks