修改分数类和测试驱动程序JAVA

时间:2014-09-22 17:23:32

标签: java

我的教授创建了需要修改的代码。唯一的问题是我自己不是一个相当新的程序员而根本不理解他的风格。赋值的参数如下:
•修改设置器,使它们忽略不适当的值(即除以零)
•实现从顶级Object类继承的equals()方法 •实施少于和多于方法
•实施加,减和乘法方法

•确保对于算术上相等的任何两个分数,equals方法返回true •确保equals方法不会改变要比较的分数的值 •lessThan和greaterThan方法必须各自返回一个布尔值,而不是字符串 •提供的reduce方法返回一个新的(减少的)分数对象作为其函数值

我完全迷失了这项任务,因为我没有丝毫线索从哪里开始。任何和所有的帮助将不胜感激!!!!我觉得,一旦我看到它完成,它对我来说都是有意义的。我根本不习惯这种教学方式。

public class Fraction {
        private int numer;
    	private int denom;
    
    	public Fraction() { // no-arg constructor
       		numer = 0;
        	denom = 1;
	}
    
    	public Fraction(int numer, int denom) {
                this.numer = numer;
        	this.denom = denom;
        }
    	public Fraction(Fraction frac) { // copy constructor
        	numer = frac.getNumer();
        	denom = frac.getDenom();
    	}

     	// getters and setters
    	public int getNumer() {
        	return numer;
    	}
    
    	public void setNumer(int x) {
        	numer = x;
    	}
    

        public int getDenom()  {
        	return denom;
    	}
    
    	public void setDenom(int x) {
            denom = x;
    	}
   
 	// Special Methods
    	public String toString() {
        	return numer + "/" + denom;
    	}

    	// Other Methods
    	public Fraction reduce() {
        	Fraction temp = new Fraction();
        
        	int GCD = gcd(numer, denom);
        
        	temp.setNumer(numer / GCD);
        	temp.setDenom(denom / GCD);
        
        	return  temp;
    	}
 	
	 // Private Methods
    	private int gcd(int n1, int n2)
    	{
        	int M, N, R;
        
        	if (n1 < n2)
        	{
            		N = n1;
            		M = n2;
        	}
        	else
        	{
            		N = n2;
           	 	M = n1;
        	}
        
        	R = M % N;
        
        	while (R != 0)
        	{
            		M = N;
            		N = R;
            
            		R = M % N;
        	}
        
        	return N;
        
        }
 

    public static void main(String[] args) {
    // test constructors
        	Fraction frac0 = new Fraction();
        	System.out.println("TESTING NO-ARG CONSTRUCTOR");
		System.out.println("frac0: Result should be 0/1:");
        	System.out.println("Numer = " + frac0.getNumer());
        	System.out.println("Denom = " + frac0.getDenom());
        
        	System.out.println("TESTING int/int CONSTRUCTOR");
        	Fraction frac1 = new Fraction(2,4);
		System.out.println("frac1: Result should be 2/4:");
        	System.out.println("Numer = " + frac1.getNumer());
        	System.out.println("Denom = " + frac1.getDenom());
        
        	System.out.println("TESTING Fraction CONSTRUCTOR");
        	Fraction frac2 = new Fraction(frac1);
		System.out.println("frac2: Result should be 2/4:");
        	System.out.println("Numer = " + frac2.getNumer());
        	System.out.println("Denom = " + frac2.getDenom());

		System.out.println("TESTING COPY CONSTRUCTOR frac1  frac2");
        	if (frac1.getNumer() == frac2.getNumer() &&
            		frac1.getDenom() == frac2.getDenom() &&
              		frac1 != frac2)
        	{
            		System.out.println("Copy constructor working");
        	}
        	else
            		System.out.println("PROBLEM with copy constructor");

        
        	// test equal method
        	System.out.println("TESTING EQUALITY OF frac1 and frac2 -");
		System.out.println("SHOULD BE FOUND EQUAL:");
		if (frac1.equals(frac2))
        	{
            		System.out.println("frac1 and frac2 found equal");
        	}
        	else
        	{
            		System.out.println("frac1 and frac2 NOT equal");
        	}

            	// test reduce method
        	System.out.println("TESTING reduce METHOD ON frac1");
		Fraction reduced_frac1 = frac1.reduce();
        	System.out.println("Reduced frac1 = " + reduced_frac1);
        
        	// test getters and setters
        	frac2.setNumer(8);
        	frac2.setDenom(12);
        	System.out.println("Numer = " + frac2.getNumer());
        	System.out.println("Denom = " + frac2.getDenom());
        
        	// System.out.println("GCD of 2/4 = " + frac1.gcd(1,4));
       
    	}

	//* TO BE COMPLETED *
}

1 个答案:

答案 0 :(得分:0)

他的教学方法没有任何问题,经过深入研究,我相信你能搞清楚。这里没有人会为你做这件事,我不想做你的功课,所以我会问一个常见的问题,你到目前为止尝试了什么?我给你一个修改过的setter。继续工作,更好地学习你的java,否则你将很难遇到困难。

//Here is where you start
public void setDenom(int x){

    if(x > 0){
       denom = x;
    }else{
       //throw an error
    }

}