Labels

Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Saturday, April 2, 2016

A program to use command line argument;

A program to use command line argument;

public class Q_3 {
    public static void main(String[] args)
    {
        for(int i=0;i=args.length;i++)
            System.out.println("args ["+i+"]"+args[i]);
    }
}


A program to show the concept of method overiding;

A program to show the concept of method overiding;

import java.io.*;
class A{
    private int a;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    void input() throws IOException
    {
        System.out.print("Enter the value of A: ");
        a= Integer.parseInt(br.readLine());
    }
}
class B extends A{
    private int b;
    @Override
    void input() throws IOException
    {
        //super.input();
        System.out.print("Enter the value of B: ");
        b=Integer.parseInt(br.readLine());
    }
    void output()
    {
        System.out.println("A does not take any value.\nB: "+b);
    }
}
public class Q_2 {
    public static void main(String[] args) throws IOException
    {
        B b=new B();
        b.input();
        b.output();
    }
}



Monday, February 15, 2016

A java program to calculate combinations of r objects from a set of n objects

A java program to calculate combinations of r objects from a set of n objects;

import java.util.Scanner;
class Recursion{
    double c,n;
    double A (double n){
       if (n==1)
           return 1;
       c=A(n-1)*n;
       return c;
    }
    double B1(double r)
    {
        if(r==1)
           return 1;
        n=B1(r-1)*r;
        return n;
    }
    double B2(double d)
    {
        double r;
        if(d==1)
            return 1;
        r=B2(d-1)*d;
        return r;
    }
}
public class comb {
    public static void main(String[] args)
    {
        Scanner s=new Scanner(System.in);
        System.out.print("Enter the highest power of combination: ");
        double n= s.nextDouble();
        System.out.print("Enter the power till which user wants answer: ");
        double r= s.nextDouble();
        Recursion rec=new Recursion();
        double a,b,diff;
        diff=n-r;
        a= rec.A(n);
        b= rec.B1(r)*rec.B2(diff);
        double result= a/b;
        System.out.println("Result: "+result);
    }
}

To run source code follow following steps:-
  • Save file with name comb.java
  • Open terminal or cmd and move to file location
  • Type: javac comb.java
  • Type: java comb

Tuesday, February 9, 2016

A Java program to show use of abstract class;

A Java program to show use of abstract class;

abstract class A{
    abstract void callme();
    void callmeto()
    {
        System.out.println("This is a concrete class");
    }
}
class B extends A{
    @Override
    void callme()
    {
        System.out.println("B's implementation on callme()");
    }
}
class C extends A{
    @Override
    void callme()
    {
        System.out.println("C's implementation of callme()");
    }
}
public class Abstract_demo {
    public static void main(String[] args)
    {
        B b=new B();
        C c=new C();
        b.callmeto();
        b.callme();
        c.callme();
    }

}

To run the program follow following step:-

  • Save code with name Abstract_demo.java
  • Open terminal and cmd and move to file location.
  • Compile: javac Abstract_demo.java
  • Run: java Abstract_demo




Monday, February 8, 2016

A Java program to find the largest and smallest in a array;

A Java program to find the largest and smallest in a array;

public class array {
       
        public static void main(String[] args) {
              
                int numbers[] = new int[]{12,34,1034,34,56,67,89,01,345,235,56};
                int smallest = numbers[0];
                int largetst = numbers[0];
                for(int i=1; i< numbers.length; i++)
                {
                        if(numbers[i] > largetst)
                                largetst = numbers[i];
                        else if (numbers[i] < smallest)
                                smallest = numbers[i];                      
                }
                System.out.println("Largest Number is : " + largetst);
                System.out.println("Smallest Number is : " + smallest);
        }
}

To compile follow following steps:-
  • Save the code with file name array
  • Open terminal or cmd and move to the file location.
  • Type javac array.java
  • Type java array

Saturday, February 6, 2016

A Java program to accept array element from user;

A Java program to accept array element from user;

import java.io.*;

