Java, Program: Say if you got straight approval, or have to do recovery test or failed


Problem: Create a program that receives a grade (use Scanner class) and check if you got approved straight, have to do a test recovery or failed.
The rule is as follows:
Grade 7 or more: approved straight
Between 5 and 7: is entitled to make a recovery test
Below 5: Direct disapproved



We will do this using conditions, testing with if and else what was typed by the user.
In this case, the grade
Let's put if inside if. this technique is called nesting.

Let me give you an important tip that you will use throughout your life programmer, which is one of
biggest lessons that I know and I can pass:

"A difficult problem is nothing more than a series of small easy problems."

And how to turn a difficult problem in easy?
Using the technique of Jack, the Ripper: by parts. Break the program.

Each and every project, from that I'll pass up the NASA are divided into parts ... in creating a game, for example we have specialized in drawing up the trees, the sound of gunfire, and if the echo will be realistic.

Ok, here we go.
Create a program that receives a grade user you already know, just use the Scanner class.

But our course will be focused on best practices, we'll focus on creating robust applications.
What's this?
Let's minimize as much as possible the potential problems that may appear when user is using the problem.

Part 1: Having made sure that the user will enter a valid grade

You thought of that? And if the type possessed 11 or -1.1?
You can not.

The first step of our application is to be sure that he will enter a grade BETWEEN 0.0 and 10.0!
If 0.0 to 10.0, ok, and if not? Ends the program.
Looks like this:


public class PassOrNot {
    public static void main(String[] args) {
        float grade;
        Scanner input = new Scanner(System.in);
        
        System.out.print("Type your grade [0.0 - 10.0]: " );
        grade = input.nextFloat();
        
        if( (grade <= 10.0) && (grade >= 0.0) ){
            System.out.println("Valid grade");
        } else {
            System.out.println("Invalid grade, closing application");
        }
            
       
    }
}



What we did was make sure the grade is less than 10.0 and greater than 0.0!
The two cases need to be satisfied, so the logical operator &&.

Test. Put 10.1, 10.5, 21.12, -19, -0.000001, "progressive java".
You have to do this a lot: test.
In big companies there are people hired just for that: test (interns or fellows).

Eliminated the possibility of absurd grades, lets go on.

Part 2: Checking happened Direct

Let's put another if else that will check if the student went right or not.
For now, forget the code done.

How would this code to test if it went right? Easy:


if( grade >= 7.0 ){
          System.out.println("Congratulations, you got approved. By the way, are you Java programmer?");
}
else {
          System.out.println("Don't got straight approved");
}


Let's put this code snippet below that print that says 'grade valid'.
So let's think how Java thinks:

First he gets the result.
Checks if is between 0.0 and 10.0. If not, jump to else and ended the program.

If you are between 0.0 and 10.0, says the grade is valid and then do another test on another if, to check if the grade is greater than 7.0, if got approved straight says, if not says it tha dont got.

The code would look like this:


public class PassOrNot {
    public static void main(String[] args) {
        float grade;
        Scanner input = new Scanner(System.in);
        
        System.out.print("Type your grade [0.0 - 10.0]: " );
        grade = input.nextFloat();
if( (grade <= 10.0) && (grade >= 0.0) ){ System.out.println("Valid grade"); if( nota >= 7.0 ){ System.out.println("Congratulations, you got approved. By the way, are you Java programmer?"); } else { System.out.println("Don't got straight approved"); } } else { System.out.println("Invalid grade, closing application"); } } }


Test.
Grade that Java does not forgive. If you get 6.99, you'll do the recovery test!

Part 3: Checking if you still have hope

Good, now let's see if the student can still make recovery, ie, if the grade is between 5.0 and less than 7.0.
If it is exactly 7.0, it will automatically pass.
The code for this part is:


if( grade >= 5.0 ){
       System.out.println("You'll do the test recovery");
}
else {
       System.out.println("Fail. Thankfully it is only simulation, huh?");
}



If the grad is not greater or equal to 5.0, is less, then go to else.
Although, if is smaller, failed. So what else sends message of disapproval.
The question is, where do we put this code?
In the excerpt else 'Don't got straight approved'

So, the full code is:


public class PassOrNot {
    public static void main(String[] args) {
        float grade;
        Scanner input = new Scanner(System.in);
        
        System.out.print("Type your grade [0.0 - 10.0]: " );
        grade = input.nextFloat();
     
        if( (grade <= 10.0) && (grade >= 0.0) ){
         
            if( grade >= 7.0 ){
                System.out.println("Congratulations, you got approved. By the way, are you Java programmer?");
            }
            else {
             
                if( grade >= 5.0 ){
                    System.out.println("You'll do test recovery");
                }
                else {
                    System.out.println("Fail. Thankfully it is only simulation, huh?");
                }
            }
         
        }
        else {
            System.out.println("Invalid grade, closing application");
        }
         
     
    }
}


The Java reads and interprets following these steps:

1. First check if the grade is valid (if you enter between 0 and 10)
If not, go to else, which ends the program.
If it is, if go to next step, which is step 2.

2nd. Check took up more than 7.0 through the next if
If yes, it got straight approval and ends the program.
If not more than 7.0, go to else, which is step 3.

3rd. Check took up more than 5.0 through the next if
If yes, says he was in recovery and ends the program.
If not, go to else, saying he failed and ended the program.


For the sake of organization, put the pairs if-else in a vertical line, see how it is more organized, and so you will know which if the else belongs (yes, you can get confused).
I painted the corresponding pairs for clarity.
This is called the indentation!

Exercise:
Redo the same problem.
Test if the grade is between 0.0 and 10.0.
Then, test if he failed straight, if so, the program ends.
If not, go to to else, and test in other 'if' if you have the chance to do the test recovery.
If yes, ok. If not, it's because you got straight approval.

That is, is the same problem, but opposite logic.
Obviously, should work the same way.
This will show how the same problem can be done in several ways.

No comments: