Question
Java Problem: class Dir This class helps us represent the four directions. Each direction is represented by an object that stores a String representation. As
Java Problem:
class Dir
This class helps us represent the four directions. Each direction is represented by an object that stores a String representation. As mentioned, the implementation details will lend to a fuller understanding of enumerations. We could have just used a single String directly, but then there's the possibility of any arbitrary String showing up when we only want one of these four objects, so our approach will avoid that.
fields
public final String TEXT. The text version never changes, so it may safely be public final.
public static Dir UP. It may be surprising to see an object of the class itself showing up as a field, but that's possible. This creates a Dir whose TEXT is "up".
public static Dir DOWN. The TEXT is "down".
public static Dir LEFT. The TEXT is "left".
public static Dir RIGHT. The TEXT is "right".
public static Dir[] values. This array of Dir must contain references to the four static fields just defined, in this order:{UP,DOWN,LEFT,RIGHT}.
Manual Inspection Criteria (5%): these five static definitions are all correctly instantiated - values references the other four, it doesn't create more Dir objects.
methods
private Dir(String TEXT). Notice that the only constructor is private! We can call it while inside this class (to make the four objects), but nowhere else, so that guarantees those are the only Dir objects.
Manual Inspection Criteria (5%): the constructor is private (and all submitted code compiles with this, i.e. no attempts to create Dir objects exist outside of the four expected ones in this class).
@Override public String toString(). Returns the TEXT.
(note: there's no need to define an equals method; the double-equals operator is sufficient, because only these four direction objects will ever exist!)
Tester: https://paste.ee/p/pvX2F
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started