LPU Java Unit 1 Lecture 8 Snippet Conditional Statements RB | LPUCOLAB

LPU Java Unit 1 Lecture 8 Snippet Conditional Statements RB | LPUCOLAB

LPU_Java_Unit 1_Lecture 8_Snippet_Conditional Statements_RB

1. Problem Statement

Sandeep is tasked with creating a credit score calculator program that assesses an individual's creditworthiness based on different criteria. The program should provide credit scores in three categories: Credit Score, Debt Score, and Income Score.

Credit Score Calculation

  • If the choice is 1, input the number of late payments.
    • If the late payments are 0, the credit score is 800.
    • If the late payments are 1, the credit score is 750.
    • If the late payments are 2, the credit score is 700.
    • If the late payments are 3, the credit score is 650.
    • If the late payments are 4, the credit score is 600.
    • If the late payments are 5, the credit score is 550.

Debt Score Calculation

  • If the choice is 2, input the debt-to-income ratio.
    • If the debt-to-income ratio is 30 or less, the debt score is "Good."
    • If the debt-to-income ratio is between 30 (exclusive) and 50 (inclusive), the debt score is "Average."
    • If the debt-to-income ratio is above 50, the debt score is "Poor."

Income Score Calculation

  • If the choice is 3, input the annual income.
    • If the annual income is 50000 or more, the income score is "Good."
    • If the annual income is between 30000 (exclusive) and 50000 (inclusive), the income score is "Average."
    • If the annual income is less than 30000, the income score is "Poor."

Help Sandeep to complete the task using 'switch-case' statements.

Input format:

  • The first line of input consists of an integer, representing the choice.
  • Depending on the choice, the second line of input consists of the number of late payments (integer) or debt-to-income ratio (double) or annual income (double).

Output format:

  • The output prints the ranking of the corresponding credit, or debt, or income score.

Solution:

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();

        switch (choice) {
            case 1:
                int latePayments = scanner.nextInt();
                int creditScore;
                switch (latePayments) {
                    case 0: creditScore = 800; break;
                    case 1: creditScore = 750; break;
                    case 2: creditScore = 700; break;
                    case 3: creditScore = 650; break;
                    case 4: creditScore = 600; break;
                    case 5: creditScore = 550; break;
                    default: creditScore = 0; break;
                }
                System.out.println("Credit Score: " + creditScore);
                break;

            case 2:
                double debtToIncome = scanner.nextDouble();
                String debtScore;
                if (debtToIncome <= 30) {
                    debtScore = "Good";
                } else if (debtToIncome <= 50) {
                    debtScore = "Average";
                } else {
                    debtScore = "Poor";
                }
                System.out.println("Debt Score: " + debtScore);
                break;

            case 3:
                double annualIncome = scanner.nextDouble();
                String incomeScore;
                if (annualIncome >= 50000) {
                    incomeScore = "Good";
                } else if (annualIncome >= 30000) {
                    incomeScore = "Average";
                } else {
                    incomeScore = "Poor";
                }
                System.out.println("Income Score: " + incomeScore);
                break;
        }

        scanner.close();
    }

2. Problem Statement

Sophie is a meteorologist, and she needs a program to convert temperatures from Fahrenheit to Celsius. However, she also wants the program to warn her when the entered temperature is outside the human-survivable temperature range. The program should take a temperature in Fahrenheit as input, validate whether it's within the safe range, and convert it to Celsius if it is.

Formula: celsius = (fahrenheit - 32) * 5/9

Input format:

  • The input consists of a double value, representing the temperature in Fahrenheit.

Output format:

  • If the input temperature is within the safe range (between -100°F and 150°F), print the converted temperature in Celsius (rounded to two decimal places).
  • If the input temperature is outside the safe range, print "Temperature outside the human-survivable range."

Solution:

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double fahrenheit = scanner.nextDouble();

        if (fahrenheit >= -100 && fahrenheit <= 150) {
            double celsius = (fahrenheit - 32) * 5 / 9;
            System.out.printf("Temperature in Celsius: %.2f C\n", celsius);
        } else {
            System.out.println("Temperature outside the human-survivable range");
        }

        scanner.close();
    }

3. Problem Statement

Maria is building a program to classify triangles based on side lengths. The program should take three integer inputs and identify if the triangle is equilateral, isosceles, scalene, or right-angled. Help Maria create this classification program.

Note: - Equilateral - all sides equal - Isosceles - two sides equal - Scalene - all sides different - Right-angled - The square of one side equals the sum of the squares of the other two sides.

Input format:

  • The input consists of three space-separated integers, representing the lengths of the sides of the triangle.

Output format:

  • The output should display the type of triangle ("equilateral", "isosceles", or "scalene").
  • If it is right-angled: "The triangle is also right-angled."

Solution:

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int c = scanner.nextInt();

        if (a == b && b == c) {
            System.out.println("The triangle is equilateral");
        } else if (a == b || b == c || a == c) {
            System.out.println("The triangle is isosceles");
        } else {
            System.out.println("The triangle is scalene");
        }

        if (a * a + b * b == c * c || b * b + c * c == a * a || a * a + c * c == b * b) {
            System.out.println("The triangle is also right-angled");
        }

        scanner.close();
    }

4. Problem Statement

Brian is developing a program that requires analyzing numbers. He needs to determine whether a given number is positive, negative, or zero.

Help Brian create the program.

Input format:

  • The input consists of a single integer.

Output format:

  • The output prints whether the given integer is positive, negative, or zero.

Solution:

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int number = scanner.nextInt();

        if (number > 0) {
            System.out.println("Positive");
        } else if (number < 0) {
            System.out.println("Negative");
        } else {
            System.out.println("Zero");
        }

        scanner.close();
    }

5. Problem Statement

Maya is given a two-digit number and needs to find the sum of its digits. She wants to know if the sum is less than 10 or not. Your task is to write a program to calculate and print the sum of the digits and provide a message based on the result.

Input format:

  • The input consists of an integer n.

Output format:

  • The output prints the sum of the digits and a message indicating whether it's less than 10 or not.
    • "Digit Sum: <>"
    • "The sum of the digits is less/not less than <>."
  • If n is not a two-digit number, print "Not a valid two-digit number."

Solution:

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();

        if (n >= 10 && n <= 99) {
            int sum = (n / 10) + (n % 10);
            System.out.println("Digit Sum: " + sum);
            if (sum < 10) {
                System.out.println("The sum of the digits is less than 10.");
            } else {
                System.out.println("The sum of the digits is not less than 10.");
            }
        } else {
            System.out.println("Not a valid two-digit number.");
        }

        scanner.close();
    }

6. Problem Statement

Sarah, a student studying coordinate geometry, needs a program to calculate the slope, distance, and midpoint of line segments. She wants to understand how to work with Cartesian coordinates. Write a program to allow her to choose an operation and calculate the desired property based on two points.

Formulas: - slope = (y2 - y1) / (x2 - x1) - distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) - midpointX = (x1 + x2) / 2 - midpointY = (y1 + y2) / 2

Input format:

  • The first line of input contains an integer representing choices (1 for slope, 2 for distance, 3 for midpoint).
  • The next line contains double values x1, y1, x2, y2 representing the coordinates.

Output format:

  • The output displays:
    • For the slope operation, it prints the slope value.
    • For the distance operation, it prints the distance between the two points.
    • For the midpoint operation, it prints the coordinates of the midpoint (x, y).

Solution:

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        double x1 = scanner.nextDouble();
        double y1 = scanner.nextDouble();
        double x2 = scanner.nextDouble();
        double y2 = scanner.nextDouble();

        switch (choice) {
            case 1:
                if (x2 != x1) {
                    double slope = (y2 - y1) / (x2 - x1);
                    System.out.println("Slope: " + slope);
                } else {
                    System.out.println("Slope: Undefined");
                }
                break;
            case 2:
                double distance = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
                System.out.printf("Distance: %.2f\n", distance);
                break;
            case 3:
                double midpointX = (x1 + x2) / 2;
                double midpointY = (y1 + y2) / 2;
                System.out.printf("Midpoint: (%.1f, %.1f)\n", midpointX, midpointY);
                break;
            default:
                System.out.println("Invalid choice. Please select a valid option.");
                break;
        }

        scanner.close();
    }