Starting from:

$30

CS451/651-Project 1 Supporting Simple Operations Solved

Problem 1. (Using CLEmitter) Consider the following program IsPrime.java that receives an integer n as command-line argument and prints whether or not n is a prime number.

// IsPrime.java

public class IsPrime {

// Returns true if n is prime, and false otherwise. private static boolean isPrime(int n) { if (n < 2) {

return false;

}

for (int i = 2; i <= n / i; i++) { if (n % i == 0) { return false;

}

}

return true;

}

// Entry point.

public static void main(String[] args) { int n = Integer.parseInt(args[0]); boolean result = isPrime(n); if (result) {

System.out.println(n + " is a prime number");

}

else {

System.out.println(n + " is not a prime number");

}

}

}
Using the annotated program GenFactorial.java under $j/j--/tests/clemitter as a model, complete the implementation of the program GenIsPrime.java that uses the CLEmitter interface to programmatically generate IsPrime.class, ie, the JVM bytecode for the program IsPrime.java above.

$ javac -cp .:$j/j--/lib/j--.jar GenIsPrime.java

$ java -cp .:$j/j--/lib/j--.jar GenIsPrime

$ java IsPrime 42

42 is not a prime number

$ java IsPrime 31

31 is a prime number

Hints: There are two ways to approach this problem, the first being more intellectually rewarding.

1.   The bytecode for GenIsPrime.main() is similar to the bytecode for GenFactorial.main(). Here are some hints for generating bytecode for the isPrime() method:

if n = 2 goto A: return false

A: i = 2

D: if i n / i goto B: if n % i != 0 goto C: return false

C: increment i by 1 goto D:

B: return True
2.   Compile IsPrime.java using javac, and decompile (using javap) IsPrime.class to get the bytecode javac generated and mimic the same in GenIsPrime.

Problem 2. (Division Operation) Follow the process outlined in Section 1.5 of our text to implement the Java division operator /.

$ $j/j--/bin/j-- tests/Division.java

$ java Division 42 6

7

Problem 3. (Remainder Operation) Implement the Java remainder operator %.

$ $j/j--/bin/j-- tests/Remainder.java

$ java Remainder 42 13

3

Problem 4. (Shift Operations) Implement the Java shift operators: arithmetic left shift <<, arithmetic right shift , logical right shift .

$ $j/j--/bin/j-- tests/ArithmeticLeftShift.java

$ java ArithmeticLeftShift 1 5

32

$ $j/j--/bin/j-- tests/ArithmeticRightShift.java

$ java ArithmeticRightShift 32 5

1

$ java ArithmeticRightShift -32 5 -1

$ $j/j--/bin/j-- tests/LogicalRightShift.java

$ java LogicalRightShift 32 5

1

$ java LogicalRightShift -32 5

134217727

Problem 5. (Bitwise Operations) Implement the Java bitwise operators: unary complement ~, inclusive or |, exclusive or ^, and &. Note: there are JVM instructions for |, ^, and &, but not for ~, which must be computed as the “exclusive or” of the operand and -1.

$ $j/j--/bin/j-- tests/BitwiseNot.java

$ java BitwiseNot 42

-43

$ $j/j--/bin/j-- tests/BitwiseInclusiveOr.java

$ java BitwiseInclusiveOr 3 5 7

$ $j/j--/bin/j-- tests/BitwiseExclusiveOr.java

$ java BitwiseExclusiveOr 3 5 6

$ $j/j--/bin/j-- tests/BitwiseAnd.java

$ java BitwiseAnd 3 5

1

Problem 6. (Unary Plus Operation) Implement the Java unary plus operaor +.

$ $j/j--/bin/j-- tests/UnaryPlus.java

$ java UnaryPlus -42

More products