Homework Help please (Java language) Exercise 5.4 Write a method public static int gcd (int one, int two) for MathOp: it returns the greatest common positive divisor of its two int parameters. Hint: if either is negative, multiply it by -1; then repeatedly replace the larger by the remainder from dividing the larger by the smaller until one goes evenly into the other, in which case that one will be the greatest common divisor. What do you do if either is zero? Object class in comments.
Object class: public class MathOp { /** Return the value of 2 to the power expo. */ public static int powerOf2 (int expo) { int power = 1; for (; expo > 0; expo--) power = power * 2; return power; } //====================== /** Return sum divided by count rounded to the nearest int. */ public static int average (int sum, int count) { if (sum >= 0) return (sum + count / 2) / count; else return (sum - count / 2) / count; } //====================== }
Hi Lyrae
Hi!
Having trouble writing the actual method. Istarted as: public static int gcd (int one, int two) { // initialise instance variables gcd = greatest common denominator int one = int two = if ( //int one ) return ( // ) else return ( // ) }
I need help filling in the blanks, maybe three of four blanks. What do you think of my start?
You cant re-declare one and two below your first comment, so you have to remove int from both of them.
Otherwise i think it's good start :) Well see what we can make from it!
Okay, I've done that. How do I go on next?
Since this exercise is on divisibility, I reminded myself that we can use numbers 1-12. Since we have that now, How can I put that into the blanks?
First of all do you remember/know of gcd?
yes, the greatest common divisor
There are some ways to calculate it and they give you a few hints of how to do it in the assignment description.
Can you shoe me an example, Its tought rom the object class.
First of all look at this piece of text: "Hint: if either is negative, multiply it by -1; then repeatedly replace the larger by the remainder from dividing the larger by the smaller until one goes evenly into the other, in which case that one will be the greatest common divisor." Lets break it down into parts: 1. if either is negative, multiply it by -1; 2. then repeatedly replace the larger by the remainder from dividing the larger by the smaller. Notice the keyword 'repeatedly' indicates we probably have to use a loop. And in that loop we should 'replace the larger by the remainder from dividing the larger by the smaller'. 3. until one goes evenly into the other, in which case that one will be the greatest common divisor. Termination condition of the loop 'until one goes evenly into the other'.
How's it going?
I have: public static int gcd (int one, int two) { // initialise instance variables gcd = greatest common divisor int gcd; one = 3; two = 45; if (two > one) one = two/= 15; // i was using an example of 45/3 = 15 // return ( // ) this line might be needed else one = two; return one; }
Okay! You're making A common mistake of using \(\it specific\) numbers when you should be working more generally! But let's work with those numbers, and use the rules @Lyrae posted from the algorithm description to understand what you need to do! We'll look at the numbers \(3\) and \(15\) 1. if either is negative, multiply it by -1. Neither \(3\) nor \(15\) is negative, so we move on. 2. then repeatedly replace the larger by the remainder from dividing the larger by the smaller. Notice the keyword 'repeatedly' indicates we probably have to use a loop. And in that loop we should 'replace the larger by the remainder from dividing the larger by the smaller'. Alright, now we'll replace the larger one, \(45\), by the "remainder from dividing (\(45\)) by (\(3\))." Dividing, \(45\div3=15\), so the remainder is \(0\). Now, we replace \(45\) with \(0\). Our two numbers are now \(3\) and \(0\). Now, they went evenly into each other, so we move on to step three. Otherwise, we would keep on repeating this step with new numbers. 3. until one goes evenly into the other, in which case that one will be the greatest common divisor. Since they went evenly into each other, the non-zero number, \(3\), is the greatest common divisor. Some things in these steps are 1. testing for negatives 2. not dividing by zero (if there is a zero at all, you have a problem. Report that, and quit) 2. figuring out which number is larger 2. getting the remainder (you have to "mod", or use the modulo operator, "%" 2. replacing the larger number with that remainder 2. repeating this step if the remainder is non-zero 3. finding which number is non-zero, because that is the GCD
I suggest you work this out with the numbers -21 and 14. I think that this will make you go through the entire process. After you know the process, you can start working more generally. That means that you will use variables, rather than numbers.
Join our real-time social learning platform and learn together with your friends!