Problem overview
This is the first version of a small project: a search page that shows the top-rated brand for a product. In version 1 there is one product and one brand, so nothing needs to be compared yet. The task is to build the two output lines and print them.
The starter code gives product = "Laptop", brand = "TechPro" and rating = 4. The program must print:
Laptop
1. TechPro (4)
The first line is the product name. The second is a ranked entry in the form 1. Brand (rating): the rank 1., the brand, and the rating in parentheses.
Approach
Each output line gets its own variable, productLine and brandLine. Then one println joins the two with a newline character between them. Building lines as separate values first is a habit worth forming: later versions will need to produce many such lines, and keeping each one in a variable makes the code easy to read and change.
The ranked entry mixes text and a number, so it is built by joining pieces: "1. ", the brand, " (", the rating and ")".
Step by step
- Create
productLineas avalequal toproduct. The first line is just the product name, so nothing needs to be added. - Create
brandLine. Join, in the order you read them, the rank text"1. ",brand, the text" (",ratingand the text")". The number turns into text by itself because the left side of each+is aString. - Print both lines. Join
productLine,"\n"andbrandLine, and pass the result toprintln. The escape\ninside a string is one newline character, so the text appears on two lines.
The solution in Kotlin
val product = "Laptop"
val brand = "TechPro"
val rating = 4
val productLine = product
val brandLine = "1. " + brand + " (" + rating + ")"
println(productLine + "\n" + brandLine)
Kotlin details worth knowing:
String + Int. The+operator on aStringconverts whatever is on its right to text, so"... (" + ratingworks with theIntrating. The reverse,rating + " (", doesn't compile, becauseInthas noplusthat takes aString. In this solution every+has a string on its left, because the expression starts with"1. ".- Escapes.
"\n"is a newline. Aprintlnof a string that contains\nprints the line break and then ends its own line, so the program's output ends after1. TechPro (4). - The same text with a string template. Kotlin can insert values into a string with
$nameor${expression}:"1. $brand ($rating)"builds the same text as the lesson's concatenation. Templates are often shorter; this lesson practises+because it is the basic tool and works with any expression. val. Every variable here is assigned once and never changed, so each one is aval.- Types are inferred.
productLineandbrandLineareStrings without being declared as such.
Edge cases and common mistakes
- Missing spaces. The output must be exactly
1. TechPro (4): a space after the period, a space before the parenthesis and none after it. A missing or extra space makes the lesson's tests fail. - The rank text. The
1.is part of the text and is the same for every brand in this version. It is not a calculated value yet. - Different data. The code works for any product, brand and rating. With
product = "Phone",brand = "NeoByte"andrating = 5, it printsPhoneand then1. NeoByte (5). - Two
printlncalls. They would print the same two lines, but the lesson asks for the lines to be joined with"\n"and printed with one call. - Forgetting the parentheses. They are text, so they need quotes:
" ("and")". - Starting the join with a number. If an expression started with
rating + ...followed by a string, the compiler would reject it. Start with a string.
Testing
| product, brand, rating | output |
|---|---|
| "Laptop", "TechPro", 4 | Laptop, then 1. TechPro (4) |
| "Phone", "NeoByte", 5 | Phone, then 1. NeoByte (5) |
