Java · Coding problem
Shipping Rates and Labels
A free coding problem from the Java 17: var, Switch Expressions, Text Blocks and instanceof Patterns skill. Read it here, then solve it in the app in Java.
- Easy
- Coding problem
- Free
- Java
The problem
A fulfillment center in Columbus prices each parcel by destination zone and speed, then prints a label. Write Shipping.cost and Shipping.label. Switch expressions and a text block fit these perfectly.
Cost, in cents. First round the weight up to whole kilograms. The base price is:
| Zone | Base | Per kg |
|---|---|---|
DOMESTIC | 500 | 100 |
CANADA | 905 | 150 |
EUROPE | 1500 | 250 |
ASIA | 1800 | 300 |
Then apply the speed: STANDARD keeps the base; EXPRESS is 1.5 times the base, rounding half a cent up; OVERNIGHT is 2.5 times the base (rounded down), and is only allowed for DOMESTIC: for any other zone, throw an IllegalArgumentException with the message overnight is domestic only.
Label. Exactly these four lines, each ending with a newline:
Parcel P2
zone: EUROPE
speed: EXPRESS
cost: $26.25
(Two spaces before each field name; the values line up after zone: , speed: and cost: .)
Example: a 2.5 kg DOMESTIC STANDARD parcel weighs 3 kg after rounding up, so it costs 500 + 3 × 100 = 800 cents, $8.00.
Constraints
- weights are between 0.1 and 30 kg
- 1 to 10 parcels per input
How it's graded
- Run checks your code against the examples. Submit also runs hidden tests, and you see pass or fail for them, never their inputs.
- Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
- Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.
Solve it Shipping Rates and Labels free
No card needed. Lessons, practice questions and easy problems are free forever.
Free plan, no card · 7-day Pro trial · Cancel anytime