LearnFrenzy
Sign in Take a free test

Constructors and the this Keyword · Exercise 4 of 4

Constructors and the this Keyword Constructors Programs

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

Java programs built around constructors, this() chaining, validation, copies, factories and builders. 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

Write a Rectangle class with three constructors – no arguments (1×1), one argument (a square) and two arguments – chained with this(...), and print the area of each kind.

Question page & discussion
Question 2 Medium

Write a SimpleDate class whose constructor rejects impossible dates (including 29 February in non-leap years) and that offers a nextDay() method returning a new date.

Question page & discussion
Question 3 Medium

Write a Classroom class with a teacher name and a list of Student objects, plus a copy constructor that copies the list and each Student, so changes to the copy never affect the original.

Question page & discussion
Question 4 Medium

Write an immutable Fraction class whose constructor reduces the fraction with the GCD, keeps the sign in the numerator and rejects a zero denominator; add plus and times methods.

Question page & discussion
Question 5 Medium

Implement a Pizza class with a static nested Builder (required size, optional crust, cheese and toppings) and price each pizza: S ₹199, M ₹299, L ₹399, +₹40 per topping, +₹60 for extra cheese.

Question page & discussion
Question 6 Hard

Implement a lazily created, thread-safe singleton using the initialization-on-demand holder idiom, and show that it is created only once even when eight threads request it at the same time.

Question page & discussion
Question 7 Easy

Write an Employee class whose constructor gives every new employee a unique ID like EMP-1001, EMP-1002 … using a static counter, with an overloaded constructor for a default department.

Question page & discussion
Question 8 Medium

Write a Temperature class with a private constructor storing Kelvin and named factories ofCelsius, ofFahrenheit and ofKelvin; reject values below absolute zero and print conversions.

Question page & discussion
Question 9 Hard

Write a Matrix class with constructors for a zero matrix (rows, cols), from a 2D array (defensively copied) and a copy constructor, plus a static identity(n) factory and a multiply method.

Question page & discussion
Question 10 Medium

Write a BankAccount class with constructors for a zero-balance savings account and an account with a type and opening deposit, enforcing minimum opening balances (SAVINGS ₹500, CURRENT ₹5000) and generating account numbers.

Question page & discussion
All exercises doneBack to Constructors and the this KeywordRevise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links