Sponsored Links

Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

November 24, 2017

Factorial By Recursion


import java.util.Scanner;

public class RecursionEx {
 //main
 public static void main(String args[]){
  Scanner temp = new Scanner(System.in);
  System.out.println("Enter number to find its factorial: ");
  long x = temp.nextLong();
  System.out.println(fact(x));
  
 }
 
 //fact
 public static long fact(long n){
  if(n<=1)
   return 1;
  else
   return n * fact(n-1);
 }
}

Constructor in Java

class Sample{
 private String girlName;
 public Sample(String name){
  girlName=name;
 }
 public void setName(String name){
  girlName=name;
 }
 public String getName(){
  return girlName;
 }
 public void displaydata(){
  System.out.printf("Name of the girl is %s", getName());
 }
 
}

public class ConstructorEx {
 public static void main(String args[]){
  Sample sampleObj=new Sample("priya");
  sampleObj.displaydata();
 }
}

November 23, 2017

Program to sort numbers

class Sorting{
 public static void main(String args[]){
  int number[]={55,40,70,80,90,21};
  int n = number.length;
                System.out.print("Given list:");
                for (int i=0;i<n;i++)
                {
                    System.out.print("\t"+number[i]);
                }
                System.out.println("\n");
            //Sorting begins
                for(int i=0;i<n;i++){
                    for (int j=i+1;j<n;j++){
                        if (number[i]<number[j]){
                            //interchange values
                               int temp=number[i];
                               number[i]=number[j];
                               number[j]=temp;
                        }
                    }
                }
                System.out.print("Sorted list:");
                for(int i=0;i<n;i++){
                    System.out.print("\t"+number[i]);
                    
                }
 }

Hello World in JAVA

It is the trend to write the program to print "Hello World" while starting with any language. We will also do the same. Below is the program and I will also explain useful things used in program.

class Hello{
 public static void main(Strings[] args){
   System.out.println("Hello World!!!");
 }
}
  1. Open any text editor and write the above program. Note that some are in capital letters.
  2. Now save the file with name Hello.java
  3. Open command prompt and browse the location where the file is located.
  4. For eg: If the file is located in c:/javatut/ then goto the same folder location in cmd.
  5. Now type javac Hello.java
  6. This command will compile your java file and creates class file.
  7. Now type java Hello
  8. If the compilation worked then you will get the output.

Some explanations

  • The first statement defines the main class which is required and the class name should be in initial capital letter.
  • The second statement defines the main function or method of the class. The first letter of String is capitalized as it is the class name.
  • The third statement is used to display output. First letter of System is capitalized and the println statement creates new line after the output. You can also sumply use print only.

Basic rules while programming in JAVA

There are some of the ground rules while you are programming in JAVA. Before we start on writing the first program we will talk about common rules you must follow, which you must mock up.

  • The file name must be same as the main class name you used.
  • Class name must be start with the initial capital letter. Java is case sensitive language.
  • Any classes you used in the program must start with the capital letter and the method name with small letter.
  • The class file is obtained after the compilation of the program and it is the bytecode that can run in any platform which hava JDK.
We will start with the first program and syntax in out next post. Till then stay tune...

How to set path for JDK

In the previous post i have mentioned about the software and hardware requirements to get started with java programming. Only having those requirements is not enough. We need to set path of the JDK as the environment variables. There are two ways for doing this.

Process 1

We can set the path from the command prompt. The steps are mentioned below.
  1. Open the command prompt.
  2. Type javac and press enter.
  3. If the result is shown as the command is not recognized, that means path is not set. Or if some help commands are shown then path is already set.
  4. To set path first you need to copy the location of jdk installed in your drive.
  5. Goto c:\program files and find java folder. Inside the java folder search for folder with jdk and its version. Inside it goto bin folder. And click on any file and view properties and there copy the path. It looks like this "C:\Program Files\Java\jdk1.5.0_09\bin" . Copy it.
  6. In the command prompt, type "set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin" without the double quotes. The path is only sample example. It may vary according to the JDK version.
  7. Now to check whether the path is set correctly or not, type javac and press enter. Now the results are shown instead of showing error. Now you can run the java program.

Process 2

  1. Goto the windows properties.
  2. Click on advanced system setting.
  3. Click the environment variables.
  4. In the system variables, click on add new.
  5. Then enter "PATH" in variable name and in the variable value enter the path that is copied as in process 1.
  6. To check do as in process 1.
Sometimes following process 2 may not work properly, so process 1 is followed mostly. In the next post I will write the first program and ways to run in the command prompt.

JAVA Introduction

Java is one of the object oriented programming language which is used to make complex programs and applications. Java has many features which you will learn through out this tutorials. I here present the basic syntax, codes, program examples for you. Java is platform independent language so you can run this in any machine.
There are some of the requirements to run the java program and I will tell you what.
  1. You need to install Java Development Kit(JDK) in your machine before you run program and you also need to set path which I will show you later.
  2. You need a java editor like eclipse, netbeans or you can simply use notepad for writing the codes.
  3. You need computer system with pentium processor.

You can download the JDK from the java site http://www.oracle.com/technetwork/java/javase/downloads/index.html and select one that is compactible with your machine. In next post i will write about how to code your first program and run in command prompt. Bye Bye...