2、 编写一个类,该类有个方法public int f(int a,int b),该方法返回a和b的最大公约数。然后再编写一个该
编写一个类,该类有个方法publicintf(inta,intb),该方法返回a和b的最大公约数。然后再编写一个该类的子类,要求子类重写方法f,而且重写的方法将返回a和b...
编写一个类,该类有个方法public int f(int a,int b),该方法返回a和b的最大公约数。然后再编写一个该类的子类,要求子类重写方法f,而且重写的方法将返回a和b的最小公倍数。要求在重写的方法体中首先调用被隐藏的方法返回a和b的最大公约数m,然后将乘积(a*b)/m返回。要求先用程序的主类中分别使用父类和子类创建对象,并分别调用方法f计算两个正整数的最大公约数和最小公倍数。
发送QQ:461332757 展开
发送QQ:461332757 展开
3个回答
展开全部
木紫棘 11:25:17
Euclid:公约数的类
Eucild_son:公倍数的类
public class Euclid
{
public static int gongyueshu;
public static int num1,num2;
public int getGongyueshu(){
gongyueshu=f(num1,num2);
return gongyueshu;
}
public static void setGongyueshu(int a,int b){
num1=a;
num2=b;
}
public static int f(int a, int b) //公约数
{
if (0 == a)
{
return b;
}
if (0 == b)
{
return a;
}
if (a > b)
{
swap(a, b);
}
int c;
for (c = a % b; c > 0; c = a % b)
{
a = b;
b = c;
}
return b;
}
public static void swap(int a, int b)
{
int c = a;
a = b;
b = c;
}
}
木紫棘 11:25:39
公倍数:
public class Euclid_son extends Euclid {
public static void main(String args[])
{
Euclid a=new Euclid();
a.setGongyueshu(6,8);
Euclid_son b=new Euclid_son();
System.out.println("The 1st number is " + a.num1);
System.out.println("The 2nd number is " + a.num2);
System.out.println("The result of gongyueshu is " + a.getGongyueshu());
System.out.println("The result of gongbeishu is " + b.f(num1, num2));
}
public static int f(int c,int d){
return num1*num2/Euclid.f(c,d);
}
}
Euclid:公约数的类
Eucild_son:公倍数的类
public class Euclid
{
public static int gongyueshu;
public static int num1,num2;
public int getGongyueshu(){
gongyueshu=f(num1,num2);
return gongyueshu;
}
public static void setGongyueshu(int a,int b){
num1=a;
num2=b;
}
public static int f(int a, int b) //公约数
{
if (0 == a)
{
return b;
}
if (0 == b)
{
return a;
}
if (a > b)
{
swap(a, b);
}
int c;
for (c = a % b; c > 0; c = a % b)
{
a = b;
b = c;
}
return b;
}
public static void swap(int a, int b)
{
int c = a;
a = b;
b = c;
}
}
木紫棘 11:25:39
公倍数:
public class Euclid_son extends Euclid {
public static void main(String args[])
{
Euclid a=new Euclid();
a.setGongyueshu(6,8);
Euclid_son b=new Euclid_son();
System.out.println("The 1st number is " + a.num1);
System.out.println("The 2nd number is " + a.num2);
System.out.println("The result of gongyueshu is " + a.getGongyueshu());
System.out.println("The result of gongbeishu is " + b.f(num1, num2));
}
public static int f(int c,int d){
return num1*num2/Euclid.f(c,d);
}
}
展开全部
发了,查收一下
#include <iostream>
using namespace std;
#define M 6
#define N 10
class test {
public:
virtual int func(int a, int b) {
if(a%b == 0) return b;
return func(b, a%b);
}
};
class test_s : public test {
public:
int func(int a, int b) {
test t;
return a * b / t.func(a, b);
}
};
int main() {
test t1;
cout << M << "和" << N << "最大公约数是:" << t1.func(M, N) << endl;
test_s t2;
cout << M << "和" << N << "最小公倍数是:" << t2.func(M, N) << endl;
return 0;
}
#include <iostream>
using namespace std;
#define M 6
#define N 10
class test {
public:
virtual int func(int a, int b) {
if(a%b == 0) return b;
return func(b, a%b);
}
};
class test_s : public test {
public:
int func(int a, int b) {
test t;
return a * b / t.func(a, b);
}
};
int main() {
test t1;
cout << M << "和" << N << "最大公约数是:" << t1.func(M, N) << endl;
test_s t2;
cout << M << "和" << N << "最小公倍数是:" << t2.func(M, N) << endl;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询