Using java relay and method rewriting to calculate the maximum common approx. And the minimum public multiple

Source: Internet
Author: User
Tags greatest common divisor

Use java relay and method rewriting to calculate the maximum common approx. And the minimum common multiple. the background teacher assigned several java programming questions in the class. This is one of them. write a class in the topic content. This class has a public int f (int a, int B) method, and returns the maximum public approx. of a and B. Then write a subclass derived from this class, override the f method of the ancestor, and return the minimum public multiple of a and B. When the child class overrides the parent class method, it is required that the f method of the parent class be called first to obtain the maximum public approx. m, and then use the formula (a * B)/m to obtain the minimum public multiple. Finally, write a test program to call the methods of the parent class and subclass respectively. 3. code and explanation [java] <span style = "font-family: SimSun; font-size: 14px"> package Three;/*** @ author Kun Sun * @ Date: 2013.10.15 */public class Gcd {// maximum Common Divisor class, named from the first letter (Greatest Common Divisor) public int f (int a, int B) {if (a <B) {// ensure that a is the maximum value int temp = a; a = B; B = temp;} while (B> 0) {// calculate the maximum public approx. if (a = B) {return a ;}else {int temp = a % B; a = B; B = temp ;}} return ;}} </span> [java] <span style = "font-family: SimSun; font-size: 14px"> package Three;/*** @ author Kun Sun * @ Date: 2013.10.15 */public class Lcm extends Gcd {// minimum public Multiple class, named from the first letter (Lowest Common Multiple) public int f (int a, int B) {// method rewriting, returns the smallest public multiple Gcd gcd = new Gcd (); int m = gcd. f (a, B); int result = a * B/m; return result ;}</span> [java] <span style = "font-family: SimSun; font-size: 14px "> package Three;/*** @ author Kun Sun * @ Date: 2013.10.15 */public class MainClass {// used to test the maximum public approx. class and minimum public multiples class/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub Gcd gcd = new Gcd (); int result1 = gcd. f (12, 24); System. out. println (result1); Lcm lcm = new Lcm (); int result2 = lcm. f (12, 24); System. out. println (result2) ;}</span> 4. test Run result

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.