Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-09-2008, 08:20 PM
Member
 
Join Date: Sep 2008
Posts: 4
kicker is on a distinguished road
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.
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); } }
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 08:25 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-09-2008, 11:18 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
do { get user input calculate one set of credit hours / letter grades, prompt the user (y/n) for more class info } while(userWantsToDoMore);
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 09-10-2008, 08:00 PM
Member
 
Join Date: Sep 2008
Posts: 4
kicker is on a distinguished road
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

Quote:
BTW, avoid code duplication. by avoiding the same code of the loop body
being done before the loop too.

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 08:06 PM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 09-10-2008, 08:20 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 09-10-2008, 08:58 PM
Member
 
Join Date: Sep 2008
Posts: 4
kicker is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 09-10-2008, 09:52 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Quote:
all it does is break the existing program.
Only thing to say is: keep trying.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 09-11-2008, 06:14 AM
Member
 
Join Date: Sep 2008
Posts: 4
kicker is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hibernate Duplicate Entry problem mc1392 Advanced Java 0 09-03-2008 12:03 AM
trying to improve code to avoid java.lang.OutOfMemory error bdyarem New To Java 16 08-05-2008 01:34 PM
Avoid unnecessary method calls JavaForums Java Blogs 0 05-30-2008 12:40 PM
random string are duplicate googgoo New To Java 3 04-03-2008 12:01 PM
Duplicate entry in registration form!!! anki1234 Advanced Java 1 01-04-2008 10:15 PM


All times are GMT +3. The time now is 11:18 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org