Question

Consider the interface Predicate defined as follows. interface Predicate { boolean eval(int j); } Recall that...

Consider the interface Predicate defined as follows.

interface Predicate { 
  boolean eval(int j);
}
  1. Recall that you can check if an integer “i” is even by using the expression “i%2 == 0”. Write a class IsEven that determines whether a number is even:
    class IsEven implements Predicate {
      public boolean eval(int j) {
    
    
    
    
    
    
       }
    } 
    
    For example, the following code
      Predicate p = new IsEven();
      if (  p.eval(2)) { System.out.println("2 is even"); }
      if (! p.eval(3)) { System.out.println("3 is not even");
    
    should produce the output
      2 is even
      3 is not even
    
  2. Write a class Alternate that alternates between true and false, starting with true. You may add fields if necessary.
    class Alternate implements Predicate {
    
    
    
      boolean eval(int j){
    
    
    
    
    
    
    
      }
    }
    
    For example, the following code
      Predicate p = new Alternate();
      in = new DataInputStream(System.in);  
    
      for (int k=0; k<4; k++){
        int j = in.readInt();  // read a number from the user
        if (p.eval(j)) {
          System.out.println("true");
        } else {
          System.out.println("false");
        }
      } 
    
    should produce the following output, no matter what input is given:
      true
      false
      true
      false
    
  3. Write a class Not that implements logical negation:
    class Not implements Predicate {
      Predicate _p;
    
      Not(Predicate p) { _p =p; }
      boolean eval(int j){
    
    
    
    
    
    
      }
    }
    
    For example,
      Predicate p = new IsEven();
      Predicate q = new Not(p);
      if (! q.eval(2)) { System.out.println("2 is even"); }
      if (  q.eval(3)) { System.out.println("3 is not even");
    
    should produce the same output as before, even though the negation operator (!) is moved with respect to question 1:
      2 is even
      3 is not even
    
    As another example, the code
      Predicate p = new Alternate();
      Predicate q = new Not(p);
      in = new DataInputStream(System.in);  
    
      for (int k=0; k<4; k++){
        int j = in.readInt();  // read a number from the user
        if (q.eval(j)) {
          System.out.println("true");
        } else {
          System.out.println("false");
        }
      }  
    
    should produce the following output, no matter what input the user gives:
      false
      true
      false
      true
    
    Notice that the outputs are just flipped, from true to false and from false to true, in comparison with question 2.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
//Interface: Predicate.java
interface Predicate{
    boolean eval(int j);
}
----------------------------------------------------------------------
//Class: IsEven.java
class IsEven implements Predicate {
    @Override
    public boolean eval(int j) {
        if(j%2 == 0)
            return true;
        else
            return false;
    }
}
-------------------------------------------------------------
//Class: Alternate.java
class Alternate implements Predicate{

    static boolean flag;
    @Override
    public boolean eval(int j) {
        flag = !flag;
        return flag;
    }
}
----------------------------------------------------------------
//Class: Not.java
class Not implements Predicate{
    Predicate _p;

    Not(Predicate p){
        _p = p;
    }

    @Override
    public boolean eval(int j) {
        return !(_p.eval(j));
    }
}

//-------------------------------------------------------------------------------------------

//Class for executing input cases mentioned in question.

//Class: Main.java

import java.util.Scanner;
class Main{

    static void question1(){
        Predicate p = new IsEven();
        if (  p.eval(2)) {
            System.out.println("2 is even");
        }
        if (! p.eval(3)) {
            System.out.println("3 is not even");
        }
    }

    static void question2(){
        Predicate p = new Alternate();
        Scanner scanner = new Scanner(System.in);

        for (int k=0; k<4; k++){
            int j = scanner.nextInt();  // read a number from the user
            if (p.eval(j)) {
                System.out.println("true");
            } else {
                System.out.println("false");
            }
        }
    }

    static void question3(){
        Predicate p = new Alternate();
        Predicate q = new Not(p);
        Scanner scanner = new Scanner(System.in);

        for (int k=0; k<4; k++){
            int j = scanner.nextInt();  // read a number from the user
            if (q.eval(j)) {
                System.out.println("true");
            } else {
                System.out.println("false");
            }
        }
    }

    public static void main(String[] args) {
          question1();
        //question2();
        //question3();
    }
}
Add a comment
Know the answer?
Add Answer to:
Consider the interface Predicate defined as follows. interface Predicate { boolean eval(int j); } Recall that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then...

    java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then output comes like this 5 4 3 2 1 Stack is empty. here is the code that i'm going to use class Stack<T> implements Iterator<T> {    LinkedList<T> list;       public Stack() {        list = new LinkedList<T>();    }       public boolean isEmpty() {        return list.isEmpty();   ...

  • abstract class Exp { abstract void print(); abstract int eval(); } class ConstExp extends Exp {...

    abstract class Exp { abstract void print(); abstract int eval(); } class ConstExp extends Exp { private int value; ConstExp(int v) { value = v; } void print() { System.out.print(value); } int eval() { return value; } } class BinaryExp extends Exp { private Exp arg1; private Exp arg2; private char op; BinaryExp(char op, Exp arg1, Exp arg2) { this.op = op; this.arg1 = arg1; this.arg2 = arg2; } void print() { System.out.print("("); arg1.print(); System.out.print(" " + op + "...

  • Write an interface and two classes which will implements the interface. 1. Interface - StringVerification -...

    Write an interface and two classes which will implements the interface. 1. Interface - StringVerification - will have the abstract method defined - boolean verifyInput( String input ); 2. class EmailVerification implements StringVerification - implement the verifyInput() method in EmailVerification class. The method should validate a user input String, character by character and see the input string has a dot (.) and an at the rate (@) character in it. If any of the condition wrong the verifyInput method will...

  • Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs...

    Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs the user’s choice of the following functions the program exits should also be a user’s choice. Menu Item 1: Fibonacci number iterator, here you are to define an iterator class named FibonacciIterator for iterating Fibonacci numbers. The constructor takes an argument that specifies the limit of the maximum Fibonacci number. For example, prompt the user for size, use the size to call FibonacciIterator(“user input”)...

  • х CO1109 ZB 2019-2020 Q... UL20/0576 Page 10 of 22 (c) Consider the Bow Tie class,...

    х CO1109 ZB 2019-2020 Q... UL20/0576 Page 10 of 22 (c) Consider the Bow Tie class, below import java.util.Scanner; class Bowtie public static void main(Stringil args) Scanner in - new Scanner(System.in); System.out.print("Enter Size"); int size-in.nextInt(): int x = 1 int midpoint - size/ 21 if (isize 2) -- 0) midPoint- for (int i=0; i<sizes i++) for(int -0; j<size: j++){ 18 (//missing boolean condition System.out.print(""); else System.out.print(""); > 1 (midpoint) *** 1/ height of shapes whose size is an odd number...

  • CHALLENGE ACTIVITY 3.11.1: Using boolean. D Assign is Teenager with true if kidAge is 13 to...

    CHALLENGE ACTIVITY 3.11.1: Using boolean. D Assign is Teenager with true if kidAge is 13 to 19 inclusive. Otherwise, assign is Teenager with false. 1 import java.util.Scanner; 3 public class TeenagerDetector 1 public static void main (String [] args) { Scanner scnr = new Scanner(System.in); boolean isTeenager; int kidAge; D}]oll kidage = scnr.nextInt(); /* Your solution goes here */ Go USB if (isTeenager) { System.out.println("Teen"); else { System.out.println("Not teen"); 20 Run Feedback? CHALLENGE ACTIVITY 3.11.2: Boolean in branching statements. Write...

  • Can anyone add the 1) add_to_coef, add, multiply, coefficient, eval and equals method in the main...

    Can anyone add the 1) add_to_coef, add, multiply, coefficient, eval and equals method in the main function..so can take it from text file.... or 2) make main function with user input for all methods mentioned in the program. import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; public class Polynomial { //array of doubles to store the coefficients so that the coefficient for x^k is stored in the location [k] of the array. double coefficients[]; static int size; //fube the following...

  • (a)How many times does the code snippet given below display "Hello"? int x = 1; while...

    (a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) {    System.out.println ("Hello");    x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) {    sum = sum + i;    i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...

  • cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /**...

    cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /** * default constructor -- * Creates an matrix that has 2 rows and 2 columns */ public Matrix(int [][] m) { boolean valid = true; for(int r = 1; r < m.length && valid; r++) { if(m[r].length != m[0].length) valid = false; } if(valid) matrix = m; else matrix = null; } public String toString() { String output = "[ "; for (int i...

  • Consider the Automobile class: public class Automobile {    private String model;    private int rating;...

    Consider the Automobile class: public class Automobile {    private String model;    private int rating; // a number 1, 2, 3, 4, 5    private boolean isTruck;    public Automobile(String model, boolean isTruck) {       this.model = model;       this.rating = 0; // unrated       this.isTruck = isTruck;    }        public Automobile(String model, int rating, boolean isTruck) {       this.model = model;       this.rating = rating;       this.isTruck = isTruck;    }                public String getModel()...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT