SlideShare a Scribd company logo
Explain the concept of two types of network intrusions? What
devices can you use to protect a network?
Name CS117 Lab 03 – Page 1
Lab 03: Refactoring Room
Assigned: Tuesday, January 30, 2018
Due: Sunday, February 4, 2018 at 6:00am
1 Overview
In this lab we are going to continue refactoring the code that
was provided for your game to improve its
design and implementation. “Refactoring is the process of
changing a software system in such a way that
it does not alter the external behavior of the code yet improves
its internal structure.”1 It is the process
of reorganizing code to provide a better design while
maintaining equivalence. When we refactor code, we
want the quality of the code to improve while keeping the
functionality of the code exactly the same.
1.1 Cohesion
A well-designed class has all of the code that it needs, and no
more code. It should represent one clearly-
defined kind of thing. Every method should have one specific
purpose, and it should be obvious what that
purpose is. The same code should not be repeated multiple
places. Classes with these properties are cohesive.
We already improved the cohesiveness of the Game class in the
first lab when we moved duplicate code into
the new printLocationInformation method.
1.2 Coupling
A well-designed class can be used correctly without knowledge
of the details of how it works, so that when
those details change it will not be necessary to change the code
that uses it. When this is true, classes
are loosely coupled. When a class depends highly on the messy
details of another, they are highly coupled.
Currently, the Room class is tightly coupled with several other
classes. We are going to fix the design of the
Room class today to fix this.
2 Assignment
2.1 Resolving Issues
If you have any outstanding issues from prior labs, you should
fix those before doing anything else. Remember
to use one commit per issue, and to include “Fixes #4” (or
whatever number it is) in your commit message.
2.2 Fixing Some Bad Style
We have learned that unless there is a very good reason to do
otherwise, fields should always be private.
Keeping fields private decreases the coupling between classes
because it allows things to change behind the
scenes without having to change multiple classes. If you look at
the Room class, you will find that it contains
public fields. We are going to fix this now. Start by changing
each of them to private.
But now, when you try to compile the code, you will find that
there are lots of compiler errors because of
places outside the Room class that had been using those public
fields. We still need to have ways for other
code to get information about Room objects, so we need to write
some accessor and mutator methods in it.
1Martin Fowler in Refactoring: Improving the Design of
Existing Code, 1999.
Name CS117 Lab 03 – Page 2
You will then need to update every class that uses those fields
directly to use your newly written accessor
and mutator methods instead. (Hint: The easiest way to do this
is to let the compiler tell you about those
places.) Look for opportunities to simplify the code as you do
this. Again, make sure that you test
your code to make sure that it still works, then commit and push
your changes.
2.3 Improving Cohesion
Writing cohesive code means that the Game class really
shouldn’t know all of the possible exits that a Room
can have or really know the details of what it means to write out
the details of a room. It would be better
for the Room class to handle this. The perfect way to do this is
to create a toString method in the Room
class:
1 /∗ ∗
2 ∗ R e t u r n s a s t r i n g d e s c r i p t i o n i n c l u d i n g a
l l t h e d e t a i l s o f a Room .
3 ∗ F o r e x a m p l e ,
4 ∗ O u t s i d e :
5 ∗ You a r e o u t s i d e i n t h e c e n t e r o f t h e King ’ s C
o l l e g e campus .
6 ∗ E x i t s : n o r t h e a s t s o u t h w e s t
7 ∗
8 ∗ @ r e t u r n A s t r i n g r e p r e s e n t i n g a l l t h e d e t
a i l s o f a Room .
9 ∗ /
10 public String toString ()
As you can see, this method should print first the room’s name,
then its description, then a list of all the
directions out of the room (those that are not null).
Remember that we want to keep the output isolated in a single
class so we don’t really want this method
to print the exit string directly – only to return it. Modify the
Game class to use the toString method
instead of relying on knowledge of all the possible exits a Room
can have. Be sure that you test to make
sure that your modifications didn’t change the behavior of the
application. Then commit and push your
changes.
2.4 Adding Support for Multiple Directions
Currently, a player can only move in four directions. What if we
wanted to add a new direction of movement
to our game? How many places in the code would need to be
modified in order to add a new direction (e.g.,
up) or down)? Fortunately, the work we have done to clean up
the interface to the Room class means that it
is now easier to change the way exits are stored in the Room
class without any need to worry about breaking
anything in the Game class.
We would like to add support in our game for movement in any
arbitrary direction, rather then try to
anticipate all of the directions our game may need. We will
refactor the Room class to use a HashMap to
store exits. The HashMap will map a named direction (like
"north" or "east") to the Door that lies in that
direction. We will do this now.
1. Replace the individual exit fields of the Room class with a
HashMap. The HashMap should map a
direction string to the Door that is located in the specified
direction. Be sure to completely delete
the individual fields for each of the directions.
2. Since we no longer have individual fields for each exit,
replace the mutator methods for old fields we
just added with a new setExit method that can handle an exit in
any direction. This method only
needs to put the exit information into the HashMap. It should
have the following signature:
Name CS117 Lab 03 – Page 3
1 /∗ ∗
2 ∗ D e f i n e s an e x i t f r o m t h i s room .
3 ∗
4 ∗ @param d i r e c t i o n The d i r e c t i o n o f t h e e x i t .
5 ∗ @param n e i g h b o r The d o o r i n t h e g i v e n d i r e c
t i o n .
6 ∗ /
7 public void setExit(String direction , Door neighbor)
3. Similarly, we can replace the accessor methods for the old
fields we added earlier with a new getExit
method that can return the exit that exists in any direction. This
method need only get the exit
information from the HashMap. It should have the following
signature:
1 /∗ ∗
2 ∗ G e t s a d o o r i n a s p e c i f i e d d i r e c t i o n i f i t e
x i s t s .
3 ∗
4 ∗ @ r e t u r n The d o o r i n t h e s p e c i f i e d d i r e c t i
o n o r n u l l i f i t d o e s n o t
e x i s t .
5 ∗ /
6 public Door getExit(String direction)
4. Modify the rest of the Room class so that it properly uses the
HashMap. HINT: The toString() method
can iterate over the keys of the HashMap.
5. Modify the goRoom method in the Game class to use your
newly written getExit() method. HINT:
Look for an opportunity to greatly simplify this code.
6. Finally, we need to modify the World class. Notice that the
World class has methods for each of the
directions. We should be able to simplify this by replacing all
of the direction specific methods for
creating doors with a single method similar what we did in the
Room class:
1 /∗ ∗
2 ∗ H e l p e r method f o r c r e a t i n g d o o r s b e t w e e n r
o o m s .
3 ∗
4 ∗ @param f r o m The room w h e r e t h e d o o r o r i g i n a
t e s .
5 ∗ @param d i r e c t i o n The d i r e c t i o n o f t h e d o o r i
n t h e f r o m room .
6 ∗ @param t o The room w h e r e t h e d o o r g o e s .
7 ∗ /
8 private void createDoor(Room from , String direction , Room
to)
Congratulations, you have modified your game in such a way
that we can create a multi-dimensional
world in which our player can move in any direction. Be sure to
test to make sure that your modifications
didn’t change the behavior of the application and then commit
and push your changes.
2.5 Introducing Checkstyle
We have talked a lot about why it is beneficial to use good style
when programming. Sometimes, however,
we are so focused at the task that we are trying to accomplish
that we forget to following some of our coding
Name CS117 Lab 03 – Page 4
standards. The result of this is that we have code that is harder
to understand. In order to help us develop
good habits, we will use a tool called Checkstyle.
Checkstyle is a development tool to help programmers write
Java code that adheres to a coding standard.
It automates a lot of the process of checking Java code to spare
humans of this boring (but important) task.
This makes it ideal for projects that want to enforce a coding
standard like this project. You can run
Checkstyle on your project to make sure that it follows the
standards we have been teaching you this year.
The first thing we want to do is check that we are using the
correct style file. You can double check the
style file that BlueJ is using by going to the “Tools” ...
“Preferences” menu and selecting the “Extensions”
tab. At the top of this tab are two text fields for where
Checkstyle will look for its configuration. Be sure
that both of them point to http://www.kings-cs.com/kings-
cs/java/kings-cs-lower.xml.
Now run Checkstyle from the “Tools” menu. This should open a
window that lists any problems that
were detected in your program. If there are any problems, you
should fix them. You should do this at
minimum before everytime that you complete an assignment in
this course (and all future programming
courses).
If you needed to make any changes to fix Checkstyle problems,
run your program again to make sure
that it still works, and then commit and push your changes.
If you downloaded BlueJ to your own computer, it probably
does not have Checkstyle installed, because
it is an extension. You should follow the instructions at
http://bluejcheckstyle.sourceforge.net/ to
install activate Checkstyle for your copy of BlueJ, because you
should be running it before every commit
and push that you do for this project (and homework
assignments too)! If you need help with this, ask!
2.6 Design Document
That is all of the code that you need to write for today. If you
have extra time, work on / talk to me about
your design document.
3 Finishing
When you are finished, you will need to send me information
about which commit is the final one for this
assignment. To do so, refresh the github.com page for your
repository one more time. Then find the section
labeled “Latest commit” followed by a strange sequence of
seven numbers and letters, then the date. If
you click on the numbers and letters, you will go to a page
showing the changes made in your most recent
commit.
Copy the URL of that webpage and paste it into the Moodle
submission box. (I can show you how to
do all of this if you are unsure.) Make sure that you click the
“Submit Assignment” button in Moodle.
To avoid any confusion, I strongly recommend that you delete
the repository from your local computer
and re-clone it next time you are ready to work on it.
training/Main.ctxt
#BlueJ class context
comment0.target=Main
comment0.text=rnrn
comment1.params=
comment1.target=Main()
comment1.text=Default constructor.
comment2.params=e
comment2.target=void
componentResized(java.awt.event.ComponentEvent)
comment3.params=event
comment3.target=void
actionPerformed(java.awt.event.ActionEvent)
comment3.text=rn Default action listener.rn rn @param
eventrn            The action event.rn
comment4.params=args
comment4.target=void main(java.lang.String[])
comment4.text=rn The main method for the program.rn
rn @param argsrn            The command line
arguments.rn
numComments=5
training/Game.ctxt
#BlueJ class context
comment0.target=Game
comment0.text=rnrn
comment1.params=
comment1.target=Game()
comment1.text=rn Create the game and initialize its
internal map.rn
comment2.params=
comment2.target=void play()
comment2.text=rn Main play routine. Loops until end of
play.rn
comment3.params=
comment3.target=void printLocationInformation()
comment3.text=rnPrintsoutthecurrentlocationandexits.rn
comment4.params=command
comment4.target=boolean processCommand(Command)
comment4.text=rn Given a command, process (that is:
execute) the command.rn rn @param commandrn    
       The command to be processed.rn @return true
If the command ends the game, false otherwise.rn
comment5.params=command
comment5.target=void goRoom(Command)
comment5.text=rn Try to go to one direction. If there is
an exit, enter the new room,rn otherwise print an error
message.rn rn @param commandrn            The
command to be processed.rn
comment6.params=
comment6.target=void printGoodbye()
comment6.text=rn Print out the closing message for the
player.rn
comment7.params=
comment7.target=void printHelp()
comment7.text=rn Print out some help information. Here
we print some stupid, crypticrn message and a list of
the command words.rn
comment8.params=
comment8.target=void printWelcome()
comment8.text=rn Print out the opening message for the
player.rn
comment9.params=command
comment9.target=boolean quit(Command)
comment9.text=rn "Quit" wasI entered. Check the rest of
the command to see whether wern really quit the
game.rnrn @param commandrn            The
command to be processed.rn @return true, if this
command quits the game, false otherwise.rn
numComments=10
training/Player.ctxt
#BlueJ class context
comment0.target=Player
comment0.text=rn
comment1.params=currentRoom
comment1.target=Player(Room)
comment1.text= A constructor for the player class.rn
@param currentRoom for the room field.rn
comment2.params=
comment2.target=Room getRoom()
comment2.text= Accossor for getting the room.rn
@return for getting the current room that the player is
in.rn
comment3.params=newRoom
comment3.target=void setRoom(Room)
comment3.text= Mutator for setting the current
room.r[email protected] newRoom for setting the current
room.rn
numComments=4
training/Room.javatraining/Room.java/**
*/
publicclassRoom{
/** Counter for the total number of rooms created in the world.
*/
privatestaticint counter;
/** The name of this room. Room names should be unique. */
privateString name;
/** The description of this room. */
privateString description;
/** This room's north exit, null if none exits. */
privateDoor northExit;
/** This room's south exit, null if none exits. */
privateDoor southExit;
/** This room's east exit, null if none exits. */
privateDoor eastExit;
/** This room's west exit, null if none exits. */
privateDoor westExit;
/** Static initializer. */
static{
counter =0;
}
/**
* Create a room described "description". Initially, it has no e
xits.
* "description" is something like "a kitchen" or "an open cou
rt yard".
*
* @param name The room's name.
* @param description
* The room's description.
*/
publicRoom(String name,String description){
this.name = name;
this.description = description;
counter++;
}
/**
* Returns the name of this room.
*
* @return The name of this room.
*/
publicString getName(){
return name;
}
/**
* Returns the description of this room.
*
* @return The description of this room.
*/
publicString getDescription(){
return description;
}
/** Accessor for geting the northExit.
* @return getNorthExit for getting that direction.
*/
publicDoor getNorthExit(){
return northExit;
}
/** Accessor for geting the southExit.
* @return getSouthExit for getting that direction.
*/
publicDoor getSouthExit(){
return southExit;
}
/** Accessor for geting the eastExit.
* @return getEastExit for getting that direction.
*/
publicDoor getEastExit(){
return eastExit;
}
/** Accessor for geting the westExit.
* @return getWestExit for getting that direction.
*/
publicDoor getWestExit(){
return westExit;
}
/** Mutator for setting the northExit.
* @param newNorthExit for setting that direction.
*/
publicvoid setNorthExit(Door newNorthExit){
northExit = newNorthExit;
}
/** Mutator for setting the southExit.
* @param newSouthExit for setting that direction.
*/
publicvoid setSouthExit(Door newSouthExit){
southExit = newSouthExit;
}
/** Mutator for setting the eastExit.
* @param newEastExit for setting that direction.
*/
publicvoid setEastExit(Door newEastExit){
eastExit = newEastExit;
}
/** Mutator for setting the westExit.
* @param newWestExit for setting that direction.
*/
publicvoid setWestExit(Door newWestExit){
westExit = newWestExit;
}
/**
* Returns the number of rooms that have been created in the
world.
* @return The number of rooms that have been created in th
e world.
*/
publicstaticint getCounter(){
return counter;
}
/**
* Returns a string description including all the details of a R
oom .
* For example ,
*Outside :
*You are outside in the center of the King's College campus
.
*Exits : north east south west
*
* @return A string representing all the details of a Room .
*/
publicString toString (){
String roomInformation ="";
roomInformation = getName()+" "+ getDescription();
boolean memo =false;
if((getNorthExit()!=null)&&(getSouthExit()!=null)&&(getEastE
xit()!=null)&&(getWestExit()!=null)){
memo =true;// roomInformation = getNorthExit();
//roomInformation = getName() + " " + getDescription() + getN
orthExit() + getSouthExit() + getEastExit() + getWestExit() ;
}
return roomInformation;
}
}
training/Door.javatraining/Door.java/**
*/
publicclassDoor{
/** The room that this door leads to. */
privateRoom destination;
/** Whether this door is locked. */
privateboolean locked;
/**
* Constructor for the Door class.
* @param destination The room this door leads to
*/
publicDoor(Room destination){
this.destination = destination;
this.locked =false;
}
/**
* A getter for the room this door leads to.
* @return The room this door leads to
*/
publicRoom getDestination(){
return destination;
}
/**
* A getter for whether this door is locked.
* @return Whether this door is locked
*/
publicboolean isLocked(){
return locked;
}
/**
* A setter for whether this door is locked.
* @param locked Whether this door is locked.
*/
publicvoid setLocked(boolean locked){
this.locked = locked;
}
}
training/CommandWords.ctxt
#BlueJ class context
comment0.target=CommandWords
comment0.text=rnrn
comment1.params=aString
comment1.target=boolean isCommand(java.lang.String)
comment1.text=rn Check whether a given String is a
valid command word.rn rn @param aString The string
to determine whether it is a valid command.rn @return
true if a given string is a valid command, false if it
isn't.rn
numComments=2
training/Reader.classpublicsynchronizedclass Reader {
privatestatic java.util.Scanner reader;
public void Reader();
publicstatic Command getCommand();
publicstatic String getResponse();
publicstatic String getResponseKeepCase();
static void <clinit>();
}
training/World.javatraining/World.javaimport java.util.HashMa
p;
/**
*/
publicclassWorld{
/** The rooms in the world. */
privateHashMap<String,Room> rooms;
/**
* Constructor for the world.
*/
publicWorld(){
rooms =newHashMap<String,Room>();
createRooms();
}
/**
* This method takes care of creating all of the aspects of the
world for
* the "Campus of Kings" application.
*
* @param name
* The provided name of the room.
* @return The room associated with the provided name
*/
publicRoom getRoom(String name){
return rooms.get(name.toLowerCase());
}
/////////////////////////////////////////////////////////////////////////////////////
// Start of private helper methods
/**
* Helper method for recreating a Room. Ensure that the roo
m is created and
* installed in to the collection of Rooms
*
* @param theRoom
* The room to add to the world.
*/
privatevoid addRoom(Room theRoom){
rooms.put(theRoom.getName().toLowerCase(), theRoom);
}
/**
* Helper method for creating doors between rooms.
*
* @param from
* The room where the door originates.
* @param north
* The room to the north of the originating room.
*/
privatevoid createNorthDoor(Room from,Room north){
Door northDoor =newDoor(north);
from.setNorthExit(northDoor);
}
/**
* Helper method for creating doors between rooms.
*
* @param from
* The room where the door originates.
* @param east
* The room to the east of the originating room.
*/
privatevoid createEastDoor(Room from,Room east){
Door eastDoor =newDoor(east);
from.setSouthExit(eastDoor);
}
/**
* Helper method for creating doors between rooms.
*
* @param from
* The room where the door originates.
* @param south
* The room to the south of the originating room.
*/
privatevoid createSouthDoor(Room from,Room south){
Door southDoor =newDoor(south);
from.setSouthExit(southDoor);
}
/**
* Helper method for creating doors between rooms.
*
* @param from
* The room where the door originates.
* @param west
* The room to the west of the originating room.
*/
privatevoid createWestDoor(Room from,Room west){
Door westDoor =newDoor(west);
from.setWestExit(westDoor);
}
/**
* This method creates all of the individual places in this wor
ld and all
* the doors connecting them.
*/
privatevoid createRooms(){
// Creating all the rooms.
Room outside =newRoom("Outside","outside in the center of th
e King's College campus.");
Room holyCross =newRoom("Holy Cross","at one of two main
dormitories on campus.");
Room essef =newRoom("Essef","at the other main dormitory on
campus.");
Room campusCenter =newRoom("Campus Center","in the center
of student activities on campus.");
Room admin =newRoom("Admin","in the oldest building on ca
mpus and home to the computer science department.");
Room jumpOffice =newRoom("Jump's Office","in Dr Jump's off
ice.");
Room hoggOffice =newRoom("Hogg's Office","in Dr Hogg's off
ice.");
Room lab =newRoom("Computer Lab","in the Computer Scienc
e and Math computing lab.");
Room classroom =newRoom("Classroom","in the classroom whe
re the computer science classes are taught.");
// Adding all the rooms to the world.
this.addRoom(outside);
this.addRoom(holyCross);
this.addRoom(essef);
this.addRoom(campusCenter);
this.addRoom(admin);
this.addRoom(jumpOffice);
this.addRoom(hoggOffice);
this.addRoom(lab);
this.addRoom(classroom);
// Creating all the doors between the rooms.
this.createSouthDoor(essef, outside);
this.createNorthDoor(outside, essef);
this.createEastDoor(campusCenter, outside);
this.createWestDoor(outside, campusCenter);
this.createEastDoor(outside, holyCross);
this.createWestDoor(holyCross, outside);
this.createSouthDoor(outside, admin);
this.createNorthDoor(admin, outside);
this.createEastDoor(admin, lab);
this.createWestDoor(lab, admin);
this.createSouthDoor(admin, hoggOffice);
this.createNorthDoor(hoggOffice, admin);
this.createWestDoor(admin, jumpOffice);
this.createEastDoor(jumpOffice, admin);
this.createSouthDoor(lab, classroom);
this.createNorthDoor(classroom, lab);
}
}
training/Game.classpublicsynchronizedclass Game {
private World world;
private int turns;
private int score;
private Character character;
private Player playerClass;
public void Game();
public void play();
private void printLocationInformation();
private boolean processCommand(Command);
private void goRoom(Command);
private void printGoodbye();
private void printHelp();
private void printWelcome();
private boolean quit(Command);
}
training/Writer.javatraining/Writer.javaimport java.awt.Color;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
/**
*/
publicclassWriter{
/** System new line character. */
privatestaticfinalString NEW_LINE;
/** Name of the default log. */
privatestaticfinalString DEFAULT_LOG;
/** The text area that we will be writing to. */
privatestaticJTextPane textArea;
/** Static block. */
static{
NEW_LINE =System.getProperty("line.separator");
DEFAULT_LOG ="defaultlog.txt";
textArea =null;
restartLog();
}
/**
* Mutator for the text component.
*
* @param text
* The text component.
*/
publicstaticvoid setTextArea(JTextPane text){
textArea = text;
textArea.setEditable(false);
}
/**
* Print the user input in blue.
*
* @param input
* The text entered by the user.
*/
publicstaticvoid printInput(String input){
SimpleAttributeSet attributes =newSimpleAttributeSet();
StyleConstants.setForeground(attributes,Color.BLUE);
printWithAttributes(attributes, input + NEW_LINE);
}
/**
* Prints an empty line.
*/
publicstaticvoid println(){
standardPrint(NEW_LINE);
}
/**
* Prints out a single integer to a line.
*
* @param toPrint
* The integer to print.
*/
publicstaticvoid println(int toPrint){
String text =""+ toPrint + NEW_LINE;
standardPrint(text);
}
/**
* Prints out a single integer.
*
* @param toPrint
* The integer to print.
*/
publicstaticvoid print(int toPrint){
String text =""+ toPrint;
standardPrint(text);
}
/**
* Prints out a double to a line.
*
* @param toPrint
* The double to print.
*/
publicstaticvoid println(double toPrint){
String text =""+ toPrint + NEW_LINE;
standardPrint(text);
}
/**
* Prints out a double.
*
* @param toPrint
* The double to print.
*/
publicstaticvoid print(double toPrint){
String text =""+ toPrint;
standardPrint(text);
}
/**
* Prints out an object to a line.
*
* @param toPrint
* The object to print.
*/
publicstaticvoid println(Object toPrint){
String text =""+ toPrint + NEW_LINE;
standardPrint(text);
}
/**
* Prints out a object.
*
* @param toPrint
* The object to print.
*/
publicstaticvoid print(Object toPrint){
String text =""+ toPrint;
standardPrint(text);
}
/**
* Prints a string after word-
wrapping it to 80 characters if possible. Note
* that this fails to calculate correct widths if the string conta
ins tabs.
* Ends with a line return.
*
* @param toPrint
* The String to print.
*/
publicstaticvoid println(String toPrint){
String text = toPrint + NEW_LINE;
standardPrint(text);
}
/**
* Prints a string after word-
wrapping it to 80 characters if possible. Note
* that this fails to calculate correct widths if the string conta
ins tabs.
*
* @param toPrint
* The String to print.
*/
publicstaticvoid print(String toPrint){
standardPrint(toPrint);
}
/**
* Helper method for standard printing.
*
* @param toPrint
* The String to print.
*/
privatestaticvoid standardPrint(String toPrint){
SimpleAttributeSet attributes =newSimpleAttributeSet();
printWithAttributes(attributes, toPrint);
}
/**
* Helper method printing with attributes.
*
* @param attributes
* A set of attributes to use when printing.
* @param toPrint
* The String to print.
* @throws IllegalStateException
* If the text area has not been set and we are trying t
o print
* to it.
*/
privatestaticvoid printWithAttributes(SimpleAttributeSet attribu
tes,String toPrint)throwsIllegalStateException{
if(textArea ==null){
thrownewIllegalStateException("Need to set the text area before
printing to it.");
}
try{
Document document = textArea.getDocument();
document.insertString(document.getLength(), toPrint, at
tributes);
textArea.setCaretPosition(document.getLength());
BufferedWriter log =newBufferedWriter(newFileWriter(DEFAU
LT_LOG,true));
log.write(toPrint);
log.close();
}catch(BadLocationException ex){
System.err.println("ERROR: Should never get this ["+ toPrint +
"]");
System.exit(2);
}catch(IOException ex){
System.err.println("ERROR printing to default log (see instruct
or for help)");
System.exit(1);
}
}
/**
* Restart the default log.
*/
publicstaticvoid restartLog(){
try{
BufferedWriter log =newBufferedWriter(newFileWriter(DEFAU
LT_LOG,false));
log.close();
}catch(IOException ex){
System.err.println("ERROR resetting the default log (see instru
ctor for help)");
System.exit(1);
}
}
/**
* Copy the default log.
*/
publicstaticvoid copyDefaultLog(){
Scanner input =null;
BufferedWriter output =null;
try{
JFileChooser chooser =newJFileChooser();
chooser.setCurrentDirectory(newFile("."));
int result = chooser.showOpenDialog(null);
if(result ==JFileChooser.APPROVE_OPTION){
input =newScanner(newFile(DEFAULT_LOG));
output =newBufferedWriter(newFileWriter(chooser.g
etSelectedFile(),false));
while(input.hasNextLine()){
String line = input.nextLine();
output.write(line + NEW_LINE);
}
output.close();
input.close();
}
}catch(FileNotFoundException exception){
System.err.println("ERROR: default log file cannot be found");
System.exit(3);
}catch(IOException exception){
System.err.println("ERROR: file for copy cannot be written to")
;
System.exit(4);
}
}
}
training/Main$1.classsynchronizedclass Main$1 extends
java.awt.event.ComponentAdapter {
void Main$1(Main);
public void
componentResized(java.awt.event.ComponentEvent);
}
training/CommandWords.classpublicsynchronizedclass
CommandWords {
privatestatic String[] validCommands;
public void CommandWords();
publicstatic boolean isCommand(String);
static void <clinit>();
}
training/Command.javatraining/Command.javaimport java.util.A
rrayList;
/**
*/
publicclassCommand{
/** The command word for this command. */
privateString commandWord;
/** The rest of the line with all the spaces removed. */
privateArrayList<String> restOfLine;
/**
* Create a command object. First is supplied. The second wo
rd is assumed
* to be null.
*
* @param firstWord
* The first word of the command. Null if the comman
d was not
* recognized.
*/
publicCommand(String firstWord){
commandWord = firstWord;
restOfLine =newArrayList<String>();
}
/**
* Create a command object. First and second word must be s
upplied, but
* either one (or both) can be null.
*
* @param firstWord
* The first word of the command. Null if the comman
d was not
* recognized.
* @param rest
* The rest of the command.
*/
publicCommand(String firstWord,ArrayList<String> rest){
commandWord = firstWord;
restOfLine = rest;
}
/**
* Return the command word (the first word) of this comman
d. If the command
* was not understood, the result is null.
*
* @return The command word.
*/
publicString getCommandWord(){
return commandWord;
}
/**
* Returns if this command was not understood.
*
* @return true if this command was not understood.
*/
publicboolean isUnknown(){
return(commandWord ==null);
}
/**
* Returns if this command has a second word.
*
* @return true if the command has a second word.
*/
publicboolean hasSecondWord(){
return restOfLine !=null;
}
/**
* Returns if this command has more words.
*
* @param index The index of the word needed.
* @return true if the command has a word at given index.
*/
publicboolean hasWord(int index){
return index >=0&& index < restOfLine.size();
}
/**
* Returns the word at the requested index in the command.
*
* @param index
* The index of word in the command that is being req
uested.
*
* @return A particular word in the command. Returns null if
there is no
* word corresponding to that requested index.
*
*/
publicString getWord(int index){
String result =null;
if(index >=0&& index < restOfLine.size()){
result = restOfLine.get(index);
}
return result;
}
/**
* Returns the second word of this command, if it exists.
*
* @return The second word of this command. Returns null if
there was no
* second word.
*/
publicString getRestOfLine(){
StringBuffer buffer =null;
if(restOfLine.size()!=0){
for(String word : restOfLine){
if(buffer ==null){
buffer =newStringBuffer();
buffer.append(word);
}
else{
buffer.append(" ");
buffer.append(word);
}
}
}
String result ="";
if(buffer !=null){
result += buffer.toString();
}
return result;
}
}
training/Command.classpublicsynchronizedclass Command {
private String commandWord;
private java.util.ArrayList restOfLine;
public void Command(String);
public void Command(String, java.util.ArrayList);
public String getCommandWord();
public boolean isUnknown();
public boolean hasSecondWord();
public boolean hasWord(int);
public String getWord(int);
public String getRestOfLine();
}
training/Room.classpublicsynchronizedclass Room {
privatestatic int counter;
private String name;
private String description;
private Door northExit;
private Door southExit;
private Door eastExit;
private Door westExit;
public void Room(String, String);
public String getName();
public String getDescription();
public Door getNorthExit();
public Door getSouthExit();
public Door getEastExit();
public Door getWestExit();
public void setNorthExit(Door);
public void setSouthExit(Door);
public void setEastExit(Door);
public void setWestExit(Door);
publicstatic int getCounter();
public String toString();
static void <clinit>();
}
training/defaultlog.txt
Welcome to the Campus of Kings!
Campus of Kings is a new, incredibly boring adventure game.
Type 'help' if you need help.
Outside:
You are outside in the center of the King's College campus.
Exits: north east south west
> go west
Campus Center:
You are in the center of student activities on campus.
Exits: north east south west
>
training/Reader.javatraining/Reader.javaimport java.util.ArrayL
ist;
import java.util.Scanner;
/**
*/
publicclassReader{
/** The source of command input. */
privatestaticScanner reader;
/**
* Create a parser to read from the terminal window.
*/
static{
reader =newScanner(System.in);
}
/**
* Returns the next command from the user.
* @return The next command from the user.
*/
publicstaticCommand getCommand(){
String inputLine;// will hold the full input line
String word1 =null;
ArrayList<String> restOfLine =null;
Writer.print("> ");// print prompt
inputLine = reader.nextLine().toLowerCase();
Writer.printInput(inputLine);
// Find up to two words on the line.
Scanner tokenizer =newScanner(inputLine);
if(tokenizer.hasNext()){
word1 = tokenizer.next();// get first word
if(tokenizer.hasNext()){
restOfLine =newArrayList<String>();
while(tokenizer.hasNext()){
restOfLine.add(tokenizer.next());
}
}
}
tokenizer.close();
// Now check whether this word is known. If so, create a comma
nd
// with it. If not, create a "null" command (for unknown comma
nd).
Command result =null;
if(CommandWords.isCommand(word1)){
result =newCommand(word1, restOfLine);
}
else{
result =newCommand(null, restOfLine);
}
return result;
}
/**
* Return the response to a question in all lower case.
*
* @return The response typed in by the user.
*/
publicstaticString getResponse(){
return getResponseKeepCase().toLowerCase();
}
/**
* Return the response to a question in the case used by the pl
ayer.
*
* @return The response typed in by the user.
*/
publicstaticString getResponseKeepCase(){
String response = reader.nextLine().trim();
Writer.printInput(response);
return response;
}
}
training/team.defs
#Sat Feb 03 11:17:26 EST 2018
bluej.teamsettings.groupname=
bluej.teamsettings.git.repositoryPrefix=/kings-cs/CS117-S18-
AlyousifMustafa.git
bluej.teamsettings.git.protocol=https
bluej.teamsettings.user=mustafa77a
bluej.teamsettings.git.server=github.com
[email protected]
bluej.teamsettings.ignore8=.DS_Store
bluej.teamsettings.vcs=git
bluej.teamsettings.ignore7=.*#backup
bluej.teamsettings.yourName=Mustafa
bluej.teamsettings.ignore6=.*#
bluej.teamsettings.ignore5=.*~
bluej.teamsettings.ignore4=.*.ctxt
bluej.teamsettings.ignore3=team.defs
bluej.teamsettings.ignore2=bluej.pkh
bluej.teamsettings.ignore1=.*.class
training/README.md
Project: CampusOfKings-bad
Authors: Maria Jump
This project is a simple framework for an text adventure game.
In this version,
it has a few rooms and the ability for a player to walk between
these rooms.
That's all.
This version of the game contains some very bad class design. It
should NOT
be used as a basis for extending the project without fixing these
design
problems. It serves as an example to discuss good and bad
design.
We will fix the problems with this project through the next
couple of labs which
walk students through fixing bad design decisions and give
them an opportunity
to become familiar with the existing code.
training/World.ctxt
#BlueJ class context
comment0.target=World
comment0.text=rnrn
comment1.params=
comment1.target=World()
comment1.text=rn Constructor for the world.rn
comment2.params=name
comment2.target=Room getRoom(java.lang.String)
comment2.text=rn This method takes care of creating all
of the aspects of the world forrn the "Campus of
Kings" application.rn rn @param namern          
 The provided name of the room.rn @return The room
associated with the provided namern
comment3.params=theRoom
comment3.target=void addRoom(Room)
comment3.text=rn Helper method for recreating a Room.
Ensure that the room is created andrn installed in to
the collection of Roomsrn rn @param theRoomrn    
       The room to add to the world.rn
comment4.params=from north
comment4.target=void createNorthDoor(Room, Room)
comment4.text=rn Helper method for creating doors
between rooms.rn rn @param fromrn           
The room where the door originates.rn @param northrn
           The room to the north of the originating
room.rn
comment5.params=from east
comment5.target=void createEastDoor(Room, Room)
comment5.text=rn Helper method for creating doors
between rooms.rn rn @param fromrn           
The room where the door originates.rn @param eastrn 
          The room to the east of the originating
room.rn
comment6.params=from south
comment6.target=void createSouthDoor(Room, Room)
comment6.text=rn Helper method for creating doors
between rooms.rn rn @param fromrn
The room where the door originates.rn @param southrn
           The room to the south of the originating
room.rn
comment7.params=from west
comment7.target=void createWestDoor(Room, Room)
comment7.text=rn Helper method for creating doors
between rooms.rn rn @param fromrn           
The room where the door originates.rn @param westrn
           The room to the west of the originating
room.rn
comment8.params=
comment8.target=void createRooms()
comment8.text=rn This method creates all of the
individual places in this world and allrn the doors
connecting them.rn
numComments=9
training/.gitignore
*.class
*.ctxt
*.*~
doc
/.project
default.log
training/Player.javatraining/Player.java
/**
*/
publicclassPlayer{
/** This stores the room that the player is currnetly in. */
privateRoom room;
/** A constructor for the player class.
* @param currentRoom for the room field.
*/
publicPlayer(Room currentRoom){
room = currentRoom;
}
/** Accossor for getting the room.
* @return for getting the current room that the player is in.
*/
publicRoom getRoom(){
return room;
}
/** Mutator for setting the current room.
*@param newRoom for setting the current room.
*/
publicvoid setRoom(Room newRoom){
room = newRoom;
}
}
training/Room.ctxt
#BlueJ class context
comment0.target=Room
comment0.text=rnrn
comment1.params=name description
comment1.target=Room(java.lang.String, java.lang.String)
comment1.text=rn Create a room described "description".
Initially, it has no exits.rn "description" is something
like "a kitchen" or "an open court yard".rn rn @param
name  The room's name.rn @param descriptionrn     
      The room's description.rn
comment10.params=newEastExit
comment10.target=void setEastExit(Door)
comment10.text= Mutator for setting the eastExit.rn
@param newEastExit for setting that direction.rn
comment11.params=newWestExit
comment11.target=void setWestExit(Door)
comment11.text= Mutator for setting the westExit.rn
@param newWestExit for setting that direction.rn
comment12.params=
comment12.target=int getCounter()
comment12.text=rn Returns the number of rooms that
have been created in the world.rn @return The number
of rooms that have been created in the world.rn
comment13.params=
comment13.target=java.lang.String toString()
comment13.text=rn Returns a string description including
all the details of a Room .rn For example ,rnOutside
:rnYou are outside in the center of the King's College
campus .rnExits : north east south westrnrn @return
A string representing all the details of a Room .rn
comment2.params=
comment2.target=java.lang.String getName()
comment2.text=rn Returns the name of this room.rn
rn @return The name of this room.rn
comment3.params=
comment3.target=java.lang.String getDescription()
comment3.text=rn Returns the description of this
room.rn rn @return The description of this room.rn
comment4.params=
comment4.target=Door getNorthExit()
comment4.text= Accessor for geting the northExit.rn
@return getNorthExit for getting that direction.rn
comment5.params=
comment5.target=Door getSouthExit()
comment5.text= Accessor for geting the southExit.rn
@return getSouthExit for getting that direction.rn
comment6.params=
comment6.target=Door getEastExit()
comment6.text= Accessor for geting the eastExit.rn
@return getEastExit for getting that direction.rn
comment7.params=
comment7.target=Door getWestExit()
comment7.text= Accessor for geting the westExit.rn
@return getWestExit for getting that direction.rn
comment8.params=newNorthExit
comment8.target=void setNorthExit(Door)
comment8.text= Mutator for setting the northExit.rn
@param newNorthExit for setting that direction.rn
comment9.params=newSouthExit
comment9.target=void setSouthExit(Door)
comment9.text= Mutator for setting the southExit.rn
@param newSouthExit for setting that direction.rn
numComments=14
training/2018sp-cs117-assignment03-Refactoring (2).pdf
Name CS117 Lab 03 – Page 1
Lab 03: Refactoring Room
Assigned: Tuesday, January 30, 2018
Due: Sunday, February 4, 2018 at 6:00am
1 Overview
In this lab we are going to continue refactoring the code that
was provided for your game to improve its
design and implementation. “Refactoring is the process of
changing a software system in such a way that
it does not alter the external behavior of the code yet improves
its internal structure.”1 It is the process
of reorganizing code to provide a better design while
maintaining equivalence. When we refactor code, we
want the quality of the code to improve while keeping the
functionality of the code exactly the same.
1.1 Cohesion
A well-designed class has all of the code that it needs, and no
more code. It should represent one clearly-
defined kind of thing. Every method should have one specific
purpose, and it should be obvious what that
purpose is. The same code should not be repeated multiple
places. Classes with these properties are cohesive.
We already improved the cohesiveness of the Game class in the
first lab when we moved duplicate code into
the new printLocationInformation method.
1.2 Coupling
A well-designed class can be used correctly without knowledge
of the details of how it works, so that when
those details change it will not be necessary to change the code
that uses it. When this is true, classes
are loosely coupled. When a class depends highly on the messy
details of another, they are highly coupled.
Currently, the Room class is tightly coupled with several other
classes. We are going to fix the design of the
Room class today to fix this.
2 Assignment
2.1 Resolving Issues
If you have any outstanding issues from prior labs, you should
fix those before doing anything else. Remember
to use one commit per issue, and to include “Fixes #4” (or
whatever number it is) in your commit message.
2.2 Fixing Some Bad Style
We have learned that unless there is a very good reason to do
otherwise, fields should always be private.
Keeping fields private decreases the coupling between classes
because it allows things to change behind the
scenes without having to change multiple classes. If you look at
the Room class, you will find that it contains
public fields. We are going to fix this now. Start by changing
each of them to private.
But now, when you try to compile the code, you will find that
there are lots of compiler errors because of
places outside the Room class that had been using those public
fields. We still need to have ways for other
code to get information about Room objects, so we need to write
some accessor and mutator methods in it.
1Martin Fowler in Refactoring: Improving the Design of
Existing Code, 1999.
Name CS117 Lab 03 – Page 2
You will then need to update every class that uses those fields
directly to use your newly written accessor
and mutator methods instead. (Hint: The easiest way to do this
is to let the compiler tell you about those
places.) Look for opportunities to simplify the code as you do
this. Again, make sure that you test
your code to make sure that it still works, then commit and push
your changes.
2.3 Improving Cohesion
Writing cohesive code means that the Game class really
shouldn’t know all of the possible exits that a Room
can have or really know the details of what it means to write out
the details of a room. It would be better
for the Room class to handle this. The perfect way to do this is
to create a toString method in the Room
class:
1 /∗ ∗
2 ∗ R e t u r n s a s t r i n g d e s c r i p t i o n i n c l u d i n g a
l l t h e d e t a i l s o f a Room .
3 ∗ F o r e x a m p l e ,
4 ∗ O u t s i d e :
5 ∗ You a r e o u t s i d e i n t h e c e n t e r o f t h e King ’ s C
o l l e g e campus .
6 ∗ E x i t s : n o r t h e a s t s o u t h w e s t
7 ∗
8 ∗ @ r e t u r n A s t r i n g r e p r e s e n t i n g a l l t h e d e t
a i l s o f a Room .
9 ∗ /
10 public String toString ()
As you can see, this method should print first the room’s name,
then its description, then a list of all the
directions out of the room (those that are not null).
Remember that we want to keep the output isolated in a single
class so we don’t really want this method
to print the exit string directly – only to return it. Modify the
Game class to use the toString method
instead of relying on knowledge of all the possible exits a Room
can have. Be sure that you test to make
sure that your modifications didn’t change the behavior of the
application. Then commit and push your
changes.
2.4 Adding Support for Multiple Directions
Currently, a player can only move in four directions. What if we
wanted to add a new direction of movement
to our game? How many places in the code would need to be
modified in order to add a new direction (e.g.,
up) or down)? Fortunately, the work we have done to clean up
the interface to the Room class means that it
is now easier to change the way exits are stored in the Room
class without any need to worry about breaking
anything in the Game class.
We would like to add support in our game for movement in any
arbitrary direction, rather then try to
anticipate all of the directions our game may need. We will
refactor the Room class to use a HashMap to
store exits. The HashMap will map a named direction (like
"north" or "east") to the Door that lies in that
direction. We will do this now.
1. Replace the individual exit fields of the Room class with a
HashMap. The HashMap should map a
direction string to the Door that is located in the specified
direction. Be sure to completely delete
the individual fields for each of the directions.
2. Since we no longer have individual fields for each exit,
replace the mutator methods for old fields we
just added with a new setExit method that can handle an exit in
any direction. This method only
needs to put the exit information into the HashMap. It should
have the following signature:
Name CS117 Lab 03 – Page 3
1 /∗ ∗
2 ∗ D e f i n e s an e x i t f r o m t h i s room .
3 ∗
4 ∗ @param d i r e c t i o n The d i r e c t i o n o f t h e e x i t .
5 ∗ @param n e i g h b o r The d o o r i n t h e g i v e n d i r e c
t i o n .
6 ∗ /
7 public void setExit(String direction , Door neighbor)
3. Similarly, we can replace the accessor methods for the old
fields we added earlier with a new getExit
method that can return the exit that exists in any direction. This
method need only get the exit
information from the HashMap. It should have the following
signature:
1 /∗ ∗
2 ∗ G e t s a d o o r i n a s p e c i f i e d d i r e c t i o n i f i t e
x i s t s .
3 ∗
4 ∗ @ r e t u r n The d o o r i n t h e s p e c i f i e d d i r e c t i
o n o r n u l l i f i t d o e s n o t
e x i s t .
5 ∗ /
6 public Door getExit(String direction)
4. Modify the rest of the Room class so that it properly uses the
HashMap. HINT: The toString() method
can iterate over the keys of the HashMap.
5. Modify the goRoom method in the Game class to use your
newly written getExit() method. HINT:
Look for an opportunity to greatly simplify this code.
6. Finally, we need to modify the World class. Notice that the
World class has methods for each of the
directions. We should be able to simplify this by replacing all
of the direction specific methods for
creating doors with a single method similar what we did in the
Room class:
1 /∗ ∗
2 ∗ H e l p e r method f o r c r e a t i n g d o o r s b e t w e e n r
o o m s .
3 ∗
4 ∗ @param f r o m The room w h e r e t h e d o o r o r i g i n a
t e s .
5 ∗ @param d i r e c t i o n The d i r e c t i o n o f t h e d o o r i
n t h e f r o m room .
6 ∗ @param t o The room w h e r e t h e d o o r g o e s .
7 ∗ /
8 private void createDoor(Room from , String direction , Room
to)
Congratulations, you have modified your game in such a way
that we can create a multi-dimensional
world in which our player can move in any direction. Be sure to
test to make sure that your modifications
didn’t change the behavior of the application and then commit
and push your changes.
2.5 Introducing Checkstyle
We have talked a lot about why it is beneficial to use good style
when programming. Sometimes, however,
we are so focused at the task that we are trying to accomplish
that we forget to following some of our coding
Name CS117 Lab 03 – Page 4
standards. The result of this is that we have code that is harder
to understand. In order to help us develop
good habits, we will use a tool called Checkstyle.
Checkstyle is a development tool to help programmers write
Java code that adheres to a coding standard.
It automates a lot of the process of checking Java code to spare
humans of this boring (but important) task.
This makes it ideal for projects that want to enforce a coding
standard like this project. You can run
Checkstyle on your project to make sure that it follows the
standards we have been teaching you this year.
The first thing we want to do is check that we are using the
correct style file. You can double check the
style file that BlueJ is using by going to the “Tools” ...
“Preferences” menu and selecting the “Extensions”
tab. At the top of this tab are two text fields for where
Checkstyle will look for its configuration. Be sure
that both of them point to http://www.kings-cs.com/kings-
cs/java/kings-cs-lower.xml.
Now run Checkstyle from the “Tools” menu. This should open a
window that lists any problems that
were detected in your program. If there are any problems, you
should fix them. You should do this at
minimum before everytime that you complete an assignment in
this course (and all future programming
courses).
If you needed to make any changes to fix Checkstyle problems,
run your program again to make sure
that it still works, and then commit and push your changes.
If you downloaded BlueJ to your own computer, it probably
does not have Checkstyle installed, because
it is an extension. You should follow the instructions at
http://bluejcheckstyle.sourceforge.net/ to
install activate Checkstyle for your copy of BlueJ, because you
should be running it before every commit
and push that you do for this project (and homework
assignments too)! If you need help with this, ask!
2.6 Design Document
That is all of the code that you need to write for today. If you
have extra time, work on / talk to me about
your design document.
3 Finishing
When you are finished, you will need to send me information
about which commit is the final one for this
assignment. To do so, refresh the github.com page for your
repository one more time. Then find the section
labeled “Latest commit” followed by a strange sequence of
seven numbers and letters, then the date. If
you click on the numbers and letters, you will go to a page
showing the changes made in your most recent
commit.
Copy the URL of that webpage and paste it into the Moodle
submission box. (I can show you how to
do all of this if you are unsure.) Make sure that you click the
“Submit Assignment” button in Moodle.
To avoid any confusion, I strongly recommend that you delete
the repository from your local computer
and re-clone it next time you are ready to work on it.
__MACOSX/training/._2018sp-cs117-assignment03-Refactoring
(2).pdf
training/Game.javatraining/Game.java/**
*/
publicclassGame{
/** The world where the game takes place. */
privateWorld world;
///** The room the player character is currently in. */
//private Room currentRoom;
/** The number of movments that the player has made. */
privateint turns;
/** The score that the player earns. */
privateint score;
/** This is the character that is controlled by the player. */
privateCharacter character;
/** This is an object for getting the room from the Player class.
*/
privatePlayer playerClass;
/**
* Create the game and initialize its internal map.
*/
publicGame(){
world =newWorld();
playerClass =newPlayer(world.getRoom("outside"));
score =0;
turns =0;
character = character;
// set the starting room
//playerClass = world.getRoom("outside");
}
/**
* Main play routine. Loops until end of play.
*/
publicvoid play(){
printWelcome();
// Enter the main game loop. Here we repeatedly read commands
and
// execute them until the game is over.
boolean wantToQuit =false;
while(!wantToQuit){
Command command =Reader.getCommand();
wantToQuit = processCommand(command);
// other stuff that needs to happen every turn can be added here.
turns++;
}
printGoodbye();
}
/**
*Printsoutthecurrentlocationandexits.
*/
privatevoid printLocationInformation(){
boolean wantToQuit =false;
Writer.println(playerClass.getRoom().getName()+":");
//printWelcome();
Writer.println("You are "+ playerClass.getRoom().getDescriptio
n());
Writer.print("Exits: ");
if(playerClass.getRoom()!=null){
Writer.print("north ");
}
if(playerClass.getRoom()!=null){
Writer.print("east ");
}
if(playerClass.getRoom()!=null){
Writer.print("south ");
}
if(playerClass.getRoom()!=null){
Writer.print("west ");
}
Writer.println();
// other stuff that needs to happen every turn can be added here.
}
///////////////////////////////////////////////////////////////////////////
// Helper methods for processing the commands
/**
* Given a command, process (that is: execute) the command.
*
* @param command
* The command to be processed.
* @return true If the command ends the game, false otherwis
e.
*/
privateboolean processCommand(Command command){
boolean wantToQuit =false;
if(command.isUnknown()){
Writer.println("I don't know what you mean...");
}else{
String commandWord = command.getCommandWord();
if(commandWord.equals("help")){
printHelp();
}elseif(commandWord.equals("go")){
goRoom(command);
}elseif(commandWord.equals("quit")){
wantToQuit = quit(command);
}else{
Writer.println(commandWord +" is not implemented yet!");
}
}
return wantToQuit;
}
///////////////////////////////////////////////////////////////////////////
// Helper methods for implementing all of the commands.
// It helps if you organize these in alphabetical order.
/**
* Try to go to one direction. If there is an exit, enter the new
room,
* otherwise print an error message.
*
* @param command
* The command to be processed.
*/
privatevoid goRoom(Command command){
Room currentRoom = playerClass.getRoom();
if(!command.hasSecondWord()){
// if there is no second word, we don't know where to go...
Writer.println("Go where?");
}else{
String direction = command.getRestOfLine();
// Try to leave current.
Door doorway =null;
if(direction.equals("north")){
doorway = currentRoom.getNorthExit();
}
if(direction.equals("east")){
doorway = currentRoom.getEastExit();
}
if(direction.equals("south")){
doorway = currentRoom.getSouthExit();
}
if(direction.equals("west")){
doorway = currentRoom.getWestExit();
}
if(doorway ==null){
Writer.println("There is no door!");
}else{
Room newRoom = doorway.getDestination();
playerClass.setRoom(newRoom);
printLocationInformation();
Writer.println();
}
}
}
/**
* Print out the closing message for the player.
*/
privatevoid printGoodbye(){
Writer.println("I hope you weren't too bored here on the Campu
s of Kings!");
Writer.println("Thank you for playing. Good bye.");
Writer.println("You have earned"+ score +"points in"+ turns +"t
urns.");
}
/**
* Print out some help information. Here we print some stupi
d, cryptic
* message and a list of the command words.
*/
privatevoid printHelp(){
Writer.println("You are lost. You are alone. You wander");
Writer.println("around at the university.");
Writer.println();
Writer.println("Your command words are:");
Writer.println(" go quit help");
}
/**
* Print out the opening message for the player.
*/
privatevoid printWelcome(){
Room currentRoom = playerClass.getRoom();
Writer.println();
Writer.println("Welcome to the Campus of Kings!");
Writer.println("Campus of Kings is a new, incredibly boring ad
venture game.");
Writer.println("Type 'help' if you need help.");
Writer.println();
//Writer.println(currentRoom.getName() + ":");
Writer.println();
printLocationInformation();
}
/**
* "Quit" wasI entered. Check the rest of the command to see
whether we
* really quit the game.
*
* @param command
* The command to be processed.
* @return true, if this command quits the game, false otherw
ise.
*/
privateboolean quit(Command command){
boolean wantToQuit =true;
if(command.hasSecondWord()){
Writer.println("Quit what?");
wantToQuit =false;
}
return wantToQuit;
}
}
training/Main.javatraining/Main.javaimport java.awt.BorderLay
out;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.InputStream;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
/**
*/
publicclassMainextendsJFrameimplementsActionListener{
/** Generated unique serial unique id. */
privatestaticfinallong serialVersionUID =-
4610552759287004513L;
/** Starting dimension of the window. */
privatestaticfinalDimension WINDOW_DIMENSION;
/** The scroll pane so we can resize it when the window is resiz
ed. */
privateJScrollPane outputScrollPane;
/** The save log menu item. */
privateJMenuItem saveItem;
/** The exit menu item. */
privateJMenuItem exitItem;
/** The game instance. */
privateGame game;
/** Static block for initializing static fields. */
static{
WINDOW_DIMENSION =newDimension(500,500);
}
/** Default constructor. */
publicMain(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(newBorderLayout());
// Setting up the menu bar
JMenuBar menuBar =newJMenuBar();
setJMenuBar(menuBar);
JMenu fileMenu =newJMenu("File");
menuBar.add(fileMenu);
saveItem =newJMenuItem("Save Log ...");
saveItem.addActionListener(this);
fileMenu.add(saveItem);
exitItem =newJMenuItem("Exit");
exitItem.addActionListener(this);
fileMenu.add(exitItem);
// Setting out the output area
JTextPane output =newJTextPane();
outputScrollPane =newJScrollPane(output);
Dimension outputSize =newDimension();
outputSize.setSize(WINDOW_DIMENSION.getWidth(), W
INDOW_DIMENSION.getHeight()-100);
outputScrollPane.setPreferredSize(outputSize);
// So that the scroll pane will resize when the window is resized
addComponentListener(newComponentAdapter(){
publicvoid componentResized(ComponentEvent e){
Dimension outputSize =newDimension();
outputSize.setSize(getContentPane().getWidth(), get
ContentPane().getHeight()-100);
outputScrollPane.setPreferredSize(outputSize);
}
});
add(BorderLayout.NORTH, outputScrollPane);
// Set up the Writer so that it can be used throughout the game.
Writer.setTextArea(output);
// Setting up the bottom panel for input
JPanel bottomPane =newJPanel();
bottomPane.setLayout(newBorderLayout());
JButton enterButton =newJButton("Enter");
JTextField commandField =newJTextField();
TextFieldStreamer streamer =newTextFieldStreamer(commandF
ield);
// maybe this next line should be done in the TextFieldStreamer
ctor
// but that would cause a "leak a this from the ctor" warning
commandField.addActionListener(streamer);
enterButton.addActionListener(streamer);
System.setIn(streamer);
bottomPane.add(BorderLayout.CENTER, commandField);
bottomPane.add(BorderLayout.EAST, enterButton);
add(BorderLayout.SOUTH, bottomPane);
setSize(WINDOW_DIMENSION);
setVisible(true);
commandField.requestFocus();
game =newGame();
game.play();
}
/**
* Default action listener.
*
* @param event
* The action event.
*/
@Override
publicvoid actionPerformed(ActionEvent event){
if(event.getSource()== saveItem){
Writer.copyDefaultLog();
}elseif(event.getSource()== exitItem){
System.exit(0);
}
}
/**
* The main method for the program.
*
* @param args
* The command line arguments.
*/
publicstaticvoid main(String[] args){
newMain();
}
/**
* Implementation of InputStream that uses the text from a JT
extField as the
* input buffer.
*
* @author Maria Jump
*/
privateclassTextFieldStreamerextendsInputStreamimplementsAc
tionListener{
/** The JTextField to use for input. */
privateJTextField textField;
/** The string of text that being passed as input. */
privateString text;
/** Used for checking if the available input has reached its end.
*/
privateint position;
/**
* Default constructor for TextFieldStreamer.
*
* @param field
* JTextField component being used as input buffer
.
*/
publicTextFieldStreamer(JTextField field){
position =0;
text =null;
textField = field;
}
// gets
/**
* Invoked when an action occurs. In this case, prints the t
ext of the
* JTextField to StdOut as an error message to differentiat
e between
* user input and game output. Triggered every time that "
Enter" is
* pressed on the JTextField.
*
* Triggered every time that "Enter" is pressed on the textf
ield
*
* @param event
* ActionEvent passed by the component.
*/
@Override
publicvoid actionPerformed(ActionEvent event){
text = textField.getText()+System.getProperty("line.sep
arator");
position =0;
textField.setText("");
synchronized(this){
// maybe this should only notify() as multiple threads may
// be waiting for input and they would now race for input
this.notifyAll();
}
}
/**
* Reads the next byte of data from the input stream. The v
alue byte is
* returned as an <code>int</code> in the range <code>0<
/code> to
* <code>255</code>. If no byte is available because the e
nd of the
* stream has been reached, the value <code>-
1</code> is returned. This
* method blocks until input data is available, the end of th
e stream is
* detected, or an exception is thrown.
*
* <p>
* A subclass must provide an implementation of this meth
od.
*
* @return the next byte of data, or <code>-
1</code> if the end of the
* stream is reached.
* @exception IOException
* if an I/O error occurs.
*/
@Override
publicint read()throwsIOException{
int result =0xDEADBEEF;
// test if the available input has reached its end
// and the EOS should be returned
if(text !=null&& position == text.length()){
text =null;
// this is supposed to return -1 on "end of stream"
// but I'm having a hard time locating the constant
result = java.io.StreamTokenizer.TT_EOF;
}
if(result ==0xDEADBEEF){
// no input available, block until more is available because
// that's
// the behavior specified in the Javadocs.
while(text ==null|| position >= text.length()){
try{
// read() should block until new input is available.
synchronized(this){
this.wait();
}
}catch(InterruptedException ex){
ex.printStackTrace();
}
}
// read an additional character, return it and increment the
// index.
result = text.charAt(position++);
}
return result;
}
}
}
training/CommandWords.javatraining/CommandWords.java/**
*/
publicclassCommandWords{
/** A constant array that holds all valid command words. */
privatestaticString[] validCommands;
/**
* Static block to initialize the fields of CommandWords.
*/
static{
String[] tempCommands ={"go","quit","help"};
validCommands = tempCommands;
}
/**
* Check whether a given String is a valid command word.
*
* @param aString The string to determine whether it is a val
id command.
* @return true if a given string is a valid command, false if i
t isn't.
*/
publicstaticboolean isCommand(String aString){
boolean valid =false;
int index =0;
while(!valid && index < validCommands.length){
if(validCommands[index].equals(aString)){
valid =true;
}
index++;
}
// if we get here, the string was not found in the commands
return valid;
}
}
training/Main$TextFieldStreamer.classsynchronizedclass
Main$TextFieldStreamer extends java.io.InputStream
implements java.awt.event.ActionListener {
private javax.swing.JTextField textField;
private String text;
private int position;
public void Main$TextFieldStreamer(Main,
javax.swing.JTextField);
public void actionPerformed(java.awt.event.ActionEvent);
public int read() throws java.io.IOException;
}
training/Door.ctxt
#BlueJ class context
comment0.target=Door
comment0.text=rnrn
comment1.params=destination
comment1.target=Door(Room)
comment1.text=rn Constructor for the Door class.rn
@param destination The room this door leads torn
comment2.params=
comment2.target=Room getDestination()
comment2.text=rn A getter for the room this door leads
to.rn @return The room this door leads torn
comment3.params=
comment3.target=boolean isLocked()
comment3.text=rn A getter for whether this door is
locked.rn @return Whether this door is lockedrn
comment4.params=locked
comment4.target=void setLocked(boolean)
comment4.text=rn A setter for whether this door is
locked.rn @param locked Whether this door is
locked.rn
numComments=5
training/Player.classpublicsynchronizedclass Player {
private Room room;
public void Player(Room);
public Room getRoom();
public void setRoom(Room);
}
training/World.classpublicsynchronizedclass World {
private java.util.HashMap rooms;
public void World();
public Room getRoom(String);
private void addRoom(Room);
private void createNorthDoor(Room, Room);
private void createEastDoor(Room, Room);
private void createSouthDoor(Room, Room);
private void createWestDoor(Room, Room);
private void createRooms();
}
training/documents/Untitled Document
The Stolen Treasure
The player is a guy named Kevin. He can enter some restaurant
and get some water and some food. He can get keys from the
ground when he finds one and these keys open some specific
cars. He can get two weapons, a small one like a rock and a big
one like a baseball bat.
He needs to get some water if he made 100 turns during midday
and 170 turns during night time.
Some achievement:
He gets 10 points when he finds the silver key and 15 point
when he finds the golden key.
If he earns 40 point, he will get a map with the next item he
finds which makes his mission easier like hunts such as a
picture of building that he might find something valuable in or
the a picture of a car that he might find something that leads
him to figure something out.
When he earns 80 points he will get a partner named Sarah who
will help him.
training/package.bluej
#BlueJ package file
dependency1.from=Player
dependency1.to=Room
dependency1.type=UsesDependency
dependency10.from=Game
dependency10.to=Door
dependency10.type=UsesDependency
dependency11.from=Game
dependency11.to=Reader
dependency11.type=UsesDependency
dependency12.from=Game
dependency12.to=Writer
dependency12.type=UsesDependency
dependency13.from=Main
dependency13.to=Game
dependency13.type=UsesDependency
dependency14.from=Main
dependency14.to=Writer
dependency14.type=UsesDependency
dependency15.from=Reader
dependency15.to=Command
dependency15.type=UsesDependency
dependency16.from=Reader
dependency16.to=Writer
dependency16.type=UsesDependency
dependency17.from=Reader
dependency17.to=CommandWords
dependency17.type=UsesDependency
dependency2.from=Door
dependency2.to=Room
dependency2.type=UsesDependency
dependency3.from=World
dependency3.to=Room
dependency3.type=UsesDependency
dependency4.from=World
dependency4.to=Door
dependency4.type=UsesDependency
dependency5.from=Room
dependency5.to=Door
dependency5.type=UsesDependency
dependency6.from=Game
dependency6.to=World
dependency6.type=UsesDependency
dependency7.from=Game
dependency7.to=Player
dependency7.type=UsesDependency
dependency8.from=Game
dependency8.to=Command
dependency8.type=UsesDependency
dependency9.from=Game
dependency9.to=Room
dependency9.type=UsesDependency
editor.fx.0.height=680
editor.fx.0.width=800
editor.fx.0.x=316
editor.fx.0.y=23
objectbench.height=101
objectbench.width=776
package.divider.horizontal=0.599476439790576
package.divider.vertical=0.8007380073800738
package.editor.height=427
package.editor.width=651
package.editor.x=236
package.editor.y=85
package.frame.height=600
package.frame.width=800
package.numDependencies=17
package.numTargets=10
package.showExtends=true
package.showUses=true
project.charset=UTF-8
readme.height=58
[email protected]
readme.width=47
readme.x=10
readme.y=10
target1.height=50
target1.name=Player
target1.showInterface=false
target1.type=ClassTarget
target1.width=80
target1.x=460
target1.y=10
target10.height=50
target10.name=Writer
target10.showInterface=false
target10.type=ClassTarget
target10.width=80
target10.x=30
target10.y=170
target2.height=50
target2.name=Game
target2.naviview.expanded=true
target2.showInterface=false
target2.type=ClassTarget
target2.width=80
target2.x=240
target2.y=20
target3.height=50
target3.name=Command
target3.showInterface=false
target3.type=ClassTarget
target3.width=90
target3.x=40
target3.y=340
target4.height=50
target4.name=Reader
target4.showInterface=false
target4.type=ClassTarget
target4.width=80
target4.x=120
target4.y=110
target5.height=50
target5.name=World
target5.showInterface=false
target5.type=ClassTarget
target5.width=80
target5.x=370
target5.y=20
target6.height=50
target6.name=CommandWords
target6.showInterface=false
target6.type=ClassTarget
target6.width=130
target6.x=10
target6.y=260
target7.height=50
target7.name=Room
target7.showInterface=false
target7.type=ClassTarget
target7.width=80
target7.x=430
target7.y=220
target8.height=50
target8.name=Main
target8.showInterface=false
target8.type=ClassTarget
target8.width=80
target8.x=70
target8.y=20
target9.height=50
target9.name=Door
target9.showInterface=false
target9.type=ClassTarget
target9.width=80
target9.x=510
target9.y=80
training/Door.classpublicsynchronizedclass Door {
private Room destination;
private boolean locked;
public void Door(Room);
public Room getDestination();
public boolean isLocked();
public void setLocked(boolean);
}
training/Command.ctxt
#BlueJ class context
comment0.target=Command
comment0.text=rnrn
comment1.params=firstWord
comment1.target=Command(java.lang.String)
comment1.text=rn Create a command object. First is
supplied. The second word is assumedrn to be null.rn
rn @param firstWordrn            The first word
of the command. Null if the command was notrn     
      recognized.rn
comment2.params=firstWord rest
comment2.target=Command(java.lang.String,
java.util.ArrayList)
comment2.text=rn Create a command object. First and
second word must be supplied, butrn either one (or
both) can be null.rn rn @param firstWordrn       
    The first word of the command. Null if the
command was notrn            recognized.rn
@param restrn            The rest of the
command.rn
comment3.params=
comment3.target=java.lang.String getCommandWord()
comment3.text=rn Return the command word (the first
word) of this command. If the commandrn was not
understood, the result is null.rn rn @return The
command word.rn
comment4.params=
comment4.target=boolean isUnknown()
comment4.text=rn Returns if this command was not
understood.rn rn @return true if this command was not
understood.rn
comment5.params=
comment5.target=boolean hasSecondWord()
comment5.text=rn Returns if this command has a second
word.rn rn @return true if the command has a second
word.rn
comment6.params=index
comment6.target=boolean hasWord(int)
comment6.text=rn Returns if this command has more
words.rnrn @param index The index of the word
needed.rn @return true if the command has a word at
given index.rn
comment7.params=index
comment7.target=java.lang.String getWord(int)
comment7.text=rn Returns the word at the requested
index in the command.rn rn @param indexrn       
    The index of word in the command that is being
requested.rn rn @return A particular word in the
command. Returns null if there is norn         word
corresponding to that requested index.rn rn
comment8.params=
comment8.target=java.lang.String getRestOfLine()
comment8.text=rn Returns the second word of this
command, if it exists.rn rn @return The second word
of this command. Returns null if there was norn      
  second word.rn
numComments=9
training/Main.classpublicsynchronizedclass Main extends
javax.swing.JFrame implements java.awt.event.ActionListener {
privatestaticfinal long serialVersionUID = -
4610552759287004513;
privatestaticfinal java.awt.Dimension
WINDOW_DIMENSION;
private javax.swing.JScrollPane outputScrollPane;
private javax.swing.JMenuItem saveItem;
private javax.swing.JMenuItem exitItem;
private Game game;
public void Main();
public void actionPerformed(java.awt.event.ActionEvent);
publicstatic void main(String[]);
static void <clinit>();
}
training/Reader.ctxt
#BlueJ class context
comment0.target=Reader
comment0.text=rnrn
comment1.params=
comment1.target=Command getCommand()
comment1.text=rn Returns the next command from the
user.rn @return The next command from the user.rn
comment2.params=
comment2.target=java.lang.String getResponse()
comment2.text=rn Return the response to a question in
all lower case.rnrn @return The response typed in by
the user.rn
comment3.params=
comment3.target=java.lang.String getResponseKeepCase()
comment3.text=rn Return the response to a question in
the case used by the player.rnrn @return The response
typed in by the user.rn
numComments=4
training/Writer.ctxt
#BlueJ class context
comment0.target=Writer
comment0.text=rnrn
comment1.params=text
comment1.target=void setTextArea(javax.swing.JTextPane)
comment1.text=rn Mutator for the text component.rn
rn @param textrn            The text component.rn
comment10.params=toPrint
comment10.target=void println(java.lang.String)
comment10.text=rn Prints a string after word-wrapping it
to 80 characters if possible. Notern that this fails to
calculate correct widths if the string contains tabs.rn
Ends with a line return.rnrn @param toPrintrn      
     The String to print.rn
comment11.params=toPrint
comment11.target=void print(java.lang.String)
comment11.text=rn Prints a string after word-wrapping it
to 80 characters if possible. Notern that this fails to
calculate correct widths if the string contains tabs.rn
rn @param toPrintrn            The String to
print.rn
comment12.params=toPrint
comment12.target=void standardPrint(java.lang.String)
comment12.text=rn Helper method for standard
printing.rn rn @param toPrintrn            The
String to print.rn
comment13.params=attributes toPrint
comment13.target=void
printWithAttributes(javax.swing.text.SimpleAttributeSet,
java.lang.String)
comment13.text=rn Helper method printing with
attributes.rnrn @param attributesrn            A
set of attributes to use when printing.rn @param
toPrintrn            The String to print.rn @throws
IllegalStateExceptionrn             If the text area
has not been set and we are trying to printrn        
    to it.rn
comment14.params=
comment14.target=void restartLog()
comment14.text=rn Restart the default log.rn
comment15.params=
comment15.target=void copyDefaultLog()
comment15.text=rn Copy the default log.rn
comment2.params=input
comment2.target=void printInput(java.lang.String)
comment2.text=rn Print the user input in blue.rn rn
@param inputrn            The text entered by the
user.rn
comment3.params=
comment3.target=void println()
comment3.text=rn Prints an empty line.rn
comment4.params=toPrint
comment4.target=void println(int)
comment4.text=rn Prints out a single integer to a
line.rn rn @param toPrintrn            The integer
to print.rn
comment5.params=toPrint
comment5.target=void print(int)
comment5.text=rn Prints out a single integer.rn rn
@param toPrintrn            The integer to print.rn
comment6.params=toPrint
comment6.target=void println(double)
comment6.text=rn Prints out a double to a line.rn rn
@param toPrintrn            The double to print.rn
comment7.params=toPrint
comment7.target=void print(double)
comment7.text=rn Prints out a double.rn rn @param
toPrintrn            The double to print.rn
comment8.params=toPrint
comment8.target=void println(java.lang.Object)
comment8.text=rn Prints out an object to a line.rn rn
@param toPrintrn            The object to print.rn
comment9.params=toPrint
comment9.target=void print(java.lang.Object)
comment9.text=rn Prints out a object.rn rn @param
toPrintrn            The object to print.rn
numComments=16
training/Writer.classpublicsynchronizedclass Writer {
privatestaticfinal String NEW_LINE;
privatestaticfinal String DEFAULT_LOG;
privatestatic javax.swing.JTextPane textArea;
public void Writer();
publicstatic void setTextArea(javax.swing.JTextPane);
publicstatic void printInput(String);
publicstatic void println();
publicstatic void println(int);
publicstatic void print(int);
publicstatic void println(double);
publicstatic void print(double);
publicstatic void println(Object);
publicstatic void print(Object);
publicstatic void println(String);
publicstatic void print(String);
privatestatic void standardPrint(String);
privatestatic void
printWithAttributes(javax.swing.text.SimpleAttributeSet,
String) throws IllegalStateException;
publicstatic void restartLog();
publicstatic void copyDefaultLog();
static void <clinit>();
}

More Related Content

PPTX
refactoring code by clean code rules
PPT
Writing Good Code
PDF
PPTX
20.1 Java working with abstraction
PDF
Sam wd programs
PPTX
Tech talks#6: Code Refactoring
PDF
A comparison between C# and Java
PDF
You are given a specification for some Java classes as follows.  A.pdf
refactoring code by clean code rules
Writing Good Code
20.1 Java working with abstraction
Sam wd programs
Tech talks#6: Code Refactoring
A comparison between C# and Java
You are given a specification for some Java classes as follows.  A.pdf

Similar to Explain the concept of two types of network intrusions What devic.docx (20)

PDF
C++ Programming (Please help me!! Thank you!!)Problem A Win SimU.pdf
PPT
Practices For Becoming A Better Programmer
PPTX
R. herves. clean code (theme)2
PDF
55 new things in Java 7 - Devoxx France
PDF
Programming methodology lecture08
PPTX
Code reviews
PDF
Refactoring In Tdd The Missing Part
PDF
Clean code
PPTX
Oct27
PPTX
Refactoring workshop
DOCX
1 Project 2 Introduction - the SeaPort Project seri.docx
PPTX
Clean code
PDF
Source Code Quality
PDF
Refactoring Practice: Cleaner Code
PDF
Refactoring Simple Example
PPTX
Software Craftsmanship - 2
PPTX
Icom4015 lecture3-f16
PDF
Introduction to programming class 11 exercises
PDF
core java
PDF
Clean code
C++ Programming (Please help me!! Thank you!!)Problem A Win SimU.pdf
Practices For Becoming A Better Programmer
R. herves. clean code (theme)2
55 new things in Java 7 - Devoxx France
Programming methodology lecture08
Code reviews
Refactoring In Tdd The Missing Part
Clean code
Oct27
Refactoring workshop
1 Project 2 Introduction - the SeaPort Project seri.docx
Clean code
Source Code Quality
Refactoring Practice: Cleaner Code
Refactoring Simple Example
Software Craftsmanship - 2
Icom4015 lecture3-f16
Introduction to programming class 11 exercises
core java
Clean code

More from SANSKAR20 (20)

DOCX
The Assignment (3–5 pages)Complete a leadership development plan .docx
DOCX
The assignment consist of a Case Study.  I have attached the Case St.docx
DOCX
The annotated bibliography will present an introduction and five ref.docx
DOCX
The artist Georges Seurat is one of the worlds most fascinating art.docx
DOCX
The Assignment (2–3 pages including a budget worksheet)Explain th.docx
DOCX
The assigment is to Research and find me resources on  Portland Sta.docx
DOCX
the article.httpwww.nytimes.com20120930opinionsundaythe-m.docx
DOCX
The Arts and Royalty; Philosophers Debate Politics Please respond .docx
DOCX
The assassination of Archduke Franz Ferdinand was the immediate caus.docx
DOCX
The article Fostering Second Language Development in Young Children.docx
DOCX
The Article Critique is required to be a minimum of two pages to a m.docx
DOCX
The Apple Computer Company is one of the most innovative technology .docx
DOCX
The artist Georges Seurat is one of the worlds most fascinating art.docx
DOCX
The Article Attached A Bretton Woods for InnovationBy St.docx
DOCX
The analysis must includeExecutive summaryHistory and evolution.docx
DOCX
The annotated bibliography for your course is now due. The annotated.docx
DOCX
The Americans With Disabilities Act (ADA) was designed to protect wo.docx
DOCX
The air they have of person who never knew how it felt to stand in .docx
DOCX
The agreement is for the tutor to write a Microsoft word doc of a .docx
DOCX
The abstract is a 150-250 word summary of your Research Paper, and i.docx
The Assignment (3–5 pages)Complete a leadership development plan .docx
The assignment consist of a Case Study.  I have attached the Case St.docx
The annotated bibliography will present an introduction and five ref.docx
The artist Georges Seurat is one of the worlds most fascinating art.docx
The Assignment (2–3 pages including a budget worksheet)Explain th.docx
The assigment is to Research and find me resources on  Portland Sta.docx
the article.httpwww.nytimes.com20120930opinionsundaythe-m.docx
The Arts and Royalty; Philosophers Debate Politics Please respond .docx
The assassination of Archduke Franz Ferdinand was the immediate caus.docx
The article Fostering Second Language Development in Young Children.docx
The Article Critique is required to be a minimum of two pages to a m.docx
The Apple Computer Company is one of the most innovative technology .docx
The artist Georges Seurat is one of the worlds most fascinating art.docx
The Article Attached A Bretton Woods for InnovationBy St.docx
The analysis must includeExecutive summaryHistory and evolution.docx
The annotated bibliography for your course is now due. The annotated.docx
The Americans With Disabilities Act (ADA) was designed to protect wo.docx
The air they have of person who never knew how it felt to stand in .docx
The agreement is for the tutor to write a Microsoft word doc of a .docx
The abstract is a 150-250 word summary of your Research Paper, and i.docx

Recently uploaded (20)

PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Classroom Observation Tools for Teachers
PDF
Pre independence Education in Inndia.pdf
PPTX
Lesson notes of climatology university.
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Institutional Correction lecture only . . .
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Microbial diseases, their pathogenesis and prophylaxis
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Cell Types and Its function , kingdom of life
O7-L3 Supply Chain Operations - ICLT Program
FourierSeries-QuestionsWithAnswers(Part-A).pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
TR - Agricultural Crops Production NC III.pdf
Complications of Minimal Access Surgery at WLH
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Classroom Observation Tools for Teachers
Pre independence Education in Inndia.pdf
Lesson notes of climatology university.
Sports Quiz easy sports quiz sports quiz
Abdominal Access Techniques with Prof. Dr. R K Mishra
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Institutional Correction lecture only . . .

Explain the concept of two types of network intrusions What devic.docx

  • 1. Explain the concept of two types of network intrusions? What devices can you use to protect a network? Name CS117 Lab 03 – Page 1 Lab 03: Refactoring Room Assigned: Tuesday, January 30, 2018 Due: Sunday, February 4, 2018 at 6:00am 1 Overview In this lab we are going to continue refactoring the code that was provided for your game to improve its design and implementation. “Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure.”1 It is the process of reorganizing code to provide a better design while maintaining equivalence. When we refactor code, we want the quality of the code to improve while keeping the functionality of the code exactly the same. 1.1 Cohesion A well-designed class has all of the code that it needs, and no more code. It should represent one clearly- defined kind of thing. Every method should have one specific purpose, and it should be obvious what that purpose is. The same code should not be repeated multiple places. Classes with these properties are cohesive.
  • 2. We already improved the cohesiveness of the Game class in the first lab when we moved duplicate code into the new printLocationInformation method. 1.2 Coupling A well-designed class can be used correctly without knowledge of the details of how it works, so that when those details change it will not be necessary to change the code that uses it. When this is true, classes are loosely coupled. When a class depends highly on the messy details of another, they are highly coupled. Currently, the Room class is tightly coupled with several other classes. We are going to fix the design of the Room class today to fix this. 2 Assignment 2.1 Resolving Issues If you have any outstanding issues from prior labs, you should fix those before doing anything else. Remember to use one commit per issue, and to include “Fixes #4” (or whatever number it is) in your commit message. 2.2 Fixing Some Bad Style We have learned that unless there is a very good reason to do otherwise, fields should always be private. Keeping fields private decreases the coupling between classes because it allows things to change behind the scenes without having to change multiple classes. If you look at the Room class, you will find that it contains public fields. We are going to fix this now. Start by changing each of them to private.
  • 3. But now, when you try to compile the code, you will find that there are lots of compiler errors because of places outside the Room class that had been using those public fields. We still need to have ways for other code to get information about Room objects, so we need to write some accessor and mutator methods in it. 1Martin Fowler in Refactoring: Improving the Design of Existing Code, 1999. Name CS117 Lab 03 – Page 2 You will then need to update every class that uses those fields directly to use your newly written accessor and mutator methods instead. (Hint: The easiest way to do this is to let the compiler tell you about those places.) Look for opportunities to simplify the code as you do this. Again, make sure that you test your code to make sure that it still works, then commit and push your changes. 2.3 Improving Cohesion Writing cohesive code means that the Game class really shouldn’t know all of the possible exits that a Room can have or really know the details of what it means to write out the details of a room. It would be better for the Room class to handle this. The perfect way to do this is to create a toString method in the Room class: 1 /∗ ∗ 2 ∗ R e t u r n s a s t r i n g d e s c r i p t i o n i n c l u d i n g a l l t h e d e t a i l s o f a Room .
  • 4. 3 ∗ F o r e x a m p l e , 4 ∗ O u t s i d e : 5 ∗ You a r e o u t s i d e i n t h e c e n t e r o f t h e King ’ s C o l l e g e campus . 6 ∗ E x i t s : n o r t h e a s t s o u t h w e s t 7 ∗ 8 ∗ @ r e t u r n A s t r i n g r e p r e s e n t i n g a l l t h e d e t a i l s o f a Room . 9 ∗ / 10 public String toString () As you can see, this method should print first the room’s name, then its description, then a list of all the directions out of the room (those that are not null). Remember that we want to keep the output isolated in a single class so we don’t really want this method to print the exit string directly – only to return it. Modify the Game class to use the toString method instead of relying on knowledge of all the possible exits a Room can have. Be sure that you test to make sure that your modifications didn’t change the behavior of the application. Then commit and push your changes. 2.4 Adding Support for Multiple Directions Currently, a player can only move in four directions. What if we wanted to add a new direction of movement to our game? How many places in the code would need to be modified in order to add a new direction (e.g., up) or down)? Fortunately, the work we have done to clean up the interface to the Room class means that it is now easier to change the way exits are stored in the Room class without any need to worry about breaking
  • 5. anything in the Game class. We would like to add support in our game for movement in any arbitrary direction, rather then try to anticipate all of the directions our game may need. We will refactor the Room class to use a HashMap to store exits. The HashMap will map a named direction (like "north" or "east") to the Door that lies in that direction. We will do this now. 1. Replace the individual exit fields of the Room class with a HashMap. The HashMap should map a direction string to the Door that is located in the specified direction. Be sure to completely delete the individual fields for each of the directions. 2. Since we no longer have individual fields for each exit, replace the mutator methods for old fields we just added with a new setExit method that can handle an exit in any direction. This method only needs to put the exit information into the HashMap. It should have the following signature: Name CS117 Lab 03 – Page 3 1 /∗ ∗ 2 ∗ D e f i n e s an e x i t f r o m t h i s room . 3 ∗ 4 ∗ @param d i r e c t i o n The d i r e c t i o n o f t h e e x i t . 5 ∗ @param n e i g h b o r The d o o r i n t h e g i v e n d i r e c t i o n . 6 ∗ / 7 public void setExit(String direction , Door neighbor)
  • 6. 3. Similarly, we can replace the accessor methods for the old fields we added earlier with a new getExit method that can return the exit that exists in any direction. This method need only get the exit information from the HashMap. It should have the following signature: 1 /∗ ∗ 2 ∗ G e t s a d o o r i n a s p e c i f i e d d i r e c t i o n i f i t e x i s t s . 3 ∗ 4 ∗ @ r e t u r n The d o o r i n t h e s p e c i f i e d d i r e c t i o n o r n u l l i f i t d o e s n o t e x i s t . 5 ∗ / 6 public Door getExit(String direction) 4. Modify the rest of the Room class so that it properly uses the HashMap. HINT: The toString() method can iterate over the keys of the HashMap. 5. Modify the goRoom method in the Game class to use your newly written getExit() method. HINT: Look for an opportunity to greatly simplify this code. 6. Finally, we need to modify the World class. Notice that the World class has methods for each of the directions. We should be able to simplify this by replacing all of the direction specific methods for creating doors with a single method similar what we did in the Room class: 1 /∗ ∗ 2 ∗ H e l p e r method f o r c r e a t i n g d o o r s b e t w e e n r o o m s .
  • 7. 3 ∗ 4 ∗ @param f r o m The room w h e r e t h e d o o r o r i g i n a t e s . 5 ∗ @param d i r e c t i o n The d i r e c t i o n o f t h e d o o r i n t h e f r o m room . 6 ∗ @param t o The room w h e r e t h e d o o r g o e s . 7 ∗ / 8 private void createDoor(Room from , String direction , Room to) Congratulations, you have modified your game in such a way that we can create a multi-dimensional world in which our player can move in any direction. Be sure to test to make sure that your modifications didn’t change the behavior of the application and then commit and push your changes. 2.5 Introducing Checkstyle We have talked a lot about why it is beneficial to use good style when programming. Sometimes, however, we are so focused at the task that we are trying to accomplish that we forget to following some of our coding Name CS117 Lab 03 – Page 4 standards. The result of this is that we have code that is harder to understand. In order to help us develop good habits, we will use a tool called Checkstyle. Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. It automates a lot of the process of checking Java code to spare humans of this boring (but important) task.
  • 8. This makes it ideal for projects that want to enforce a coding standard like this project. You can run Checkstyle on your project to make sure that it follows the standards we have been teaching you this year. The first thing we want to do is check that we are using the correct style file. You can double check the style file that BlueJ is using by going to the “Tools” ... “Preferences” menu and selecting the “Extensions” tab. At the top of this tab are two text fields for where Checkstyle will look for its configuration. Be sure that both of them point to http://www.kings-cs.com/kings- cs/java/kings-cs-lower.xml. Now run Checkstyle from the “Tools” menu. This should open a window that lists any problems that were detected in your program. If there are any problems, you should fix them. You should do this at minimum before everytime that you complete an assignment in this course (and all future programming courses). If you needed to make any changes to fix Checkstyle problems, run your program again to make sure that it still works, and then commit and push your changes. If you downloaded BlueJ to your own computer, it probably does not have Checkstyle installed, because it is an extension. You should follow the instructions at http://bluejcheckstyle.sourceforge.net/ to install activate Checkstyle for your copy of BlueJ, because you should be running it before every commit and push that you do for this project (and homework assignments too)! If you need help with this, ask! 2.6 Design Document
  • 9. That is all of the code that you need to write for today. If you have extra time, work on / talk to me about your design document. 3 Finishing When you are finished, you will need to send me information about which commit is the final one for this assignment. To do so, refresh the github.com page for your repository one more time. Then find the section labeled “Latest commit” followed by a strange sequence of seven numbers and letters, then the date. If you click on the numbers and letters, you will go to a page showing the changes made in your most recent commit. Copy the URL of that webpage and paste it into the Moodle submission box. (I can show you how to do all of this if you are unsure.) Make sure that you click the “Submit Assignment” button in Moodle. To avoid any confusion, I strongly recommend that you delete the repository from your local computer and re-clone it next time you are ready to work on it. training/Main.ctxt #BlueJ class context comment0.target=Main comment0.text=rnrn comment1.params=
  • 10. comment1.target=Main() comment1.text=Default constructor. comment2.params=e comment2.target=void componentResized(java.awt.event.ComponentEvent) comment3.params=event comment3.target=void actionPerformed(java.awt.event.ActionEvent) comment3.text=rn Default action listener.rn rn @param eventrn The action event.rn comment4.params=args comment4.target=void main(java.lang.String[]) comment4.text=rn The main method for the program.rn rn @param argsrn The command line arguments.rn numComments=5 training/Game.ctxt #BlueJ class context comment0.target=Game comment0.text=rnrn
  • 11. comment1.params= comment1.target=Game() comment1.text=rn Create the game and initialize its internal map.rn comment2.params= comment2.target=void play() comment2.text=rn Main play routine. Loops until end of play.rn comment3.params= comment3.target=void printLocationInformation() comment3.text=rnPrintsoutthecurrentlocationandexits.rn comment4.params=command comment4.target=boolean processCommand(Command) comment4.text=rn Given a command, process (that is: execute) the command.rn rn @param commandrn The command to be processed.rn @return true If the command ends the game, false otherwise.rn comment5.params=command comment5.target=void goRoom(Command) comment5.text=rn Try to go to one direction. If there is an exit, enter the new room,rn otherwise print an error
  • 12. message.rn rn @param commandrn The command to be processed.rn comment6.params= comment6.target=void printGoodbye() comment6.text=rn Print out the closing message for the player.rn comment7.params= comment7.target=void printHelp() comment7.text=rn Print out some help information. Here we print some stupid, crypticrn message and a list of the command words.rn comment8.params= comment8.target=void printWelcome() comment8.text=rn Print out the opening message for the player.rn comment9.params=command comment9.target=boolean quit(Command) comment9.text=rn "Quit" wasI entered. Check the rest of the command to see whether wern really quit the game.rnrn @param commandrn The command to be processed.rn @return true, if this command quits the game, false otherwise.rn numComments=10
  • 13. training/Player.ctxt #BlueJ class context comment0.target=Player comment0.text=rn comment1.params=currentRoom comment1.target=Player(Room) comment1.text= A constructor for the player class.rn @param currentRoom for the room field.rn comment2.params= comment2.target=Room getRoom() comment2.text= Accossor for getting the room.rn @return for getting the current room that the player is in.rn comment3.params=newRoom comment3.target=void setRoom(Room) comment3.text= Mutator for setting the current room.r[email protected] newRoom for setting the current room.rn numComments=4
  • 14. training/Room.javatraining/Room.java/** */ publicclassRoom{ /** Counter for the total number of rooms created in the world. */ privatestaticint counter; /** The name of this room. Room names should be unique. */ privateString name; /** The description of this room. */ privateString description; /** This room's north exit, null if none exits. */ privateDoor northExit; /** This room's south exit, null if none exits. */ privateDoor southExit; /** This room's east exit, null if none exits. */ privateDoor eastExit; /** This room's west exit, null if none exits. */ privateDoor westExit; /** Static initializer. */ static{ counter =0; } /** * Create a room described "description". Initially, it has no e xits. * "description" is something like "a kitchen" or "an open cou rt yard". * * @param name The room's name. * @param description * The room's description. */ publicRoom(String name,String description){
  • 15. this.name = name; this.description = description; counter++; } /** * Returns the name of this room. * * @return The name of this room. */ publicString getName(){ return name; } /** * Returns the description of this room. * * @return The description of this room. */ publicString getDescription(){ return description; } /** Accessor for geting the northExit. * @return getNorthExit for getting that direction. */ publicDoor getNorthExit(){ return northExit; } /** Accessor for geting the southExit. * @return getSouthExit for getting that direction. */ publicDoor getSouthExit(){ return southExit; }
  • 16. /** Accessor for geting the eastExit. * @return getEastExit for getting that direction. */ publicDoor getEastExit(){ return eastExit; } /** Accessor for geting the westExit. * @return getWestExit for getting that direction. */ publicDoor getWestExit(){ return westExit; } /** Mutator for setting the northExit. * @param newNorthExit for setting that direction. */ publicvoid setNorthExit(Door newNorthExit){ northExit = newNorthExit; } /** Mutator for setting the southExit. * @param newSouthExit for setting that direction. */ publicvoid setSouthExit(Door newSouthExit){ southExit = newSouthExit; } /** Mutator for setting the eastExit. * @param newEastExit for setting that direction. */ publicvoid setEastExit(Door newEastExit){ eastExit = newEastExit; }
  • 17. /** Mutator for setting the westExit. * @param newWestExit for setting that direction. */ publicvoid setWestExit(Door newWestExit){ westExit = newWestExit; } /** * Returns the number of rooms that have been created in the world. * @return The number of rooms that have been created in th e world. */ publicstaticint getCounter(){ return counter; } /** * Returns a string description including all the details of a R oom . * For example , *Outside : *You are outside in the center of the King's College campus . *Exits : north east south west * * @return A string representing all the details of a Room . */ publicString toString (){ String roomInformation =""; roomInformation = getName()+" "+ getDescription(); boolean memo =false; if((getNorthExit()!=null)&&(getSouthExit()!=null)&&(getEastE xit()!=null)&&(getWestExit()!=null)){ memo =true;// roomInformation = getNorthExit();
  • 18. //roomInformation = getName() + " " + getDescription() + getN orthExit() + getSouthExit() + getEastExit() + getWestExit() ; } return roomInformation; } } training/Door.javatraining/Door.java/** */ publicclassDoor{ /** The room that this door leads to. */ privateRoom destination; /** Whether this door is locked. */ privateboolean locked; /** * Constructor for the Door class. * @param destination The room this door leads to */ publicDoor(Room destination){ this.destination = destination; this.locked =false; } /** * A getter for the room this door leads to. * @return The room this door leads to */ publicRoom getDestination(){ return destination; }
  • 19. /** * A getter for whether this door is locked. * @return Whether this door is locked */ publicboolean isLocked(){ return locked; } /** * A setter for whether this door is locked. * @param locked Whether this door is locked. */ publicvoid setLocked(boolean locked){ this.locked = locked; } } training/CommandWords.ctxt #BlueJ class context comment0.target=CommandWords comment0.text=rnrn comment1.params=aString comment1.target=boolean isCommand(java.lang.String) comment1.text=rn Check whether a given String is a valid command word.rn rn @param aString The string to determine whether it is a valid command.rn @return true if a given string is a valid command, false if it isn't.rn
  • 20. numComments=2 training/Reader.classpublicsynchronizedclass Reader { privatestatic java.util.Scanner reader; public void Reader(); publicstatic Command getCommand(); publicstatic String getResponse(); publicstatic String getResponseKeepCase(); static void <clinit>(); } training/World.javatraining/World.javaimport java.util.HashMa p; /** */ publicclassWorld{ /** The rooms in the world. */ privateHashMap<String,Room> rooms; /** * Constructor for the world. */ publicWorld(){ rooms =newHashMap<String,Room>(); createRooms(); } /** * This method takes care of creating all of the aspects of the world for * the "Campus of Kings" application.
  • 21. * * @param name * The provided name of the room. * @return The room associated with the provided name */ publicRoom getRoom(String name){ return rooms.get(name.toLowerCase()); } ///////////////////////////////////////////////////////////////////////////////////// // Start of private helper methods /** * Helper method for recreating a Room. Ensure that the roo m is created and * installed in to the collection of Rooms * * @param theRoom * The room to add to the world. */ privatevoid addRoom(Room theRoom){ rooms.put(theRoom.getName().toLowerCase(), theRoom); } /** * Helper method for creating doors between rooms. * * @param from * The room where the door originates. * @param north * The room to the north of the originating room. */ privatevoid createNorthDoor(Room from,Room north){ Door northDoor =newDoor(north); from.setNorthExit(northDoor); }
  • 22. /** * Helper method for creating doors between rooms. * * @param from * The room where the door originates. * @param east * The room to the east of the originating room. */ privatevoid createEastDoor(Room from,Room east){ Door eastDoor =newDoor(east); from.setSouthExit(eastDoor); } /** * Helper method for creating doors between rooms. * * @param from * The room where the door originates. * @param south * The room to the south of the originating room. */ privatevoid createSouthDoor(Room from,Room south){ Door southDoor =newDoor(south); from.setSouthExit(southDoor); } /** * Helper method for creating doors between rooms. * * @param from * The room where the door originates. * @param west * The room to the west of the originating room. */ privatevoid createWestDoor(Room from,Room west){
  • 23. Door westDoor =newDoor(west); from.setWestExit(westDoor); } /** * This method creates all of the individual places in this wor ld and all * the doors connecting them. */ privatevoid createRooms(){ // Creating all the rooms. Room outside =newRoom("Outside","outside in the center of th e King's College campus."); Room holyCross =newRoom("Holy Cross","at one of two main dormitories on campus."); Room essef =newRoom("Essef","at the other main dormitory on campus."); Room campusCenter =newRoom("Campus Center","in the center of student activities on campus."); Room admin =newRoom("Admin","in the oldest building on ca mpus and home to the computer science department."); Room jumpOffice =newRoom("Jump's Office","in Dr Jump's off ice."); Room hoggOffice =newRoom("Hogg's Office","in Dr Hogg's off ice."); Room lab =newRoom("Computer Lab","in the Computer Scienc e and Math computing lab."); Room classroom =newRoom("Classroom","in the classroom whe re the computer science classes are taught."); // Adding all the rooms to the world. this.addRoom(outside); this.addRoom(holyCross); this.addRoom(essef); this.addRoom(campusCenter); this.addRoom(admin);
  • 24. this.addRoom(jumpOffice); this.addRoom(hoggOffice); this.addRoom(lab); this.addRoom(classroom); // Creating all the doors between the rooms. this.createSouthDoor(essef, outside); this.createNorthDoor(outside, essef); this.createEastDoor(campusCenter, outside); this.createWestDoor(outside, campusCenter); this.createEastDoor(outside, holyCross); this.createWestDoor(holyCross, outside); this.createSouthDoor(outside, admin); this.createNorthDoor(admin, outside); this.createEastDoor(admin, lab); this.createWestDoor(lab, admin); this.createSouthDoor(admin, hoggOffice); this.createNorthDoor(hoggOffice, admin); this.createWestDoor(admin, jumpOffice); this.createEastDoor(jumpOffice, admin); this.createSouthDoor(lab, classroom); this.createNorthDoor(classroom, lab); } } training/Game.classpublicsynchronizedclass Game { private World world; private int turns;
  • 25. private int score; private Character character; private Player playerClass; public void Game(); public void play(); private void printLocationInformation(); private boolean processCommand(Command); private void goRoom(Command); private void printGoodbye(); private void printHelp(); private void printWelcome(); private boolean quit(Command); } training/Writer.javatraining/Writer.javaimport java.awt.Color; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; import javax.swing.JFileChooser; import javax.swing.JTextPane; import javax.swing.text.BadLocationException; import javax.swing.text.Document; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; /** */ publicclassWriter{ /** System new line character. */
  • 26. privatestaticfinalString NEW_LINE; /** Name of the default log. */ privatestaticfinalString DEFAULT_LOG; /** The text area that we will be writing to. */ privatestaticJTextPane textArea; /** Static block. */ static{ NEW_LINE =System.getProperty("line.separator"); DEFAULT_LOG ="defaultlog.txt"; textArea =null; restartLog(); } /** * Mutator for the text component. * * @param text * The text component. */ publicstaticvoid setTextArea(JTextPane text){ textArea = text; textArea.setEditable(false); } /** * Print the user input in blue. * * @param input * The text entered by the user. */ publicstaticvoid printInput(String input){ SimpleAttributeSet attributes =newSimpleAttributeSet(); StyleConstants.setForeground(attributes,Color.BLUE); printWithAttributes(attributes, input + NEW_LINE);
  • 27. } /** * Prints an empty line. */ publicstaticvoid println(){ standardPrint(NEW_LINE); } /** * Prints out a single integer to a line. * * @param toPrint * The integer to print. */ publicstaticvoid println(int toPrint){ String text =""+ toPrint + NEW_LINE; standardPrint(text); } /** * Prints out a single integer. * * @param toPrint * The integer to print. */ publicstaticvoid print(int toPrint){ String text =""+ toPrint; standardPrint(text); } /** * Prints out a double to a line. * * @param toPrint * The double to print.
  • 28. */ publicstaticvoid println(double toPrint){ String text =""+ toPrint + NEW_LINE; standardPrint(text); } /** * Prints out a double. * * @param toPrint * The double to print. */ publicstaticvoid print(double toPrint){ String text =""+ toPrint; standardPrint(text); } /** * Prints out an object to a line. * * @param toPrint * The object to print. */ publicstaticvoid println(Object toPrint){ String text =""+ toPrint + NEW_LINE; standardPrint(text); } /** * Prints out a object. * * @param toPrint * The object to print. */ publicstaticvoid print(Object toPrint){ String text =""+ toPrint;
  • 29. standardPrint(text); } /** * Prints a string after word- wrapping it to 80 characters if possible. Note * that this fails to calculate correct widths if the string conta ins tabs. * Ends with a line return. * * @param toPrint * The String to print. */ publicstaticvoid println(String toPrint){ String text = toPrint + NEW_LINE; standardPrint(text); } /** * Prints a string after word- wrapping it to 80 characters if possible. Note * that this fails to calculate correct widths if the string conta ins tabs. * * @param toPrint * The String to print. */ publicstaticvoid print(String toPrint){ standardPrint(toPrint); } /** * Helper method for standard printing. * * @param toPrint * The String to print.
  • 30. */ privatestaticvoid standardPrint(String toPrint){ SimpleAttributeSet attributes =newSimpleAttributeSet(); printWithAttributes(attributes, toPrint); } /** * Helper method printing with attributes. * * @param attributes * A set of attributes to use when printing. * @param toPrint * The String to print. * @throws IllegalStateException * If the text area has not been set and we are trying t o print * to it. */ privatestaticvoid printWithAttributes(SimpleAttributeSet attribu tes,String toPrint)throwsIllegalStateException{ if(textArea ==null){ thrownewIllegalStateException("Need to set the text area before printing to it."); } try{ Document document = textArea.getDocument(); document.insertString(document.getLength(), toPrint, at tributes); textArea.setCaretPosition(document.getLength()); BufferedWriter log =newBufferedWriter(newFileWriter(DEFAU LT_LOG,true)); log.write(toPrint); log.close(); }catch(BadLocationException ex){ System.err.println("ERROR: Should never get this ["+ toPrint + "]");
  • 31. System.exit(2); }catch(IOException ex){ System.err.println("ERROR printing to default log (see instruct or for help)"); System.exit(1); } } /** * Restart the default log. */ publicstaticvoid restartLog(){ try{ BufferedWriter log =newBufferedWriter(newFileWriter(DEFAU LT_LOG,false)); log.close(); }catch(IOException ex){ System.err.println("ERROR resetting the default log (see instru ctor for help)"); System.exit(1); } } /** * Copy the default log. */ publicstaticvoid copyDefaultLog(){ Scanner input =null; BufferedWriter output =null; try{ JFileChooser chooser =newJFileChooser(); chooser.setCurrentDirectory(newFile(".")); int result = chooser.showOpenDialog(null); if(result ==JFileChooser.APPROVE_OPTION){ input =newScanner(newFile(DEFAULT_LOG)); output =newBufferedWriter(newFileWriter(chooser.g
  • 32. etSelectedFile(),false)); while(input.hasNextLine()){ String line = input.nextLine(); output.write(line + NEW_LINE); } output.close(); input.close(); } }catch(FileNotFoundException exception){ System.err.println("ERROR: default log file cannot be found"); System.exit(3); }catch(IOException exception){ System.err.println("ERROR: file for copy cannot be written to") ; System.exit(4); } } } training/Main$1.classsynchronizedclass Main$1 extends java.awt.event.ComponentAdapter { void Main$1(Main); public void componentResized(java.awt.event.ComponentEvent); } training/CommandWords.classpublicsynchronizedclass CommandWords { privatestatic String[] validCommands; public void CommandWords(); publicstatic boolean isCommand(String); static void <clinit>(); }
  • 33. training/Command.javatraining/Command.javaimport java.util.A rrayList; /** */ publicclassCommand{ /** The command word for this command. */ privateString commandWord; /** The rest of the line with all the spaces removed. */ privateArrayList<String> restOfLine; /** * Create a command object. First is supplied. The second wo rd is assumed * to be null. * * @param firstWord * The first word of the command. Null if the comman d was not * recognized. */ publicCommand(String firstWord){ commandWord = firstWord; restOfLine =newArrayList<String>(); } /** * Create a command object. First and second word must be s upplied, but * either one (or both) can be null. * * @param firstWord
  • 34. * The first word of the command. Null if the comman d was not * recognized. * @param rest * The rest of the command. */ publicCommand(String firstWord,ArrayList<String> rest){ commandWord = firstWord; restOfLine = rest; } /** * Return the command word (the first word) of this comman d. If the command * was not understood, the result is null. * * @return The command word. */ publicString getCommandWord(){ return commandWord; } /** * Returns if this command was not understood. * * @return true if this command was not understood. */ publicboolean isUnknown(){ return(commandWord ==null); } /** * Returns if this command has a second word. * * @return true if the command has a second word. */
  • 35. publicboolean hasSecondWord(){ return restOfLine !=null; } /** * Returns if this command has more words. * * @param index The index of the word needed. * @return true if the command has a word at given index. */ publicboolean hasWord(int index){ return index >=0&& index < restOfLine.size(); } /** * Returns the word at the requested index in the command. * * @param index * The index of word in the command that is being req uested. * * @return A particular word in the command. Returns null if there is no * word corresponding to that requested index. * */ publicString getWord(int index){ String result =null; if(index >=0&& index < restOfLine.size()){ result = restOfLine.get(index); } return result; } /**
  • 36. * Returns the second word of this command, if it exists. * * @return The second word of this command. Returns null if there was no * second word. */ publicString getRestOfLine(){ StringBuffer buffer =null; if(restOfLine.size()!=0){ for(String word : restOfLine){ if(buffer ==null){ buffer =newStringBuffer(); buffer.append(word); } else{ buffer.append(" "); buffer.append(word); } } } String result =""; if(buffer !=null){ result += buffer.toString(); } return result; } } training/Command.classpublicsynchronizedclass Command { private String commandWord; private java.util.ArrayList restOfLine; public void Command(String); public void Command(String, java.util.ArrayList); public String getCommandWord(); public boolean isUnknown();
  • 37. public boolean hasSecondWord(); public boolean hasWord(int); public String getWord(int); public String getRestOfLine(); } training/Room.classpublicsynchronizedclass Room { privatestatic int counter; private String name; private String description; private Door northExit; private Door southExit; private Door eastExit; private Door westExit; public void Room(String, String); public String getName(); public String getDescription(); public Door getNorthExit(); public Door getSouthExit(); public Door getEastExit(); public Door getWestExit(); public void setNorthExit(Door); public void setSouthExit(Door); public void setEastExit(Door); public void setWestExit(Door); publicstatic int getCounter(); public String toString(); static void <clinit>(); } training/defaultlog.txt Welcome to the Campus of Kings! Campus of Kings is a new, incredibly boring adventure game.
  • 38. Type 'help' if you need help. Outside: You are outside in the center of the King's College campus. Exits: north east south west > go west Campus Center: You are in the center of student activities on campus. Exits: north east south west > training/Reader.javatraining/Reader.javaimport java.util.ArrayL ist; import java.util.Scanner; /** */ publicclassReader{ /** The source of command input. */ privatestaticScanner reader; /** * Create a parser to read from the terminal window. */ static{ reader =newScanner(System.in); } /** * Returns the next command from the user. * @return The next command from the user. */
  • 39. publicstaticCommand getCommand(){ String inputLine;// will hold the full input line String word1 =null; ArrayList<String> restOfLine =null; Writer.print("> ");// print prompt inputLine = reader.nextLine().toLowerCase(); Writer.printInput(inputLine); // Find up to two words on the line. Scanner tokenizer =newScanner(inputLine); if(tokenizer.hasNext()){ word1 = tokenizer.next();// get first word if(tokenizer.hasNext()){ restOfLine =newArrayList<String>(); while(tokenizer.hasNext()){ restOfLine.add(tokenizer.next()); } } } tokenizer.close(); // Now check whether this word is known. If so, create a comma nd // with it. If not, create a "null" command (for unknown comma nd). Command result =null; if(CommandWords.isCommand(word1)){ result =newCommand(word1, restOfLine); } else{ result =newCommand(null, restOfLine); } return result; }
  • 40. /** * Return the response to a question in all lower case. * * @return The response typed in by the user. */ publicstaticString getResponse(){ return getResponseKeepCase().toLowerCase(); } /** * Return the response to a question in the case used by the pl ayer. * * @return The response typed in by the user. */ publicstaticString getResponseKeepCase(){ String response = reader.nextLine().trim(); Writer.printInput(response); return response; } } training/team.defs #Sat Feb 03 11:17:26 EST 2018 bluej.teamsettings.groupname= bluej.teamsettings.git.repositoryPrefix=/kings-cs/CS117-S18- AlyousifMustafa.git bluej.teamsettings.git.protocol=https bluej.teamsettings.user=mustafa77a
  • 42. This version of the game contains some very bad class design. It should NOT be used as a basis for extending the project without fixing these design problems. It serves as an example to discuss good and bad design. We will fix the problems with this project through the next couple of labs which walk students through fixing bad design decisions and give them an opportunity to become familiar with the existing code. training/World.ctxt #BlueJ class context comment0.target=World comment0.text=rnrn comment1.params= comment1.target=World() comment1.text=rn Constructor for the world.rn comment2.params=name comment2.target=Room getRoom(java.lang.String) comment2.text=rn This method takes care of creating all of the aspects of the world forrn the "Campus of Kings" application.rn rn @param namern The provided name of the room.rn @return The room associated with the provided namern
  • 43. comment3.params=theRoom comment3.target=void addRoom(Room) comment3.text=rn Helper method for recreating a Room. Ensure that the room is created andrn installed in to the collection of Roomsrn rn @param theRoomrn The room to add to the world.rn comment4.params=from north comment4.target=void createNorthDoor(Room, Room) comment4.text=rn Helper method for creating doors between rooms.rn rn @param fromrn The room where the door originates.rn @param northrn The room to the north of the originating room.rn comment5.params=from east comment5.target=void createEastDoor(Room, Room) comment5.text=rn Helper method for creating doors between rooms.rn rn @param fromrn The room where the door originates.rn @param eastrn The room to the east of the originating room.rn comment6.params=from south comment6.target=void createSouthDoor(Room, Room) comment6.text=rn Helper method for creating doors between rooms.rn rn @param fromrn
  • 44. The room where the door originates.rn @param southrn The room to the south of the originating room.rn comment7.params=from west comment7.target=void createWestDoor(Room, Room) comment7.text=rn Helper method for creating doors between rooms.rn rn @param fromrn The room where the door originates.rn @param westrn The room to the west of the originating room.rn comment8.params= comment8.target=void createRooms() comment8.text=rn This method creates all of the individual places in this world and allrn the doors connecting them.rn numComments=9 training/.gitignore *.class *.ctxt *.*~ doc /.project default.log training/Player.javatraining/Player.java
  • 45. /** */ publicclassPlayer{ /** This stores the room that the player is currnetly in. */ privateRoom room; /** A constructor for the player class. * @param currentRoom for the room field. */ publicPlayer(Room currentRoom){ room = currentRoom; } /** Accossor for getting the room. * @return for getting the current room that the player is in. */ publicRoom getRoom(){ return room; } /** Mutator for setting the current room. *@param newRoom for setting the current room. */ publicvoid setRoom(Room newRoom){ room = newRoom; } } training/Room.ctxt #BlueJ class context comment0.target=Room comment0.text=rnrn
  • 46. comment1.params=name description comment1.target=Room(java.lang.String, java.lang.String) comment1.text=rn Create a room described "description". Initially, it has no exits.rn "description" is something like "a kitchen" or "an open court yard".rn rn @param name The room's name.rn @param descriptionrn The room's description.rn comment10.params=newEastExit comment10.target=void setEastExit(Door) comment10.text= Mutator for setting the eastExit.rn @param newEastExit for setting that direction.rn comment11.params=newWestExit comment11.target=void setWestExit(Door) comment11.text= Mutator for setting the westExit.rn @param newWestExit for setting that direction.rn comment12.params= comment12.target=int getCounter() comment12.text=rn Returns the number of rooms that have been created in the world.rn @return The number of rooms that have been created in the world.rn comment13.params= comment13.target=java.lang.String toString()
  • 47. comment13.text=rn Returns a string description including all the details of a Room .rn For example ,rnOutside :rnYou are outside in the center of the King's College campus .rnExits : north east south westrnrn @return A string representing all the details of a Room .rn comment2.params= comment2.target=java.lang.String getName() comment2.text=rn Returns the name of this room.rn rn @return The name of this room.rn comment3.params= comment3.target=java.lang.String getDescription() comment3.text=rn Returns the description of this room.rn rn @return The description of this room.rn comment4.params= comment4.target=Door getNorthExit() comment4.text= Accessor for geting the northExit.rn @return getNorthExit for getting that direction.rn comment5.params= comment5.target=Door getSouthExit() comment5.text= Accessor for geting the southExit.rn @return getSouthExit for getting that direction.rn comment6.params=
  • 48. comment6.target=Door getEastExit() comment6.text= Accessor for geting the eastExit.rn @return getEastExit for getting that direction.rn comment7.params= comment7.target=Door getWestExit() comment7.text= Accessor for geting the westExit.rn @return getWestExit for getting that direction.rn comment8.params=newNorthExit comment8.target=void setNorthExit(Door) comment8.text= Mutator for setting the northExit.rn @param newNorthExit for setting that direction.rn comment9.params=newSouthExit comment9.target=void setSouthExit(Door) comment9.text= Mutator for setting the southExit.rn @param newSouthExit for setting that direction.rn numComments=14 training/2018sp-cs117-assignment03-Refactoring (2).pdf Name CS117 Lab 03 – Page 1 Lab 03: Refactoring Room
  • 49. Assigned: Tuesday, January 30, 2018 Due: Sunday, February 4, 2018 at 6:00am 1 Overview In this lab we are going to continue refactoring the code that was provided for your game to improve its design and implementation. “Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure.”1 It is the process of reorganizing code to provide a better design while maintaining equivalence. When we refactor code, we want the quality of the code to improve while keeping the functionality of the code exactly the same. 1.1 Cohesion A well-designed class has all of the code that it needs, and no more code. It should represent one clearly- defined kind of thing. Every method should have one specific purpose, and it should be obvious what that purpose is. The same code should not be repeated multiple places. Classes with these properties are cohesive. We already improved the cohesiveness of the Game class in the first lab when we moved duplicate code into the new printLocationInformation method. 1.2 Coupling A well-designed class can be used correctly without knowledge of the details of how it works, so that when those details change it will not be necessary to change the code that uses it. When this is true, classes are loosely coupled. When a class depends highly on the messy
  • 50. details of another, they are highly coupled. Currently, the Room class is tightly coupled with several other classes. We are going to fix the design of the Room class today to fix this. 2 Assignment 2.1 Resolving Issues If you have any outstanding issues from prior labs, you should fix those before doing anything else. Remember to use one commit per issue, and to include “Fixes #4” (or whatever number it is) in your commit message. 2.2 Fixing Some Bad Style We have learned that unless there is a very good reason to do otherwise, fields should always be private. Keeping fields private decreases the coupling between classes because it allows things to change behind the scenes without having to change multiple classes. If you look at the Room class, you will find that it contains public fields. We are going to fix this now. Start by changing each of them to private. But now, when you try to compile the code, you will find that there are lots of compiler errors because of places outside the Room class that had been using those public fields. We still need to have ways for other code to get information about Room objects, so we need to write some accessor and mutator methods in it. 1Martin Fowler in Refactoring: Improving the Design of Existing Code, 1999.
  • 51. Name CS117 Lab 03 – Page 2 You will then need to update every class that uses those fields directly to use your newly written accessor and mutator methods instead. (Hint: The easiest way to do this is to let the compiler tell you about those places.) Look for opportunities to simplify the code as you do this. Again, make sure that you test your code to make sure that it still works, then commit and push your changes. 2.3 Improving Cohesion Writing cohesive code means that the Game class really shouldn’t know all of the possible exits that a Room can have or really know the details of what it means to write out the details of a room. It would be better for the Room class to handle this. The perfect way to do this is to create a toString method in the Room class: 1 /∗ ∗ 2 ∗ R e t u r n s a s t r i n g d e s c r i p t i o n i n c l u d i n g a l l t h e d e t a i l s o f a Room . 3 ∗ F o r e x a m p l e , 4 ∗ O u t s i d e : 5 ∗ You a r e o u t s i d e i n t h e c e n t e r o f t h e King ’ s C o l l e g e campus . 6 ∗ E x i t s : n o r t h e a s t s o u t h w e s t 7 ∗ 8 ∗ @ r e t u r n A s t r i n g r e p r e s e n t i n g a l l t h e d e t a i l s o f a Room . 9 ∗ / 10 public String toString ()
  • 52. As you can see, this method should print first the room’s name, then its description, then a list of all the directions out of the room (those that are not null). Remember that we want to keep the output isolated in a single class so we don’t really want this method to print the exit string directly – only to return it. Modify the Game class to use the toString method instead of relying on knowledge of all the possible exits a Room can have. Be sure that you test to make sure that your modifications didn’t change the behavior of the application. Then commit and push your changes. 2.4 Adding Support for Multiple Directions Currently, a player can only move in four directions. What if we wanted to add a new direction of movement to our game? How many places in the code would need to be modified in order to add a new direction (e.g., up) or down)? Fortunately, the work we have done to clean up the interface to the Room class means that it is now easier to change the way exits are stored in the Room class without any need to worry about breaking anything in the Game class. We would like to add support in our game for movement in any arbitrary direction, rather then try to anticipate all of the directions our game may need. We will refactor the Room class to use a HashMap to store exits. The HashMap will map a named direction (like "north" or "east") to the Door that lies in that direction. We will do this now. 1. Replace the individual exit fields of the Room class with a
  • 53. HashMap. The HashMap should map a direction string to the Door that is located in the specified direction. Be sure to completely delete the individual fields for each of the directions. 2. Since we no longer have individual fields for each exit, replace the mutator methods for old fields we just added with a new setExit method that can handle an exit in any direction. This method only needs to put the exit information into the HashMap. It should have the following signature: Name CS117 Lab 03 – Page 3 1 /∗ ∗ 2 ∗ D e f i n e s an e x i t f r o m t h i s room . 3 ∗ 4 ∗ @param d i r e c t i o n The d i r e c t i o n o f t h e e x i t . 5 ∗ @param n e i g h b o r The d o o r i n t h e g i v e n d i r e c t i o n . 6 ∗ / 7 public void setExit(String direction , Door neighbor) 3. Similarly, we can replace the accessor methods for the old fields we added earlier with a new getExit method that can return the exit that exists in any direction. This method need only get the exit information from the HashMap. It should have the following signature: 1 /∗ ∗ 2 ∗ G e t s a d o o r i n a s p e c i f i e d d i r e c t i o n i f i t e x i s t s . 3 ∗
  • 54. 4 ∗ @ r e t u r n The d o o r i n t h e s p e c i f i e d d i r e c t i o n o r n u l l i f i t d o e s n o t e x i s t . 5 ∗ / 6 public Door getExit(String direction) 4. Modify the rest of the Room class so that it properly uses the HashMap. HINT: The toString() method can iterate over the keys of the HashMap. 5. Modify the goRoom method in the Game class to use your newly written getExit() method. HINT: Look for an opportunity to greatly simplify this code. 6. Finally, we need to modify the World class. Notice that the World class has methods for each of the directions. We should be able to simplify this by replacing all of the direction specific methods for creating doors with a single method similar what we did in the Room class: 1 /∗ ∗ 2 ∗ H e l p e r method f o r c r e a t i n g d o o r s b e t w e e n r o o m s . 3 ∗ 4 ∗ @param f r o m The room w h e r e t h e d o o r o r i g i n a t e s . 5 ∗ @param d i r e c t i o n The d i r e c t i o n o f t h e d o o r i n t h e f r o m room . 6 ∗ @param t o The room w h e r e t h e d o o r g o e s . 7 ∗ / 8 private void createDoor(Room from , String direction , Room to) Congratulations, you have modified your game in such a way
  • 55. that we can create a multi-dimensional world in which our player can move in any direction. Be sure to test to make sure that your modifications didn’t change the behavior of the application and then commit and push your changes. 2.5 Introducing Checkstyle We have talked a lot about why it is beneficial to use good style when programming. Sometimes, however, we are so focused at the task that we are trying to accomplish that we forget to following some of our coding Name CS117 Lab 03 – Page 4 standards. The result of this is that we have code that is harder to understand. In order to help us develop good habits, we will use a tool called Checkstyle. Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. It automates a lot of the process of checking Java code to spare humans of this boring (but important) task. This makes it ideal for projects that want to enforce a coding standard like this project. You can run Checkstyle on your project to make sure that it follows the standards we have been teaching you this year. The first thing we want to do is check that we are using the correct style file. You can double check the style file that BlueJ is using by going to the “Tools” ... “Preferences” menu and selecting the “Extensions” tab. At the top of this tab are two text fields for where Checkstyle will look for its configuration. Be sure
  • 56. that both of them point to http://www.kings-cs.com/kings- cs/java/kings-cs-lower.xml. Now run Checkstyle from the “Tools” menu. This should open a window that lists any problems that were detected in your program. If there are any problems, you should fix them. You should do this at minimum before everytime that you complete an assignment in this course (and all future programming courses). If you needed to make any changes to fix Checkstyle problems, run your program again to make sure that it still works, and then commit and push your changes. If you downloaded BlueJ to your own computer, it probably does not have Checkstyle installed, because it is an extension. You should follow the instructions at http://bluejcheckstyle.sourceforge.net/ to install activate Checkstyle for your copy of BlueJ, because you should be running it before every commit and push that you do for this project (and homework assignments too)! If you need help with this, ask! 2.6 Design Document That is all of the code that you need to write for today. If you have extra time, work on / talk to me about your design document. 3 Finishing When you are finished, you will need to send me information about which commit is the final one for this assignment. To do so, refresh the github.com page for your repository one more time. Then find the section
  • 57. labeled “Latest commit” followed by a strange sequence of seven numbers and letters, then the date. If you click on the numbers and letters, you will go to a page showing the changes made in your most recent commit. Copy the URL of that webpage and paste it into the Moodle submission box. (I can show you how to do all of this if you are unsure.) Make sure that you click the “Submit Assignment” button in Moodle. To avoid any confusion, I strongly recommend that you delete the repository from your local computer and re-clone it next time you are ready to work on it. __MACOSX/training/._2018sp-cs117-assignment03-Refactoring (2).pdf training/Game.javatraining/Game.java/** */ publicclassGame{ /** The world where the game takes place. */ privateWorld world; ///** The room the player character is currently in. */ //private Room currentRoom; /** The number of movments that the player has made. */ privateint turns; /** The score that the player earns. */ privateint score; /** This is the character that is controlled by the player. */ privateCharacter character; /** This is an object for getting the room from the Player class. */
  • 58. privatePlayer playerClass; /** * Create the game and initialize its internal map. */ publicGame(){ world =newWorld(); playerClass =newPlayer(world.getRoom("outside")); score =0; turns =0; character = character; // set the starting room //playerClass = world.getRoom("outside"); } /** * Main play routine. Loops until end of play. */ publicvoid play(){ printWelcome(); // Enter the main game loop. Here we repeatedly read commands and // execute them until the game is over. boolean wantToQuit =false; while(!wantToQuit){ Command command =Reader.getCommand(); wantToQuit = processCommand(command); // other stuff that needs to happen every turn can be added here. turns++; } printGoodbye(); } /** *Printsoutthecurrentlocationandexits.
  • 59. */ privatevoid printLocationInformation(){ boolean wantToQuit =false; Writer.println(playerClass.getRoom().getName()+":"); //printWelcome(); Writer.println("You are "+ playerClass.getRoom().getDescriptio n()); Writer.print("Exits: "); if(playerClass.getRoom()!=null){ Writer.print("north "); } if(playerClass.getRoom()!=null){ Writer.print("east "); } if(playerClass.getRoom()!=null){ Writer.print("south "); } if(playerClass.getRoom()!=null){ Writer.print("west "); } Writer.println(); // other stuff that needs to happen every turn can be added here. } /////////////////////////////////////////////////////////////////////////// // Helper methods for processing the commands /** * Given a command, process (that is: execute) the command. * * @param command * The command to be processed. * @return true If the command ends the game, false otherwis e. */
  • 60. privateboolean processCommand(Command command){ boolean wantToQuit =false; if(command.isUnknown()){ Writer.println("I don't know what you mean..."); }else{ String commandWord = command.getCommandWord(); if(commandWord.equals("help")){ printHelp(); }elseif(commandWord.equals("go")){ goRoom(command); }elseif(commandWord.equals("quit")){ wantToQuit = quit(command); }else{ Writer.println(commandWord +" is not implemented yet!"); } } return wantToQuit; } /////////////////////////////////////////////////////////////////////////// // Helper methods for implementing all of the commands. // It helps if you organize these in alphabetical order. /** * Try to go to one direction. If there is an exit, enter the new room, * otherwise print an error message. * * @param command * The command to be processed. */ privatevoid goRoom(Command command){ Room currentRoom = playerClass.getRoom(); if(!command.hasSecondWord()){
  • 61. // if there is no second word, we don't know where to go... Writer.println("Go where?"); }else{ String direction = command.getRestOfLine(); // Try to leave current. Door doorway =null; if(direction.equals("north")){ doorway = currentRoom.getNorthExit(); } if(direction.equals("east")){ doorway = currentRoom.getEastExit(); } if(direction.equals("south")){ doorway = currentRoom.getSouthExit(); } if(direction.equals("west")){ doorway = currentRoom.getWestExit(); } if(doorway ==null){ Writer.println("There is no door!"); }else{ Room newRoom = doorway.getDestination(); playerClass.setRoom(newRoom); printLocationInformation(); Writer.println(); } } } /** * Print out the closing message for the player. */ privatevoid printGoodbye(){ Writer.println("I hope you weren't too bored here on the Campu
  • 62. s of Kings!"); Writer.println("Thank you for playing. Good bye."); Writer.println("You have earned"+ score +"points in"+ turns +"t urns."); } /** * Print out some help information. Here we print some stupi d, cryptic * message and a list of the command words. */ privatevoid printHelp(){ Writer.println("You are lost. You are alone. You wander"); Writer.println("around at the university."); Writer.println(); Writer.println("Your command words are:"); Writer.println(" go quit help"); } /** * Print out the opening message for the player. */ privatevoid printWelcome(){ Room currentRoom = playerClass.getRoom(); Writer.println(); Writer.println("Welcome to the Campus of Kings!"); Writer.println("Campus of Kings is a new, incredibly boring ad venture game."); Writer.println("Type 'help' if you need help."); Writer.println(); //Writer.println(currentRoom.getName() + ":"); Writer.println(); printLocationInformation(); } /**
  • 63. * "Quit" wasI entered. Check the rest of the command to see whether we * really quit the game. * * @param command * The command to be processed. * @return true, if this command quits the game, false otherw ise. */ privateboolean quit(Command command){ boolean wantToQuit =true; if(command.hasSecondWord()){ Writer.println("Quit what?"); wantToQuit =false; } return wantToQuit; } } training/Main.javatraining/Main.javaimport java.awt.BorderLay out; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.io.InputStream; import java.io.IOException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel;
  • 64. import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.JTextPane; /** */ publicclassMainextendsJFrameimplementsActionListener{ /** Generated unique serial unique id. */ privatestaticfinallong serialVersionUID =- 4610552759287004513L; /** Starting dimension of the window. */ privatestaticfinalDimension WINDOW_DIMENSION; /** The scroll pane so we can resize it when the window is resiz ed. */ privateJScrollPane outputScrollPane; /** The save log menu item. */ privateJMenuItem saveItem; /** The exit menu item. */ privateJMenuItem exitItem; /** The game instance. */ privateGame game; /** Static block for initializing static fields. */ static{ WINDOW_DIMENSION =newDimension(500,500); } /** Default constructor. */ publicMain(){ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  • 65. setLayout(newBorderLayout()); // Setting up the menu bar JMenuBar menuBar =newJMenuBar(); setJMenuBar(menuBar); JMenu fileMenu =newJMenu("File"); menuBar.add(fileMenu); saveItem =newJMenuItem("Save Log ..."); saveItem.addActionListener(this); fileMenu.add(saveItem); exitItem =newJMenuItem("Exit"); exitItem.addActionListener(this); fileMenu.add(exitItem); // Setting out the output area JTextPane output =newJTextPane(); outputScrollPane =newJScrollPane(output); Dimension outputSize =newDimension(); outputSize.setSize(WINDOW_DIMENSION.getWidth(), W INDOW_DIMENSION.getHeight()-100); outputScrollPane.setPreferredSize(outputSize); // So that the scroll pane will resize when the window is resized addComponentListener(newComponentAdapter(){ publicvoid componentResized(ComponentEvent e){ Dimension outputSize =newDimension(); outputSize.setSize(getContentPane().getWidth(), get ContentPane().getHeight()-100); outputScrollPane.setPreferredSize(outputSize); } }); add(BorderLayout.NORTH, outputScrollPane); // Set up the Writer so that it can be used throughout the game. Writer.setTextArea(output); // Setting up the bottom panel for input JPanel bottomPane =newJPanel();
  • 66. bottomPane.setLayout(newBorderLayout()); JButton enterButton =newJButton("Enter"); JTextField commandField =newJTextField(); TextFieldStreamer streamer =newTextFieldStreamer(commandF ield); // maybe this next line should be done in the TextFieldStreamer ctor // but that would cause a "leak a this from the ctor" warning commandField.addActionListener(streamer); enterButton.addActionListener(streamer); System.setIn(streamer); bottomPane.add(BorderLayout.CENTER, commandField); bottomPane.add(BorderLayout.EAST, enterButton); add(BorderLayout.SOUTH, bottomPane); setSize(WINDOW_DIMENSION); setVisible(true); commandField.requestFocus(); game =newGame(); game.play(); } /** * Default action listener. * * @param event * The action event. */ @Override publicvoid actionPerformed(ActionEvent event){ if(event.getSource()== saveItem){
  • 67. Writer.copyDefaultLog(); }elseif(event.getSource()== exitItem){ System.exit(0); } } /** * The main method for the program. * * @param args * The command line arguments. */ publicstaticvoid main(String[] args){ newMain(); } /** * Implementation of InputStream that uses the text from a JT extField as the * input buffer. * * @author Maria Jump */ privateclassTextFieldStreamerextendsInputStreamimplementsAc tionListener{ /** The JTextField to use for input. */ privateJTextField textField; /** The string of text that being passed as input. */ privateString text; /** Used for checking if the available input has reached its end. */ privateint position;
  • 68. /** * Default constructor for TextFieldStreamer. * * @param field * JTextField component being used as input buffer . */ publicTextFieldStreamer(JTextField field){ position =0; text =null; textField = field; } // gets /** * Invoked when an action occurs. In this case, prints the t ext of the * JTextField to StdOut as an error message to differentiat e between * user input and game output. Triggered every time that " Enter" is * pressed on the JTextField. * * Triggered every time that "Enter" is pressed on the textf ield * * @param event * ActionEvent passed by the component. */ @Override publicvoid actionPerformed(ActionEvent event){ text = textField.getText()+System.getProperty("line.sep arator"); position =0; textField.setText(""); synchronized(this){
  • 69. // maybe this should only notify() as multiple threads may // be waiting for input and they would now race for input this.notifyAll(); } } /** * Reads the next byte of data from the input stream. The v alue byte is * returned as an <code>int</code> in the range <code>0< /code> to * <code>255</code>. If no byte is available because the e nd of the * stream has been reached, the value <code>- 1</code> is returned. This * method blocks until input data is available, the end of th e stream is * detected, or an exception is thrown. * * <p> * A subclass must provide an implementation of this meth od. * * @return the next byte of data, or <code>- 1</code> if the end of the * stream is reached. * @exception IOException * if an I/O error occurs. */ @Override publicint read()throwsIOException{ int result =0xDEADBEEF; // test if the available input has reached its end // and the EOS should be returned if(text !=null&& position == text.length()){ text =null;
  • 70. // this is supposed to return -1 on "end of stream" // but I'm having a hard time locating the constant result = java.io.StreamTokenizer.TT_EOF; } if(result ==0xDEADBEEF){ // no input available, block until more is available because // that's // the behavior specified in the Javadocs. while(text ==null|| position >= text.length()){ try{ // read() should block until new input is available. synchronized(this){ this.wait(); } }catch(InterruptedException ex){ ex.printStackTrace(); } } // read an additional character, return it and increment the // index. result = text.charAt(position++); } return result; } } } training/CommandWords.javatraining/CommandWords.java/** */ publicclassCommandWords{ /** A constant array that holds all valid command words. */ privatestaticString[] validCommands;
  • 71. /** * Static block to initialize the fields of CommandWords. */ static{ String[] tempCommands ={"go","quit","help"}; validCommands = tempCommands; } /** * Check whether a given String is a valid command word. * * @param aString The string to determine whether it is a val id command. * @return true if a given string is a valid command, false if i t isn't. */ publicstaticboolean isCommand(String aString){ boolean valid =false; int index =0; while(!valid && index < validCommands.length){ if(validCommands[index].equals(aString)){ valid =true; } index++; } // if we get here, the string was not found in the commands return valid; } } training/Main$TextFieldStreamer.classsynchronizedclass Main$TextFieldStreamer extends java.io.InputStream implements java.awt.event.ActionListener { private javax.swing.JTextField textField; private String text;
  • 72. private int position; public void Main$TextFieldStreamer(Main, javax.swing.JTextField); public void actionPerformed(java.awt.event.ActionEvent); public int read() throws java.io.IOException; } training/Door.ctxt #BlueJ class context comment0.target=Door comment0.text=rnrn comment1.params=destination comment1.target=Door(Room) comment1.text=rn Constructor for the Door class.rn @param destination The room this door leads torn comment2.params= comment2.target=Room getDestination() comment2.text=rn A getter for the room this door leads to.rn @return The room this door leads torn comment3.params= comment3.target=boolean isLocked() comment3.text=rn A getter for whether this door is locked.rn @return Whether this door is lockedrn
  • 73. comment4.params=locked comment4.target=void setLocked(boolean) comment4.text=rn A setter for whether this door is locked.rn @param locked Whether this door is locked.rn numComments=5 training/Player.classpublicsynchronizedclass Player { private Room room; public void Player(Room); public Room getRoom(); public void setRoom(Room); } training/World.classpublicsynchronizedclass World { private java.util.HashMap rooms; public void World(); public Room getRoom(String); private void addRoom(Room); private void createNorthDoor(Room, Room); private void createEastDoor(Room, Room); private void createSouthDoor(Room, Room); private void createWestDoor(Room, Room); private void createRooms(); } training/documents/Untitled Document The Stolen Treasure
  • 74. The player is a guy named Kevin. He can enter some restaurant and get some water and some food. He can get keys from the ground when he finds one and these keys open some specific cars. He can get two weapons, a small one like a rock and a big one like a baseball bat. He needs to get some water if he made 100 turns during midday and 170 turns during night time. Some achievement: He gets 10 points when he finds the silver key and 15 point when he finds the golden key. If he earns 40 point, he will get a map with the next item he finds which makes his mission easier like hunts such as a picture of building that he might find something valuable in or the a picture of a car that he might find something that leads him to figure something out. When he earns 80 points he will get a partner named Sarah who will help him. training/package.bluej #BlueJ package file dependency1.from=Player dependency1.to=Room dependency1.type=UsesDependency dependency10.from=Game dependency10.to=Door dependency10.type=UsesDependency dependency11.from=Game dependency11.to=Reader
  • 75. dependency11.type=UsesDependency dependency12.from=Game dependency12.to=Writer dependency12.type=UsesDependency dependency13.from=Main dependency13.to=Game dependency13.type=UsesDependency dependency14.from=Main dependency14.to=Writer dependency14.type=UsesDependency dependency15.from=Reader dependency15.to=Command dependency15.type=UsesDependency dependency16.from=Reader dependency16.to=Writer dependency16.type=UsesDependency dependency17.from=Reader dependency17.to=CommandWords dependency17.type=UsesDependency dependency2.from=Door dependency2.to=Room dependency2.type=UsesDependency dependency3.from=World dependency3.to=Room dependency3.type=UsesDependency dependency4.from=World dependency4.to=Door dependency4.type=UsesDependency dependency5.from=Room dependency5.to=Door dependency5.type=UsesDependency dependency6.from=Game dependency6.to=World dependency6.type=UsesDependency dependency7.from=Game dependency7.to=Player
  • 76. dependency7.type=UsesDependency dependency8.from=Game dependency8.to=Command dependency8.type=UsesDependency dependency9.from=Game dependency9.to=Room dependency9.type=UsesDependency editor.fx.0.height=680 editor.fx.0.width=800 editor.fx.0.x=316 editor.fx.0.y=23 objectbench.height=101 objectbench.width=776 package.divider.horizontal=0.599476439790576 package.divider.vertical=0.8007380073800738 package.editor.height=427 package.editor.width=651 package.editor.x=236 package.editor.y=85 package.frame.height=600 package.frame.width=800 package.numDependencies=17 package.numTargets=10 package.showExtends=true package.showUses=true project.charset=UTF-8 readme.height=58 [email protected] readme.width=47 readme.x=10 readme.y=10 target1.height=50 target1.name=Player target1.showInterface=false target1.type=ClassTarget target1.width=80
  • 79. public Room getDestination(); public boolean isLocked(); public void setLocked(boolean); } training/Command.ctxt #BlueJ class context comment0.target=Command comment0.text=rnrn comment1.params=firstWord comment1.target=Command(java.lang.String) comment1.text=rn Create a command object. First is supplied. The second word is assumedrn to be null.rn rn @param firstWordrn The first word of the command. Null if the command was notrn recognized.rn comment2.params=firstWord rest comment2.target=Command(java.lang.String, java.util.ArrayList) comment2.text=rn Create a command object. First and second word must be supplied, butrn either one (or both) can be null.rn rn @param firstWordrn The first word of the command. Null if the command was notrn recognized.rn @param restrn The rest of the command.rn
  • 80. comment3.params= comment3.target=java.lang.String getCommandWord() comment3.text=rn Return the command word (the first word) of this command. If the commandrn was not understood, the result is null.rn rn @return The command word.rn comment4.params= comment4.target=boolean isUnknown() comment4.text=rn Returns if this command was not understood.rn rn @return true if this command was not understood.rn comment5.params= comment5.target=boolean hasSecondWord() comment5.text=rn Returns if this command has a second word.rn rn @return true if the command has a second word.rn comment6.params=index comment6.target=boolean hasWord(int) comment6.text=rn Returns if this command has more words.rnrn @param index The index of the word needed.rn @return true if the command has a word at given index.rn comment7.params=index
  • 81. comment7.target=java.lang.String getWord(int) comment7.text=rn Returns the word at the requested index in the command.rn rn @param indexrn The index of word in the command that is being requested.rn rn @return A particular word in the command. Returns null if there is norn word corresponding to that requested index.rn rn comment8.params= comment8.target=java.lang.String getRestOfLine() comment8.text=rn Returns the second word of this command, if it exists.rn rn @return The second word of this command. Returns null if there was norn second word.rn numComments=9 training/Main.classpublicsynchronizedclass Main extends javax.swing.JFrame implements java.awt.event.ActionListener { privatestaticfinal long serialVersionUID = - 4610552759287004513; privatestaticfinal java.awt.Dimension WINDOW_DIMENSION; private javax.swing.JScrollPane outputScrollPane; private javax.swing.JMenuItem saveItem; private javax.swing.JMenuItem exitItem; private Game game; public void Main(); public void actionPerformed(java.awt.event.ActionEvent); publicstatic void main(String[]); static void <clinit>();
  • 82. } training/Reader.ctxt #BlueJ class context comment0.target=Reader comment0.text=rnrn comment1.params= comment1.target=Command getCommand() comment1.text=rn Returns the next command from the user.rn @return The next command from the user.rn comment2.params= comment2.target=java.lang.String getResponse() comment2.text=rn Return the response to a question in all lower case.rnrn @return The response typed in by the user.rn comment3.params= comment3.target=java.lang.String getResponseKeepCase() comment3.text=rn Return the response to a question in the case used by the player.rnrn @return The response typed in by the user.rn numComments=4
  • 83. training/Writer.ctxt #BlueJ class context comment0.target=Writer comment0.text=rnrn comment1.params=text comment1.target=void setTextArea(javax.swing.JTextPane) comment1.text=rn Mutator for the text component.rn rn @param textrn The text component.rn comment10.params=toPrint comment10.target=void println(java.lang.String) comment10.text=rn Prints a string after word-wrapping it to 80 characters if possible. Notern that this fails to calculate correct widths if the string contains tabs.rn Ends with a line return.rnrn @param toPrintrn The String to print.rn comment11.params=toPrint comment11.target=void print(java.lang.String) comment11.text=rn Prints a string after word-wrapping it to 80 characters if possible. Notern that this fails to calculate correct widths if the string contains tabs.rn rn @param toPrintrn The String to print.rn comment12.params=toPrint
  • 84. comment12.target=void standardPrint(java.lang.String) comment12.text=rn Helper method for standard printing.rn rn @param toPrintrn The String to print.rn comment13.params=attributes toPrint comment13.target=void printWithAttributes(javax.swing.text.SimpleAttributeSet, java.lang.String) comment13.text=rn Helper method printing with attributes.rnrn @param attributesrn A set of attributes to use when printing.rn @param toPrintrn The String to print.rn @throws IllegalStateExceptionrn If the text area has not been set and we are trying to printrn to it.rn comment14.params= comment14.target=void restartLog() comment14.text=rn Restart the default log.rn comment15.params= comment15.target=void copyDefaultLog() comment15.text=rn Copy the default log.rn comment2.params=input comment2.target=void printInput(java.lang.String)
  • 85. comment2.text=rn Print the user input in blue.rn rn @param inputrn The text entered by the user.rn comment3.params= comment3.target=void println() comment3.text=rn Prints an empty line.rn comment4.params=toPrint comment4.target=void println(int) comment4.text=rn Prints out a single integer to a line.rn rn @param toPrintrn The integer to print.rn comment5.params=toPrint comment5.target=void print(int) comment5.text=rn Prints out a single integer.rn rn @param toPrintrn The integer to print.rn comment6.params=toPrint comment6.target=void println(double) comment6.text=rn Prints out a double to a line.rn rn @param toPrintrn The double to print.rn comment7.params=toPrint comment7.target=void print(double)
  • 86. comment7.text=rn Prints out a double.rn rn @param toPrintrn The double to print.rn comment8.params=toPrint comment8.target=void println(java.lang.Object) comment8.text=rn Prints out an object to a line.rn rn @param toPrintrn The object to print.rn comment9.params=toPrint comment9.target=void print(java.lang.Object) comment9.text=rn Prints out a object.rn rn @param toPrintrn The object to print.rn numComments=16 training/Writer.classpublicsynchronizedclass Writer { privatestaticfinal String NEW_LINE; privatestaticfinal String DEFAULT_LOG; privatestatic javax.swing.JTextPane textArea; public void Writer(); publicstatic void setTextArea(javax.swing.JTextPane); publicstatic void printInput(String); publicstatic void println(); publicstatic void println(int); publicstatic void print(int); publicstatic void println(double); publicstatic void print(double); publicstatic void println(Object); publicstatic void print(Object);
  • 87. publicstatic void println(String); publicstatic void print(String); privatestatic void standardPrint(String); privatestatic void printWithAttributes(javax.swing.text.SimpleAttributeSet, String) throws IllegalStateException; publicstatic void restartLog(); publicstatic void copyDefaultLog(); static void <clinit>(); }