Zeller Java

  1. Zeller Pascal
  2. Zeller Java
  3. validDate Revisited – Python
  4. validDate Revisited – Java
  5. Java GUI
  6. GUI – Refinements – ComboBoxes
  7. GUI – Refinements – Error Messages
  8. GUI Refinements – Focus
  9. GUI Refinements – Today

The next evolution of this project is to eventually create a graphical user interface(GUI). This is possible to do using Python. But, I want to use Java for this job so recreating the work I have already done in Java is the way to go.

So what’s first. The first thing is to write the extra function which I created to check if a value is in an array, the python code is:

def inArray(value, array):
    isIn = False
    for i in range(0, len(array)):
        if array[i] == value:
            isIn = True
    return isIn

The Java code does not look that dissimilar

public boolean inArray(int x, int[] array)
    {
        boolean isIn = false;
        for (int n : array)
        {
            if (n == x){
                isIn = true;
                break;
            }
        }
        return isIn;
    }

The next function is the function which validates a date, again the python code looks like this:

def validDate(d, m, y):
    months_thirty0 = [4,6,7,11]
    months_thirty1 = [1,3,5,7,8,10,12]
    daysInMonth = 0

    if inArray(m, months_thirty0):
        daysInMonth = 30
    elif inArray(m, months_thirty1):
        daysInMonth  = 31
    elif (y % 4) == 0:
        daysInMonth = 29
    else:
        daysInMonth = 28

    return (d <= daysInMonth)

Again the Java code does not look that dissimilar:

public boolean validDate(int d, int m, int y)
    {
        int[] monthsThirty0 = {4,6,7,11};
        int[] monthsThirty1 = {1,3,5,7,8,10,12};
        int daysInMonth = 0;
        
        if (inArray(m, monthsThirty0)){
            daysInMonth = 30;
        }else if((inArray(m, monthsThirty1))){
            daysInMonth = 31;
        }else if(y%4 == 0){
            daysInMonth = 29;
        }else{
            daysInMonth = 28;
        }
        
        return (d <= daysInMonth);
    }

All that we have to do now is translate the calculation function in the Zeller program to Java. As before the python code is:

import math
from myfuncs import validDate

def zeller(day, month, year):
    days = ['Saturday', 'Sunday', 'Monday', 'Tuesday',
              'Wednesday', 'Thursday', 'Friday']
    if validDate(day, month, year):
        if month < 3:
            month = month + 12
            year = year -1
        calc = (day
                + math.floor((13*(month+1))/5)
                + year
                + math.floor(year/4)
                - math.floor(year/100)
                + math.floor(year/400)) % 7
        return days[calc]
    else:
        return 'Error: Invalid Date'

Again the Java function is not that dissimilar, the only difference is we first have to create a myFunctions object on line 6:

public String Calculate(int day, int month, int year)
    {
        String[] days = {"Saturday", "Sunday", "Monday", "Tuesday", 
                "Wednesday", "Thursday", "Friday"};
        double calc;
        myFunctions mf = new myFunctions();

        if (mf.validDate(day,month,year)){
            if (month < 3) 
            {
                month = month + 12;
                year = year - 1;
            }
            calc = (day
                + Math.floor((13*(month+1)/5))
                + year
                + Math.floor(year/4)
                - Math.floor(year/100)
                + Math.floor(year/400))%7;
            return days[(int)calc];
        }else{
            return "Error: Invalid Date";
        }
    }

The translation is now complete the next job is to create the GUI.