Methods

Discussion in 'Software' started by ll_llniQue, Mar 23, 2005.

  1. ll_llniQue

    ll_llniQue Private E-2

    Good Afternoon :rolleyes:


    Am new here and new in Java too ... am seeking help and knowledge from you guys ... Ive been working on methods for a week now and i have a guestion which i don't seem to understand how it goes ... i tried it but i need you guys to check if am on the right track ...


    Question:
    Create a class LearnToMultiply

    The class should contain one main method. In the main method, use Math.random method to produce two positive one digit integers(between 1 and 10). The program should ask the user about the result of multiplying these two integers. "HOW MUCH IS 3 TIMES 7?"

    The user will input the answer. The program checks the answer, if it is correct, it will display "Excellent, You got it". If it is wrong it will display "No, Try again". The user will continue to enter until he guesses the correct answer. The program will display at the end, the number of trials.



    Code:
     
    
    import javax.swing.JOptionPane; 
    import java.lang.Math; 
    
    class LearnToMultiply 
    { 
        public static void main (String args[]) 
       { 
    
    String strmsg1, strmsg2, strmsg3; 
    String strNumber; 
    int Number; 
    int counter; 
    
    strNumber= JOptionPane.showInputDialog("How much is 3 times 7 ? "); 
    Number= Integer.parseInt (strNumber); 
            
    strmsg1= "Excellent. You got it"; 
    strmsg2= "No. Try again"; 
    //strmsg3= "You fail, the right answer is :" + Number ; 
    
    Number = 1 + ( int ) ( Math.random() * 10); 
    counter = 0; 
    
        for ( counter= 0; counter < 11; counter++){if (Number == 21)      
        {    
            JOptionPane.showMessageDialog(null,strmsg1);    
        } 
          
            else 
            {      
                JOptionPane.showMessageDialog(null, strmsg2);      
            }              
         } 
    
      if (Number == 21) 
            { 
               JOptionPane.showMessageDialog(null, strmsg1); 
            } 
              
               else 
                if (Number != 21) 
                    { 
                        JOptionPane.showMessageDialog(null, strmsg2); 
                    } 
                        
                        
                                  
                for ( counter= 0; counter < 11; counter++); 
    
    
    
    
    }// end for 
    
    }// end class 
     
    
     
  2. ll_llniQue

    ll_llniQue Private E-2

    No help :rolleyes:
     
  3. QuickSilver

    QuickSilver Corporal

    Ok what you have is looking like a great start... but you are missing out a few things that are being asked of you...

    I think the idea of the class is that it picks 2 positive integers at random (between 1 and 10) and then asks the user to enter the answer.
    The example uses the numbers 3 and 7 but this is an example only. So you certainly shouldn't be hard coding 3, 7, or 21 in there...

    Think about what is being asked of you, adn then break it down into steps... So...

    So you want a single class with a single method.
    You want the program to create 2 random integers between 1 and 10.
    You want to repeatedly (eg loop)
    - ask the user what the answer is
    - check the users answer against the real answer (maybe a third integer with the result of the mulitplication of the 2 integers???)
    - if the user is correct
    - display the number of attempts it took
    - if the user is incorrect
    - Display an error message
    - loop

    So now you need to be considering what it is you need to accomplish this (and I can see that you have the knowledge to impleement these things so believe me - you are almost there!!!

    For this I would use...
    a do-while loop
    a counter (integer) for the number of attempts taken
    an integer for the first number (randomly picked - your code for this looks good)
    an integer for the second number (randomly picked)
    an integer for num1 multiplied by num2
    an if-else statement for checking what the user has entered.

    I hope this helps get you on track and getting your program finished... If you have some specific questions about how to do some things ask away and we'll see what we can do to help...

    Good luck!
     
  4. ll_llniQue

    ll_llniQue Private E-2

    Thank you guys for your help :)

    I really appreciate it ... I tried to work on your hints and comments and this is what i reaches ...

    Code:
    import javax.swing.JOptionPane;
    import java.lang.Math;
    
    class LearnToMultiply
    {
        public static void main (String args[])
    	{
         
         String strmsg1, strmsg2, strmsg3;
         String strNumber, strUserNum;
         int Number, Number1, Number2, intUserNum;
         int counter, intCount;
         
         //ask the user to answer the question
         strNumber= JOptionPane.showInputDialog("How much is 3 times 7? ");
         Number= Integer.parseInt (strNumber);//converting the entered data [[string]] to integer
        
         strmsg1= "Excellent. You got it";
         strmsg2= "No. Try again";
        
         //creating  random numbers
         Number1 = 1 + ( int ) ( Math.random() * 10);
         Number2 = 1 + ( int ) ( Math.random() * 10);
         
         counter = 0; //setting counter to 0
     
           
            if (Number == 21) //the guessed number is equal to 21     
              
              { 
                     
               JOptionPane.showMessageDialog(null, strmsg1);     
               
              }       
        
                else //the guessed number is not equal to 21
                       
                {    
                               
                JOptionPane.showMessageDialog(null, strmsg2);  
                
                }                                                 
                
            while (intUserNum != 21)
    
                {
                        
            strUserNum = JOptionPane.showInputDialog("Guess another number");
            intUserNum = Integer.parseInt(strUserNum);
            intCount = count + 1; //adding to the number of trials
             
             }
    
            
        
              }// end loop
              
        }// end class 
    
    :rolleyes: Hope that i am on the right track
     
  5. QuickSilver

    QuickSilver Corporal

    This is almost there... but again you seem to have got the idea that you have to hard code the example provided in the question in....

    Consider the following lines first :

    Code:
         Number1 = 1 + ( int ) ( Math.random() * 10);
        Number2 = 1 + (int) (Math.random() * 10);
    What does the method want to do with these numbers???
    To what end has it initialised them???

    Code:
    strNumber= JOptionPane.showInputDialog("How much is 3 times 7? ");
    You've fixated on this example... The idea of the program is not that it asks the person how much 3 times 7... its that it asks how much Number1 times Number2 is...

    Code:
    if (Number == 21) //the guessed number is equal to 21
    And again your comparison here is against a hard coded number....
    You want to be comparing Number against the result of NUmber1 times Number2.

    Does this make sense...

    OK - one good thing to do when writing a program is to break it down into bits and look at each individual bit...

    So... lets see if we can't get you tackling this good and proper :)

    Using what you have already as a basis... see if you can write the following for me...

    A single class with a single method.
    This method will have 3 integer variables. 2 of them will be initialised with a random number between 1 and 10. The third will be the result of multiplying variables 1 and 2...
    Ask the user what the result of multiplying variables 1 and 2 is.
    If they get it right then tell them... if they get it wrong, then tell them that... then exit.

    If you've noticed that its a subset of what you have already been asked to do then good for you... we'll look at adding the rest in shortly...

    Good luck.. if anything is baffling you about what I have said above then feel free to ask a question - we're here to help.
    And I'm sorry if I'm not giving you the code to answer this with a simple c&p... I want you to benefit from learning this and discovering the answer as much on your own as you can... Good luck :)
     
  6. ll_llniQue

    ll_llniQue Private E-2

    Thank you so much :]
     
  7. ll_llniQue

    ll_llniQue Private E-2

    :D Hello

    I want to thank you so much :eek: i was able to solve it because of your help thank you alot :D ....


    Have a look

    Code:
    import javax.swing.JOptionPane;
    import java.lang.Math;
    
    class LearnToMultiply
    {
        public static void main (String args[])
    	{
         
         int num1,num2,product;
        int count, inputVal;
        String input;
        
        
           // pick two random integers from 1 to 10
           num1 = 1 + (int)(Math.random() * 10);
           num2 = 1 + (int)(Math.random() * 10);
           product = num1 * num2;
    
           // ask user for product
           count = 0; 
           
          
           do
           {
               input = JOptionPane.showInputDialog("How much is " + num1 +
                                                          " times " + num2 + "?");
               inputVal = Integer.parseInt(input);
               count++;
               
               if(inputVal == product)
                   JOptionPane.showMessageDialog(null, "Excellent, you got it in " +
                                                 count + " attempts.");
               else
                   JOptionPane.showMessageDialog(null, "No, try again.");
           }
           while(inputVal != product);
    
           System.exit(0);
       }
    }
    
    
    
     
  8. QuickSilver

    QuickSilver Corporal

    Excellent work - you did the work on your own - I just tried to change the way you were lookign at the problem and how to implement it.
    Well done
     
  9. ll_llniQue

    ll_llniQue Private E-2

    Thank you so much :) am so happy .. if you didn't help me i might not have done it thank you :D
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds