Results 1 to 2 of 2
  1. #1
    maddiexc is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Help with using methods (Begginer)

    We're supposed to use methods/functions in our program, but im having trouble incorporating them into it. My program is like a math game, asking simple addition and addition/subtraction questions. I want all the calculations to be in methods but i'm having trouble returning a whole equation back into the main method (sorry if that doesnt make sense, maybe the code will help better).

    I need some assistance when it comes to the direction I need to go in order to use methods effectively in my program, and where I should do my calculations! As well as what should be in its own methods and what should be in the main method.
    Thanks so much for the help!


    Java Code:
    import javax.swing.*;
    import java.io.*;
    
    
    public class SummativeTwo
    {
      public static void main (String[] args) throws IOException
      {
        BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));
        {
          
          Object[] options = {"Yes! I'm Excited!","Im kind of nervous..","Now Way! Ewww!"};
          int n = JOptionPane.showOptionDialog(null, "Welcome Million Dollar Madness! "+ "Are you ready to play?","Welcome",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[2]);
          if (n == JOptionPane.YES_OPTION) {
            JOptionPane.showMessageDialog(null,"Okie Dokie! Lets get started!", "Okie Dokie", JOptionPane.INFORMATION_MESSAGE);
          }
          if (n == JOptionPane.NO_OPTION) {
            JOptionPane.showMessageDialog(null,"Aw thats okay! Im sure you'll have fun!", "It's okay", JOptionPane.INFORMATION_MESSAGE);
          }
          if (n == JOptionPane.CANCEL_OPTION) {
            JOptionPane.showMessageDialog(null,"Well too bad so sad! You're going to have to play anyways!", "Too bad so sad", JOptionPane.INFORMATION_MESSAGE);
          }
          
          Object[] optionsA = {"Yes Please", "Nope! I'm good!"};
          int x = JOptionPane.showOptionDialog(null,"Would you like to know the instructions for this game?","INSTRUCTIONS?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,optionsA,optionsA[0]); 
          if (x == JOptionPane.YES_OPTION) {
            JOptionPane.showMessageDialog(null,"There will be 3 levels in this game. As you go farther, the questions get harder and harder and each question \n. Each time you get a question right you will recieve a certain amount of money, which increases each time you pass each question. \n But be careful, because if you get 3 questions wrong in one level \n it's GAME OVER for you! Meaning you lose all your money. \n You may Cash Out at any point if you're not willing to risk losing the amount of money you have. Reach the end of \n the 3 levels and you take home all your money! \n GOOD LUCK!", "Instructions", JOptionPane.INFORMATION_MESSAGE);
          }
          if (x == JOptionPane.NO_OPTION) {
            JOptionPane.showMessageDialog(null,"Suit Yourself! Lets Start!", "Your Choice", JOptionPane.INFORMATION_MESSAGE);
          }
          JOptionPane.showMessageDialog(null,"Welcome to Level: 1 " ," level 1", JOptionPane.INFORMATION_MESSAGE);
          
          //LEVEL ONE
          int wrong = 0;
          int run = 1;
          int questionnum = 0;
          int amount = 500;
          
          while (run <= 6 && wrong != 3)
          {
            run ++;
            questionnum++;
            int increment = 150;
            amount += 150;
            
            int numOne = additionOne ();
            int numTwo = additionTwo ();
            int answer = numOne + numTwo;
            
            
            String useranswerA = JOptionPane.showInputDialog(null,"Question #" + questionnum + " is for: $" + increment + "\n" + numOne + " + " + numTwo + " = ?", "Question", JOptionPane.INFORMATION_MESSAGE);
            useranswerA = myInput.readLine ();
            int useranswer = Integer.parseInt(useranswerA);
            
            if (useranswer != answer)
            {
              wrong ++;
              JOptionPane.showMessageDialog(null,"You got the wrong answer! \n The correct answer is: " + answer + " \n Questions Wrong: " + wrong, "Wrong Answer", JOptionPane.INFORMATION_MESSAGE);
              int y = JOptionPane.showOptionDialog(null,"CASH OUT with a total of $" + amount + "?","Cash Out?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,optionsA,optionsA[0]);
              if (y == JOptionPane.YES_OPTION) {
                JOptionPane.showMessageDialog(null,"Thanks for Playing!", "Thank You!", JOptionPane.INFORMATION_MESSAGE);
                System.exit(0);
              }
              if (y == JOptionPane.NO_OPTION) {}
            }
            else if (useranswer == answer)
            {
              JOptionPane.showMessageDialog(null,"Correct!", "Right Answer", JOptionPane.INFORMATION_MESSAGE);
              int y = JOptionPane.showOptionDialog(null,"CASH OUT with a total of $" + amount + "?","Cash Out?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,optionsA,optionsA[0]);
              if (y == JOptionPane.YES_OPTION) {
                JOptionPane.showMessageDialog(null,"Thanks for Playing!", "Thank You!", JOptionPane.INFORMATION_MESSAGE);
                System.exit(0);
              }
              if (y == JOptionPane.NO_OPTION) {}
            }
          }
          JOptionPane.showMessageDialog(null,"LEVEL ONE COMPLETE!", "LEVEL 1", JOptionPane.INFORMATION_MESSAGE);
          JOptionPane.showMessageDialog(null,"Cash on Hand: $ " + amount, "Cash", JOptionPane.INFORMATION_MESSAGE);
          int y = JOptionPane.showOptionDialog(null,"CASH OUT with a total of $" + amount + "?","Cash Out?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,optionsA,optionsA[0]);
          if (y == JOptionPane.YES_OPTION) {
            JOptionPane.showMessageDialog(null,"Thanks for Playing!", "Thank You!", JOptionPane.INFORMATION_MESSAGE);
            System.exit(0);
          }
          if (y == JOptionPane.NO_OPTION) {}
          
          //LEVEL TWO
          JOptionPane.showMessageDialog(null,"Welcome to Level: 2 " ," level 2", JOptionPane.INFORMATION_MESSAGE);
          int wrongB = 0;
          int runB = 1;
          int questionnumB = 0;
          
          
          while (runB <= 8 && wrongB != 3)
          {
            runB ++;
            questionnumB++;
            amount += 250;
            
            int increment = 250;
            
            int numOne = additionOne ();
            int numTwo = additionTwo ();
            int numThree = numThree ();
            int answer = numOne + numTwo - numThree;
            
            
            String useranswerA = JOptionPane.showInputDialog(null,"Question #" + questionnum + " is for: $" + increment + "\n" + numOne + " + " + numTwo + " - " + numThree + " = ?", "Question", JOptionPane.INFORMATION_MESSAGE);
            useranswerA = myInput.readLine ();
            int useranswer = Integer.parseInt(useranswerA);
            
            if (useranswer != answer)
            {
              wrong ++;
              JOptionPane.showMessageDialog(null,"You got the wrong answer! \n The correct answer is: " + answer + " \n Questions Wrong: " + wrongB, "Wrong Answer", JOptionPane.INFORMATION_MESSAGE);
              int z = JOptionPane.showOptionDialog(null,"CASH OUT with a total of $" + amount + "?","Cash Out?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,optionsA,optionsA[0]);
              if (z == JOptionPane.YES_OPTION) {
                JOptionPane.showMessageDialog(null,"Thanks for Playing!", "Thank You!", JOptionPane.INFORMATION_MESSAGE);
                System.exit(0);
              }
              if (z == JOptionPane.NO_OPTION) {}
            }
            else if (useranswer == answer)
            {
              JOptionPane.showMessageDialog(null,"Correct!", "Right Answer", JOptionPane.INFORMATION_MESSAGE);
              int z = JOptionPane.showOptionDialog(null,"CASH OUT with a total of $" + amount + "?","Cash Out?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,optionsA,optionsA[0]);
              if (z == JOptionPane.YES_OPTION) {
                JOptionPane.showMessageDialog(null,"Thanks for Playing!", "Thank You!", JOptionPane.INFORMATION_MESSAGE);
                System.exit(0);
              }
              if (z == JOptionPane.NO_OPTION) {}
            }
          }
          JOptionPane.showMessageDialog(null,"LEVEL TWO COMPLETE!", "LEVEL 2", JOptionPane.INFORMATION_MESSAGE);
          int z = JOptionPane.showOptionDialog(null,"CASH OUT with a total of $" + amount + "?","Cash Out?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,optionsA,optionsA[0]);
          if (z == JOptionPane.YES_OPTION) {
            JOptionPane.showMessageDialog(null,"Thanks for Playing!", "Thank You!", JOptionPane.INFORMATION_MESSAGE);
            System.exit(0);
          }
          if (y == JOptionPane.NO_OPTION) {}
        }
      }
      
      public static int additionOne ()
      {
        int x = (int)( Math.random () * 30);
        return x;
      }
      public static int additionTwo ()
      {
        int x = (int)( Math.random () * 30);
        return x;
      }
      public static int numThree ()
      {
        int x = (int) (Math.random () * 15);
        return x;
      }
    }

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Re: Help with using methods (Begginer)

    The first thing I notice is that additionOne() and additionTwo() are identical. This defeats the purpose of methods - delete one, and call the remaining method both times. In fact, numThree() can even be combined with these methods by supplying a parameter. It would look something like this

    public static int randInt(int bound)
    {
    return (int)(Math.random() * bound);
    }

    That being said, you can also create a Random object and just ask it for random integers as well.

    Random rand = new Random();
    int a = rand.nextInt(30);
    int b = rand.nextInt(30);
    int c = rand.nextInt(15);

    Anyway, back to methods. A method should be used for anything that will be reused many times, and (usually) is more complicated than just 1 or 2 lines of code. For example, creating a method to see if two ints are equal is pointless because you can just say if(a==b). So my question is, which "calculations" do you want to move to another method?

    Perhaps consider creating a method that accepts a difficulty level as a parameter and does all of the question asking within that method, then returns true or false based on whether or not the question was answered correctly.

    Another method might be used to compute the amount of money received for answering a question correctly given a certain level.
    Last edited by AndrewM16921; 06-17-2013 at 04:12 AM.

Similar Threads

  1. begginer needs help..?
    By lina in forum New To Java
    Replies: 4
    Last Post: 01-15-2010, 05:48 PM
  2. Any ideas on Program for begginer?
    By SwEeTAcTioN in forum New To Java
    Replies: 5
    Last Post: 11-02-2009, 02:45 AM
  3. Begginer and project
    By Gym in forum New To Java
    Replies: 6
    Last Post: 03-24-2009, 02:30 AM
  4. Student--begginer--help!!!
    By AmandaIT in forum New To Java
    Replies: 6
    Last Post: 05-09-2008, 04:37 AM
  5. Tic Tac Toe Ggame Help (Begginer)
    By zero2008 in forum New To Java
    Replies: 6
    Last Post: 01-11-2008, 11:18 PM

Tags for this Thread

Posting Permissions

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