Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Project2 {
  4.  
  5. private static Scanner input = new Scanner(System.in);
  6.  
  7. private static final int MIN_FIRST = 1;
  8. private static final int MIN_SECOND = 1;
  9. private static final int MIN_THIRD = 1;
  10.  
  11. private static final int MAX_FIRST = 10000;
  12. private static final int MAX_SECOND = 12;
  13. private static final int MAX_THIRD = 4000;
  14.  
  15. private static int first;
  16. private static int second;
  17. private static int third;
  18.  
  19. private static int firstOut;
  20. private static int secondOut;
  21. private static String thirdOut;
  22.  
  23. public static void main(String[] args) {
  24. first = input.nextInt();
  25. second = input.nextInt();
  26. third = input.nextInt();
  27. if(validateInput(args)) {
  28. performOperations();
  29. displayReport();
  30. }
  31. }
  32.  
  33. private static void displayReport() {
  34. System.out.println("Robert Karaitis \n Project 2 - Simple Operations \n \n Operation Results: \n ------------------ \n");
  35. System.out.println("Exactly " + firstOut + " prime numbers exist between " + MIN_FIRST + " and " + first);
  36. System.out.println("The value of " + second + "! is " + secondOut);
  37. System.out.println("The year " + third + thirdOut);
  38. }
  39.  
  40. private static boolean validateInput(String[] input){
  41.  
  42. try{ //Tries to parse 1st number. If it's not an integer, error displays and program is exited.
  43. first = Integer.parseInt(input[0]);
  44. }
  45. catch(Exception ex) {
  46. System.out.println("\nInput #1 is not a valid Integer.");
  47. System.exit(1);
  48. }
  49. try{ //Tries to parse 2nd number. If it's not an integer, error displays and program is exited.
  50. second = Integer.parseInt(input[1]);
  51. }
  52. catch(Exception ex2){
  53. System.out.println("\nInput #2 is not a valid Integer.");
  54. System.exit(1);
  55. }
  56. try{ //Tries to parse 3rd number. If it's not an integer, error displays and program is exited.
  57. third = Integer.parseInt(input[2]);
  58. }
  59. catch(Exception ex3){
  60. System.out.println("\nInput #3 is not a valid Integer.");
  61. System.exit(1);
  62. }
  63. if (withinRange()){
  64. return true;
  65. }
  66. else{
  67. return false;
  68. }
  69.  
  70. }
  71.  
  72. private static boolean withinRange() {
  73. boolean test = true;
  74. if(first<MIN_FIRST || first>MAX_FIRST) {
  75. test = false;
  76. System.out.println("The value " + first + " is out of range [" + MIN_FIRST + " to " + MAX_FIRST + "].");
  77. }
  78. if(second<MIN_SECOND || second>MAX_SECOND) {
  79. test = false;
  80. System.out.println("The value " + second + " is out of range [" + MIN_SECOND + " to " + MAX_SECOND + "].");
  81. }
  82. if(third<MIN_THIRD || third>MAX_THIRD) {
  83. test = false;
  84. System.out.println("The value " + third + " is out of range [" + MIN_THIRD + " to " + MAX_THIRD + "].");
  85. }
  86. return test;
  87. }
  88.  
  89. private static void performOperations() {
  90. firstOut = countPrimes();
  91. secondOut = getFactorial(second);
  92. if(isLeapYear()==true) {
  93. thirdOut = " is a leap year";
  94. }
  95. if(isLeapYear()==false) {
  96. thirdOut = " is not a leap year";
  97. }
  98. }
  99.  
  100. private static boolean isPrime(int n) {
  101. boolean prime = true;
  102. for(int x=2; x<=Math.sqrt(n);x++) {
  103. if(n%x==0) {
  104. prime = false;
  105. }
  106. }
  107. return prime;
  108. }
  109.  
  110. private static int countPrimes() {
  111. int count=0;
  112. for(int x=2; x<=first; x++){
  113. if(isPrime(x)==true){
  114. count++;
  115. }
  116. }
  117. return count;
  118. }
  119.  
  120. private static int getFactorial(int n) {
  121. if (n==1) {
  122. return 1;
  123. }
  124. else {
  125. return n * getFactorial(n - 1);
  126. }
  127. }
  128.  
  129. private static boolean isLeapYear() {
  130. boolean leapYear = false;
  131. if(third%4==0) {
  132. leapYear = true;
  133. if(third%100==0) {
  134. leapYear = false;
  135. if(third%400==0) {
  136. leapYear = true;
  137. }
  138. }
  139. }
  140. return leapYear;
  141. }
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement