Question 1: Assume that you have written some classes. Belatedly, you decide that they should be split into three packages, as listed in the table below. Furthermore, assume that the classes are currently in the default package (they have nopackage
statements).
Package Name
Class Name
mygame.server
Server
mygame.shared
Utilities
mygame.client
Client
a. What line of code will you need to add to each source file to put each class in the right package?
Answer 1a: The first line of each file must specify the package:b. To adhere to the directory structure, you will need to create some subdirectories in your development directory, and put source files in the correct subdirectories. What subdirectories must you create? Which subdirectory does each source file go in?
- In
Client.java
add:package mygame.client;
- In
Server.java
add:package mygame.server;
:- In
Utilities.java
add:package mygame.shared;
Answer 1b: Within themygame
directory, you need to create three subdirectories:client
,server
, andshared
.c. Do you think you'll need to make any other changes to the source files to make them compile correctly? If so, what?
- In
mygame/client/
place:Client.java
- In
mygame/server/
place:Server.java
- In
mygame/shared/
place:Utilities.java
Answer 1c: Yes, you need to add import statements.Client.java
andServer.java
need to import theUtilities
class, which they can do in one of two ways:Also,import mygame.shared.*; --or-- import mygame.shared.Utilities;Server.java
needs to import theClient
class:import mygame.client.Client;
Exercise 1: Download three source files:a. Implement the changes you proposed in question 1, using the source files you just downloaded.Answer 1: Download this zip file with the solution: mygame.zip
b. Compile the revised source files. (Hint: If you're invoking the compiler from the command line (as opposed to using a builder), invoke the compiler from the directory that contains themygame
directory you just created.)
You might need to change your proposed import code to reflect our implementation.