Wednesday, October 6, 2010

Java - Basics by Example



Continue with the Basics by Example; today's version of the post written in Java Enjoy!

You can copy and paste the code below in your favorite IDE/Editor and start playing and learning with it. This little "working" program will teach you the basics of the Programming Language.

There are some "comments" on the code added just to tell you what are or how are some features called. In case you want to review the theory, you can read my previous post, where I give a definition of each of the concepts mentioned on the code. You can find it here: http://carlosqt.blogspot.com/2010/08/new-series-languages-basics-by-example.html 


Greetings Program - Verbose
// Java Basics
package jgreetprogram;
import java.util.Calendar;

public class Greet {
    // Fields or Attributes
    private String message;
    private String name;
    private int loopMessage;
    // Getters and Setters. No Properties built-in syntax available
    public String getMessage() {
        return this.message;
    }
    public void setMessage(String val) {
        this.message = this.Capitalize(val);
    }
    public String getName() {
        return this.name;
    }
    public void setName(String val) {
        this.name = this.Capitalize(val);
    }
    public int getLoopMessage() {
        return this.loopMessage;
    }
    public void setLoopMessage(int val) {
        this.loopMessage = val;
    }
    // Constructor
    public Greet() {
        this.message = "";
        this.name = "";
        this.loopMessage = 0;
    }
    // Overloaded Constructor
    public Greet(String message, String name, int loopMessage) {
        this.message = this.Capitalize(message);
        this.name = this.Capitalize(name);
        this.loopMessage = loopMessage;
    }
    // Method 1
    private String Capitalize(String val) {
        // "if-then-else" statement
        if (val.length() >= 1) {
            return val.substring(0,1).toUpperCase()
                    + val.substring(1, val.length());
        }
        else  {
            return "";
        }
    }
    // Method 2
    public void Salute() {
        // "for" statement
        for (int i = 0; i < this.loopMessage; i++) {
            System.out.println(this.message + " " + this.name + "!");
        }
    }
    // Overloaded Method 2.1
    public void Salute(String message, String name, int loopMessage) {
        // "while" statement
        int i = 0;
        while(i < loopMessage) {
            System.out.println(this.Capitalize(message) + " "
                    + this.Capitalize(name) + "!");
            i++;
        }
    }
    // Overloaded Method 2.2
    public void Salute(String name) {
        // "switch/case" statement
        Calendar dtNow = Calendar.getInstance();
        switch (dtNow.HOUR_OF_DAY)
        {
            case 6: case 7: case 8: case 9: case 10: case 11:
                this.message = "good morning,";
                break;
            case 12: case 13: case 14: case 15: case 16: case 17:
                this.message = "good afternoon,";
                break;
            case 18: case 19: case 20: case 21: case 22:
                this.message = "good evening,";
                break;
            case 23: case 0: case 1: case 2: case 3: case 4: case 5:
                this.message = "good night,";
                break;
            default:
                this.message = "huh?";
                break;
        }
        System.out.println(this.Capitalize(this.message) + " "
            + this.Capitalize(name) + "!");
    }
}

// second source file
package jgreetprogram;
import java.util.Scanner;
// Console Program
public class Main { 
    public static void main(String[] args) {
        // Define variable object of type Greet
        Greet g;
        // Instantiate Greet. Call Constructor
        g = new Greet();
        // Call Setters
        g.setMessage("hello");
        g.setName("world");
        g.setLoopMessage(5);
        // Call Method 2
        g.Salute();
        // Overloaded Method 2.1 and Getters
        g.Salute(g.getMessage(), "java", g.getLoopMessage());
        // Overloaded Method 2.2
        g.Salute("carlos");

        // Stop and exit
        System.out.println("Press any key to exit...");
        Scanner in = new Scanner(System.in);
        String line = in.nextLine();
        in.close();
    }
}

Greetings Program - Minimal
// Java Basics
package jgreetprogrammin;
import java.util.Calendar;

class Greet {
    // Fields or Attributes
    private String message;
    private String name;
    private int loopMessage;
    // Getters and Setters. No Properties built-in syntax available
    String getMessage() {
        return message;
    }
    void setMessage(String val) {
        message = Capitalize(val);
    }
    String getName() {
        return name;
    }
    void setName(String val) {
        name = Capitalize(val);
    }
    int getLoopMessage() {
        return loopMessage;
    }
    void setLoopMessage(int val) {
        loopMessage = val;
    }
    // Constructor
    Greet() {
        message = "";
        name = "";
        loopMessage = 0;
    }
    // Overloaded Constructor
    Greet(String message, String name, int loopMessage) {
        this.message = Capitalize(message);
        this.name = Capitalize(name);
        this.loopMessage = loopMessage;
    }
    // Method 1
    private String Capitalize(String val) {
        // "if-then-else" statement
        if (val.length() >= 1) {
            return val.substring(0,1).toUpperCase()
                    + val.substring(1, val.length());
        }
        else  {
            return "";
        }
    }
    // Method 2
    void Salute() {
        // "for" statement
        for (int i = 0; i < loopMessage; i++) {
            System.out.println(message + " " + name + "!");
        }
    }
    // Overloaded Method 2.1
    void Salute(String message, String name, int loopMessage) {
        // "while" statement
        int i = 0;
        while(i < loopMessage) {
            System.out.println(Capitalize(message) + " "
                    + Capitalize(name) + "!");
            i++;
        }
    }
    // Overloaded Method 2.2
    void Salute(String name) {
        // "switch/case" statement
        Calendar dtNow = Calendar.getInstance();
        switch (dtNow.HOUR_OF_DAY)
        {
            case 6: case 7: case 8: case 9: case 10: case 11:
                message = "good morning,";
                break;
            case 12: case 13: case 14: case 15: case 16: case 17:
                message = "good afternoon,";
                break;
            case 18: case 19: case 20: case 21: case 22:
                message = "good evening,";
                break;
            case 23: case 0: case 1: case 2: case 3: case 4: case 5:
                message = "good night,";
                break;
            default:
                message = "huh?";
                break;
        }
        System.out.println(Capitalize(message) + " "
            + Capitalize(name) + "!");
    }
}

// second source file
package jgreetprogrammin;
import java.util.Scanner;
// Console Program
public class Main {
    public static void main(String[] args) {
        // Define variable object of type Greet and Instantiate. Call Constructor
        Greet g = new Greet();
        // Call Setters
        g.setMessage("hello");
        g.setName("world");
        g.setLoopMessage(5);
        // Call Method 2
        g.Salute();
        // Overloaded Method 2.1 and Getters
        g.Salute(g.getMessage(), "java", g.getLoopMessage());
        // Overloaded Method 2.2
        g.Salute("carlos");

        // Stop and exit
        System.out.println("Press any key to exit...");
        Scanner in = new Scanner(System.in);
        String line = in.nextLine();
        in.close();
    }
}


And the Output is:

1 comment:

  1. personally dislike java :)
    It's looking like C++ but it is not >_<

    ReplyDelete