Consider the interface Predicate defined as follows.
interface Predicate {
boolean eval(int j);
}
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
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
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 evenAs 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 trueNotice that the outputs are just flipped, from true to false and from false to true, in comparison with question 2.
//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();
}
}
Consider the interface Predicate defined as follows. interface Predicate { boolean eval(int j); } Recall that...
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 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, 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 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 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 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; /** * 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; // 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 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 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...