data structures
student start_code_U08223_cwk1 (1)/.DS_Store
__MACOSX/student start_code_U08223_cwk1 (1)/._.DS_Store
student start_code_U08223_cwk1 (1)/Connection.java
student start_code_U08223_cwk1 (1)/Connection.java
package
cwk1test
;
/**
* Interface to be implemented by classes representing undirected connections
* between Stations in a Network
*
* N.B. You may change the package name for this interface, but you should
* not modify it in any other way.
*/
public
interface
Connection
{
/**
* Get the distance value for this Connection
*
*
@return
distance value for the Connection
*/
double
getDistance
();
/**
* Get a reference to one of the stations that are linked by this Connection
* The returned Sation is different to that returned by getStationB()
*
*
@return
a reference to one of the Stations linked by this Connection
*/
Station
getStationA
();
/**
* Get a reference to one of the stations that are linked by this Connection
* The returned Sation is different to that returned by getStationA()
*
*
@return
a reference to one of the Stations linked by this Connection
*/
Station
getStationB
();
/**
* Given one of the stations linked by this Connection, return the other
*
*
@param
station one of the Stations linked by the Connection
*
@return
the other Station
* PRECONDITION: station==getStationA() || station==getStationB()
*/
Station
getOtherStation
(
Station
station
);
}
__MACOSX/student start_code_U08223_cwk1 (1)/._Connection.java
student start_code_U08223_cwk1 (1)/GraphicsUtil.java
student start_code_U08223_cwk1 (1)/GraphicsUtil.java
/**
* Utility glass for drawing networks
*/
package
cwk1test
;
import
java
.
awt
.
Color
;
import
java
.
awt
.
FontMetrics
;
import
java
.
awt
.
Graphics
;
import
javax
.
swing
.
JComponent
;
/**
*
*
@author
p0073862
*/
public
class
GraphicsUtil
{
private
static
final
double
NOTIONAL_WIDTH
=
255
;
private
static
final
double
NOTIONAL_HEIGHT
=
255
;
private
static
final
int
CIRCLE_RADIUS
=
5
;
private
static
final
int
[]
xPoints
=
new
int
[
2
];
private
static
final
int
[]
yPoints
=
new
int
[
2
];
/**
* Draw a network onto a component
*
*
*
@param
network Network to be drawn
*
@param
component Component on to which the network is to be drawn (e.g a
* JPanel)
*
@param
g Graphics object used for drawing
*/
public
static
void
drawNetwork
(
Network
network
,
JComponent
component
,
Graphics
g
)
{
Color
oldColor
=
g
.
getColor
();
double
xScale
=
component
.
getWidth
()
/
NOTIONAL_WIDTH
;
double
yScale
=
component
.
getHeight
()
/
NOTIONAL_HEIGHT
;
for
(
Station
station
:
network
.
getStations
())
{
FontMetrics
metrics
=
g
.
getFontMetrics
();
int
textHeight
=
metrics
.
getHeight
();
int
x
=
(
int
)
Math
.
round
(
xScale
*
station
.
getxPos
())
-
CIRCLE_RADIUS
;
int
y
=
(
int
)
Math
.
round
(
yScale
*
station
.
getyPos
())
-
CIRCLE_RADIUS
;
int
diameter
=
CIRCLE_RADIUS
*
2
;
g
.
setColor
(
Color
.
BLACK
);
g
.
fillOval
(
x
,
y
,
diameter
,
diameter
);
g
.
setColor
(
Color
.
RED
);
g
.
drawString
(
station
.
getName
(),
x
+
CIRCLE_RADIUS
,
y
-
CIRCLE_RADIUS
);
for
(
Connection
connection
:
network
.
getConnectionsFrom
(
station
))
{
xPoints
[
0
]
=
(
int
)
Math
.
round
(
xScale
*
connection
.
getStationA
().
getxPos
());
xPoints
[
1
]
=
(
int
)
Math
.
round
(
xScale
*
connection
.
getStationB
().
getxPos
());
yPoints
[
0
]
=
(
int
)
Math
.
round
(
yScale
*
connection
.
getStationA
().
getyPos
());
yPoints
[
1
]
=
(
int
)
Math
.
round
(
yScale
*
connection
.
getStationB
().
getyPos
());
g
.
setColor
(
Color
.
GRAY
);
g
.
drawPolyline
(
xPoints
,
yPoints
,
2
);
int
markX
=
xPoints
[
0
]
/
2
+
xPoints
[
1
]
/
2
;
int
markY
=
yPoints
[
0
]
/
2
+
yPoints
[
1
]
/
2
;
g
.
setColor
(
Color
.
RED
);
g
.
drawString
(
Double
.
toString
(
connection
.
getDistance
()),
markX
,
markY
);
}
}
g
.
setColor
(
oldColor
);
}
}
__MACOSX/student start_code_U08223_cwk1 (1)/._GraphicsUtil.java
student start_code_U08223_cwk1 (1)/Network.java
student start_code_U08223_cwk1 (1)/Network.java
package
cwk1test
;
import
java
.
util
.
Collection
;
/**
*
* Interface to be implemented by classes that implement networks
*
* N.B. You may change the package name for this interface, but you should not
* modify it in any other way.
*/
public
interface
Network
{
/**
* Add a station to the network
*
*
@param
name name of the station
*
@param
xPos x position at which station should be displayed in a 255x255
* graph area
*
*
@param
yPos y position at which station should be displayed in a 255x255
* graph area
*
*/
void
addStation
(
String
name
,
int
xPos
,
int
yPos
);
/**
* Get a reference to the station with a specified name
*
*
@param
name name of the station
*
@return
If the network contains a station with the specified name then a
* reference to that station is returned. Otherwise the return value is
* null.
*/
Station
getStation
(
String
name
);
/**
* Return a Collection containing all the stations in the network
*
*
@return
a Collection containing all the stations in the network
*/
Collection
<
Station
>
getStations
();
/**
* Return a Collection containing all the connections in the network
*
*
@return
a Collection containing all the connections in the network
*/
Collection
<
Connection
>
getConnections
();
/**
* Return a Collection containing all the Connections in the network that
* start or end at a specified station
*
*
@param
station Station to/from which the Connection should run
*
*
@return
a Collection containing all the connections that start or end at
* the specified station
*/
Collection
<
Connection
>
getConnectionsFrom
(
Station
station
);
/**
* Add a connection between two stations
*
*
@param
stationA name of one of the stations connected
*
@param
stationB name of the other station connected
*
@param
distance distance value for the connection
*
* PRECONDITION:
* getStation(StationA) != null
* && getStation(StationB) != null
* && getStations.contains(getStation(StationA)) &&
* getStations.contains(getStation(StationB)
*/
void
addConnection
(
String
stationA
,
String
stationB
,
double
distance
);
/**
* Delete all of the Stations and Connections in the Network
*/
void
clear
();
}
__MACOSX/student start_code_U08223_cwk1 (1)/._Network.java
student start_code_U08223_cwk1 (1)/Station.java
student start_code_U08223_cwk1 (1)/Station.java
/**
* Interface to be implemented by classes that represent Stations
*
* N.B. You may change the package name for this interface, but you should not
* modify it in any other way.
*/
package
cwk1test
;
import
java
.
util
.
Collection
;
public
interface
Station
{
/**
* Get the name of the station
*
*
@return
the name of the station
*/
String
getName
();
/**
* Get the x position that the station would have when displayed in a
* 255x255 graph area
*
*
@return
the x position as specified above
*/
int
getxPos
();
/**
* Get the x position that the station would have when displayed in a
* 255x255 graph area
*
*
@return
the y position as specified above
*/
int
getyPos
();
}
__MACOSX/student start_code_U08223_cwk1 (1)/._Station.java
student start_code_U08223_cwk1 (1)/StationQueue.java
student start_code_U08223_cwk1 (1)/StationQueue.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package
cwk1test
;
/**
* Interface to be implemented by queues of Stations
*
* N.B. You may change the package name for this interface, but you should
* not modify it in any other way.
*/
public
interface
StationQueue
{
/**
* Add a Station to the back of the queue
*
*
@param
s Station to be added
*/
void
add
(
Station
s
);
/**
* Get the number of Stations in the queue
*
*
@return
number of stations in the queue
*/
int
getSize
();
/**
* Return, but do not remove, the station
* at the front of the queue
*
*
@return
the Station at the front of the queue
*
* PRECONDITION: queue is not empty
*/
Station
peek
();
/**
* Return, and remove, the station at the
* front of the queue
*
*
@return
the Station at the front of the queue
*
* PRECONDITION: queue is not empty
*/
Station
remove
();
}
__MACOSX/student start_code_U08223_cwk1 (1)/._StationQueue.java
student start_code_U08223_cwk1 (1)/StationStack.java
student start_code_U08223_cwk1 (1)/StationStack.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package
cwk1test
;
/**
* Interface to be implemented by classes that represent stacks of Stations
*
* N.B. You may change the package name for this interface, but you should not
* modify it in any other way.
*/
public
interface
StationStack
{
/**
* Add a Station to the top of the stack
*
*
@param
s Station to be added
*/
void
push
(
Station
s
);
/**
* Get the number of Stations in the queue
*
*
@return
number of stations in the queue
*/
int
getSize
();
/**
* Return, but do not remove, the station at the top of the stack
*
*
@return
the Station at the top of the stack
*
* PRECONDITION: stack is not empty
*/
Station
peek
();
/**
* Return, and remove, the station at the top of the stack
*
*
@return
the Station at the top of the stack
*
* PRECONDITION: stack is not empty
*/
Station
pop
();
}