This is a age calculator made in java, it's a console application by the way :)
source :
screenshot :
Hope it helped you :)
source :
import java.util.GregorianCalendar;
import java.util.Scanner;
import java.util.InputMismatchException;
public class age_calculator {
public static void main(String[] args) {
int day = 0;
int month = 0;
int year = 0;
Scanner scan = new Scanner(System.in);
try{
System.out.print("Day: ");
day = scan.nextInt();
System.out.print("Month: ");
month = scan.nextInt();
System.out.print("Year: ");
year = scan.nextInt();
}
catch(InputMismatchException e){
System.out.println("Error: You entered an invalid number");
System.exit(1);
}
System.out.println("\nYou are " + getAge(day,month,year) + " years old");
}
public static int getAge(int day, int month, int year){
final int ERROR = -1;
//initialize GregorianCalendar with current date
GregorianCalendar cal = new GregorianCalendar();
//constant holds current day
final int CDAY = cal.get(GregorianCalendar.DAY_OF_MONTH);
//constant hold current month
final int CMONTH = cal.get(GregorianCalendar.MONTH) + 1;
//constant to hold current year
final int CYEAR = cal.get(GregorianCalendar.YEAR);
//check if current day of the month is greater then birth day
if (month == CMONTH){
if(CDAY >= day)
return CYEAR - year;
if(CDAY < day)
return CYEAR - year - 1;
}
//check if birth month is less then current month
else if(month < CMONTH)
return CYEAR - year;
//subtract 1 from the age because the current day in the month is less then birth date
else
return CYEAR - year - 1;
return ERROR;
}
}
screenshot :
Hope it helped you :)
2 comments
Working with Windows and AWT
ReplyWorking with graphics
Using AWT Controls, Layout Managers and Menus
Handling events by Extending AWT Components
Networking in Java
Networking Basics
Domain Naming Service (DNS)
InetAddress
TCP/IP Client Sockets
TCP/IP Server Sockets
URL Format
URLConnection class
Data grams Data gram packets, Data gram server & Client
Java Data-Base Connectivity
Java as Database Front End
What is JDBC?
The Tour of Swing
Japplet
Trees
Servlets
Background
The javax.servlet.http package
Using Cookies
home and business 2011 key free , change product key windows 7 ultimate 32 bit sp1 , windows 10 serial keys for installation , windows 7 pro key sale , windows 10 serial key install , microsoft sql server 2008 r2 enterprise keygen , windows 10 activation servers , microsoft visio professional 2016 , s5Pnpa
ReplyPost a Comment
Note: Only a member of this blog may post a comment.