LearnFrenzy
Sign in Take a free test

Records, Enums and Sealed Classes · Exercise 4 of 4

Records, Enums and Sealed Classes Records, Enums and Sealed Programs

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Java programs that model real domains with enums, records and sealed hierarchies. Write each one yourself first, then open the solution – every solution was compiled and run on Java 21, and the output shown is the real output.

Question 1 Easy

Model products as an enum with code, name and price; process purchase requests by code with inserted money, printing the product and change, or an error.

Question page & discussion
Question 2 Medium

Model LineItem(name, qty, unitPrice) and Invoice(number, items) records with validation in compact constructors, a total() method and GST of 18%, then print a formatted invoice.

Question page & discussion
Question 3 Medium

Model a payment outcome as a sealed interface with Success, Declined and Failed records; simulate several payments and produce user messages and a summary with an exhaustive switch.

Question page & discussion
Question 4 Easy

Use enums for Size (with base price) and Topping (with price and veg flag), a record Pizza(size, toppings) with a price() method and a veg() check; price three pizzas.

Question page & discussion
Question 5 Medium

Define an enum of traffic violations with a fine and penalty points, plus a method that doubles the fine for repeat offences; compute the total for a list of challans and flag licence suspension at 12+ points.

Question page & discussion
Question 6 Hard

Model JSON values as a sealed interface (JNull, JBool, JNum, JStr, JArr, JObj records) and write a recursive render(JsonValue) using an exhaustive pattern-matching switch; render a nested document.

Question page & discussion
Question 7 Hard

Model chess pieces (King, Rook, Bishop, Knight) as records implementing a sealed interface, each with a position; implement canMove(piece, toFile, toRank) with a switch and test several moves.

Question page & discussion
Question 8 Medium

Model LeaveType (with yearly quota) and Status enums and an immutable LeaveRequest record with a withStatus method; approve requests while quota remains and print each decision and the remaining balances.

Question page & discussion
Question 9 Medium

Use a Zone enum (LOCAL, STATE, NATIONAL, INTERNATIONAL) and a sealed Parcel hierarchy (Document, Box with weight, Fragile box) to compute charges with a switch on both, and print a rate table.

Question page & discussion
Question 10 Easy

Build a 52-card deck from Suit and Rank enums and a Card record, then deal a fixed hand (cards 0, 13, 26, 39 and 51), print it and its total points (A=11, face=10).

Question page & discussion
All exercises doneBack to Records, Enums and Sealed ClassesRevise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links