I have a program that calculates whether or not a date entered by a user is a leap year or not. I think I got that all down but I also need to see if the date that is entered is a binary date (i.e. 1/1/11. I’m not really sure the best way to go about this, maybe an charAt reference? Any help would be must appreciated!
//****************************
import java.util.Scanner;
public class leapYearCalc {
private int day = 0;
private int month = 0;
private int year = 0;
Scanner myScan = new Scanner (System.in);
//---------------------------------
//Constructor to accept and initialize instance data
//---------------------------------
public leapYearCalc(int day, int month, int year){
this.day=day;
this.month=month;
this.year=year;
}
//--------------------------------
//Get day
//--------------------------------
public int getDay(){
System.out.println("Whats the day?");
day = myScan.nextInt();
return day;
}
//--------------------------------
//Get day
//--------------------------------
public int getMonth(){
System.out.println("Whats the month in numerical form?");
month = myScan.nextInt();
return month;
}
//--------------------------------
//Get day
//--------------------------------
public int getYear(){
System.out.println("Whats the year (i.e. 2004)?");
year = myScan.nextInt();
if (year<1582)
System.out.println("Please enter a value above 1582");
return year;
}
//--------------------------------
//1. If a year is divisible by 4 it is a leap year if 2 does not apply.
//2. If a year is divisible by 100 it is not a leap year unless #3 applies.
//3. If a year is divisible by 400 it is a leap year.
//--------------------------------
//Calculate leap year
public String toString() {
if (year % 4 == 0) {
if (year % 100 != 0) {
System.out.println(year + " is a leap year.");
}
else if (year % 400 == 0) {
System.out.println(year + " is a leap year.");
}
else {
System.out.println(year + " is not a leap year.");
}
}
else {
System.out.println(year + " is not a leap year.");
}
return null;
}
//--------------------------------
//Check to see if date is binary
//--------------------------------
public int getBinary(){
while(month == 01 || month == 10)
if(day == 01 || day == 10 && year == 00 || year == 01)
System.out.println("It's a binary date!");
System.out.println("It's not a binary date");
return month;
}
}
So you are checking if the day, month and year fields are either 1, 10 0r 11. If they are then it is a binary date, otherwise it is not a binary date. Maybe your getBinary() method should simply return a boolean value. You do not need a while loop there, rather an if statement would work:
In addition your leap year calculation can be simplified if you start with the divisible by 400. e.g.
or it can all be made into a simple boolean expression like this
===================
Considering a four digit year then you could split each digit off like this: