Search code examples
javabooleanboolean-logicboolean-expression

I have trouble with changing the process of that code in Java


I have trouble with changing my code. I want to do that with a boolean and I have no clue how to do that. It is just confusing me.

This is my task:

Tyrepressure-test

It is to check whether the pressure of all tires is between 35 and 45. If a tire is outside this range, a warning message is issued immediately. After that, the program continues reading and processing the values.

If a warning message occurs, the program issues a final warning message at the end. We declare a Boolean variable pressureOK for this purpose.

boolean pressureOK

We initialize it with true and set the value to false if a tire is outside the valid range.

They're even helping me in the task with the boolean thing but I'm so clueless and confused so that I just don't know how to do that. To be honest I have tried literally everything. Before I ask for a solution I always try everything that is possible because if I would just ask for a solution without trying it, it would be useless.

my code:

import java.util.Scanner;

public class Tyrepressure {

    public static void main(String[] args) {

        Scanner scanner1 = new Scanner(System.in);

        System.out.println("Enter your tyrepressure of your right-front-tyre in pounds per square inch: ");
        double rightFrontTyre = scanner1.nextDouble();

        System.out.println(" "); // For space in between the user input

        System.out.println("Enter your tyrepressure of your left-front-tyre in pounds per square inch: ");
        double leftFrontTyre = scanner1.nextDouble();

        System.out.println(" "); // For space in between the user input

        System.out.println("Enter your tyrepressure of your left-back-tyre in pounds per square inch: ");
        double rightBackTyre = scanner1.nextDouble();

        System.out.println(" "); // For space in between the user input

        System.out.println("Enter your tyrepressure of your right-back-tyre in pounds per square inch: ");
        double leftBackTyre = scanner1.nextDouble();

        System.out.println(" "); // For space in between the user input

        if (rightFrontTyre >= 35 && rightFrontTyre <= 45) {
            System.out.println("right-front-tyre = " + rightFrontTyre + " psi, your tyrepressure is okay!");
        } else {
            System.out.println("right-front-tyre = " + rightFrontTyre
                    + " psi, your tyrepressure is critical, it is out of the allowed range!");
        }

        System.out.println(" "); // For space in between the user input

        if (leftFrontTyre >= 35 && leftFrontTyre <= 45) {
            System.out.println("left-front-tyre = " + leftFrontTyre + " psi, your tyrepressure is okay!");
        } else {
            System.out.println("left-front-tyre = " + leftFrontTyre
                    + " psi, your tyrepressure is critical, it is out of the allowed range!");
        }

        System.out.println(" "); // For space in between the user input

        if (rightBackTyre >= 35 && rightBackTyre <= 45) {
            System.out.println("right-back-tyre = " + rightBackTyre + " psi, your tyrepressure is okay!");
        } else {
            System.out.println("right-back-tyre = " + rightBackTyre
                    + " psi, your tyrepressure is critical, it is out of the allowed range!");
        }

        System.out.println(" "); // For space in between the user input

        if (leftBackTyre >= 35 && leftBackTyre <= 45) {
            System.out.println("left-back-tyre = " + leftBackTyre + " psi, your tyrepressure is okay!");
        } else {
            System.out.println("left-back-tyre = " + leftBackTyre
                    + " psi, your tyrepressure is critical, it is out of the allowed range!");
        }

        System.out.println(" "); // For space in between the user input

        if (rightFrontTyre < 35 || rightFrontTyre > 45 || leftFrontTyre < 35 || leftFrontTyre > 45
                || rightBackTyre < 35 || rightBackTyre > 45 || leftBackTyre < 35 || leftBackTyre > 45) {
            System.out.println("You have to check your tyrepressure!");
        }
    }
}

output:

Enter your tyrepressure of your right-front-tyre in pounds per square inch: 
40
 
Enter your tyrepressure of your left-front-tyre in pounds per square inch: 
32
 
Enter your tyrepressure of your left-back-tyre in pounds per square inch: 
11
 
Enter your tyrepressure of your right-back-tyre in pounds per square inch: 
40
 
right-front-tyre = 40.0 psi, your tyrepressure is okay!
 
left-front-tyre = 32.0 psi, your tyrepressure is critical, it is out of the allowed range!
 
right-back-tyre = 11.0 psi, your tyrepressure is critical, it is out of the allowed range!
 
left-back-tyre = 40.0 psi, your tyrepressure is okay!
 
You have to check your tyrepressure!

It works fine but I have to do the last part with a boolean datatype and as I already said I just don't know how.....


Solution

  • You can assign the expression inside the condition in the if statement - and then reuse that in the if, like so:

    boolean pressureOkRightFront = (rightFrontTyre >= 35 && rightFrontTyre <= 45);
    
    if (pressureOkRightFront) {
        System.out.println("right-front-tyre = " + rightFrontTyre + " psi, your tyrepressure is okay!");
    } else {
        System.out.println("right-front-tyre = " + rightFrontTyre
                + " psi, your tyrepressure is critical, it is out of the allowed range!");
    }
    
    boolean pressureOkLeftFront = (leftFrontTyre >= 35 && leftFrontTyre <= 45);
    
    // continue with the code for the other three tyres - remember to use a new boolean variable for each.
    
    // then do this for the final check:
    
    if (!(pressureOkRightFront && pressureOkLeftFront && 
            pressureOkRightBack && pressureOkLeftBack)) {
        System.out.println("You have to check your tyrepressure!");
    }
    

    Please note that in your original code you checked the opposite conditions for the last output - if any of the pressures is out of range. I used boolean logic to get the same final result from the single prechecked results for the individual tyres:

    If NOT all of the four pressures are OK, I print the warning. The NOT is the exclamation mark ! in that condition, and the parentheses are important because otherwise the ! would be evaluated only for the pressureOkRightFront.