Beginners Programs :

Java Code to Calculate Love Percentage [FLAMES] | Programs

Java program to calculate love percentage or Flames Java Program – In this article, we will detail in on all the possible ways to calculate love percentage in Java code.

The methods used in this article are as follows:

  • Using Scanner Class – Check generic class in java
  • Using Static Method
  • Using Separate Class
  • Using Command Line Arguments

Love percentage is something that the youth, especially college going students check out on the internet. It pretty much works on something related to the names of both the partners. Here we cover detailed program in Java for software developers.

love calculator in java

The percentage part is actually a continuation of the famous game FLAMES.

Calculating the same can be done in various ways in Java Programming. Thus, the methods are as follows:

Love Calculator Java – Using Scanner Class

For any problem to be solved, the first step is to take inputs. In our problem to calculate Love Percentage,  the inputs are two strings- one is your name (s1) while another is your lover’s name (s2) at runtime making use of the Scanner Class in Java.

To calculate the love percentage, we need to compare individual character-wise. Hence, we convert the two strings s1 and s2 to character array arr1 and arr2 respectively using String.toCharArray() method.

Every character has its own integer value in ASCII. We make use to this integer value to find the love percentage.

We traverse through both the arrays until the end, convert each character into its integer equivalent using type conversion as follows:

Each ASCII value is added and sum of integer equivalent of both the names are calculated individually and stored in different variables sum1 and sum2 respectively. After calculating the sum, both the sums are compared.

If sum1 is lesser than sum2 then love percentage is,      per=(sum1/sum2)*100;

Else,      per=(sum2/sum1)*100;

Output:

Love (FLAMES) – Using a Static Method

In the above method, we have seen the logic used for solving our problem and the code for it. One thing to be observed is that, the entire code was written within the main method only.

It could be quite challenging task to find a particular part of the code if ever needed in the future.

To avoid such a circumstance, we can split up the code into parts and but the logic is another separate static method.

By doing so, we can also reuse this logic and set of instructions if ever needed in another part as well instead of writing the same set of statements all over again.

Here, the main method will consist of all the input output operations and calls the static method (percentage) which uses the same logic and inputs as above and return a double value denoting the resultant love percentage.

Output1:

Using Separate Class

When the entire code is within the same class, it can get clumsy and confusing at times.

To increase the readability of the code, we can make use a different classes. According to the logic, the code can be split up and written in separate classes.

In our problem, the class containing the main method (Love) has input statements by making use of scanner class followed by creating an object referencing another class (Percentage).

This is then followed by using the object to finally give the  desired output. There is another class (Percentage) which consists of a variable used to storing the love percentage value (per) followed by a constructor.

This constructor takes in the inputs read by main method as arguments/parameters and performs the same logic discussed above to finally arrive at our resultant love percentage, at the time of creation of object for this class (Percentage).

(FLAMES) – Using Command-Line Arguments

All this while, we have taken input at runtime using the Scanner class in Java. But, there is another method that can be used to take inputs while running the code. This method is nothing but, by using command-line arguments.

In here, while giving the run command itself followed by space, we can pass to arguments of string format. Arguments passed in command-line are always of string type and need to converted to our desired type using type casting and or parsing as per requirement.

But in our code, we require inputs in string format itself. So, the first argument (arg[0]) is considered as your name while the second argument (arg[1]) is considered as your lover’s name.

After storing these arguments in variables (s1 and s2 respectively), the same set of instructions and steps are used as discussed above to give us the love percentage.

Output1: