Problem overview
Two brands sell the same product, and the page must show the one with the higher rating. The starter code has brand1 with rating1 and brand2 with rating2, plus two variables declared without a value: bestBrand and bestRating. The program fills those in and prints the winner in the form 1. Brand (rating).
TechPro has a rating of 4 and MegaCorp has 5, so MegaCorp wins:
1. MegaCorp (5)
Approach: decide first, format once
An if/else compares the two ratings and copies the winner's brand and rating into bestBrand and bestRating. Only after that does the program build the output text. The formatting is written once, and it works whichever branch ran. This is easier to read and to change than a design with two complete println calls, one per branch.
The rule is: brand1 wins when rating1 > rating2, and brand2 wins otherwise. The else branch also covers a tie, so equal ratings give the second brand.
Step by step
- Write the
ifforrating1 > rating2, and setbestBrandtobrand1inside it. The starter already declaredbestBrandandbestRating, so the branches only assign them. - Set
bestRatingtorating1in the same branch. The winner's name and rating go together. - Add the
elseand setbestBrandtobrand2. It runs when brand 1 is not strictly better. - Set
bestRatingtorating2inside theelse. - Print the result:
"1. ", thenbestBrand, then" (",bestRatingand")", all joined with+.
The solution in Java
String brand1 = "TechPro";
int rating1 = 4;
String brand2 = "MegaCorp";
int rating2 = 5;
String bestBrand;
int bestRating;
if (rating1 > rating2) {
bestBrand = brand1;
bestRating = rating1;
} else {
bestBrand = brand2;
bestRating = rating2;
}
System.out.println("1. " + bestBrand + " (" + bestRating + ")");
The two variables have no value when they are declared. That is allowed in Java as long as every path reaches an assignment before the variable is read. Both branches assign both variables, so the println compiles. If one assignment were missing, the compiler would report that the variable might not have been initialized.
How the program runs with the starter values
The condition rating1 > rating2 asks whether 4 is greater than 5. It is not, so Java skips the if block and runs the else block: bestBrand becomes "MegaCorp" and bestRating becomes 5. The last line then joins "1. ", MegaCorp, " (", 5 and ")" into 1. MegaCorp (5).
Exactly one of the two blocks runs, never both and never neither. That guarantee is what makes the two variables safe to read afterwards.
A shorter way to write the same choice
Java also has the conditional operator, condition ? valueIfTrue : valueIfFalse. The same choice can be written like this:
String bestBrand = rating1 > rating2 ? brand1 : brand2;
int bestRating = rating1 > rating2 ? rating1 : rating2;
With the starter values this also gives 1. MegaCorp (5). The lesson uses if/else because it is the more general tool: a branch can hold several statements, and it is the form you need once the decision does more than pick a value. For picking only the larger rating, Math.max(rating1, rating2) returns 5 as well, but it cannot tell you which brand owns that rating, which is why the brand is chosen by the condition.
Edge cases and common mistakes
Equal ratings
When the ratings are equal, rating1 > rating2 is false, so the else branch runs and brand 2 is shown. With TechPro 5 and MegaCorp 5, the output is 1. MegaCorp (5). The lesson's rules state this: a tie goes to the second brand.
Setting only one variable
If a branch sets bestBrand but forgets bestRating, the code does not compile. A brand paired with the other brand's rating would be a worse bug, so keep the two assignments together in each branch.
Declaring the variables inside the branches
The starter declares bestBrand and bestRating before the if for a reason. A variable declared inside a block, such as String bestBrand = brand1; in the if body, exists only until that block's closing brace. The println after the if/else would then fail to compile with a "cannot find symbol" error. Leave the declarations where the starter put them and write only the assignments in the branches.
Assignment instead of comparison
The condition needs a comparison operator such as >. Writing rating1 = rating2 in the parentheses would try to assign, and Java rejects it because an if needs a boolean, not an int.
Comparing with the wrong operator
Using < in place of > flips the result: the lower rating wins. Checking both orders is a cheap test: with TechPro 5 and MegaCorp 4 the program prints 1. TechPro (5), and with TechPro 4 and MegaCorp 5 it prints 1. MegaCorp (5).
Testing
Change rating1 and rating2 in the starter code and run all three situations: the first brand wins, the second brand wins, and the ratings are equal. The first two exercise the two branches, and the third checks the boundary where the condition flips. Each row below shows what the program printed for that pair.
| Ratings (brand 1, brand 2) | Output |
|---|---|
| 4, 5 | 1. MegaCorp (5) |
| 5, 4 | 1. TechPro (5) |
| 5, 5 | 1. MegaCorp (5) |
Key takeaways
- An
if/elsecan assign variables that were declared without a value, if both branches assign them. - Choose the winner first, then build the output text once.
- Know which branch handles a tie, and write the condition so that it is the one you want.
