Archive for the ‘Java’ Category

Java Program 3 – Constructor

Program

class Car{

    public Car(){
        System.out.println("Inside car..Constructor");
    }
    
    public void Car(){
        System.out.println("Inside car..Method");
    }
    
    public Car(String color){
        System.out.println("Inside car.." + color);
    }
    
    public void acceleration(){
        System.out.println("Car Acceleration");
    }

}

class TestCar{
    
    public static void main(String args[]){
        Car c1=new Car();
        c1.Car();
        Car c2=new Car("Red");
        c1.acceleration();
    }
}

Read more »

Java Program 2 – Methods

Program

class Counter{
    private int count;

    int a;
    public void increment() {
        count++;
    }

    public void display() {
        
        System.out.println(count);
    }
}

class TestCounter{

    public static void main(String args[]) {
    
        Counter c1=new Counter();
        Counter c2=new Counter();
        
        c1.increment();
        c1.display();
        
        c2.increment();
        c2.display();
        
        System.out.println(c1.a++);
        //System.out.println(c1.count);
        System.out.println(c1.a);
    }

}
 

Read more »

Java Program 1 – Compile and Run a Hello-world Java Program

Step 1
 
Open "Folder" and create a new directory called "myproject" under your home directory to keep all your works.


Step 2
 
Open "Text Editor" (gedit). Enter the following source code and save as "Hello.java" under the "~/myproject" directory created eariler.
 
    public class Hello {   // To save as "Hello.java" under "~/myproject"
       public static void main(String[] args) {
          System.out.println("Hello, world from Ubuntu!");
       }

    }


Read more »