Guest User

Untitled

a guest
Dec 13th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. Total points: 100
  2. You must work on this assignment ALONE. It is split into two parts. Submit them separately following the instructions for each part.
  3. Part 1 (50 points)
  4. a. (40 points) What is the output of the following Java program? You must type your answer on
  5. a separate sheet with your name on it. For full credit, match the program’s output EXACTLY,
  6. including white space, newline characters, upper/lowercase characters, vertical alignment, etc.
  7. Important advice: To better prepare for quizzes and exams, solve this problem on paper, without
  8. typing, compiling and running the Java program. Recall that Java uses 32-bit integers.
  9. class Logical
  10. {
  11. static String hex(int x)
  12. {
  13. return Integer.toHexString(x).toUpperCase();
  14. }// hex method
  15. static void print_values(int x)
  16. {
  17. System.out.format("x = %d (base 10) = 0x%s\n", x, hex(x));
  18. System.out.format("Line 1: %6s = %11d (dec.) = 0x%s\n",
  19. "x<<1" , x<<1 , hex(x<<1));
  20. System.out.format("Line 2: %6s = %11d (dec.) = 0x%s\n",
  21. "x<<2" , x<<2 , hex(x<<2));
  22. System.out.format("Line 3: %6s = %11d (dec.) = 0x%s\n",
  23. "x<<31", x<<31, hex(x<<31));
  24. System.out.println();
  25. System.out.format("Line 4: %6s = %11d (dec.) = 0x%s\n",
  26. "x>>1" , x>>1 , hex(x>>1));
  27. System.out.format("Line 5: %6s = %11d (dec.) = 0x%s\n",
  28. "x>>2" , x>>2 , hex(x>>2));
  29. System.out.format("Line 6: %6s = %11d (dec.) = 0x%s\n",
  30. "x>>31", x>>31, hex(x>>31));
  31. System.out.println();
  32. System.out.format("Line 7: %6s = %11d (dec.) = 0x%s\n",
  33. "x>>>1", x>>>1 , hex(x>>>1));
  34. System.out.format("Line 8: %6s = %11d (dec.) = 0x%s\n",
  35. "x>>>2", x>>>2 , hex(x>>>2));
  36. System.out.format("Line 9: %6s = %11d (dec.) = 0x%s\n",
  37. "x>>>31", x>>>31, hex(x>>>31));
  38. System.out.println("\n");
  39. }// print_values method
  40. public static void main(String[] args)
  41. {
  42. print_values( 14 );
  43. print_values( -14 );
  44. }// main method
  45. }// Logical class
Add Comment
Please, Sign In to add comment