First if condition check whether (x – y) is greater than 0 and (x – z) is greater than 0. If we subtract small number from big number then this condition fail, otherwise it will be True. If this condition is True then x is greater than both y, z.

Else statement will execute when the first If condition is False so, there is no need to check for x value. In the Else statement we are inserting one more if condition (Nested IF) to check whether (y – z) is greater than 0. If this condition is True then y is greater than both x, z.

We declared an integer variable called largest and we assigned this variable to the conditional operator functionality.

First condition check whether x is greater than y and x is greater than z. If this condition is True then it will return first value after the ? symbol, which is variable x (x is greater than both y, z).

If the first condition fail then it will execute the variable after the : symbol. By using Nested conditional operator we are checking one more condition here (y > z). If this condition is True then it will return first value after the ? symbol, which is variable y (y is greater than both x, z.

If the Nested condition fails then it will execute the variable after the : symbol, which is variable z. It means z is greater than both x, y.

OUTPUT 1: Lets enter the values x = 15, y = 6, z = 9

OUTPUT 2: Lets enter the values x = 5, y = 98, z = 87

OUTPUT 3: Lets enter the values x = 100, y = 500, z = 980

Java Program for Largest of Three Numbers using Oops

In this example, we are dividing the code using the Object Oriented Programming. To do this, First we will create a class which holds two methods to find largest of two numbers.

First we declared a function LargestOfThree with zero arguments. Within the function, we used Nested If statement to find largest of three numbers. Within the function we used System.out.println statements to print the output as well.

Next we declared an integer function LargestOfThreeNumbers with three arguments. We already explained the Logic in above example.

TIP: If you declare a method with void keyword then we can’t return any value. If you want to return any value then replace void with data type and add return keyword.

Main Class Analysis:

First we created an instance / created an Object of the LargestNumber Class

LargestNumber ln = new LargestNumber();

Next, we are assigning the user entered values to the LargestNumber Class variables.

ln.x = a;
ln.y = b;
ln.z = c;

Next, we are calling the LargestOfThree method. Note, this is the first method that we created with void keyword and this method will find largest of three numbers and print the output from the LargestNumber Class itself.

ln.LargestofThree();

Next, we are calling the LargestOfThreeNumbers method by passing three arguments. This function will the find largest of three numbers and return the largest value that’s why we are assigning the output to largest variable