First Java Programs

Thierry Coulbois

November, 2nd 2002



Here are the first programs in Java that we studied in my lecture. Look at them carefully and copy them on your computer and pre-compile and execute them.

  1. Hello World
  2. Employee (first version)
  3. Employee with accessors and mutators
  4. Employee with a static field

barre

Hello World

public class HelloWorld{
    
    public static void main(String[] args){
           System.out.println("Hello World !");
           System.out.println(" اهلا و سهلا  !");
    }
}

barre

Employee (first version) and a class to test it.

class Employee{
    public String name;
    public int salary;
    
    public Employee(){}
    
    public Employee(String aName, int aSalary){
        name=aName;
        salary=aSalary;
    }
    
    public void raiseSalary(int rate){
        salary=salary+salary*rate/100;
    }
    
}


public class EmployeeTest{
    
    public static void main(String[] args){
        Employee thierry=new Employee();
        thierry.name="Thierry Coulbois";
        thierry.salary=1000;
        thierry.raiseSalary(10);
        System.out.println(thierry.name+"'s salary is "+thierry.salary);
        Employee rais=new Employee("ساري نسيبه",2000);
        System.out.println("The president is "+rais.name);
    }
}

barre

Employee with accessors and mutators

/** An Employee up to now has a name and a salary.
    The Employee class is better than the previous 
    version, because the fields are private, and you 
    have mutators and accessors. */
class Employee{
    private String name; //no need to write documentation for these fields they are private.
    private int salary;
    
    /** The default constructor */
    public Employee(){}
    
    /** The correct constructor that set the name and the salary.
    */
    public Employee(String aName, int aSalary){
        name=aName;
        salary=aSalary;
    }
    

    /** Accessor to the name field. */    
    public String getName(){
        return name;
    }
    
    /** Accessor to the salary field. */
    public int getSalary(){
        return salary;
    }
    
    /** Mutator of the name field. */
    public void setName(String aName){
        name=aName;
    }
    
    /** Mutator of the salary field. */
    public void setSalary(int aSalary){
        salary=aSalary;
    }
    
    /** Raise the salary by rate percent. */
    public void raiseSalary(int rate){
        salary=salary+salary*rate/100;
    }
    
}

/** Tests our Employee class version 2.*/
public class EmployeeTest{
    
    /** Create two employees and rase the salary of one of them. */
    public static void main(String[] args){
        Employee thierry=new Employee();
        thierry.setName("Thierry Coulbois"); //we have to use the mutators
        thierry.setSalary(1000);
        thierry.raiseSalary(10);
        System.out.println(thierry.getName()+"'s salary is "+thierry.getSalary()); //we have to use the accessors
        Employee rais=new Employee("ساري نسيبه",2000);
        System.out.println("The president is "+rais.getName());
    }
}

barre

Latest Version: with a static field

/** An Employee has a name and a salary and an identity number.
    The Employee class is better than the previous 
    version, because the fields are private, and you 
    have mutators and accessors. 
    @version 3*/
class Employee{
    private String name; //no need to write documentation for these fields they are private.
    private int salary;
    private final int idNumber; //the identity number of an employee cannot be modified : it is final.
    
    private static int nextIdNumber=1; //this is a class field that is initialized with 1.
    
    /** The default constructor */
    public Employee(){
        idNumber=nextIdNumber;
        nextIdNumber++;
    }
    
    /** The correct constructor that set the name and the salary.
    */
    public Employee(String aName, int aSalary){
        this();    //We first execute the Employee() constructor to set the identity number.
        name=aName;
        salary=aSalary;
    }
    

    /** Accessor to the name field. */    
    public String getName(){
        return name;
    }
    
    /** Accessor to the salary field. */
    public int getSalary(){
        return salary;
    }
    
    /** Accessor ot the identity number field. */
    public int getIdNumber(){
        return idNumber;
    }
    
    /** Mutator of the name field. */
    public void setName(String aName){
        name=aName;
    }
    
    /** Mutator of the salary field. */
    public void setSalary(int aSalary){
        salary=aSalary;
    }
    
    /** Raise the salary by rate percent. */
    public void raiseSalary(int rate){
        salary=salary+salary*rate/100;
    }
    
    /** Accessor for the next identity number. */
    public static int getNextIdNumber(){
        return nextIdNumber;
    }
    
}


/** Tests our Employee class 
@version 3*/
public class EmployeeTest{
    
    /** Create two employees and rase the salary of one of them. */
    public static void main(String[] args){
        Employee thierry=new Employee();
        thierry.setName("Thierry Coulbois"); //we have to use the mutators
        thierry.setSalary(1000);
        thierry.raiseSalary(10);
        System.out.println(thierry.getName()+" ("+thierry.getIdNumber()+")'s salary is "+thierry.getSalary()); //we have to use the accessors
        Employee rais=new Employee("ساري نسيبه",2000);
        System.out.println("The president (number : "+rais.getIdNumber()+") is "+rais.getName());
        System.out.println("The next employee will have identity number : "+Employee.getNextIdNumber());
    }
}