Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.41 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3. import java.math.*;
  4. import java.text.DecimalFormat;
  5.  
  6. public class Main {
  7.  
  8. private static boolean str1;
  9.  
  10. public static void main(String[] args) { // String[] is a parameter and public static void
  11. // main(String[] args) is a header
  12. // TODO Auto-generated method stub
  13.  
  14. // I'm Joseph Cisar and welcome to my integration project! This project is a
  15. // combination of my programming exercises and code demonstrated in module resources.
  16. // The main set-up is a main menu. By entering the number of your choice, the option will
  17. // appear.
  18. // Use ctrl + shift + f to auto-format jjjjj
  19. System.out.println("Welome to my Integration Project!");
  20. int x = 4;
  21. int y = 4;
  22. int weeks = x + y;
  23. System.out.println(
  24. "This project will show you " + weeks + " weeks of what Java I have learned so far.");
  25. System.out.println(
  26. "In the box below, select any of the following to learn or to use a program made with Java.");
  27. // List of Data Types:
  28. // Integer (int) is used for whole numbers and will return a whole number during division.
  29. // Double (double) is used for decimal values and will return a decimal in division.
  30. // Boolean The boolean data type has only two possible values: true and false.
  31. // Use this data type for simple flags that track true/false conditions.
  32. // String (str) represents character strings. Strings are constant and cannot be changed after
  33. // being created.
  34.  
  35. System.out.println("------------------------------------------------------------");
  36. System.out.println("| 1. Basic Calculator |");
  37. System.out.println("| 2. Use relational and conditional operators |");
  38. System.out.println("| 3. Increment and decrement operators |");
  39. System.out.println("| 4. compareTo() and equalTo() strings |");
  40. System.out.println("| 5. Math/Random class example |");
  41. System.out.println("| 6. Switch statements |");
  42. System.out.println("| 7. Casting example |");
  43. System.out.println("| 8. Final example |");
  44. System.out.println("| 9. Escape equences |");
  45. System.out.println("| 10. View code: Create a method with arguments and return |");
  46. System.out.println("| 11. View code: 3 string methods |");
  47. System.out.println("------------------------------------------------------------");
  48.  
  49. Scanner sc = new Scanner(System.in);
  50. System.out.println("Enter choice here: ");
  51. int mainMenuChoice = sc.nextInt();
  52.  
  53. while (mainMenuChoice > 0) {
  54.  
  55. if (mainMenuChoice == 1) {
  56.  
  57. System.out
  58. .println("Enter a number in the box below to chose what arthimic operation you want.");
  59. System.out.println("----------------------");
  60. System.out.println("| 1. Addition |");
  61. System.out.println("| 2. Subtraction |");
  62. System.out.println("| 3. Multiplication |");
  63. System.out.println("| 4. Division |");
  64. System.out.println("| 5. Module |");
  65. System.out.println("----------------------");
  66.  
  67. int num = sc.nextInt();
  68.  
  69. if (num == 1) {
  70.  
  71. // Operator Precedence:
  72. // In Java, operator precedence is when two operators share an operand the operator with
  73. // the higher precedence goes first
  74. // For example, multiplication and division have a higher precedence than addition and
  75. // subtraction -- another example is PEMDAS
  76. // Precedence rules can be overridden by parentheses and arrays
  77. System.out.println("Enter two decimal numbers to find the sum.");
  78. System.out.println("Number 1: ");
  79. double input1 = sc.nextDouble();
  80. System.out.println("Number 2: ");
  81. double input2 = sc.nextDouble();
  82. System.out.println(input1 + input2); // (input1 + input2) is an argument
  83. } else if (num == 2) {
  84. System.out.println("Enter two decimal numbers to find the difference.");
  85. System.out.println("Number 1: ");
  86. double input1 = sc.nextDouble();
  87. System.out.println("Number 2: ");
  88. double input2 = sc.nextDouble();
  89. System.out.println(input1 - input2);
  90. } else if (num == 3) {
  91. System.out.println("Enter two decimal numbers to find the product.");
  92. System.out.println("Number 1: ");
  93. double input1 = sc.nextDouble();
  94. System.out.println("Number 2: ");
  95. double input2 = sc.nextDouble();
  96. System.out.println(input1 * input2);
  97. } else if (num == 4) {
  98. System.out.println("Enter two decimal numbers to find the quotient.");
  99. System.out.println("Number 1: ");
  100. double input1 = sc.nextDouble();
  101. System.out.println("Number 2: ");
  102. double input2 = sc.nextDouble();
  103. System.out.println(input1 / input2);
  104. } else if (num == 5) {
  105. System.out.println("Enter two decimal numbers to find the difference.");
  106. System.out.println("Number 1: ");
  107. double input1 = sc.nextDouble();
  108. System.out.println("Number 2: ");
  109. double input2 = sc.nextDouble();
  110. System.out.println(input1 % input2);
  111. }
  112. }
  113.  
  114. else if (mainMenuChoice == 2) {
  115.  
  116. System.out.println("In the box below are relational and conditional statements.");
  117. // Relational and conditional statements are: <, >, ==, >=, <=, !=
  118. System.out.println("Enter two numbers and see if the first number you entered is: ");
  119. System.out.println(
  120. "greater than (>), less than (<), equal to (=), greater than or equal to (>=), less than or equal to (<=), or not equal (!=)");
  121. // >, <, <=,>= are relational
  122. // != and == are equality
  123. System.out.println("Enter two integers: ");
  124. System.out.println("Number 1: ");
  125. int input1 = sc.nextInt();
  126. System.out.println("Number 2: ");
  127. int input2 = sc.nextInt();
  128. if (input1 > input2) {
  129. System.out.println(input1 + " is greater than " + input2);
  130. }
  131. if (input1 < input2) {
  132. System.out.println(input1 + " is less than " + input2);
  133. }
  134. if (input1 == input2) {
  135. System.out.println(input1 + " is equal than " + input2);
  136. }
  137. if (input1 <= input2) {
  138. System.out.println(input1 + " is less than or equal to " + input2);
  139. }
  140. if (input1 >= input2) {
  141. System.out.println(input1 + " is greater than or equal to " + input2);
  142. }
  143. if (input1 != input2) {
  144. System.out.println(input1 + " is not equal to " + input2);
  145. } else {
  146. System.out.println("Incorrect input. Please try again.");
  147. }
  148. } else if (mainMenuChoice == 3) {
  149.  
  150. System.out.println("Increment Operator example:");
  151. System.out.println("View code for explanation.");
  152. int a = 5, b = 5;
  153.  
  154. System.out.println(++a); // outputs 6, ++ increments the value of a and then returns a
  155. System.out.println(a); // outputs 6
  156.  
  157. System.out.println(b++); // outputs 5, ++ returns the value of b and then increments
  158. System.out.println(b); // outputs 6
  159.  
  160. System.out.println("Compound Assignment Operator example");
  161. System.out.println("View code for explanation.");
  162. int c = 2;
  163. // c += 3 is the equivalent to c = c + 3
  164. c += 3; // c now equals 5
  165. System.out.println(c);
  166.  
  167. System.out.println("Decrement Operator example");
  168. System.out.println("View code for explanation.");
  169. int d = 8;
  170.  
  171. System.out.println(d--); // outputs 8, -- decrements the value of d and then returns d
  172. System.out.println(d); // outputs 7
  173. break;
  174. } else if (mainMenuChoice == 4) {
  175.  
  176. System.out.println("Enter 1 or 2 to compare strings or to see if the strings are equal.");
  177. int value = sc.nextInt();
  178. System.out.println("----------------------------");
  179. System.out.println("| 1. compareTo() strings |");
  180. System.out.println("| 2. equalTo() strings |");
  181. System.out.println("----------------------------");
  182.  
  183. if (value == 1) {
  184.  
  185. System.out.println("You chose to compare three strings!");
  186. String str1 = "You chose to compare three strings!";
  187. String str2 = "compareTo example";
  188. String str3 = "You chose to compare three strings!";
  189.  
  190. int variable1 = str1.compareTo(str2); // Compares str1 and str2
  191. System.out.println("str1 and str2 comparison: " + variable1);
  192.  
  193. int variable2 = str1.compareTo(str3); // Compares str1 and str3
  194. System.out.println("str1 and str3 comparison: " + variable2);
  195.  
  196. int variable3 = str2.compareTo("compareTo example"); // Compares str2 and the argument
  197. System.out.println("str2 and string argument comparison: " + variable3);
  198. }
  199. if (value == 2) {
  200.  
  201. System.out.println("You chose to see if the strings are equal to one another.");
  202. // Could not get this test to work
  203. // str1.equals(str2);
  204. // str1 = String("true1").equals("true1"); // this test is true, both strings have the
  205. // same value
  206. // System.out.println(str1);
  207.  
  208. boolean str5 = String("false1") == "false1"; // this test is false, they are not the same
  209. // object "==" compares the two object
  210. // references and determines
  211. // whether they refer to the same
  212. // instance
  213. System.out.println(str5);
  214.  
  215. boolean str3 = String("false2") == new String("false2"); // this test is false, a new
  216. // string will not
  217. System.out.println(str3); // be equal
  218.  
  219. boolean str = "true2" == "true2"; // this test is true, both strings have the same value
  220. // and are assigned to the same object
  221. System.out.println(str); // assigned to the same object
  222.  
  223. boolean str4 = "true3" == "tr" + "ue" + "3"; // this test is true, the string literals are
  224. // concatenated which will print them
  225. // together
  226. System.out.println(str4);
  227. }
  228. }
  229. if (mainMenuChoice == 5) {
  230. useMath(sc);
  231. }
  232. if (mainMenuChoice == 6) {
  233.  
  234. System.out.println("Enter a number 1-7 to find the corresponding day of the week.");
  235. int day = sc.nextInt();
  236. // A switch statement allows a variable to be tested for equality against a list of values,
  237. // the values are called "case"
  238. switch (day) {
  239. case 1:
  240. System.out.println("It's Sunday");
  241. break;
  242. case 2:
  243. System.out.println("It's Monday");
  244. break;
  245. case 3:
  246. System.out.println("It's Tuesday");
  247. break;
  248. case 4:
  249. System.out.println("It's Wednesday");
  250. break;
  251. case 5:
  252. System.out.println("It's Thursday");
  253. break;
  254. case 6:
  255. System.out.println("It's Friday");
  256. break;
  257. case 7:
  258. System.out.println("It's Saturday");
  259. break;
  260. default: // default output is what's printed when the incorrect input is entered
  261. System.out.println("Incorrect input.");
  262. }
  263. }
  264. if (mainMenuChoice == 7) {
  265. // Casting is used to convert an object or variable of one type into another
  266. double calculatedMark = 86.6;
  267. int finalGrade = (int) calculatedMark; // In this example, 86.6 is converted into 87 by
  268. // widening
  269. System.out.println("The casted number is " + finalGrade);
  270.  
  271. }
  272. if (mainMenuChoice == 8) {
  273. // When final is used, the final value of the variable cannot be changed once it has been
  274. // assigned
  275. final double PI = 3.14;
  276. DecimalFormat df = new DecimalFormat("##.##"); // Decimal format allows you to control how
  277. // many decimals you want in your answer
  278. // The # sign holds the places
  279. System.out.println("Enter an integer to multiply it by PI: ");
  280. double choice8 = sc.nextDouble();
  281. double product = choice8 * PI;
  282. System.out.println(choice8 + " * " + PI + " is " + (df.format(product))); // Decimal format
  283. // is assigned
  284. }
  285. if (mainMenuChoice == 9) {
  286. System.out.println("My Integration Project...\t is almost finshed!"); // Insert a tab in the
  287. // text at this point
  288. System.out.println("My Integration Project... is almost finshed!\b"); // Insert a backspace
  289. // in the text at this
  290. // point
  291. System.out.println("My Integration Project...\n is almost finshed!"); // Insert a newline in
  292. // the text at this
  293. // point
  294. System.out.println("My\r Integration Project... is almost finshed!"); // Insert a carriage
  295. // return in the text
  296. // at this point.
  297. System.out.println("My Integration\f Project... is almost finshed!"); // Insert a formfeed
  298. // in the text at this
  299. // point.
  300. System.out.println("My \'Integration Project\'... is almost finshed!"); // Insert a single
  301. // quote character
  302. // in the text at
  303. // this point.
  304. System.out.println("My \"Integration Project\"... is almost finshed!"); // Insert a double
  305. // quote character
  306. // in the text at
  307. // this point.
  308. System.out.println("My \\Integration Project\\... is almost finshed!"); // Insert a
  309. // backslash
  310. // character in the
  311. // text at this
  312. // point.
  313. }
  314. if (mainMenuChoice == 10) {
  315.  
  316. double length = 17.0;
  317. double width = 9.7;
  318. //calculateArea here is a call
  319. // inside the () is arguments
  320. System.out.println(calculateArea(length, width));
  321. }
  322.  
  323. }
  324. if (mainMenuChoice == 11) {
  325. System.out.println("The code is there but they are not printing.");
  326.  
  327. String Str1 = new String("How long is this string?");
  328. System.out.print("The string is " + Str1.length() + "characters long.");
  329. System.out.println(Str1.length()); // The length() method gives the length of how many
  330. // characters long the string is
  331.  
  332. String Str2 = "What number character is \"c\" in this string?";
  333. char charNum = Str2.charAt(11);
  334. System.out.println(charNum); // The charAt() method returns the character located at the
  335. // number given
  336.  
  337. String Str3 = new String("is finally done!");
  338. System.out.print("My Integration Project");
  339. System.out.println(Str3.toString()); // The toString() method returns itself as a string
  340. }
  341. System.out.println("Enter choice here: ");
  342. mainMenuChoice = sc.nextInt();
  343.  
  344. }
  345. // this is a method
  346. // the top line is called a header
  347. // inside () is parameters
  348. public static double calculateArea(double side1, double side2) {
  349. double area;
  350. area = side1 * side2;
  351. return area;
  352. }
  353.  
  354. public static void useMath(Scanner sc) {
  355. System.out.println(
  356. "Chose 1 to raise a random integer to a random integer or 2 to find the sine, cosine, and tangent of a random degree.");
  357. int mathChoice = sc.nextInt();
  358. System.out.println("------------------------------------------------------");
  359. System.out.println("| 1. Raising a number to a power |");
  360. System.out.println("| 2. Find the sine, cosine and tangent of a degree |");
  361. System.out.println("------------------------------------------------------");
  362.  
  363. if (mathChoice == 1) {
  364. Random rand = new Random(); // Generates a random number
  365. int value = rand.nextInt(4);
  366. int value2 = rand.nextInt(4);
  367. System.out.println(value + " to the " + value2 + "th power is " + Math.pow(value, value2));
  368. }
  369. if (mathChoice == 2) {
  370. Random rand = new Random();
  371. int degree = rand.nextInt(360); // 360 is the limit
  372. System.out.println("The sine of " + degree + "° is " + Math.sin(degree));
  373. System.out.println("The cosine of " + degree + "° is " + Math.cos(degree));
  374. System.out.println("The tangent of " + degree + "° is " + Math.tan(degree));
  375. }
  376. }
  377.  
  378.  
  379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement