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 10 of 10

Thread: Confused on how to use multiple Methods...

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    17
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Confused on how to use multiple Methods...

    Working on an assignment for school, cant figure out what i need to do to move on in this project...

    We are suppose to implement these exact methods into my calculator program.
    public static int getMenuOption()
    public static double getOperand(String prompt)
    public static double add (double operand1, double operand2)
    public static double subtract (double operand1, double operand2)
    public static double multiply(double operand1, double operand2)
    public static double divide(double operand1, double operand2)
    public static double random(double operand1, double operand2)

    Now my issue is that i cant seem to figure out how to get the get menu option to choose the static double, maybe im not fully understanding the question that is being asked by my teacher, but he seems hard to get ahold of.

    here is my current code...
    import java.util.Scanner;
     
    public class CaluclatorWithMethods {
    	public static void main(String[] args) {
     
    		System.out.println(" Menu: ");
    		System.out.println("1. Add ");
    		System.out.println("2. Sub ");
    		System.out.println("3. Multiply ");
    		System.out.println("4. Divide ");
    		System.out.println("5. Generate a Random Number");
    		Scanner input = new Scanner(System.in);
    		System.out.println(" What would you like to do?");
    		int getMenuOption = input.nextInt();
    	}
     
    	public static int getMenuOption( int 1, int 2, int 3, int 4, int 5){
    		if (getMenuOption == 1 ) { 
    			return add();
    		}
    		if (getMenuOption == 2) { 
    			return subtract();
    		}
    		if (getMenuOption == 3 ) { 
    			return multiply();
    		}
    		if (getMenuOption == 4 ) { 
    			return divide();
    		}
    		if (getMenuOption == 5 ) { 
    			return random();
    		}
    	}
     
     
     
     
     
     
    	public static double getOperand(String prompt) {
    		Scanner input = new Scanner(System.in);
    		double operand1 = getOperand("What is the First number? ");
    		operand1 = input.nextDouble();
    		double operand2 = getOperand("What is the Second number? ");
    		operand2 = input.nextDouble();
    		return operand1;
    	}
     
    	public static double add(double operand1, double operand2) {
    		getOperand(null);
    		double answer = operand1 + operand2;
    		return answer;
    	}
     
    	public static double subtract(double operand1, double operand2) {
    		getOperand(null);
    		double answer = operand1 - operand2;
    		return answer;
    	}
     
    	public static double multiply(double operand1, double operand2) {
    		getOperand(null);
    		double answer = operand1 * operand2;
    		return answer;
    	}
     
    	public static double divide(double operand1, double operand2) {
    		getOperand(null);
    		double answer = operand1 / operand2;
    		return answer;
    	}
     
    	public static double random(double lowerLimit, double upperLimit) {
    		Scanner input = new Scanner(System.in);
    		double operand1 = getOperand("What is the Lower Limit? ");
    		operand1 = input.nextDouble();
    		double operand2 = getOperand("What is the Upper Limit? ");
    		operand2 = input.nextDouble();
    		double answer = (double) (Math.ceil(Math.random()
    				* (upperLimit - lowerLimit)) + lowerLimit);
    		return answer;
    	}
     
    }

    My instructions state this "
    In the case of subtraction, you would want to do something like this :
    double operand1 = getOperand("What is the First number? ");
    double operand2 = getOperand("What is the Second number? ");
    // call your subtract method and pass it these inputs
    "

    Any help would be grateful!
    Last edited by Famous112; September 18th, 2014 at 11:47 AM. Reason: Edited current code with updates.


  2. #2
    Junior Member
    Join Date
    Sep 2014
    Posts
    21
    My Mood
    Cheeky
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Confused on how to use multiple Methods...

    Sounds like you are looking for an if statement.
    or for extra credit, a switch.
    You have enumerated the options, and stored the received integer value.

    if value is 1 then do addition
    if value is 2 then do subtraction

    Of course, then you also need to call your getOperands somehow.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    21
    My Mood
    Cheeky
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Confused on how to use multiple Methods...

    let me psuedocode the logic for you and see if you can connect it together

    begin
    print(options)
    print("Select an option")
    value = selection chosen
     
    is value a number?
      if no, start over
     
    is value between 1 and 5?
      if no, start over
     
    if value is 1 do addition
    if value is 2 do subtraction
    end

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Confused on how to use multiple Methods...

    This is an odd statement:

    int getMenuOption = input.nextInt();

    especially since you have a method called, "getMenuOption()". The method should be used to obtain and return the user's menu choice, something like:

    int usersChoice = getMenuOption();

    Note that your getMenuOption() signature does not comply with the assignment's specification.

  5. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    17
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Confused on how to use multiple Methods...

    Ive changed it alittle now...
    import java.util.Scanner;
     
    public class CaluclatorWithMethods {
    	public static void main(String[] args) {
     
    		System.out.println(" Menu: ");
    		System.out.println("1. Add ");
    		System.out.println("2. Sub ");
    		System.out.println("3. Multiply ");
    		System.out.println("4. Divide ");
    		System.out.println("5. Generate a Random Number");
    		Scanner input = new Scanner(System.in);
    		System.out.println(" What would you like to do?");
    		int usersChoice = getMenuOption();
    	}
     
    	public static int getMenuOption(){
    		if  ( usersChoice == 1 ) { 
    			return getOperand();
    			return add();
    		}
    		if ( usersChoice == 2) { 
    			return getOperand();
    			return subtract();
    		}
    		if ( usersChoice == 3) { 
    			return getOperand();
    			return mulitply();
    		}
    		if ( usersChoice == 4){ 
    			return getOperand();
    			return divide();
    		}
    		if ( usersChoice == 5) { 
    			return random();
    		}
    	    if ( usersChoice < 1) { 
    			return main();
    		}
    	    if ( usersChoice > 5) { 
    			return main();
    	    }
    	}
     
     
     
     
    	public static double getOperand(String prompt) {
    		Scanner input = new Scanner(System.in);
    		double operand1 = getOperand("What is the First number? ");
    		operand1 = input.nextDouble();
    		double operand2 = getOperand("What is the Second number? ");
    		operand2 = input.nextDouble();
    		return operand1;
    	}
     
    	public static double add(double operand1, double operand2) {
    		getOperand(null);
    		double answer = operand1 + operand2;
    		return answer;
    	}
     
    	public static double subtract(double operand1, double operand2) {
    		getOperand(null);
    		double answer = operand1 - operand2;
    		return answer;
    	}
     
    	public static double multiply(double operand1, double operand2) {
    		getOperand(null);
    		double answer = operand1 * operand2;
    		return answer;
    	}
     
    	public static double divide(double operand1, double operand2) {
    		getOperand(null);
    		double answer = operand1 / operand2;
    		return answer;
    	}
     
    	public static double random(double lowerLimit, double upperLimit) {
    		Scanner input = new Scanner(System.in);
    		double operand1 = getOperand("What is the Lower Limit? ");
    		operand1 = input.nextDouble();
    		double operand2 = getOperand("What is the Upper Limit? ");
    		operand2 = input.nextDouble();
    		double answer = (double) (Math.ceil(Math.random()
    				* (upperLimit - lowerLimit)) + lowerLimit);
    		return answer;
    	}
     
    }

    getting closer... but it seems like it doesn't want to accept the user input.

  6. #6
    Junior Member
    Join Date
    Sep 2014
    Posts
    21
    My Mood
    Cheeky
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Confused on how to use multiple Methods...

    You aren't getting 50 errors for that?
    You are seriously cover complicating (and probably over thinking) the situation.

    Store user input as choice
    Validate choice
    Choose option based off of choice
    Ask for operands
    Do operation
    Print result

  7. #7
    Junior Member
    Join Date
    Sep 2014
    Posts
    17
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Confused on how to use multiple Methods...

    Quote Originally Posted by Hamenopi View Post
    You aren't getting 50 errors for that?
    You are seriously cover complicating (and probably over thinking) the situation.

    Store user input as choice
    Validate choice
    Choose option based off of choice
    Ask for operands
    Do operation
    Print result

    I am getting a good amount of errors... i think i really am overthinking this... I got put into a lower level programming class , I dont use java... I work with python...

    Ill take another look, thank you for the help. I finally got ahold of my teacher, hes gonna help me too.
    Will keep you guys updated !

  8. #8
    Junior Member
    Join Date
    Sep 2014
    Posts
    17
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Confused on how to use multiple Methods...

    import java.util.Scanner;
     
    public class CaluclatorWithMethods {
    // main method
    	public static void main(String[] args) {
    		// input scanner
    		Scanner stdIn = new Scanner(System.in);
     
    	int answer = showMenu(stdIn);
    	double operand1 = getOperand(stdIn);
    	double operand2 = getOperand(stdIn);
    	double problemAnswer = performOp (answer, operand1, operand2);
    	System.out.println(problemAnswer);
     
    	}
    	public static int showMenu (Scanner stdIn)
    	{
    		System.out.println("Menu:");
    		System.out.println("1. add");
    		System.out.println("2. subtract");
    		System.out.println("3. multiply");
    		System.out.println("4. divide");
    		System.out.println("5. random");
    		System.out.println("What would you like to do?");
    		int answer = stdIn.nextInt();
    		return answer;
    	}
    	public static double getOperand(Scanner stdIn)
    	{
    		System.out.println("What is the First Number?:");
    		System.out.println("What is the Second Number?:");
    		double operand = stdIn.nextDouble();
    		return operand;
    	}
    	public static double performOp (int answer, double operand1, double operand2)
    	{
    		double arithmatic = 0; //needs to be initialized outside of if statement
    		if (answer == 1)
    		{
    			arithmatic = add(operand1, operand2);
    		} else if (answer == 2)
    		{
    			 arithmatic = subtract(operand1, operand2);
    		} else if (answer == 3)
    		{
    			 arithmatic = multiply(operand1, operand2);
    		} else if (answer == 4)
    		{
    			 arithmatic = divide(operand1, operand2);
    		} else if 
    				(answer == 5)
    			 arithmatic = random(operand1, operand2);
     
    		return arithmatic;
    	}
    	public static double add(double operand1, double operand2)
    	{
    		double answer = operand1 + operand2;
    		return answer;
    	}
    	public static double subtract(double operand1, double operand2)
    	{
    		double answer = operand1 - operand2;
    		return answer;
    	}
    	public static double multiply(double operand1, double operand2)
    	{
    		double answer = operand1 * operand2;
    		return answer;
    	}
    	public static double divide(double operand1, double operand2)
    	{
    		double answer = operand1 / operand2;
    		return answer;
    	}
    	public static double random(double operand1, double operand2)
    	{
     
    		double answer = (double) (Math.ceil(Math.random()
    				* (operand2 - operand1)) + operand1);
    		return answer;
     
    	}
    }

    Finished... i changed ALOT in this... seems cleaner and works. i got help from my teacher... thanks guys!

  9. #9
    Junior Member
    Join Date
    Sep 2014
    Posts
    21
    My Mood
    Cheeky
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Confused on how to use multiple Methods...

    Much better!
    Cheers!

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Confused on how to use multiple Methods...

    Still lots of errors.

    The variable usersChoice is local to main() so invisible to getMenuOption().

    Two returns in a block results in unreachable code.

    Most of your method calls do not comply with the method signatures.

    And I'm so baffled by "return main()" that I can't continue. What are you trying to do with that statement? I'm beginning to think that you think a return statement is like a GOTO statement.

Similar Threads

  1. multiple methods in one class
    By wamidh in forum Java Theory & Questions
    Replies: 6
    Last Post: September 16th, 2014, 03:44 PM
  2. Confused about invoking methods?
    By ShamelessTeenie in forum Object Oriented Programming
    Replies: 5
    Last Post: April 9th, 2013, 07:50 PM
  3. Forming Multiple Methods - please help explain this for a newbie?
    By RedCloudTsunami in forum Java Theory & Questions
    Replies: 1
    Last Post: July 5th, 2012, 04:56 PM
  4. JOptionPane menu and multiple methods
    By kubera in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 3rd, 2012, 07:03 PM
  5. Multiple methods in the same inner class?
    By tommyf in forum What's Wrong With My Code?
    Replies: 12
    Last Post: January 24th, 2012, 12:10 PM