Results 1 to 7 of 7
  1. #1
    kicker is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default 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.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    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);

  3. #3
    kicker is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default

    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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    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?

  5. #5
    kicker is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default

    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.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    all it does is break the existing program.
    Only thing to say is: keep trying.

  7. #7
    kicker is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default

    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

  1. Hibernate Duplicate Entry problem
    By mc1392 in forum Advanced Java
    Replies: 0
    Last Post: 09-02-2008, 10:03 PM
  2. Replies: 16
    Last Post: 08-05-2008, 11:34 AM
  3. random string are duplicate
    By googgoo in forum New To Java
    Replies: 3
    Last Post: 04-03-2008, 10:01 AM
  4. Duplicate entry in registration form!!!
    By anki1234 in forum Advanced Java
    Replies: 1
    Last Post: 01-04-2008, 08:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •