Click here to Skip to main content
15,906,816 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.....

What I have tried:

I have mentioned it at the top of my question.
Posted
Updated 23-Jan-23 17:26pm
v2

1 solution

Your write-up tells you exactly what to do:

Quote:
...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.


At the top of the main function where you define scanner1, define the boolean and set it true.

Java
boolean pressureOK = true;


Then set it false if a tire is outside the valid range. As each tire is checked, if it is invalid you print an out of range message in an else clause. In that same clause set the pressureOK variable to false.
Java
pressureOK = false;


Now after all tires have been checked you know if any are invalid. So, instead of rechecking each tire at the end as you are now you only need to see if pressureOK is false to see if you need to output the final warning.
Java
if (! pressureOK) {
// Output your final warning message
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900