Navigation Menu

Currently the navigation bar is not functioning. Use the Blog Archive or the Google Custom Search to find content.
Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Monday, 1 July 2013

Java program based on static members

Program to count the number of objects created for a class using static member.

Code :
class Obj{
    static int count;
    Obj(){
        count++;
        System.out.print("\nObject  "+count+" created...");
    }
    static void showCount(){
        System.out.print("\nTotal Object Count  :  "+count);
    }
}
class ObjCount{
    public static void main(String args[]){
        Obj o1=new Obj();
        Obj o2=new Obj();
        Obj.showCount();
        Obj o3=new Obj();
        Obj.showCount();
    }  
}
Output :
Object  1 created...
Object  2 created...
Total Object Count  :  2
Object  3 created...
Total Object Count  :  3
Note : Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.

Sunday, 30 June 2013

Java program to sort a given array

Java program to sort a given array.

Code :
class Array{
    int a[]={9,7,4,-1,0};
    voidsortArray(){
    int i,j,temp;
    for(i=0;i<a.length;i++){
        for(j=0;j<a.length-1;j++){
            if(a[j]>a[j+1]){
                    temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }
    }
    voidshowArray(){
        for(inti=0;i<a.length;i++){
            System.out.print(" a["+i+"] : "+a[i]+"\t");
        }
    }
}
class Sort{
    public static void main(String args[]){
        Array t=new Array();
        System.out.print("\n Array Elements : \n ");
        t.showArray();
        System.out.print("\n Sorted Array Elements : \n ");
        t.sortArray();
        t.showArray();
    }
}
Output :
Array Elements :
  a[0] : 9       a[1] : 7        a[2] : 4        a[3] : -1       a[4] : 0

 Sorted Array Elements :
  a[0] : -1      a[1] : 0        a[2] : 4        a[3] : 7        a[4] : 9
Note : Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.

Saturday, 29 June 2013

Java program based on constructor

Program to keep record of height and weight of people.

Code :
import java.io.*;
class Person{
    float height;
    float weight;
    Person(float h,float w){
        height=h;
        weight=w;
    }
    void putPersonData(){
        System.out.print("\nHeight : "+height+" Weight : "+weight);
    }
};
class PersonCount{
    static int count;
    static Person p[]=new Person[10];
    static void showCount(){
        for(int i=0;i<10;i++){
            p[i].putPersonData();
            if(p[i].height>170 && p[i].weight<50){
                count=count+1;            
            }
        }
        System.out.print("\n\nTotal Count (weight<50kg AND height>170cm ) : "+count+"\n");
    }
    public static void main(String args[]){
        p[0]=new Person(173,40);
        p[1]=new Person(170,30);
        p[2]=new Person(168,55); 
        p[3]=new Person(172,55);
        p[4]=new Person(169,55);
        p[5]=new Person(175,70);
        p[6]=new Person(171,45);
        p[7]=new Person(172,55);
        p[8]=new Person(168,55);
        p[9]=new Person(168,65);
        showCount();
    }
}
Output :
Height : 173.0 Weight : 40.0
Height : 170.0 Weight : 30.0
Height : 168.0 Weight : 55.0
Height : 172.0 Weight : 55.0
Height : 169.0 Weight : 55.0
Height : 175.0 Weight : 70.0
Height : 171.0 Weight : 45.0
Height : 172.0 Weight : 55.0
Height : 168.0 Weight : 55.0
Height : 168.0 Weight : 65.0

Total Count (weight<50kg AND height>170cm ) : 2


Program to maintain score card of students.

Code :
import java.io.*;
class Student{
    String name;
    Student(String nm){
        name=nm;
    }
    void putStudentName(){
        System.out.print("\n Student Name : "+name);
    }
};
class Marks{
    Student s;
    int mark1,mark2;
    Marks(String name,int m1,int m2){
        s=new Student(name);
        mark1=m1;
        mark2=m2;
    }
    void putmarks(){
        s.putStudentName();
        System.out.print("\nMark 1 : "+mark1);
        System.out.print("\nMark 2 : "+mark2);
        putAverage();  
    } 
    void putAverage(){
        float t=(mark1+mark2)/(float)2;
        System.out.print("\nAverage : "+t);
    } 
};
class Result{
    public static void main(String args[]){
        Marks m=new Marks("Maverick",80,85);
        m.putmarks();
    }
}
Output :
Student Name : Maverick
Mark 1 : 80
Mark 2 : 85
Average : 82.5
Note : Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.

Tuesday, 18 June 2013

Java program to swap two numbers

Given below is a Java program to swap two numbers.

Code :
// Class Swap
class Swap{
    // method swap 
    void swap(int num1,int num2){
        num1 = num1 + num2;
        num2 = num1 - num2;
        num1 = num1 - num2;
        System.out.println("After swapping\nNumber 1= " + num1 + " and Number 2= "+ num2);
    }
}
// Class SwapDemo 
class SwapDemo{
    public static void main(String[] args) {
        int num1=10,num2=20;
        System.out.println("Number 1: "+num1);
        System.out.println("Number 2: "+num2);
        Swap s=new Swap();
        s.swap(num1,num2);//call to the swap method
    }
}
Output :
Number 1: 10
Number 2: 20
After swapping
Number 1= 20 and Number 2= 10
Note : Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.

Saturday, 1 June 2013

Setting path for running Java programs

The path is an environment variable in operating system like MS Windows,Linux. This variable can be set to values as per users convenience. The path environment variable is usually set so that it enables to compile and run Java program directly from any directory without having to set the path again and again.

Steps to set path :

Method 1 : 
Click on start menu
Click on Run (Alternatively you can press Win+R keys)
Type cmd in the run window and press Enter key
In order to compile Java program you need to set path
set path=[drive:]\Program Files\Java\jdkx.x.x_x\bin;
where x.x.x_x is the version
Note : The above method requires you to set the variable for every new instance of command prompt window opened.

Method 2 : 
Right click on My Computer icon on Desktop
Click on Properties
Select the Advanced tab from the System Properties window
(Alternatively you can press Win+Pause/Break keys)
Click on the button named Environment Variables
In the Environment Variables window find variable named PATH under 
System variables and select it
Either double click the selection or click on Edit option below
In the field named Variable value put a semicolon (;) 
at the end of line (to go to end of line press the END key).
Copy paste the whole path to the bin directory in java
[drive:]\Program Files\Java\jdkx.x.x_x\bin
where x.x.x_x is the version
Click Ok in Edit Environment Variable window
Click Ok in Environment Variables window
Click Ok in System Properties window.
Note : If you feel something is wrong or missing leave a comment below. Please report about broken links.

Friday, 31 May 2013

Compiling and running Java program

To create a Java program type the code in a file and save it as ClassName.java (where ClassName is the name of the class in java file). If the ClassName doesn't match with the name of the class in ClassName.java file the program wont compile. The class you are compiling should have a main() method since this is where the program begins its execution. A program can have one main() method and any number of classes. You dont need to compile individual classes.

Steps to compile and run :

Method 1 :
Click on start menu
Click on Run (Alternatively you can press Win+R keys)
Type cmd in the run window and press Enter key
In order to compile Java program you need to set path
set path=[drive:]\Program Files\Java\jdkx.x.x_x\bin;
where x.x.x_x is the version
In the command prompt type the path to locate the class file
cd /d [drive:]\path
Then to compile the program type 
javac ClassName.java
where ClassName.java is the file that contains the main() method.
Then to run the program type
java ClassName
Note : Notice in the above we didnt mentioned the .java extension that is because the ClassName.java file doesn't run. Instead the ClassName.class file that is created after compilation is run.

Method 2:
Click on start menu
Click on Run (Alternatively you can press Win+R keys)
Type notepad in the run window and press Enter key
In order to compile Java program you need to set path. 
Type in notepad
set path=[drive:]\Program Files\Java\jdkx.x.x_x\bin;
where x.x.x_x is the version
Then to compile the program type in notepad
javac ClassName.java
where ClassName.java is the file that contains the main() method.
Then to run the program type in notepad
java ClassName
Save the file in the same directory where your .java file exists.
Name it as Filename.bat (where Filename is name of the file for 
example run.bat)
Double click on the .bat file to run.
Note : For programs that require an argument while running you have to specify the parameter/s in the file while saving (for example java ClassName 1 2 3)where 1 2 3 are three parameters that are separated using space.

For Netbeans click on the link below :
https://netbeans.org/kb/docs/java/quickstart.html

Note :If you feel something is incorrect or missing leave a comment below. Please report about broken links.