public class user_array {
    public static void main(String[] args) throws IOException
    {
        int n;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the length of array: ");
        n= Integer.parseInt(br.readLine());
        int a[]= new int[n];
        for(int i=0;i        {
            System.out.print("Enter the "+(i+1)+" element of the array: ");
            a[i]= Integer.parseInt(br.readLine());
        }
        for(int i=0;i        {
            System.out.println("The "+(i+1)+" element of array: "+a[i]);
        }
    }
}



To run the above code follow following steps:-

  • Save the above code with file name user_array and extension .java
  • Open terminal or cmd and move to location of file using cd command.
  • To compile type: javac user_array.java
  • To run type: java user_array

A Java program to use multiple methods in a class;

A Java program to use multiple methods in a class;

import java.util.Scanner;

class Teacher{
    String n, a, s, h;
    long p;
    double ms, it;
    void accept()
    {
        Scanner i=new Scanner(System.in);
        System.out.print("Enter name of the teacher: ");
        n= i.nextLine();
        System.out.print("Enter address of the teacher: ");
        a= i.nextLine();
        System.out.print("Enter phone number of the teacher: ");
        p= i.nextLong();
        System.out.print("Enter highest qualification of the teacher: ");
        h= i.nextLine();
        System.out.print("Enter subject specialization of the teacher: ");
        s= i.nextLine();
        System.out.print("Enter monthly salary of the teacher: ");
        ms= i.nextDouble();
    }
    void display()
    {
        System.out.println("Detail of teacher:-\nName: "+n+"\nAddress: "+a+
                        "\nPhone Number: "+p+"\nHighest Qualification: "+h+
                        "\nSubject Specialization: "+s+"\nMonthly Salary: "+ms);
    }
    double isalary()
    {
        int year;
        Scanner i= new Scanner(System.in);
        System.out.print("Enter the years of service: ");
        year= i.nextInt();
        if (year>1)
        {
            ms= ms+ (year*5000);
        }
        return ms;
    }
    double annual()
    {
        double ann;
        ann= ms*12;
        if(ann<=150000)
            it=0;
        else
        {
            it= (5*ann)/100;
        }
        return it;
    }
}
public class multi {
    public static void main(String[] args){
        Teacher t=new Teacher();
        t.accept();
        t.display();
        System.out.println("Monthly Salary of teacher: "+t.isalary());
        System.out.println("Income tax of the teacher: "+t.annual());
    }
}

To run the above code follow following steps:-

  • Save the above code with file name multi and extension .java
  • Open terminal or cmd and move to location of file using cd command.
  • To compile type: javac multi.java
  • To run type: java multi

Thursday, February 4, 2016

Accept user data using Java IO package;

Accept user data using Java IO Package;

import java.io.*;

public class accept {
public static void main(String[] args) throws IOException{
int a;
String b;
double c;
char d;
long e;
short f;
float g;
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter an integer: ");
a= Integer.parseInt(br.readLine());
System.out.println("Enter a string: ");
b= br.readLine();
System.out.println("Enter a double: ");
c= Double.parseDouble(br.readLine());
System.out.println("Enter a character: ");
d= (char) br.read();
System.out.println("Enter a long: ");
e= Long.parseLong(br.readLine());
System.out.println("Enter a short: ");
f= Short.parseShort(br.readLine());
System.out.println("Enter a float: ");
g= Float.parseFloat(br.readLine());
System.out.println(a+"\n"+b+"\n"+c+"\n"+d+"\n"+e+"\n"+f+"\n"+g);
}

}

To run the above code follow following steps:-

  • Save the above code with file name accept and extension .java
  • Open terminal or cmd and move to location of file using cd command.
  • To compile type: javac accept.java
  • To run type: java accept

Accept user data using Scanner class;

Using Scanner Class in Java to input user data;

import java.util.Scanner;

public class scan {
public static void main(String[] args){
int a;
String b;
double c;
long e;
float f;
short g;
Scanner s= new Scanner(System.in);
System.out.print("Enter a integer: ");
a= s.nextInt();
System.out.print("Enter a string: ");
b= s.nextLine();
System.out.print("Enter a double: ");
c= s.nextDouble();
System.out.print("Enter a long: ");
e= s.nextLong();
System.out.print("Enter a float: ");
f= s.nextFloat();
System.out.print("Enter a short: ");
g= s.nextShort();
System.out.println(a+"\n"+b+"\n"+c+"\n"+e+"\n"+f+"\n"+g);
}

}

To compile the following code do following things:-

  • Save the above code with a file name scan and with extension .java
  • Now, open your terminal or cmd and move to the location where your file is being saved using cd command.
  • Compile java code using javac . Here file name is scan so type javac scan.java
  • Run the code using java . Here file name is scan so type java scan


Monday, September 28, 2015

Program to print an integer

Open Notepad and type the following:-

public class ex2
{
    public static void main(String [] args)
    {
        int num;
        num=100;
        System.out.println("The number is "+num);
     }
}



  1. Save the file in desktop.
  2. Open cmd in windows or terminal in Linux.
  3. In windows enter cd desktop
  4. to compile enter javac <file_name>.java   //in this condition this is ex2.
  5. then enter java <file_name>   // to run the bytecode.

Simple Java Program

Open Notepad and type the following:-

public class ex1
{
    public static void main(String[] args)
    {
        System.out.println("Hello Java!");
    }
}


  1. Save the file in desktop.
  2. Open cmd in windows or terminal in Linux.
  3. In windows enter cd desktop
  4. to compile enter javac <file_name>.java   //in this condition this is ex1.
  5. then enter java <file_name>   // to run the bytecode.

Thursday, September 17, 2015

How to install jdk in Linux


  1. Press Ctrl+Alt+T to open the terminal
  2. write a command "sudo apt-get install openJDK8" and press enter.
  3. To complete the process you should be connected to a network.
                                 OR
If you are a Ubuntu user you can use its software center to download the JDK and other software.

NOTE: Tutorial Link

Lets Start Java

Things needed for starting Java Programming in Windows

  1. JDK (Java Development Kit) try to use the latest one.
  2. JRE (Java Runtime enviorment) same version of the JDK.
  3. Notepad++ 

Note: User can also use Notepad which come pre-installed in Windows.

Note: If you like you could also use IDE (integrated development environment) their are two very popular Java IDE's 


NOTE: Link for tutorial to install Java in Windows