ECS2301 Software Engineering and Project – Lesson 3 (Data types & operators)

    • Java data types
    • Operators
    • Variables
    • Generating JavaDoc with NetBeans.
    • Using TODO to track action items

Java data types

The following code tests various data types including sinhala unicode.

package datatype;

/**
 * Testing Java data types
 * @author azmeer
 */
public class W03aDataTypes {

    public static void main(String[] args) {
        int age = 25;
        float f = 10.345f;
        double d = 123.4567;
        long l = 1234567897l;
        char c = 'A';
        char sinhala = '\u0D85'; //Sinhala unicode ayanna
        String name = "Azmeer";
        
        System.out.println(age);
        System.out.println("Your age is " + age);
        
        System.out.println("f = " + f);
        System.out.println("d = " + d);
        System.out.println("l = " + l);
        System.out.println("c = " + c);
        System.out.println("Sinhala  = " + sinhala);
        System.out.println("\u0d85 \u0db8\u0dca"); //sinhala unicode
        System.out.println("My name is " + name);
    }
}

Java arithmatic operators

package operators;

/**
 * Testing Java arithmatic operators
 * @author azmeer
 */
public class W03bOperators {

    public static void main(String[] args) {
        System.out.println("Java Operators");
        System.out.println("Arithmatic Operators");

        int n1 = 10;
        int n2 = 6;
        int x;

        System.out.println("n1 = " + n1 + " n2 = " + n2);

        x = n1 + n2;
        System.out.println("Addition n1 + n2 = " + x);

        x = n1 - n2;
        System.out.println("Substraction n1 - n2 = " + x);

        x = n1 * n2;
        System.out.println("Multiplication n1 * n2 = " + x);

        x = n1 / n2;
        System.out.println("Division n1 / n2 = " + x);

        x = n1 % n2;
        System.out.println("Modulo n1 % n2 = " + x);

        System.out.println("\nUnary Operators");
        int a = 10, b = 0;
        System.out.println("a=" + a + " b=" + b);

        b = a;
        System.out.println("a=" + a + " b=" + b);

        b = a++;
        System.out.println("a=" + a + " b=" + b);

        b = ++a;
        System.out.println("a=" + a + " b=" + b);

        System.out.println("\nAssignment Operators");
        int m = 10, n = 1;
        System.out.println("m=" + m + " n=" + n);

        n += m; //same as n = n+m
        System.out.println("m=" + m + " n=" + n);

        n -= m; //same as n = n-m
        System.out.println("m=" + m + " n=" + n);

        System.out.println("\nRelational Operators");
        int y = 10;
        if (y == 10) {
            System.out.println("Yes y is 10");
        }
        
        if(y != 20){
            System.out.println("Yes y is NOT equal to 20");
        }
        
        if(y > 8){
            System.out.println("Yes y is bigger than 8");
        }
        
        if(y < 20){
            System.out.println("Yes y is smaller than 20");
        }
        
        if(y >= 10){
            System.out.println("Yes y is bigger than or equal to 10");
        }
        
        if(y <= 10){
            System.out.println("Yes y is smaller than or equal to 10");
        }
        
    }
}

Testing whether a number is within given range

Here we are testing whether a user given number is within 10 and 20 (inclusive). Notice how we use the “Scanner” object.

package testnumberrange;

import java.util.Scanner;

/**
 *
 * @author azmeer
 */
public class W03cTestNumberRange {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int x;
        System.out.println("Test whether the given number is between 10 and 20, inclusive.");
        System.out.print("Please enter a number: ");
        x = in.nextInt();

        if (x >= 10 && x <= 20) {
            System.out.println("YES your number is within 10 and 20");
        } else {
            System.out.println("NO your number is not within 10 and 20");
        }
    }
}

All lessons >

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.