Advertisement
tolem

housework

Mar 10th, 2022
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. import java.math.BigDecimal;
  2. import java.math.BigInteger;
  3. import java.util.Scanner;
  4.  
  5. public class Blank2 {
  6. public static void main(String[] args) {
  7.  
  8. for (int i = 7; i > 0; i--) {
  9. System.out.println("*".repeat(i));
  10. }
  11. for (int i = 0; i < 6; i++) {
  12. System.out.print("#");
  13. System.out.print(" ".repeat(i));
  14. System.out.println("#");
  15.  
  16. }
  17.  
  18. Scanner scanner = new Scanner(System.in);
  19. boolean error;
  20.  
  21. do {
  22. error = false;
  23. try {
  24. System.out.println("Enter the length in feet below to get he value to inches.");
  25.  
  26. System.out.println("Result: " + feet_to_inches(scanner.nextBigDecimal()) + " in.");
  27. } catch (Exception e) {
  28. System.out.println("Not allowed format! Try again!");
  29. error = true;
  30. scanner.nextLine();
  31. }
  32. } while (error);
  33.  
  34. do {
  35. error = false;
  36. try {
  37.  
  38. System.out.println("Enter five test scores to calculate grades and the average score.");
  39. double s1 = scanner.nextDouble();
  40. double s2 = scanner.nextDouble();
  41. double s3 = scanner.nextDouble();
  42. double s4 = scanner.nextDouble();
  43. double s5 = scanner.nextDouble();
  44. if (!(s1 >= 0 && s1 <= 100) ||
  45. !(s2 >= 0 && s2 <= 100) ||
  46. !(s3 >= 0 && s3 <= 100) ||
  47. !(s4 >= 0 && s4 <= 100) ||
  48. !(s5 >= 0 && s5 <= 100)) {
  49. error = true;
  50. System.out.println("The score cannot be a negative number or more than 100. Try again");
  51. continue;
  52. }
  53. System.out.println("score: " + s1 + " = " + determine_grade(s1) + " grade");
  54. System.out.println("score: " + s2 + " = " + determine_grade(s2) + " grade");
  55. System.out.println("score: " + s3 + " = " + determine_grade(s3) + " grade");
  56. System.out.println("score: " + s4 + " = " + determine_grade(s4) + " grade");
  57. System.out.println("score: " + s5 + " = " + determine_grade(s5) + " grade");
  58.  
  59. System.out.println("The average score is: " + calc_average(s1, s2, s3, s4, s5));
  60. } catch (Exception e) {
  61. System.out.println("Not allowed format. The score must be a number and cannot be a negative number or more than 100. Try again.");
  62. scanner.nextLine();
  63. error = true;
  64.  
  65. }
  66. } while (error);
  67.  
  68. do {
  69. error = false;
  70. try {
  71. System.out.println("Enter a number between 0 and 2.140.000.000 to figure out whether it is a prime.");
  72. System.out.println(is_prime(scanner.nextInt())?"It's a prime!":"It's just a number.");
  73. } catch (Exception e) {
  74. System.out.println("Not allowed format.");
  75. error = true;
  76. scanner.nextLine();
  77. }
  78. } while (error);
  79.  
  80. scanner.close();
  81. System.out.println();
  82. System.out.print("The primes are the folowing from 1 to 100: ");
  83. for (int i = 1; i <= 100; i++) {
  84. System.out.print(is_prime(i)?i+" ":"");
  85. }
  86.  
  87. }
  88.  
  89. public static BigDecimal feet_to_inches(BigDecimal foot) {
  90. BigDecimal b = new BigDecimal("12");
  91. return foot.multiply(b);
  92. }
  93.  
  94. public static double calc_average(double s1, double s2, double s3, double s4, double s5) {
  95.  
  96. return (s1 + s2 + s3 + s4 + s5) / 5;
  97. }
  98.  
  99. public static char determine_grade(double score) {
  100.  
  101. char grade = 'F';
  102.  
  103. if (score > 89) {
  104. grade = 'A';
  105. } else if (score > 79) {
  106. grade = 'B';
  107. } else if (score > 69) {
  108. grade = 'C';
  109. } else if (score > 59) {
  110. grade = 'D';
  111. }
  112.  
  113. return grade;
  114. }
  115.  
  116. public static boolean is_prime(int number) {
  117.  
  118. BigInteger bigInteger = BigInteger.valueOf(number);
  119. return bigInteger.isProbablePrime(100);
  120. }
  121.  
  122. }
  123.  
  124.  
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement