Вы находитесь на странице: 1из 6

Questions

Assume you have written some classes. Belatedly, you decide they should be split into
three packages, as listed in the following table. Furthermore, assume the classes are
currently in the default package (they have no package statements).

Destination Packages
Package Name Class Name
mygame.server Server
mygame.shared Utilities
mygame.client Client
1. Which line of code will you need to add to each source file to put each class in
the right package?
2. To adhere to the directory structure, you will need to create some subdirectories
in the development directory and put source files in the correct subdirectories.
What subdirectories must you create? Which subdirectory does each source file
go in?
3. Do you think you'll need to make any other changes to the source files to make
them compile correctly? If so, what?
Exercises
Download the source files as listed here.
Client
Server
Utilities
1. Implement the changes you proposed in questions 1 through 3 using the source
files you just downloaded.
2. 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 the mygame directory you just created.)



/*Client

* Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights
reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle or the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import java.io.*;
import java.net.*;
import java.util.*;

public class Client extends Thread {
Socket clientSocket = null;

public Client(Socket s) {
clientSocket = s;
}

public void run() {
if (clientSocket == null) {
return;
}

PrintStream out = null;

Utilities.printMsg("creating output stream");

try {
out = new PrintStream(clientSocket.getOutputStream());
} catch (IOException e) {
System.err.println("Error binding output to socket, " + e);
System.exit(1);
}

Utilities.printMsg("writing current date");

Date d = new Date();
out.println(d);

try {
out.close();
clientSocket.close();
} catch (IOException e) {
}
}

protected void finalize() {
if (clientSocket != null) {
try {
clientSocket.close();
} catch (IOException e) {
}
clientSocket = null;
}
}
}

Server
/*
* Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights
reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle or the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import java.io.*;
import java.net.*;

public class Server {

public static void main(String args[]) {
ServerSocket serverSocket = null;

Utilities.printMsg("creating server socket");

try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Unable to create server socket, " + e);
System.exit(1);
}

Utilities.printMsg("accepting client connections");

while (true) {
try {
Socket clientSocket = serverSocket.accept();
new Client(clientSocket).start();
} catch (IOException e) {
System.err.println("Unable to accept socket connection, " + e);
System.exit(1);
}
}
}
}
Utilities

*
* Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights
reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle or the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

public class Utilities {
// set DEBUG = false and compile to stop debug messages
final static boolean DEBUG = true;

public static void printMsg(String msg) {
if (DEBUG) {
System.out.println(msg);
}
}
}



Answers
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
no package 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:
In Client.java add:
package mygame.client;
In Server.java add:
package mygame.server;:
In Utilities.java add:
package mygame.shared;
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?
Answer 1b: Within the mygame directory, you need to create three
subdirectories: client, server, and shared.
In mygame/client/ place:
Client.java
In mygame/server/ place:
Server.java
In mygame/shared/ place:
Utilities.java
c. Do you think you'll need to make any other changes to the source files to make
them compile correctly? If so, what?
Answer 1c: Yes, you need to add import
statements. Client.java and Server.java need to import the Utilities class, which
they can do in one of two ways:
import mygame.shared.*;
--or--
import mygame.shared.Utilities;
Also, Server.java needs to import the Client class:
import mygame.client.Client;

Вам также может понравиться