Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

what is the purpose of placing the method name before a numerical calculation


  • Please log in to reply
14 replies to this topic

#1 sidd0123

sidd0123

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:11:21 PM

Posted 27 March 2010 - 01:52 PM

For example:

if( given <= 1 ) // base case
return 1;
else
return given * factorial( given - 1 );


What is the significance of the bold part, because if remove it, i get an incorrect answer in my calculations

BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:09:21 PM

Posted 27 March 2010 - 02:18 PM

It's math. Google it. :thumbsup:

#3 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:11:21 PM

Posted 27 March 2010 - 05:37 PM

is there a name for doing something like that, because i have tried searching but it might help if i knew what this process is called

#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:09:21 PM

Posted 27 March 2010 - 05:45 PM

Oh? What did you use for search terms? Maybe I can help you refine your searching techniques. Being able to effectively form a query is a vital skill for a programmer.

#5 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:11:21 PM

Posted 27 March 2010 - 06:21 PM

is there anyone else that can help, I'm tired of being mocked because I am new to Java

#6 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:09:21 PM

Posted 27 March 2010 - 06:34 PM

You are NOT being mocked. You claimed that you tried to search, I offered to help you learn how to search better; all you needed to do was tell me what you searched for. Has you Googled for "what is a factorial", you would have found the answer in less time than it took you to ask the question. I knew you didn't do that, which is why I asked about your search efforts.

If you want us to spend our valuable time to answer your questions, then at least spend some time trying to figure it out on your own. If you think you are being treated badly, then you can go ask questions elsewhere. I guarantee that you will receive the same response. I don't get paid for answering questions, so unless you are willing to pay a consultation fee, you better learn to do some research on your own.

#7 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:11:21 PM

Posted 27 March 2010 - 07:15 PM

I do take the time to research the answers to my questions, but I only use the forums if I found something I can use but dont understand how to use it or what it does, or am stuck overall. Im not the best at this, but i do try to




I may have phrased my question incorrectly, I do know what a factorial does, (5! = 5 x 4 x 3 x2 x 1 = 120).

What I meant was, why does my answer change when i remove the word in bold from the code.

Edited by sidd0123, 27 March 2010 - 07:27 PM.


#8 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:09:21 PM

Posted 27 March 2010 - 07:24 PM

Because that is a method call. Some value (whatever given-1 is) is passed toa method called factorial. That method calculates the factorial and returns it. When you remove the method name, the program just returns given * (given-1).

#9 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:11:21 PM

Posted 27 March 2010 - 07:30 PM

Ok, i think i understand what is happening. I have a loop right before my the code i have in my first post. the method call will keep repeating until it hits the loops limit right?

#10 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:09:21 PM

Posted 27 March 2010 - 09:32 PM

I am not sure what is happening with the rest of your code because I haven't seen it. Given the little snippet of code that you have provided, and assuming that code is inside a loop, this is what happens (in pseudo-code)

If given is less than or equal to one, return one
else
return given times the difference of given minus one

Pseudo-code is a way of writing out program logic in such a way that it is easy to understand what you want to do, and at the same time, is just a short step away from writing code. Looking at the pseudo-code, it is easy to tell that you are using an if-else loop, and what the conditions are.

#11 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:11:21 PM

Posted 27 March 2010 - 09:50 PM

Here is my whole code, I am still working on the permutation and combinations methods. The code box doesn't seem to work on my laptop, so i put the code in bold

package package11;

public class Probability {
	
	static int n;

	public static String getAuthor(){
		
		return "";
	}
	
	public static int factorial(int given){
		
		for (int start = 1; start <= given; start = start + 1)
		{
			if( given <= 1 )	 
				return 1;
			else
				return given * factorial(given - 1);
		}
		
		n = given;
		
		return given;
	}
	
	public static int permutations(int firstnum, int secondnum){
		
		
		
		
		return firstnum;
	}
	
	public static int combinations(int first, int second){
		return first;
		
	}
	
}

Edited by groovicus, 27 March 2010 - 10:31 PM.
fixed code tags


#12 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:09:21 PM

Posted 27 March 2010 - 10:30 PM

As I suspected, your original code snippet is inside of a loop. The loop will repeat until the sentinel is hit, which in your case is when start is equal or greater than given.

#13 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:11:21 PM

Posted 27 March 2010 - 11:22 PM

Thanks, I should have the finished product soon

Edited by sidd0123, 27 March 2010 - 11:29 PM.


#14 sidd0123

sidd0123
  • Topic Starter

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:11:21 PM

Posted 28 March 2010 - 11:47 AM

Hey, it seems my program works good. I can't make them long because my teacher asked for int, but everything seems to be working good. I tested it for any irregularities and came up with none. Here is the finished code :thumbsup:


package package11;
// This is the package that holds my class and program

public class Probability {
// This is my class that holds my code

public static String getAuthor(){
// This is the method that will get and return the name of the author

return "*****";
// This will return my name in the forum of a string
}

public static int factorial(int given){
// This is the method that will calculate the factorial of the number provided by the user

for (int start = 1; start <= given; start = start + 1)
{
if( given <= 1 )
return 1;
else
return given * factorial(given - 1);
// This if-else statement will return a 1 if given is less than 1
// The else statement will calculate the factorial if the if statement proves false
}
return given;
}

public static int permutations(int n, int r){
// This is the method that will calculate the permutation of the two number provided by the user

return (n * factorial(n - 1)) / (factorial(n - r));
// This will calculate and return the permutation
}

public static int combinations(int n, int r){
// This is the method that will calculate the permutation of the two number provided by the user

return ((n * factorial(n - 1)) / factorial(n - r)) / (r * factorial(r - 1)/ 1);
// This will calculate and return the combination
}
}


#15 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:09:21 PM

Posted 28 March 2010 - 12:49 PM

Thank you for sharing.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users