Java Conditional Logic, Loops & Dues Calculator

Classified in Computers

Written on in English with a size of 3.27 KB

Java Membership Dues Calculator Example

Below is a corrected and formatted Java example that demonstrates conditionals, a switch, and loops. All original content has been preserved and corrected for spelling, grammar, and capitalization.

import java.util.Scanner;

public class DuesCalculator {
    public static void main(String[] args) {
        // TODO: application logic here
        // 1
        char standing;
        double gpa = 0, dues = 0;
        // 2
        Scanner keyboard = new Scanner(System.in); // import java.util.Scanner

        System.out.print("Enter your class standing: ");
        standing = keyboard.nextLine().charAt(0);

        System.out.println("Enter your GPA:");
        gpa = keyboard.nextDouble();

        // System.out.println(standing); // testing purposes
        // System.out.println(gpa); // testing purposes 2 to check if things run/print right
        // 3
        if (standing == 'f' || standing == 'F') // '|| standing == 'F'' considers if the input is capitalized
            dues = 150;
        else if (standing == 'm')
            dues = 120;
        else {
            dues = 0;
            System.out.println("Invalid input");
        }

        // 4
        // System.out.printf("%.2f \n", dues); // another print method (.2 prints out 2 decimal places)
        // 5
        if (standing == 'f') {
            dues = 150;
        } else if (standing == 'm') {
            if (gpa >= 3.75)
                dues = 75;
            else
                dues = 120;
        } else if (standing == 'j'){
            if (gpa >= 3.75)
                dues = 50;
            else
                dues = 100;
        } else {
            dues = 0;
            System.out.println("Invalid input");
        }
       // 6
        System.out.println("Your class standing is " + standing);
        System.out.println("Your dues are " + dues);
// 7
        switch (standing){ // You can only use switches to check when the standing value matches cases; it's equality-based (not less than or greater than)
            case 'f':
                dues = 150;
                break;
            case 'm':
                dues = (gpa >= 3.75) ? 75 : 120; // ternary operator: if the condition is true (gpa >= 3.75), assign 75, else assign 120
                break;
            default:
                dues = 0;
                System.out.println("Invalid input");
                break;
        }
        // 8
        System.out.println("The dues are: ");
        System.out.printf("%.2f \n", dues);
    }
}

If Statement

If statement syntax (corrected):

if (expression) {
    statements;
} else if (expression) {
    statements;
} else {
    statements;
}

While Loop

while (expression) {
    statements;
}

Do-While Loop

do {
    statements;
} while (expression);

For Loop

for (int i = 0; i < max; ++i) {
    statements;
}

Switch Statement

Switches check equality against cases; they do not compare ranges.

switch (expression) {
    case value:
        statements;
        break;
    case value2:
        statements;
        break;
    default:
        statements;
}

Related entries: