Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Shrinking my code?

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Shrinking my code?

    Hello, I am working on this math quiz Java program which I have gotten to work. The only thing is when I went to hand it into my teacher he wanted me to shirk down my code. I have tried everything I could think of doing with my limited knowledge but could not come up with a way to break it down even more. Any tips in the right directions would be most appreciated.
       import javax.swing.JOptionPane;
     
       public class inputmathquizx {
          public static void main(String[] args) {
             bigMethod();
          }
     
          public static void bigMethod() {
             //big declare  
             long time = 0;
             final int NUMBER_OF_QUESTIONS = 5;
             int input1 = 0;
             int input2 = 0;
             int answer = 0;
             int option = 0;
             int data = 0;
             int count = 0;
             int wrong = 0;
             String output = "";
     
             do {	
                //re declare
                time = System.currentTimeMillis();
                wrong = 0;
                count = 0;       
                output = "";      
                do{
     
                	   //choose a quiz
                   String operaterString = JOptionPane.showInputDialog(null,
                      "Choose a quiz.\n Addtion: 1\n" + 
                      "Subtraction: 2\n Multiplication: 3\n Division: 4\n Modulus: 5\n");
     
                   //data string	
                   data = Integer.parseInt(operaterString);
     
                	//invalid Operator
                   if ((data <= 0)||(data > 5)){
                      JOptionPane.showMessageDialog(null, "Invalid Operator!");
                      continue;					
                   }
     
     
                	// count number of questions left
                   int timesleft = (NUMBER_OF_QUESTIONS - (count + wrong));
     
                   if(timesleft == 1){
                      JOptionPane.showMessageDialog(null, "You can do "  + 
                         (NUMBER_OF_QUESTIONS - (count + wrong)) + " more question.");
                   }
                   else
                      JOptionPane.showMessageDialog(null, "You can do "  + 
                         (NUMBER_OF_QUESTIONS - (count + wrong)) + " more questions.");
     
                		     	//User input for the 1st number 	            	               
                   do{String number1String = JOptionPane.showInputDialog("Enter first number");
                      input1 = Integer.parseInt(number1String);
     
                      do{//User input for the 2nd number
                         String number2String = JOptionPane.showInputDialog("Enter second number");
                         input2 = Integer.parseInt(number2String);
     
                      // cant divide by zero
                         if ((input2 == 0) && (data == 4) || (input2 == 0) && (data == 5)){ 
                            JOptionPane.showMessageDialog(null, "you can't divide by zero.");
                         }
                         // cant  subtract from zero
                         else if((input1 == 0) && (data == 2)) {
                            JOptionPane.showMessageDialog(null, "you can't subtract from  zero.");
                         }
                      }while((data == 4) && (input2 == 0) || (data == 5) && (input2 == 0));
                   }while((input1 == 0) && (data == 2));
     
                   do {
     
                    //Cases
                      switch (data){   
                         case 1: answer = addition(input1, input2);   
                            if (answer == input1 + input2){
                               output += "\n" + input1 + " + " + input2 + " = " + answer +
                                  ((input1 + input2 == answer) ? " correct" : " wrong");
                               count++;								
                            }
                            else{
                               output += "\n" + input1 + " + " + input2 + " = " + answer +
                                  ((input1 + input2 == answer) ? " correct" : " wrong");
                               wrong++;
                            }
                            break;
     
                         case 2: answer = subtraction (input1, input2);
                            if (answer == input1 - input2){
                               output += "\n" + input1 + " - " + input2 + " = " + answer +
                                  ((input1 - input2 == answer) ? " correct" : " wrong");
                               count++;		
                            }
                            else{
                               output += "\n" + input1 + " - " + input2 + " = " + answer +
                                  ((input1 - input2 == answer) ? " correct" : " wrong");
                               wrong++;
                            }
                            break;
     
                         case 3: answer = multiply (input1, input2);
                            if (answer == input1 * input2){
                               output += "\n" + input1 + " - " + input2 + " = " + answer +
                                  ((input1 * input2 == answer) ? " correct" : " wrong");
                               count++;								
                            }
                            else{
                               output += "\n" + input1 + " * " + input2 + " = " + answer +
                                  ((input1 * input2 == answer) ? " correct" : " wrong");
                               wrong++;
                            }
                            break;
     
                         case 4: answer = divide (input1, input2);
                            if (answer == input1 / input2){
                               output += "\n" + input1 + " / " + input2 + " = " + answer +
                                  ((input1 / input2 == answer) ? " correct" : " wrong");
                               count++;								
                            }
                            else{
                               output += "\n" + input1 + " / " + input2 + " = " + answer +
                                  ((input1 / input2 == answer) ? " correct" : " wrong");
                               wrong++;
                            }
                            break;
     
                         case 5: answer = modulus (input1, input2);
                            if (answer == input1 % input2){
                               output += "\n" + input1 + " % " + input2 + " = " + answer +
                                  ((input1 % input2 == answer) ? " correct" : " wrong");
                               count++;								
                            }
                            else{
                               output += "\n" + input1 + " % " + input2 + " = " + answer +
                                  ((input1 % input2 == answer) ? " correct" : " wrong");
                               wrong++;
                            }
                            break;
     
                      }
     
                   } while((data != 1) && (data != 2) && (data != 3) && (data != 4) && (data != 5));  
     
                } while (count + wrong < NUMBER_OF_QUESTIONS);	
     
             	 //time the quiz took      
                long endTime = System.currentTimeMillis();
                long testTime = (endTime - time);
     
             	 //repeat quiz   
                option = JOptionPane.showConfirmDialog(null, "Would you like to take another quiz?");
     
     
             	//GUI to show right and wrong answer
                JOptionPane.showMessageDialog(null, " Correct answers = " + count + "\nwrong answers =" +wrong+
                   "\nIt took you  " + testTime /1000 + " seconds to complete this quiz\n " + output);
     
             }while (option == JOptionPane.YES_OPTION);
     
          }
     
       //add
          public static int addition(int input1, int input2) {
             int answer = 0;
     
             String aString = JOptionPane.showInputDialog ("What is " + input1 + " + " + input2 + "? ");
             answer = Integer.parseInt(aString);	
     
             if (input1 + input2 == answer){
                JOptionPane.showMessageDialog(null, "You are correct!");
     
             }
             else
                JOptionPane.showMessageDialog(null, "Incorrect.\n" + input1 + " + " + 
                   input2 + " should be " + (input1 + input2));
     
             return answer;
          }
     
       //sub
          public static int subtraction(int input1, int input2) {
             int answer = 0;
     
             String sString = JOptionPane.showInputDialog ("What is " + input1 + " - " + input2 + "? ");
             answer = Integer.parseInt(sString);	
     
             if (input1 - input2 == answer){
                JOptionPane.showMessageDialog(null, "You are correct!");
     
             }
             else
                JOptionPane.showMessageDialog(null, "Incorrect.\n" + input1 + " - " + 
                   input2 + " should be " + (input1 - input2));
     
             return answer;
          }
     
       	//mult
          public static int multiply(int input1, int input2) {
             int answer = 0;
     
             String mString = JOptionPane.showInputDialog ("What is " + input1 + " x " + input2 + "? ");
             answer = Integer.parseInt(mString);		
     
             if (input1 * input2 == answer){
                JOptionPane.showMessageDialog(null, "You are correct!");
     
             }
             else
                JOptionPane.showMessageDialog(null, "Incorrect.\n" + input1 + " x " + 
                   input2 + " should be " + (input1 * input2));
     
             return answer;
     
          }
       //divide
          public static int divide(int input1, int input2) {
             int answer = 0;
     
             String dString = JOptionPane.showInputDialog ("What is " + input1 + " / " + input2 + "? ");
             answer = Integer.parseInt(dString);		
     
             if (input1 / input2 == answer){
                JOptionPane.showMessageDialog(null, "You are correct!");
     
             }
             else
                JOptionPane.showMessageDialog(null, "Incorrect.\n" + input1 + " / " + 
                   input2 + " should be " + (input1 / input2));
     
             return answer;
     
          }
       //mod
          public static int modulus(int input1, int input2) {
             int answer = 0;
     
             String moduloString = JOptionPane.showInputDialog ("What is " + input1 + " % " + input2 + "? ");
             answer = Integer.parseInt(moduloString);		
     
             if (input1 % input2 == answer){
                JOptionPane.showMessageDialog(null, "You are correct!");
     
             }
             else
                JOptionPane.showMessageDialog(null, "Incorrect.\n" + input1 + " % " + 
                   input2 + " should be " + (input1 % input2));
     
             return answer;	
          }
       }
    Last edited by Delgado21; March 15th, 2011 at 07:21 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Shrinking my code?

    For starters, the line of code
    output += "\n" + input1 + " % " + input2 + " = " + answer +
                                  ((input1 % input2 == answer) ? " correct" : " wrong");
    is repetitively being called (in one form or another). Duplicates such as this can readily be boiled down, for example using a method that returns the string based upon some parameter.