Sunday, March 27, 2016

Check if given date is valid or not , using java coding



In order to check if the given date is valid or not you need to know the logic behind it.
  • During Leap year, February have 29 days.While generally February have 28 days in it.
  • April , June , September and November consists of 30 days.
  • While remaining months have 31 days in them.
We will be using only "if & else" conditions throughout the program coding.There is nothing else included in it.So let's get started.Here is the code for checking if given date is valid or not:


package labsessional;

import java.util.Scanner;
public class LabSessional
{
    public static void main( String [] args)
    {
        int day,month,year,leepYear ;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter day : ");
        day=input.nextInt();
        System.out.print("Enter month: ");
        month=input.nextInt();
        System.out.print("Enter year: ");
        year=input.nextInt();
        leepYear=year%4;
       
    if(day>=1 && day<=31 && month>=1 && month<=12 && year>=1950 && year<=2099)
           
        {
            if (leepYear==0 && month==2 && day>=0 && day<=29)
                System.out.print("Your date is valid. ");
         else
         {
       
               
            if(month==4 || month==6 || month==9 || month==11 )
           
             {
                 if(day>=0 && day<=30)
                System.out.print("Your date is valid. ");
             else
                System.out.print("Your date is invalid. ");   
             }
   
            else if (month==2)
             {
                if(day>=0 && day<=28)
                    System.out.print("Your date is valid. ");
                else
                    System.out.print("Your date is invalid. ");
             }
             else if ( month==1|| month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
              {
                  if(day>=0 && day<=31)
                      System.out.print("Your date is valid. ");
                  else
                      System.out.print("Your date is invalid. ");
              }
           
        }
    }
        else
            System.out.print("Your given date is invalid.");
 }
}


If you have any confusion or you found any error in our code , then please feel free to contact us . You can email us.Our email address is : k.ahmed2k19@gmail.com
If you liked it , then please share it with your friends. Keep visiting & Keep learning.

Saturday, March 26, 2016

Java code for finding Area Of a Circle



Here is the java code for finding the area of a circle.
You need to import "Scanner class", so that you can take input from user.That input will be radius of the circle.

package javaapplication2;
import java.util.Scanner;
public class JavaApplication2 
{
    public static void main(String[] args) 
    {
    double radius;
    double area;
    Scanner input=new Scanner(System.in);
    System.out.print("Enter Radius: ");
    radius=input.nextDouble();
    area = radius *radius * 3.14;
    System.out.println("The area of the circle of radius " + radius +" is " +area);
    
    }
    
}


If you found any error in this code or if you have a better and short java code for this work then please feel free to send it on our email so that we can upload it too.
Our contact email is :  K.ahmed2k19@gmail.com
If you like it then share it with your friends.Keep visiting and keep learning.

Thursday, March 24, 2016

Rules and Compilation of first java program

Rules:
Here are some of the rules and conventions that you need to follow while writing a code.

  • You need to import a package before starting anything else.
  • Package must contain at least one "Class".
  • Each "Class" must contain one "Method (function) ".
  • If you use more than one word jointly then start first word after the dot with a small letter while first letter of each word next to it will be capital.For example: " ItIsAnExample ".
  • If you are creating an app then you must have to use "main" method.
    If you haven't read our article on "Basics of Java" then i would recommend you read it first before reading it further.
  • A data type for a variable must be defined before using that variable.
    For example " int a ; " ,this will define that "a" is a variable and it can be only an integer.
    if we directly write "a" without writing any data type with that then it will display us an error.
  • You need to terminate each and every syntax if it is terminating . For example:
                            System.out.print("Hello world!");
           Here , ";" is used to terminate the syntax.
           Example of non-terminating syntax is :
            if( a>b)
            We can't terminate if and else conditions , because if we do so then it will not read its                            remaining portion .

  •  "Else" cannot be used without "if " but "if " can be used without "else ".
  • Don't change the "casing" of words during coding .For example if you used "a" as a variable then you cannot use "A" in place of that because they both are different words in Java's point of view.
  • While using "if " and "else" syntax , if you need more than one function or syntax, you will need to use curly brackets for that.
  • Pre-defined words (keywords) cannot be used as identifies ( variables ).


Writing your first java program:

import java.lang.*;
public class Test
{
   public static void main (String[] args)
     {
            System.out.println("Hello world!");
            System.out.print("This is our first Java program.");
      }
}



The output of Code will be:

"Hello word
This is our first Java program. "

Explanation of code:
  • "import java.lang.*;" .This command is importing the package.
  • "public class Test". This command will create a new class with name "test".
  • "public static void main(String[] args) ".This command is used to differentiate between application and applet.
  • "System.out,println("Hello world!");". This command will generate an output showing "Hello world!" and then cursor will move to next line because "ln" after "print" means that after generating the display of the given string , cursor will move to new line.
  • "System.out.print("This is our first Java Code.");" . This command is also used to display the output to user  ,but the difference is that it doesn't move cursor to next line  after displaying the string if anything is written after that syntax,it will execute that in same line.

So here was our first and simplest java program.If you like this then please share it with your friends .
Keep Visiting and Keep learning.

Sunday, March 20, 2016

Basics of Java programming language

As we are already aware of the Java's benefits and introduction , now we can start implementing it ..
If you have not read the introduction and benefits of Java then click here to read it first:
"Introduction to Java".
So lets get started.In this tutorial we will learn about some basics of Java programming language.
Before proceeding further , keep in mind that Java is a case sensitive language . It treats "a" and "A" as two different variables which means we cannot use "A" in place of "a".
Here are the few important and commonly used terms , commands (Syntax) , keywords and their role.


A brief Introduction to Java language

Java is an object oriented language.It is platform independent language and it is a case sensitive language.For example it treats "Test" and "test" as two different variables. It is considered to be fourth generation language.
Java can be used for two main purposes:
  • For Creating Application
  • For Creating Applets

What is difference between an Application and an Applet ?

Application is the program which can be executed (run ) on windows.While an Applet is a program which can't be executed on windows , it can only be used on websites.

introduction to java