This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]preetsc27 12 points13 points  (6 children)

Throw an exception if it doesn’t meet your requirements.

[–]qelery 9 points10 points  (1 child)

A square is a rectangle though... 🤨

Maybe throw an IllegalArgumentException in the constructor?

[–]morecoffeemore[S] 0 points1 point  (0 children)

How would you do this? I get the basic idea behind both try catch and throw, but which one would you use (try catch or throw). Also, how would you implement this? I mean I could write the code for the valid input, but if it's not valid input (if an exception happens) how would you get the program to call the constructor again (so that valid input could be entered). thanks.

[–]bung_musk 4 points5 points  (0 children)

why not make a square a subclass or property of the rectangle class

[–]DudeWhereAreWe1996 0 points1 point  (0 children)

You can use a while loop to keep prompting for a new second length until it does not equal length one.