Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.56 KB | None | 0 0
  1. public class Exam1 {
  2. public static void main (String[] args) {
  3. problem1();
  4. problem2();
  5. problem3();
  6. problem4();
  7. }
  8.  
  9. public static void problem1() {
  10. System.out.println("==================================================");
  11. System.out.println("Problem 1.");
  12. System.out.println("==================================================");
  13.  
  14. int A = 8 + 5 * 3 / 2;
  15. System.out.println("int A, " + A);
  16.  
  17. int B = 3 * (7 + 1) - 3 - 7 + 2;
  18. System.out.println("int B, " + B);
  19.  
  20. int C = 23 % 5 + 31 / 4 % 3 - 17 % (16 % 10);
  21. System.out.println("int C, " + C);
  22.  
  23. String D = "1" + 2 + 3 + "4" + 5 * 6 + "7" + (8 + 9);
  24. System.out.println("String D, " + D);
  25.  
  26. String E = 1 + 1 + "(8 ; 2)" + (8 - 2) + 1 + 1;
  27. System.out.println("String E, " + E);
  28.  
  29. double F = 29 / 4 / 2.0 + 18 / 5 + 1.5;
  30. System.out.println("double F, " + F);
  31.  
  32. double G = 5 / 2 + 123 / 10 / 10.0;
  33. System.out.println("double G, " + G);
  34.  
  35. int H = 13 % 5 + 43 % (11 % 3);
  36. System.out.println("int H, " + H);
  37.  
  38. int I = -(6 + 3 - 2 * 3);
  39. System.out.println("int I, " + I);
  40.  
  41. double J = 9 / 2 / 2.0 + 9 / 2.0 / 2;
  42. System.out.println("double J, " + J);
  43.  
  44. String K = 1 + "x" + 11 / 10 + " is" + 10 / 2;
  45. System.out.println("String K, " + K);
  46.  
  47. double L = 1 / 2 + -(157 / 10 / 10.0) + 9.0 * 1 / 2;
  48. System.out.println("double L, " + L);
  49.  
  50. String M = 2 + "(int)2.0" + 2 * 2 + 2;
  51. System.out.println("String M, " + M);
  52.  
  53. double N = Math.round(-6.4) + Math.ceil(6.5);
  54. System.out.println("double N, " + N);
  55.  
  56. double O = Math.floor(Math.max(Math.min(-5, 5.5), Math.max(-4.5, -6)));
  57. System.out.println("double O, " + O);
  58.  
  59. double P = Math.pow(3, 2);
  60. System.out.println("double P, " + P);
  61.  
  62. double Q = Math.ceil(5.2) + Math.floor(-5.2);
  63. System.out.println("double Q, " + Q);
  64.  
  65. double R = (double) 3 / 4 + (int) (5.0 / 6 + 1 / 6.0);
  66. System.out.println("double R, " + R);
  67. }
  68.  
  69. public static void problem2() {
  70. System.out.println("==================================================");
  71. System.out.println("Problem 2.");
  72. System.out.println("==================================================");
  73.  
  74. System.out.println("Part A.");
  75. int xa = 2;
  76. xa = -3;
  77. xa -= -5;
  78. xa++;
  79. System.out.println(xa);
  80.  
  81. System.out.println("Part B.");
  82. int xb = 4;
  83. xb++;
  84. int zb = xb + 2;
  85. double ab = zb / xb;
  86. ab -= 2 + xb;
  87. System.out.println(xb + " " + zb + " " + ab);
  88.  
  89. System.out.println("Part C.");
  90. int xc = 3;
  91. int yc = 4;
  92. String sc = "xc";
  93. xc++;
  94. sc = xc + yc + sc;
  95. System.out.println(xc + " " + sc);
  96.  
  97. System.out.println("Part D.");
  98. int yd = 2;
  99. for(int j = 0; j <= 3; j++) {
  100. int xd = 3 - j;
  101. yd = yd + xd;
  102.  
  103. }
  104. System.out.println(yd);
  105.  
  106. System.out.println("Part E.");
  107. int ae = 5;
  108. int be = 12;
  109. double xe = be / ae;
  110. int re = be % ae;
  111. System.out.println(re + " " + xe);
  112.  
  113. System.out.println("Part F.");
  114. for (int i = 1; i <= 5; i++) {
  115. for (int j = 0; j < 5; j++) {
  116. System.out.print("*");
  117.  
  118. }
  119. }
  120. System.out.println();
  121.  
  122. System.out.println("Part G.");
  123. for (int i = 1; i <= 10; i++) {
  124. for (int j = i; j <= 10; j++) {
  125. System.out.print("*");
  126. }
  127. }
  128. System.out.println();
  129.  
  130.  
  131. System.out.println("Part H.");
  132. for (int i = 7; i > -3; i--) {
  133. System.out.print("*");
  134. for (int j = 0; j < i; j++) {
  135. System.out.print("*");
  136. }
  137. System.out.println("*");
  138. }
  139.  
  140. System.out.println("Part I.");
  141. int x = 5;
  142. int y = 13;
  143. if (x % y == 3) {
  144. System.out.print("A");
  145. } else if (x + y > 15) {
  146. System.out.print("B");
  147. }
  148. if (x >= 5) {
  149. System.out.print("C");
  150.  
  151. } else {
  152. System.out.print("D");
  153. }
  154. System.out.println();
  155. }
  156.  
  157. public static void problem3() {
  158. System.out.println("==================================================");
  159. System.out.println("Problem 3.");
  160. System.out.println("==================================================");
  161.  
  162. System.out.println("Part A.");
  163. System.out.println("for (int i = 0; i < da; i++) {");
  164. System.out.println("error: variable i is already defined in method");
  165. /* int aa = 12; */
  166. /* int i = 31; */
  167. /* double da = i * aa++ - 2; */
  168. /* for (int i = 0; i < da; i++) { */
  169. /* System.out.print("*"); */
  170. /* } */
  171.  
  172. System.out.println("Part B.");
  173. System.out.println("int by = Math.pow(_bx, bz);");
  174. System.out.println("error: incompatible types: possible lossy conversion");
  175. /* int _bx; */
  176. /* _bx = 5; */
  177. /* double bz = 3; */
  178. /* int by = Math.pow(_bx, bz); */
  179. /* System.out.print(bz + " " + by * bz); */
  180.  
  181. System.out.println("Part C.");
  182. System.out.println("double 2x = 2 * cx;");
  183. System.out.println("error: not a statement");
  184. /* int cx = 5; */
  185. /* double 2x = 2 * cx; */
  186. /* String cs = "CS" + 2x; */
  187. /* System.out.print(cx + " " + cs); */
  188.  
  189. System.out.println("Part D.");
  190. System.out.println("sum /= i;");
  191. System.out.println("error: variable sum might not have been initialized");
  192. /* double sum; */
  193. /* int dz = 5; */
  194. /* for (int i = 1; i <= dz; i++) { */
  195. /* sum /= i; */
  196. /* } */
  197. /* System.out.println(sum); */
  198.  
  199. System.out.println("Part E.");
  200. System.out.println("if (ez = 5)");
  201. System.out.print("error: incompatible types: double cannot be ");
  202. System.out.println("converted to boolean");
  203. /* int ea = -3; */
  204. /* double ez = 5 * ea; */
  205. /* if (ez = 5) { */
  206. /* System.out.print(ez); */
  207. /* } */
  208. }
  209.  
  210. public static class Mystery {
  211. public static void doStuff() {
  212. int one = 4;
  213. int two = 3;
  214. int three = 10;
  215. int num = 17;
  216. int four = 3;
  217. racket(one, two, three);
  218. racket(three, four, 5);
  219. racket(2, two * 2, num);
  220. racket(num, three * one, four);
  221. racket(three - four, one, two + 8);
  222. }
  223.  
  224. public static void racket(int two, int one, int three) {
  225. System.out.println(three + " is roughly " + two + " plus " + one);
  226. if (one + two > three) {
  227. bark();
  228. } else if (one >= three - two) {
  229. sneeze();
  230. } else {
  231. holler();
  232. }
  233. System.out.println();
  234. }
  235.  
  236. public static void holler() {
  237. System.out.println(" YES");
  238. bark();
  239. sneeze();
  240. }
  241.  
  242. public static void bark() {
  243. sneeze();
  244. System.out.println(" NO");
  245. }
  246.  
  247. public static void sneeze() {
  248. System.out.println(" maybe");
  249. }
  250. }
  251.  
  252. public static void problem4() {
  253. System.out.println("==================================================");
  254. System.out.println("Problem 4.");
  255. System.out.println("==================================================");
  256.  
  257. Mystery.doStuff();
  258. }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement