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();   ...

  • What is wrong with my code? Trying to fix the Boolean situation and it keeps skipping...

    What is wrong with my code? Trying to fix the Boolean situation and it keeps skipping my boolean question and repeating. import java.util.Scanner; public class MyTest{       public static void main (String [] args){               Scanner input = new Scanner(System.in);               System.out.println("JAVA QUIZ");        System.out.println("This quiz includes three questions about the Java Programming Language.");        System.out.println("Each question has 4 possible answers. (numbered 1,2,3,4)");        System.out.println("Enter 0 to exit the test.");...

  • х 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...

  • 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”)...

  • 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...

  • 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()...

  • Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a...

    Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a string s as a parameter and returns true if the title of the string is equal to s. Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class MY CODE **************************************************************** LibraryCard: import java.util.List; import java.util.ArrayList; import java.time.LocalDate; import    java.time.temporal.ChronoUnit; public class LibraryCard {    private String id;    private String cardholderName;   ...

  • Here is everything I have on my codes, the compiler does not allow me to input...

    Here is everything I have on my codes, the compiler does not allow me to input my name, it will print out "Please enter your name: " but totally ignores it and does not allow me to input my name and just proceeds to my result of ID and Sale. Please help. I've bolded the part where it I should be able to input my name package salesperson; public class Salesperson { String Names; int ID; double Sales; // craeting...

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