Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Project2 {
- private static Scanner input = new Scanner(System.in);
- private static final int MIN_FIRST = 1;
- private static final int MIN_SECOND = 1;
- private static final int MIN_THIRD = 1;
- private static final int MAX_FIRST = 10000;
- private static final int MAX_SECOND = 12;
- private static final int MAX_THIRD = 4000;
- private static int first;
- private static int second;
- private static int third;
- private static int firstOut;
- private static int secondOut;
- private static String thirdOut;
- public static void main(String[] args) {
- first = input.nextInt();
- second = input.nextInt();
- third = input.nextInt();
- if(validateInput(args)) {
- performOperations();
- displayReport();
- }
- }
- private static void displayReport() {
- System.out.println("Robert Karaitis \n Project 2 - Simple Operations \n \n Operation Results: \n ------------------ \n");
- System.out.println("Exactly " + firstOut + " prime numbers exist between " + MIN_FIRST + " and " + first);
- System.out.println("The value of " + second + "! is " + secondOut);
- System.out.println("The year " + third + thirdOut);
- }
- private static boolean validateInput(String[] input){
- try{ //Tries to parse 1st number. If it's not an integer, error displays and program is exited.
- first = Integer.parseInt(input[0]);
- }
- catch(Exception ex) {
- System.out.println("\nInput #1 is not a valid Integer.");
- System.exit(1);
- }
- try{ //Tries to parse 2nd number. If it's not an integer, error displays and program is exited.
- second = Integer.parseInt(input[1]);
- }
- catch(Exception ex2){
- System.out.println("\nInput #2 is not a valid Integer.");
- System.exit(1);
- }
- try{ //Tries to parse 3rd number. If it's not an integer, error displays and program is exited.
- third = Integer.parseInt(input[2]);
- }
- catch(Exception ex3){
- System.out.println("\nInput #3 is not a valid Integer.");
- System.exit(1);
- }
- if (withinRange()){
- return true;
- }
- else{
- return false;
- }
- }
- private static boolean withinRange() {
- boolean test = true;
- if(first<MIN_FIRST || first>MAX_FIRST) {
- test = false;
- System.out.println("The value " + first + " is out of range [" + MIN_FIRST + " to " + MAX_FIRST + "].");
- }
- if(second<MIN_SECOND || second>MAX_SECOND) {
- test = false;
- System.out.println("The value " + second + " is out of range [" + MIN_SECOND + " to " + MAX_SECOND + "].");
- }
- if(third<MIN_THIRD || third>MAX_THIRD) {
- test = false;
- System.out.println("The value " + third + " is out of range [" + MIN_THIRD + " to " + MAX_THIRD + "].");
- }
- return test;
- }
- private static void performOperations() {
- firstOut = countPrimes();
- secondOut = getFactorial(second);
- if(isLeapYear()==true) {
- thirdOut = " is a leap year";
- }
- if(isLeapYear()==false) {
- thirdOut = " is not a leap year";
- }
- }
- private static boolean isPrime(int n) {
- boolean prime = true;
- for(int x=2; x<=Math.sqrt(n);x++) {
- if(n%x==0) {
- prime = false;
- }
- }
- return prime;
- }
- private static int countPrimes() {
- int count=0;
- for(int x=2; x<=first; x++){
- if(isPrime(x)==true){
- count++;
- }
- }
- return count;
- }
- private static int getFactorial(int n) {
- if (n==1) {
- return 1;
- }
- else {
- return n * getFactorial(n - 1);
- }
- }
- private static boolean isLeapYear() {
- boolean leapYear = false;
- if(third%4==0) {
- leapYear = true;
- if(third%100==0) {
- leapYear = false;
- if(third%400==0) {
- leapYear = true;
- }
- }
- }
- return leapYear;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement