2
\$\begingroup\$

Since Easter holidays are close, I have decided to develop my Android skills by writing an Android app that calculate that date for the Western and Easter calendars.

The formulas were gotten from the Internet. It is a simple app where, you enter the year and you get the dates for both calendars in a TextView, I've used EditText as labels and made them unclickable, they appear when the dates are visible and get hidden when the dates are not show.

I do not have any professional or internship experience whatsoever. Any input would help. It was built and compiled on Android Studio 4.1.2 and ran on my device.

Here is the code for the XML.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/textView"
        android:layout_width="459dp"
        android:layout_height="56dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:text="@string/welcome"
        android:textSize="22sp"
        app:layout_constraintBottom_toTopOf="@+id/editTextDate"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        android:gravity="center"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editTextDate"
        android:layout_width="wrap_content"
        android:layout_height="46dp"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:gravity="center"
        android:maxLength="4"
        android:digits="0123456789"
        android:inputType="date"
        android:textSize="22sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <TextView
        android:id="@+id/textViewWestern"
        android:layout_width="150dp"
        android:layout_height="48dp"
        android:layout_marginTop="60dp"
        android:gravity="center"
        android:text=""
        android:textSize="22sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.045"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextDate" />

    <TextView
        android:id="@+id/textViewEastern"
        android:layout_width="150dp"
        android:layout_height="48dp"
        android:layout_marginTop="60dp"
        android:gravity="center"
        android:text=""
        android:textSize="22sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.969"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextDate" />

    <EditText
        android:id="@+id/western"
        android:layout_width="150dp"
        android:layout_height="44dp"
        android:layout_marginStart="12dp"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="center"
        android:inputType="textPersonName"
        android:background="@android:color/transparent"
        android:text="Western"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextDate" />

    <EditText
        android:id="@+id/eastern"
        android:layout_width="150dp"
        android:layout_height="44dp"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:inputType="textPersonName"
        android:text="Eastern"
        android:background="@android:color/transparent"
        android:gravity="center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.969"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextDate" />
</androidx.constraintlayout.widget.ConstraintLayout> 

The MainActivity.java code.

package com.example.easterdate;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText Edit = findViewById(R.id.editTextDate);
        final TextView text = findViewById(R.id.textViewWestern);
        final TextView textEastern = findViewById(R.id.textViewEastern);
        final EditText eastern = findViewById(R.id.eastern);
        final EditText western = findViewById(R.id.western);

        eastern.setVisibility(View.INVISIBLE);
        western.setVisibility(View.INVISIBLE);

        Edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if (s.toString().trim().length() == 0 || Integer.parseInt(s.toString()) < 1800) {
                    text.setText("");
                    textEastern.setText("");
                    eastern.setVisibility(View.INVISIBLE);
                    western.setVisibility(View.INVISIBLE);
                    return;
                }

                EditText Edit = findViewById(R.id.editTextDate);
                int AN = Integer.parseInt(Edit.getText().toString());

                int G = AN % 19;
                int C = AN / 100;
                int H = (C - C / 4 - (8 * C + 13) / 25 + 19 * G + 15) % 30;
                int I = H - (H / 28) * (1 - (H / 28) * (29 / (H + 1)) * ((21 - G) / 11));
                int J = (AN + AN / 4 + I + 2 - C + C / 4) % 7;

                int L = I - J;
                int MP = 3 + (L + 40) / 44;
                int JP = L + 28 - 31 * (MP / 4);

                int mon, day;

                int A = AN % 19;
                int b = AN % 7;
                int ce = AN % 4;
                int d = (19 * A + 16) % 30;
                int e = (2 * ce + 4 * b + 6 * d) % 7;
                int f = (19 * A + 16) % 30;
                int key = f + e + 3;
                if (key > 30)
                    mon = 5;
                else
                    mon = 4;
                if (key > 30)
                    day = key - 30;
                else day = key;

                String[] month = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

                eastern.setVisibility(View.VISIBLE);
                western.setVisibility(View.VISIBLE);
                text.setText("Sunday " + JP + " " + month[MP - 1]);
                textEastern.setText("Sunday " + day + " " + month[mon - 1]);

            }

            @Override
            public void afterTextChanged(Editable s) {


            }
        });

    }
}

How professional is this code? should I have used functions instead of imperative programming? The TextWatcher is used to prevent the app from crashing and to have the dates display on input, any better solution?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Small review

How professional is this code?

Scant documentation

Dates of Easter are not easy to discern and rely on some arcane calculations. "The formulas were gotten from the Internet." is insufficient documentation. Consider a programmer after you having to fix or extend this code. Better to include, in code, more details - at least a URL or citation to the source algorithm. Example below. I am not sure of OP's source.

Anonymous Gregorian algorithm: Meeus/Jones/Butcher
Dates of Easter
Astronomical Algorithms 1991
Jean Meeus
https://en.wikipedia.org/wiki/Computus#Anonymous_Gregorian_algorithm

Meeus's Julian algorithm
https://en.wikipedia.org/wiki/Computus#Meeus's_Julian_algorithm

Range

Code deserves to document the range of years in which it is valid.

Precision in description

"... calculate that date for the Western and Easter calendars."

By "Western", I suspect you mean Gregorian calendar.

By "Easter", I suspect you mean "Eastern" or Julian calendar.

\$\endgroup\$
2
  • \$\begingroup\$ Anything else about code itself? \$\endgroup\$ Feb 20, 2021 at 15:56
  • \$\begingroup\$ Nothing more to add except to emphasize the need for more documentation in the code. \$\endgroup\$ Feb 20, 2021 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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