Monday, November 4, 2024

Age Checker in Delphi

Age Checker in Delphi

 



Program Listing

unit demo;


interface


uses

  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,

  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ToolWin, Vcl.ComCtrls;


type

  TForm1 = class(TForm)

    StatusBar1: TStatusBar;

    ToolBar1: TToolBar;

    Edit1: TEdit;

    Label1: TLabel;

    Label2: TLabel;

    Button1: TButton;

    Button2: TButton;

    Button3: TButton;

    procedure Button1Click(Sender: TObject);

    procedure FormCreate(Sender: TObject);

    procedure Button2Click(Sender: TObject);

    procedure Button3Click(Sender: TObject);

  private

    { Private declarations }

     function AskToQuit: Boolean;

  public

    { Public declarations }

  end;


var

  Form1: TForm1;


implementation


{$R *.dfm}


// Programmer : Dr. Jake R. Pomperada

// Date : November 4, 2024


procedure TForm1.Button1Click(Sender: TObject);

var

  age: Integer;

begin

  try

    age := StrToInt(edit1.Text);


    if age >= 18 then

      label2.Caption := 'You are an adult. At age of ' +IntToStr(age) + ' years old.'

    else

      label2.Caption := 'You are a minor.  At age of ' +IntToStr(age) + ' years old.';

  except

    on EConvertError do

      ShowMessage('Please enter a valid age (a number).');

  end;

end;



procedure TForm1.Button2Click(Sender: TObject);

begin

label2.Caption := '';

edit1.text := '';

edit1.SetFocus;

end;


procedure TForm1.Button3Click(Sender: TObject);

begin

if AskToQuit then

    Close // Close the form if user confirms

    else

      edit1.SetFocus;

end;


function TForm1.AskToQuit: Boolean;

begin

  Result := MessageDlg('Are you sure you want to quit?', mtConfirmation, [mbYes, mbNo], 0) = mrYes;

end;


procedure TForm1.FormCreate(Sender: TObject);

begin

 // Optionally adjust the form position here if needed

  // For example, move it slightly to the left and up

  Left := (Screen.Width - Width) div 2;   // Center horizontally

  Top := (Screen.Height - Height) div 2;  // Center vertically

end;


end.


Sunday, November 3, 2024

Square a Number Using Lazarus Free Pascal

Circle Using a Class in Java

Circle Using a Class in Java

 


Program Listing

import java.text.DecimalFormat;


class Circle {

    private double radius;


    public Circle(double radius) {

        this.radius = radius;

    }


    public double getRadius() {

        return radius;

    }


    public double calculateArea() {

        return Math.PI * Math.pow(radius, 2);

    }


    public double calculateCircumference() {

        return 2 * Math.PI * radius;

    }

}


public class Main {

    public static void main(String[] args) {

        Circle circle = new Circle(8.15);


        DecimalFormat df = new DecimalFormat("#.00");


        System.out.println("\n\tCircle Using a Class in Java\n");

        

        System.out.println("\tRadius: " + df.format(circle.getRadius()));

        System.out.println("\tArea: " + df.format(circle.calculateArea()));

        System.out.println("\tCircumference: " + df.format(circle.calculateCircumference()));

         System.out.println("\n\tEnd of Program. Thank you for Using This Program\n");

    }

}