ECS2301 Software Engineering and Project – Lesson 5 (user input, decision making)

We learn about JAVA user input with Scanner class, decision making with if-then-else and switch-case, then for, while, do-while loops along with advance for loop.

 

Marks and grades

/**
 * Points   Grade 
 * 0–29     failed 
 * 30–34	E 
 * 35–39	D 
 * 40–44	C 
 * 45–49	B 
 * 50–60	A 
 * >60 A+
 *
 * @author azmeer
 */
public class W05aMarksAndGrades {

    public static void main(String[] args) {
        System.out.println("Marks and Grades");
        int mark = 61;
        String grade;

        if (mark > 60) {
            grade = "A+";
        } else if (mark >= 50) {
            grade = "A";
        } else if(mark >= 45){
            grade = "B";
        } else if(mark >= 40){
            grade = "C";
        } else if(mark >= 35){
            grade = "D";
        } else if(mark >= 30){
            grade = "E";
        } else {
            grade = "Fail";
        }

        System.out.println("Your mark=" + mark + " your grade=" + grade);
    }
}

Switch-Case example

/**
 * Switch-case example
 * Case "B" and "C" select the same response
 * @author azmeer
 */
public class W05bSwitchCase {

    public static void main(String[] args) {
        System.out.println("Switch case test");

        String grade = "C";

        switch (grade) {
            case "A+":
                System.out.println("Well done");
                break;
            case "A":
                System.out.println("Good job");
                break;
            case "B":
            case "C":
                System.out.println("Good attempt");
                break;
            default:
                System.out.println("Better luck next time !");
        }
    }
}

Simpler if-then-else with the ternary operator

/**
 * Simpler if-then-else with the ternary operator
 * @author azmee
 */
public class W05cTernaryOp {

    public static void main(String[] args) {
        System.out.println("Odd/Even detection with the ternary operator");
        int n = 5;
        String q;
        q = (n % 2 == 0) ? "Even" : "Odd";
        System.out.println(n + " is " + q);
    }
}

Loops and controls

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

    public static void main(String[] args) {
        System.out.println("While loop");
        int max = 5;
        int n = 0;
        while (n < max) {
            System.out.println(n + " Hello from While-Loop");
            n = n + 1;
        }

        System.out.println("\nFor loop");
        for (n = 0; n < 5; n++) {
            System.out.println(n + " Hello from For-Loop");
        }

        System.out.println("\nDo-While loop");
        n = 0;
        do {
            System.out.println(n + " Hello from Do-While-Loop");
            n = n + 1;
        } while (n < max);

		/* This loop type is suitable to handle arrays and collections */
        System.out.println("\nEnhaced loop");
        int[] marks = {32, 54, 34, 32};
        for (int oneMark : marks) {
            System.out.println(oneMark + " Hello from enhanced-Loop");
        }
        
        System.out.println("\nLoop control \n");
        System.out.println("\nStop at 3");
        for (n = 0; n < 5; n++) {
            if(n==3) {break;}
            System.out.println(n + " Hello from For-Loop");
        }
        
        System.out.println("\nSkip 3");
        for (n = 0; n < 5; n++) {
            if(n==3) {continue;}
            System.out.println(n + " Hello from For-Loop");
        }
    }
}

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.