Advanced Java Practical exam, 2015.11.14.

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.

Enum

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.)

Make a class MoonMain in your package with a main.

Supplier

Aspects+Unit testing

Create a class MoonTester in your package. This class will have the following unit tests.

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.

Logging+Client-server

Make two classes in your package: a MoonServer and a MoonClient. They will communicate with each other via Object(In/Out)putStreams.

Server

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.

Client

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]
[]

Logging

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.