Advertisement
Guest User

tut10

a guest
Dec 12th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. */import java.util.Scanner;
  2. public class JavaApplication22 {
  3. public static void BMI(){
  4. Scanner a = new Scanner(System.in);
  5. System.out.println("Please enter your weight in kilogram");
  6. double weight = a.nextDouble();
  7. System.out.println("Please enter your height in meter");
  8. double height = a.nextDouble();
  9. double BMI = (weight/(height*height));
  10. System.out.println("Your BMI rating is " + BMI);
  11. }
  12. public static void coin(){
  13. Scanner b = new Scanner(System.in);
  14. System.out.println("Please enter your amount of coins");
  15. int coin = b.nextInt();
  16. int quan = coin/10000;
  17. int dong = coin%10000/100;
  18. int hao = coin%10000%100/10;
  19. int xu = coin%10000%100%10;
  20. System.out.println("You have " + quan + " quan, " + dong + " dong, " + hao + " hao, " + xu + " xu.");
  21. }
  22. public static void leap(){
  23. Scanner c = new Scanner(System.in);
  24. System.out.println("Please enter the year you want to check");
  25. int y = c.nextInt();
  26. String a = "The year " + y + " is not a leap year";
  27. String b = "The year " + y + " is a leap year";
  28. if (y % 4 != 0){
  29. System.out.println(a);
  30. }else
  31. if (y % 100 != 0 ){
  32. System.out.println(b);
  33. }else
  34. if ( y % 400 != 0 ){
  35. System.out.println(a);
  36. }else
  37. System.out.println(b);
  38. }
  39. public static void quadratic(){
  40. int a, b, c;
  41. double root1, root2, d;
  42. Scanner s = new Scanner(System.in);
  43. System.out.println("Given quadratic equation:ax^2 + bx + c");
  44. System.out.print("Enter a:");
  45. a = s.nextInt();
  46. System.out.print("Enter b:");
  47. b = s.nextInt();
  48. System.out.print("Enter c:");
  49. c = s.nextInt();
  50. System.out.println("Given quadratic equation:"+a+"x^2 + "+b+"x + "+c);
  51. d = b * b - 4 * a * c;
  52. if (a==0 && b==0 && c==0){
  53. System.out.println("There is nothing to be solved");
  54. }else
  55. if (a==0 && b==0 && c != 0){
  56. System.out.println("The equation has no root");
  57. }else
  58. if (a==0 && b !=0 && c !=0){
  59. double x = -c/b;
  60. System.out.println("X= " + x);
  61. }else
  62. if (d < 0){
  63. System.out.println("The equation has no real root");
  64. }else
  65. if (d > 0){
  66. double x1 = ((-b + Math.sqrt(b*b-4*a*c))/(2*a));
  67. double x2 = ((-b - Math.sqrt(b*b-4*a*c))/(2*a));
  68. System.out.println("The equation has two roots : x1 = " +x1 + "," + "x2 = " +x2);
  69. }
  70. }
  71. public static void strength(){
  72. Scanner e = new Scanner(System.in);
  73. System.out.println("Please enter the password you wish to be evaluated");
  74. String password= e.nextLine();
  75. int strength= 0;
  76. boolean upper= false, lower= false, digit= false, symbol= false;
  77. if (password.length()>=8 && password.length()<=12)
  78. {
  79. strength+=1;
  80. }
  81. else if (password.length()>12)
  82. {
  83. strength+=2;
  84. }
  85. for (int i=0; i< password.length(); ++i) // uppercase checker
  86. {
  87. if (password.charAt(i)>= 'A' && password.charAt(i)<= 'Z')
  88. {
  89. upper= true;
  90. continue;
  91. }
  92. else if (password.charAt(i)>= 'a' && password.charAt(i)<= 'z')
  93. {
  94. lower= true;
  95. continue;
  96. }
  97. else if (password.charAt(i)>= '0' && password.charAt(i)<= '9')
  98. {
  99. digit= true;
  100. continue;
  101. }
  102. else
  103. {
  104. symbol= true;
  105. }
  106. }
  107. if (upper == true)
  108. {
  109. strength+=1;
  110. }
  111. if (lower == true)
  112. {
  113. strength+=1;
  114. }
  115. if (digit == true)
  116. {
  117. strength+=1;
  118. }
  119. if (symbol == true)
  120. {
  121. strength+=1;
  122. }
  123. if (strength<=2)
  124. {
  125. System.out.println("Your password strength: " + strength+ " (weak)");
  126. }
  127. else if (strength <=4)
  128. {
  129. System.out.println("Your password strength: " + strength+ " (medium)");
  130. }
  131. else
  132. {
  133. System.out.println("Your password strength: " + strength+ " (strong). Make sure you do not forget it.");
  134. }
  135.  
  136. }
  137. public static void GCD(){
  138. Scanner f= new Scanner(System.in);
  139. System.out.println("Enter two positive integers of which you wish to find the GCD");
  140. int a= f.nextInt();
  141. int b= f.nextInt();
  142. while (a<=0 || b<=0)
  143. {
  144. System.out.println("The program does not allow negative or zero values! Please try again.");
  145. a= f.nextInt();
  146. b= f.nextInt();
  147. }
  148. if (a<b)
  149. {
  150. int temp= a;
  151. a=b;
  152. b=temp;
  153. }
  154. while (true)
  155. {
  156. if (a%b==0)
  157. {
  158. break;
  159. }
  160. int temp=a;
  161. a= b;
  162. b= temp%b;
  163. }
  164. System.out.println("The GCD is: "+ b);
  165. }
  166. public static void main(String[] args) {
  167. Scanner f = new Scanner(System.in);
  168. System.out.println("[1] BMI calculator");
  169. System.out.println("[2] Coin converter");
  170. System.out.println("[3] Leap year checker");
  171. System.out.println("[4] Quadratic equation solver");
  172. System.out.println("[5] Password strength meter");
  173. System.out.println("[6] GCD finder");
  174. System.out.println("[7] Quit");
  175. System.out.println("Choose an option");
  176. int n = f.nextInt();
  177. while (n !=7)
  178. {
  179. switch (n)
  180. {
  181. case 1:
  182. BMI();
  183. break;
  184. case 2:
  185. coin();
  186. break;
  187. case 3:
  188. leap();
  189. break;
  190. case 4:
  191. quadratic();
  192. break;
  193. case 5:
  194. strength();
  195. break;
  196. case 6:
  197. GCD();
  198. break;
  199. }
  200. System.out.println("[1] BMI calculator");
  201. System.out.println("[2] Coin converter");
  202. System.out.println("[3] Leap year checker");
  203. System.out.println("[4] Quadratic equation solver");
  204. System.out.println("[5] Password strength finder");
  205. System.out.println("[6] GCD finder");
  206. System.out.println("[7] Quit");
  207. System.out.println("Choose an option");
  208. n = f.nextInt();
  209. }
  210. System.out.println("Program exited");
  211. }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement