赵云在蜀国的地位:作业不会啊~~~麻烦各位哥哥姐姐帮忙!JAVA编程题,

来源:百度文库 编辑:中科新闻网 时间:2024/04/27 23:45:42
1:定义一个类,类中有两个整型变量x和y,并定义构造函数初始化这两个变量。类中还定义以下方法:求两个数的和(x+y)并返回结果,求两个数的差(x-y)并返回结果,求两个数的的乘积(x*y)并返回结果,求两个数的商(x/y)并返回结果,求两个睡的余(x%y)并返回结果,求两个数的最大值并返回结果,求两个数的最小值并返回结果。
编写应用程序,测试上面定义的类,使用类中定义的各个方法并将其结果输出。
public class Class1 {
public static void main(String[] args) {
//定义几个数
int x = 37,y = 42,maxValue,minValue;
System.out.println("变量数值...");
System.out.println(" x = " + x);
System.out.println(" y = " + y);
//加
System.out.println("加...");
System.out.println(" x + y = " + (x + y));
//减
System.out.println("减...");
System.out.println(" x - y = " + (x - y));
//乘
System.out.println("乘...");
System.out.println(" x * y = " + (x * y));
//除
System.out.println("除...");
System.out.println(" x / y = " + (x / y));
//从除法中求得余数
System.out.println("计算余数...");
System.out.println(" x % y = " + (x % y));
//最大值
System.out.println("最大值...");
System.out.println(maxValue=Math.max(x,y));
//最小值
System.out.println("最小值...");
System.out.println(minValue=Math.min(x,y));
}
}

帮我看看这个行不?

public class IntegerTest
{
int x;
int y;

public IntegerTest(int x,int y)
{
this.x=x;
this.y=y;
}

public int sum()
{
return this.x+this.y;
}

public int minus()
{
return this.x-this.y;
}

public int multiply()
{
return this.x*this.y;
}

public int chu()
{
return this.x/this.y;
}

public int mod()
{
return this.x%this.y;
}

public int max()
{
if(this.x>=this.y)
{
return this.x;
}
return this.y;
}

public int min()
{
if(this.x<=this.y)
{
return this.x;
}
return this.y;
}

public static void main(String args[])
{
IntegerTest i=new IntegerTest(2,3);
System.out.println("x+y="+i.sum()+"\nx-y="+i.minus()+"\nx*y="+i.multiply()+"\nx/y="+i.chu()+"\nx%y="+i.mod()+"\n最大数为:"+i.max()+"\n最小数为:"+i.min());
}
}

class A
{
int x;
int y;
public A(int x1,int y1)
{
x=x1;
y=y1;
}

public int add(x1,y1)
{
return x1+y1;
}
public int add()
{
return add(x,y);
}
//写了加法后,减,乘,除法类似
//不过在除法的时候最好判断一下被除数是否是0

public static void main(String[] args)
{
A a=new A(10,15);
System.out.println(add(18,19));
//以下类似....
}
}