**Solved and commented Java exercise:**

A teacher, very cool, did 3 tests during a semester but will only take into account the two highest grades to average.

Write a Java application that asks the value of 3 notes, show how it would mean to these 3 tests, average with the 2 highest grades, as well as his highest score and your lowest note. These averages must be calculated using the same method, because you're a Java programmer and will not create a lot of methods for nothing.

Create another method that receives all 3 grades and returns the largest. And another that returns the smallest.

If you are following our Java course, already saw how our average method works, which takes a list of any number of arguments in our article Passing a list of arguments, of any size, to a method.

The idea to find the largest number among three numbers data is exactly the same to find the lowest number. We will only exchange operator > by <.

In highestGrade method, we find the larger value.

Let's try first if 'grade1' is the highest. How do we do that?

Now let's compare with 'grade2' and 'grade3'. If grade1 is greater than these two, it is clear that he is the greatest.

Let's check this with two consecutive tests if conditional. If the result is true for both, return the 'grade1' as the greatest number and the method ends there.

However, shortly after the first if we put a '{'.

If 'grade1' is not greater than 'grade2' is because 'grade2' is bigger (obviously). Well, let's take what we know 'grade2' is greater than 'grade1' and let's see if it is greater than 'grade3' too. If it is, we return 'grade2' as the highest number and the method ends there.

If not, comes to an end conditionals.

And there? Now if neither 'grade1' or 'grade2' was higher, it is obvious that 'grade3' is the largest and returned it, and the method ends there.

```
import java.util.Scanner;
public class variableParameters{
public static float average(float...values){
float average=0;
for(float value: values){
average += value;
}
return average/values.length;
}
public static float highestGrade(float grade1, float grade2, float grade3){
if(grade >= grade2){
if(grade1 >= grade3)
return grade1;
}else{
if(grade2 >= grade3)
return grade2;
}
return grade3;
}
public static float lowestGrade(float grade1, float grade2, float grade3){
if(grade1 <= grade2){
if(grade1 <= grade3)
return grade1;
}else{
if(grade2 <= grade3)
return grade2;
}
return grade3;
}
public static void main(String[] args){
float grade1, grade2, grade3;
Scanner input = new Scanner(System.in);
System.out.print("Type value 1: ");
grade1 = input.nextFloat();
System.out.print("Type value 2: ");
grade2 = input.nextFloat();
System.out.print("Type value 3: ");
grade3 = input.nextFloat();
System.out.println("Highest grade: " + highestGrade(grade1,grade2,grade3));
System.out.println("Lowest grade: "+ lowestGrade(grade1,grade2,grade3));
System.out.println("Your average with the 3 grade is: " + average(grade1,grade2,grade3));
System.out.println("Your average without the lower grade is "+ (grade1+grade2+grade3 - lowestGrade(grade1,grade2,grade3))/2);
}
}
```

## No comments:

Post a Comment