Results 1 to 8 of 8

Thread: java question

  1. #1
    joe98 is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default java question

    Write a method that takes 2 integer parameters, and prints to the screen the factorial of
    the smallest of the two given parameter values subtract the larger integer value. Your
    method should be called from the main method, which passes some specific parameter
    values.
    For example, if the two parameter values given to your method is 6 and 4, then the
    smallest of the parameters is 4, and the biggest is 6, so your method should calculate and
    print to the screen the value of 4! 6, which is equal to 18

    Heres my solution so far.ny1 have any clues thnx



    class ....
    {
    public static void main(String[] args)
    {
    int a=2;
    int b=1;
    calculateFactorial(a,b);
    }
    public static void calculateFactorial(int a, int b)
    {
    int answer=1;
    if (a<b)
    for (int i=0; i<a; a=a-1)
    { answer = answer*a;
    answer= answer-b;
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,365
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by joe98 View Post
    Write a method that takes 2 integer parameters, and prints to the screen the factorial of
    the smallest of the two given parameter values subtract the larger integer value. Your
    method should be called from the main method, which passes some specific parameter
    values.
    For example, if the two parameter values given to your method is 6 and 4, then the
    smallest of the parameters is 4, and the biggest is 6, so your method should calculate and
    print to the screen the value of 4! – 6, which is equal to 18

    Heres my solution so far.ny1 have any clues thnx



    class ....
    {
    public static void main(String[] args)
    {
    int a=2;
    int b=1;
    calculateFactorial(a,b);
    }
    public static void calculateFactorial(int a, int b)
    {
    int answer=1;
    if (a<b)
    for (int i=0; i<a; a=a-1)
    { answer = answer*a;
    answer= answer-b;
    }
    }
    What does your program do if a >= b? No factorial will be calculated at all. In the Math class there's are methods that find the minimum and maximum of two values. Before you start your calculations in your method do this:

    Java Code:
    int min= Math.min(a, b);
    int max= Math.max(a, b);
    ... after that you calculate the factorial of variable 'min' and subtract the value of 'max' from the result.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    You need to subtract value after completing the for loop

    check the below code

    Java Code:
    public class TestFactorial
    {
    	public static void main(String[] args)
    	{
    		int a=4;
    		int b=6;
    		calculateFactorial(a,b);
    	}
    	
    	public static void calculateFactorial(int a, int b)
    	{
    		int answer=1;
    		if (a<=b){
    			for (int i=1; i<=a; a=a-1)
    			{ 
    				answer = answer*a;
    			}
    
    		answer = answer - b;
    		}else if (b<a)
    		{
    			for (int i=1; i<=b; b=b-1)
    			{ 
    				answer = answer*b;
    			}
    
    		answer = answer - a;
    		}
    
    		System.out.println(answer);
    	}
    }
    sanjeev,संजीव

  4. #4
    joe98 is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    but it doesnt work..i need to get 4!-6=18....ie (4x3x2x1)-6=18 also for a negative result it should not be printed

  5. #5
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    I think it is working

    what is error or result when you run the program
    sanjeev,संजीव

  6. #6
    joe98 is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    its working but,
    when i put int a=3 b=3 i get a negative answer, i dont want a negative answer just 0..
    ie. If both parameters have the same value, or if the smaller parameter value is negative, then
    nothing should be printed to the screen. You should write this (and more, such as
    describing what the method does in general) as a method header comment, above your
    method.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,365
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sanjeevtarar View Post
    Java Code:
    			for (int i=1; i<=a; a=a-1)
    			{ 
    				answer = answer*a;
    			}
    
    [ ... ]
    			for (int i=1; i<=b; b=b-1)
    			{ 
    				answer = answer*b;
    			}

    Change those two multiplication lines to:

    Java Code:
    answer= answer*i;
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,365
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by joe98 View Post
    its working but,
    when i put int a=3 b=3 i get a negative answer, i dont want a negative answer just 0..
    ie. If both parameters have the same value, or if the smaller parameter value is negative, then
    nothing should be printed to the screen. You should write this (and more, such as
    describing what the method does in general) as a method header comment, above your
    method.
    You should (and can) write those simple tests yourself; also it's you who has to write the method descriptions; it's your homework, not ours.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 33
    Last Post: 01-21-2011, 03:07 PM
  2. Need help with java question
    By ccie007 in forum New To Java
    Replies: 23
    Last Post: 05-18-2010, 06:32 PM
  3. Java Question :D
    By thisisIT in forum New To Java
    Replies: 6
    Last Post: 03-12-2010, 04:04 PM
  4. question about java rmi
    By hakimade in forum Advanced Java
    Replies: 1
    Last Post: 07-01-2009, 07:15 AM
  5. Java Question
    By Jay-1.1 in forum New To Java
    Replies: 11
    Last Post: 05-01-2008, 04:04 PM

Posting Permissions

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