-2
\$\begingroup\$

This program uses user-input variables to calculate the iDays a person has been alive for (or the iDays between two input dates), taking into account for leap years and iDays in each month.

Can I please have some feedback on my code formatting and if you think this would get good marks?

//-------- Author: pumpkin

//-------- Date created: 10 April 2017

//-------- Date last changed: 17 April 2017

//-------- This program uses user-input variables to calculate the iDays a person has been alive for 

//-------- (or the iDays between two input dates), taking into account for leap years and iDays in each month.

//-------- Input: This file only reads the user input into the console. 

//-------- Output: None, or to console.


import java.util.Scanner;

public class Stage1 {


    public static void main(String[] args) {
        // declaration of variables
                    int iBirthYear, iBirthMonth, iBirthDay, iPresentYear, iPresentMonth, iPresentDay, iDaysAlive;

         // scanners, which will tell the program that it needs to scan the user input for the value of the variable that i've declared
        Scanner iBirthY = new Scanner(System.in);

        Scanner iBirthM = new Scanner(System.in);

        Scanner iBirthD = new Scanner(System.in);

        Scanner iPresentY = new Scanner(System.in);

        Scanner iPresentM = new Scanner(System.in);

        Scanner iPresentD = new Scanner(System.in);


        // asks user to input birth dates

        System.out.println("Please enter the year you were born: ");
            //takes input value and sets it to iBirthYear
                iBirthYear = iBirthY.nextInt();

        System.out.println("Please enter the month you were born: ");
            //takes input value and sets it to iBirthMonth  
                iBirthMonth = iBirthM.nextInt();

        System.out.println("Please enter the date you were born: ");
            //takes input value and sets it to iBirthDay    
                iBirthDay = iBirthD.nextInt();

        System.out.println("Please enter the current year: ");
            //takes input value and sets it to iPresentYear 
                iPresentYear = iPresentY.nextInt();

        System.out.println("Please enter the current month: ");
            //takes input value and sets it to iPresentMonth
                iPresentMonth = iPresentM.nextInt();

        System.out.println("Please enter the current day: ");
            //takes input value and sets it to iPresentDay  
                iPresentDay = iPresentD.nextInt();


/**     this is the if statements that calculate for leap years. 

*       both if/else statements do the same thing, just for the birth and present variables

*       if the month is jan or feb add 13 to the value of iMonth and subtract 1 from iYear

*       otherwise add one to iMonth and leave iYear as is.

**/ 

        if (iBirthMonth <= 2){
            iBirthMonth = iBirthMonth + 13;
            iBirthYear = iBirthYear - 1;
        }
        else {
            iBirthMonth = iBirthMonth + 1;
            iBirthYear = iBirthYear;
        }

        if (iPresentMonth <=2){
            iPresentMonth = iPresentMonth + 13;
            iPresentYear = iPresentYear - 1;
        }
        else {
            iPresentMonth = iPresentMonth + 1;
            iPresentYear = iPresentYear;
        }

//------------ the actual calculation between the dates. its the same calculation subtracted.   

        iDaysAlive = (((1461 * iPresentYear)/4) + ((153*iPresentMonth)/5) + (iPresentDay)) -
                    (((1461 * iBirthYear)/4) + ((153*iBirthMonth)/5) + (iBirthDay));

            System.out.println("You have been alive for: " + iDaysAlive + " days!");

    }

}
\$\endgroup\$
  • \$\begingroup\$ Your algorithm is wrong since not all months have the same number of days, and since leap years are at fixed dates. \$\endgroup\$ – Roland Illig Apr 17 '17 at 6:38
  • \$\begingroup\$ IMHO use the java8 date time apis. Or Joda time if java8 is not available. If its a homework then at-least look up how those apis do the calculations. \$\endgroup\$ – Dexter Apr 17 '17 at 7:29
  • \$\begingroup\$ @Dexter I'm not allowed to use Java8 date time :( \$\endgroup\$ – Pumpkin Apr 17 '17 at 9:29
  • \$\begingroup\$ @RolandIllig, I'm fairly sure I've tested it for all possibilities, I tested people born before February and after, born in leap years and not born in leap years, I tested for people born on Feb 29th as well and everything worked as tested with an online age calculator \$\endgroup\$ – Pumpkin Apr 17 '17 at 9:31
  • \$\begingroup\$ Please add to the question what you told @Dexter, and add the homework tag. \$\endgroup\$ – pacmaninbw Apr 24 '17 at 11:37
1
\$\begingroup\$

I see some code smell:

  • A single Scanner instance is enough, there's no need to create multiple Scanners
  • Magic numbers: you should move 1461, 153, 13, etc. to constants with a meaningful name
  • Readability: only one variable declaration per line
  • Useless comments: "takes input value and sets it to XYZ" is an obvious statement, there is no need to fulfil your code with noise
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.