Make the class MoonPhase
in the package advjava.exam.myName.myCode
, where myName
and myCode
is replaced by your name (without spaces) and Neptun code. All of your classes will have ot be
You have to solve the exercises in this order: first Enum, then Supplier, and then you can choose whether you want to continue with Aspects+Unit testing or Logging+Client-server. Each fully solved exercise increases your mark by one; you have to solve all of them to get a 5
.
Take care to make the code beautiful.
When you’re ready, zip the whole project, and send it to me via e-mail.
Make an enum
called MoonPhase
in your package. It will have exactly 28
elements: PHASE0
, PHASE1
, …, PHASE27
. (Note: the real moon’s period is somewhat longer.)
PHASE0
takes the string "New Moon"
, and PHASE14
takes the string "Full Moon"
. All other elements do not take any parameters.phaseName
in the enum.
"Phase 1"
, "Phase 2"
, …, "Phase 27"
for the other elements.toString
for the class that simply returns the phaseName
.getPhase
operation that takes a String
parameter. If the string is not the name of a phase, return an empty Optional
, otherwise return one with the appropriate MoonPhase
.Make a class MoonMain
in your package with a main
.
getPhase
with the strings "Full Moon"
and "No Moon"
and "Unknown"
. Print the results.MoonPhase
, create the method getSupplier
. It takes an array of int
s; you can suppose that it is not empty. Let us call this parameter steps
. It returns a Supplier
of MoonPhase
s.
Supplier
produces the MoonPhase
that the getSupplier
method was invoked on.Supplier
produces the MoonPhase
that is steps[0]
positions after the first element in the enum.Supplier
produces the MoonPhase
that is steps[1]
positions after the second element in the enum.steps[2]
, steps[3]
, … positions in the enum.
6
positions from PHASE25
should get PHASE2
.steps
array, continue with steps[0]
.main
in MoonMain
, use the getSupplier
starting from PHASE11
, using steps 3,6,7
. Since it would produce infinitely many values, only take the first 10
. Print them on a line, separated by commas. The result has to be Phase 11, Full Moon, Phase 20, Phase 27, Phase 2, Phase 8, Phase 15, Phase 18, Phase 24, Phase 3
.
PHASE11
.3
steps. We get to PHASE14
, which is Full Moon
, of course.6
steps. 14+6=20
7
steps. 20+7=27
steps
, we take 3
steps. 27+3=30
, which is over 28
, so we take element 30-28=2
.Create a class MoonTester
in your package. This class will have the following unit tests.
phaseNewMoon
: check that invoking getPhase
on New Moon
returns the proper valuephaseFullMoon
: check that invoking getPhase
on Full Moon
returns the proper valuephaseUnknown
: check that invoking getPhase
on Unknown
returns emptyphaseSupplier
: use getSupplier
in a similar manner as in MoonMain
above
Make an aspect called EvenAspect
in your package. (Note:
in Eclipse, you will have to your project into an AspectJ project.
Right click on the project, and select Configure>Convert to AspectJ project
.)
In the aspect, make an advice that will modify the setPhaseName
operation: whenever it is called on a moon phase with an even index (PHASE0
, PHASE2
etc.), its phaseName
has to be set so that it starts with "even "
: "even Full Moon"
, "even Phase 2"
etc., but the names of the odd phases have to remain as they were before: "Phase 1"
, "Phase 3"
etc.
The printout in MoonMain
now has to change to Phase 11, even Full Moon, even Phase 20, Phase 27, even Phase 2, even Phase 8, Phase 15, even Phase 18, even Phase 24, Phase 3
.
Also, add a static boolean field isActive
in EvenAspect
. The aspect only has to change the effect of setPhaseName
if this field’s value is true
.
Make a version of MoonTester
called MoonTesterWithAspects
, which should have all cases green if the aspect is active.
Make two classes in your package: a MoonServer
and a MoonClient
. They will communicate with each other via Object(In/Out)putStream
s.
The server starts up at port 12345
, and waits for connections. The incoming connections will be handled one after the other.
When a client connects to the server, it sends three objects to the server.
The server finds the moon phase by its name, and uses getSupplier
to take the given number of steps. Then it sends the resulting list of moon phases to the client as an object.
The client reads a file that is formatted like this example.
Phase 27;3,5;4
Full Moon;1;27
New Moon;27;27
Unknown;1;1
Each line contains three bits of information separated by semicolons: the infos to be sent to the server. (Note: you can use String.split
to separate the data.) Make three objects out of them (in the format
that the server expects them), and then receive the server’s answer, and
print it.
For the above example, the client printout has to be the following.
[Phase 27, Phase 2, Phase 7, Phase 10]
[Full Moon, Phase 15, Phase 16, Phase 17, Phase 18, Phase 19, Phase 20, Phase 21, Phase 22, Phase 23, Phase 24, Phase 25, Phase 26, Phase 27, New Moon, Phase 1, Phase 2, Phase 3, Phase 4, Phase 5, Phase 6, Phase 7, Phase 8, Phase 9, Phase 10, Phase 11, Phase 12]
[New Moon, Phase 27, Phase 26, Phase 25, Phase 24, Phase 23, Phase 22, Phase 21, Phase 20, Phase 19, Phase 18, Phase 17, Phase 16, Phase 15, Full Moon, Phase 13, Phase 12, Phase 11, Phase 10, Phase 9, Phase 8, Phase 7, Phase 6, Phase 5, Phase 4, Phase 3, Phase 2]
[]
Using java.util.logging
, add a static Logger
field to MoonServer
. This logger should not print anything into the console; it has to print its messages into the file server.log
.
When the server is responding to the client, log the textual form of the answer (a list of moon phases) at the info level.
(The following bit is only required if you want a 5
, the top mark.)
Make a class MoonLogServer
that contains an extremely simple, textual server on port 12346
: it receives one connection, and it prints the received lines to its standard output.
The Logger
in MoonServer
now has to send the logs not only to server.log
, but also to MoonLogServer
. For this purpose, use java.util.logging.SocketHandler
.