[Java] What\'s the difference between these two methods? The first one works jus
ID: 3578396 • Letter: #
Question
[Java] What's the difference between these two methods?
The first one works just fine, but for the second one, I'm getting an error says "variable premium might not have been initialized".
First one
Second one
public double monthlyPremium()
{
int age = client.getAge();
String gender = client.getGender();
double premium;
if(age < 16)
{
premium = -1;
}
else if(age < 25)
{
if(gender.equals("m") || gender.equals("M"))
{
premium = 85.50;
}
else
{
premium = 79.25;
}
}
else if(25 <= age && age < 85)
{
if(gender.equals("m") || gender.equals("M"))
{
premium = 55.00;
}
else
{
premium = 45.00;
}
}
else if(age >= 85)
{
if(gender.equals("m") || gender.equals("M"))
{
premium = 92.00;
}
else
{
premium = 90.00;
}
}
return premium;
}
Explanation / Answer
public static double monthlyPremium1()
{
int age = client.getAge(); //Reads age from client.
String gender = client.getGender(); //Reads gender from client.
double premium; //Premium is declared.
if ( age < 16) //If age is less than 16.
{
premium = -1; //Premium is -1.
}
else if (age < 25) //If age is between 16 - 24.
{
if (gender.equals("m")) //And gender is m.
{
premium = 85.5; //Premium is 85.5
}
else //If gender is not m.
{
premium = 79.25; //Premium is 79.25.
}
}
else if (age < 85) //If age is between 25 - 84.
{
if (gender.equals("m")) //And gender is m.
{
premium = 55; //Premium is 55.
}
else //If gender is not m.
{
premium = 45; //Premium is 45.
}
}
else //age >=85 //For any other age.
{
if (gender.equals("m")) //If gender is m.
{
premium = 92; //Premium is 92.
}
else //If gender is not m.
{
premium = 90; //Premium is 90.
}
}
return premium; //Returns premium.
}
//The above method covers all the ages using the if-else clause.
//Second one
public static double monthlyPremium2()
{
int age = client.getAge(); //Reads age from client.
String gender = client.getGender(); //Reads gender from client.
double premium; //Premium is declared.
if(age < 16) //If age is less than 16.
{
premium = -1; //Premium is -1.
}
else if(age < 25) //If age is between 16 - 24.
{
if(gender.equals("m") || gender.equals("M")) //And gender is m.
{
premium = 85.50; //Premium is 85.5
}
else //If gender is not m.
{
premium = 79.25; //Premium is 79.25
}
}
else if(25 <= age && age < 85) //If age is between 25 - 84.
{
if(gender.equals("m") || gender.equals("M")) //And gender is m.
{
premium = 55.00; //Premium is 55.0
}
else //If gender is not m.
{
premium = 45.00; //Premium is 45.0
}
}
else if(age >= 85) //If age is greater than or equal to 85.
{
if(gender.equals("m") || gender.equals("M")) //And gender is m.
{
premium = 92.00; //Premium is 92.0
}
else //If gender is not m.
{
premium = 90.00; //Premium is 90.0
}
}
return premium;
}
The problem with your second method is that, you wrote conditions exclusively, and you missed the else block. In the first method you just wrote else, which will cover all the remaining cases, where as in the second method, you missed the else block.
Two workarounds to the problem is:
1. Specify the else block exclusively.
2. Initialize premium when declaring with some random value.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.