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

Thread: REQUEST : Making a Basic program which creates two method and calls them in main method

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Location
    India
    Posts
    18
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default REQUEST : Making a Basic program which creates two method and calls them in main method

    Write a program which adds or subtract two number
    here are condition:-
    1. make a method which will add two no.
    2. make a method which subtract the two no.
    3. ask user if they wanna add or subtract and then call the method needed as per need
    i am deeply confused and will really appreciate anyone who will make this basic script from which i can observe my mistake
    here is my code for reference if you need but it is not working
    import java.util.Scanner;
    public class sum {
    	static Scanner input = new Scanner(System.in);
    	public static double addition(double s1 ,double s2 ) {
    		double sum1 = s1 + s2;
    		return sum1;
    	}
        public static double subtraction(double s1, double s2) {
    double sum1 = s1-s2;
    return sum1;
    }
        public static void main(String args[])
    {
    	double n1, n2, sum;
    	System.out.println("welcome do you want to add or divide");
    	String s = input.next();
    	if (s.equalsIgnoreCase("add") || (s.equalsIgnoreCase("addition")))
    			{
    		System.out.println("enter the two numbers");
    		n1 = input.nextDouble();
    		n2 = input.nextDouble();
    		 sum = addition(n1, n2);
    		System.out.println(sum);
    	}
    	else if (s.equalsIgnoreCase("minus") || (s.equalsIgnoreCase("subtraction")))
    	{
    		System.out.println("enter the two numbers");
    		n1 = input.nextDouble();
    		n2 = input.nextDouble();
    		 sum = subtraction(n1, n2);
    		System.out.println(sum);
    	}
     
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: REQUEST : Making a Basic program which creates two method and calls them in main method

    it is not working
    Please explain.
    If there are error messages, copy the full text and paste it here.
    If the output is not what you want, copy and paste the output here and show what the desired output is.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    kukupie123 (March 30th, 2019)

  4. #3
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: REQUEST : Making a Basic program which creates two method and calls them in main method

    Except for the problem that you prompt to add or divide and not add or subtract, the program works fine for me. What problem are you supposedly having?

    Regards,
    Jim

  5. The Following User Says Thank You to jim829 For This Useful Post:

    kukupie123 (March 30th, 2019)

  6. #4
    Junior Member
    Join Date
    Mar 2019
    Location
    India
    Posts
    18
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: REQUEST : Making a Basic program which creates two method and calls them in main method

    hello thanks for the response this thread has been solved, genuinely appreciate the answers
    I don't really know how to mark this thread as solved so I am just gonna leave it this way

Similar Threads

  1. Replies: 1
    Last Post: May 27th, 2014, 07:39 PM
  2. Replies: 2
    Last Post: May 27th, 2014, 12:36 PM
  3. Replies: 4
    Last Post: December 9th, 2013, 10:40 AM
  4. Replies: 3
    Last Post: October 31st, 2011, 12:42 AM
  5. Paint program adding classes to main method class
    By Maxfmc in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 15th, 2011, 07:01 PM

Tags for this Thread