Question

package homework1;
//author Kyle Fields


public class HomeWork1{


public static void main(String[] args) {

 int [ ] input = { 100, 37, 49 };

boolean result1 = contains( input, new Prime( ) );
boolean result2 = contains( input, new PerfectSquare( ) );
boolean result3 = contains( input, new Negative( ) );
System.out.println(result1);
System.out.println(result2);
System.out.println(result3);

}



 static boolean contains(int[] array, Condition condition) {
   return (condition(array));

}

}



package homework1;

/**
 *
 * @author Kyle Fields
 */
public interface Condition {

    boolean makeYourCondition(int[] input);

}


package homework1;

/**
 *
 * @author Kyle Fields
 */
public class Prime implements Condition {


@Override
public boolean makeYourCondition(int[] input) {
        for (int n : input) { 
        if (n >= 2) {            
        if (n == 2) {
            return true;
        }
        for (int i = 2; i <= Math.sqrt(n) + 1; i++) {
            if (!(n % i == 0)) {
                return true;
            }
        }
}
}
return false;
}

    }

other classes below

package homework1;

/**
 *
 * @author Kyle Fields
 */
public class PerfectSquare implements Condition {

@Override
public boolean makeYourCondition(int[] input) {

    for (int i : input) {
//takes the square root
long SquareRoot = (long) Math.sqrt(i);
//multiplys the sqrt by the sqrt to see if it equals the original
 if (((SquareRoot * SquareRoot) == i) == true){
 return true;
 }

    }
return false;

    }
}



package homework1;

/**
 *
 * @author Kyle Fields
 */
public class Negative implements Condition {


boolean Negative(int n){
 if (n <= -1){
 return true;
 }
 return false;
}

@Override
public boolean makeYourCondition(int[] input) { 

    for (int i : input) {
        if(i<0) return true;
    }

       return false;
    }

}

my question is this, how do I finish this code? meaning: what do I need to do for my contains method? (currently, it is telling me the method condition(int[]) is not a valid method in the homework1 class.)

Was it helpful?

Solution 2

You can write the code as follow, using the contract of an interface to do the job, look after methods that attend your condition.

public class HomeWork {
    public static void main(String[] args) {

        int[] arr=new int[] {100, 37, 49};

        Condition[] conditions= new Condition[]{
                                                new Negative(), 
                                                new Prime(), 
                                                new PerfectSquare()
                                            };

        for (Condition condition : conditions) {
            System.out.println(condition.makeYourCondition(arr));
        }
    }
}

interface Condition {
    boolean makeYourCondition(int[] input);
}

class Negative implements Condition {

    @Override
    public boolean makeYourCondition(int[] input) {

        for (int i : input) {
            if(i<0) return true;
        }

        return false;
    }

}

class Prime implements Condition {

    @Override
    public boolean makeYourCondition(int[] input) {

        //TODO PUT YOUR CONDITION

        return false;
    }

}

class PerfectSquare implements Condition {

    @Override
    public boolean makeYourCondition(int[] input) {

        //TODO PUT YOUR CONDITION

        return false;
    }

}

OTHER TIPS

dummy code is fine as long as you know what you're doing. First the contains() method, it takes an array and a Condition and returns a boolean. Let's write the signature

boolean contains(int[] array, Condition condition)

Prime, PerfectSquare, and Negative will be implementations of Condition, i.e.

interface Condition {
...
}

class Prime implements Condition {
...
}

class PerfectSquare ...

The setup of the exercise hints that in the Condition implementations you should check whether the argument int value satisfies the particular case; the contains() method iterates through the array and returns if it encounters a "true" or "false" if exhausts the list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top