Department of Civil and Environmental Engineering, Spring Semester, 2013

advertisement
Department of Civil and Environmental Engineering,
Spring Semester, 2013
ENCE 688R: Final Exam: 2 Hours, Open Book and Open Notes
Name :
Question
Points
1
50
2
30
Total
80
1
Score
Question 1: 50 points
Let’s suppose that we wish to model and visualize the “house and grounds” scene shown in
Figure 1. Since there are many items to model and visualize, use of the Java Collections Framework
makes sense.
A second potentially good idea is to model the scene as a collection of features, with the
trees, gate, house, pathway, island, and rail perimeter all being features in the “house and grounds”
scene. Figure 2 shows a tentative class hierarchy for the modeling of features, geometric features
and topological features. All features will have a name. Some features will also have geometry –
the latter is needed for visualization of the scene. Topological features are important for capturing
notions of connectivity, thereby allowing for questions of the type: How do I get from the gate to
the island? to be answered.
Now let’s suppose that you are in the very early stages of the model development, with
everything being ignored except the small group of trees adjacent to the house. You are experimenting with the following set of source code files: build.xml, Feature.java, AbstractFeature.java,
Location.java, TestScene.java and Tree.java. Details of the java source code files are as
follows:
File: Feature.java
source code
/*
* ===================================================================
* Feature.java: Geographic feature interface ...
* ===================================================================
*/
package scene;
public interface
public void
public String
public void
public double
public void
public double
}
Feature {
setName( String sName );
getName();
setX( double dX );
getX();
setY( double dY );
getY();
File: AbstractFeature.java
source code
2
Figure 1: Modeling for a house and its surrounding grounds.
1..n
Scene
<< interface >>
Feature
<< abstract >>
AbstractGeometry
Chain
1
<< abstract >>
AbstractTopology
Point
Node
1
i
2
Link
1
1.....m
Line
Coord
1
1
Figure 2: Conceptual model for a scene assembled from features.
3
1
/*
* ===================================================================
* AbstractFeature.java: Abstract class for geographic features ....
* ===================================================================
*/
package scene;
public abstract class AbstractFeature implements Feature {
protected Location c = new Location();
protected String sName;
public AbstractFeature () {}
public AbstractFeature ( double dX, double dY ) {
c.dX = dX;
c.dY = dY;
}
public void setName( String sName ) {
this.sName = sName;
}
public String getName() {
return sName;
}
public void setX( double dX ) {
c.dX = dX;
}
public double getX() {
return c.dX;
}
public void setY( double dY ) {
c.dY = dY;
}
public double getY() {
return c.dY;
}
}
File: Location.java
source code
/*
*
*
============================================================
Location.java: Store (x,y) location of a geographic feature.
4
* ============================================================
*/
package scene;
public class Location {
double dX, dY;
}
File: Tree.java
source code
/*
* ===================================================================
* Tree.java: Create a tree ...
* ===================================================================
*/
package scene.primitive;
import scene.*;
public class Tree extends AbstractFeature {
public Tree() {}
public Tree ( double dX, double dY ) {
super ( dX, dY );
}
public String toString() {
String s = "Tree: Name = " + sName + "\n";
s = s
+ "
" + "Location (x,y) = (" + getX() + "," + getY() + ")";
return s;
}
}
File: TestScene.java
source code
/*
*
*
=======================================================================
TestScene.java: Create collections of simple geographic features.
5
* =======================================================================
*/
package demo;
import java.util.*;
import scene.*;
import scene.primitive.*;
public class TestScene {
public static void main(String[] args) {
// Part 01: Create three tree objects ...
Tree tree01 = new Tree( 1.0, 1.0 );
tree01.setName("Big Fur");
Tree tree02 = new Tree( 3.0, 2.0 );
tree02.setName("Weeping Willow");
Tree tree03 = new Tree( 3.0, 5.0 );
tree03.setName("Canadian Maple");
System.out.println("Part 01: Print details of tree objects" );
System.out.println("======================================" );
System.out.println( tree01 );
System.out.println( tree02 );
System.out.println( tree03 );
// Part 02: Create an arraylist of geographic features ...
ArrayList<Feature> array = new ArrayList<Feature>();
array.add( tree01 );
array.add( tree02 );
array.add( tree03 );
// Create duplicate entry ....
array.add( tree03 );
System.out.println("Part 02: Arraylist of trees" );
System.out.println("===========================" );
System.out.println( array );
// Part 03: Create and print a hashset of geographic features ...
Set<Feature> set = new HashSet<Feature>();
set.add (tree01);
set.add (tree02);
set.add (tree03);
// Create duplicate entry ....
set.add (tree03);
6
System.out.println("Part 03: HashSet of trees" );
System.out.println("=========================" );
System.out.println( set );
// Part 04: Create and populate the hashmap of geographic features ...
Map<String, Feature> map = new HashMap<String, Feature>();
map.put ( tree01.getName(), tree01 );
map.put ( tree02.getName(), tree02 );
map.put ( tree03.getName(), tree03 );
// Create duplicate entry ....
map.put ( tree03.getName(), tree03 );
System.out.println("Part 04: HashMap of trees" );
System.out.println("=========================" );
System.out.println( map );
// Part 05: Assemble a treemap from the hashmap ...
Map treemap = new TreeMap ( map );
System.out.println("Part 05: TreeMap of trees" );
System.out.println("=========================" );
System.out.println( treemap );
}
}
The script of program input and output is as follows:
prompt >> ant run01
Buildfile: /Users/austin/ence688r.d/exam-code.d/build.xml
compile:
[javac] /Users/austin/ence688r.d/exam-code.d/build.xml:9:
run01:
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
Part 01: Print details of tree objects
======================================
Tree: Name = Big Fur
Location (x,y) = (1.0,1.0)
Tree: Name = Weeping Willow
Location (x,y) = (3.0,2.0)
Tree: Name = Canadian Maple
Location (x,y) = (3.0,5.0)
Part 02: Arraylist of trees
7
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
===========================
[Tree: Name = Big Fur
Location (x,y) = (1.0,1.0), Tree: Name = Weeping Willow
Location (x,y) = (3.0,2.0), Tree: Name = Canadian Maple
Location (x,y) = (3.0,5.0), Tree: Name = Canadian Maple
Location (x,y) = (3.0,5.0)]
Part 03: HashSet of trees
=========================
[Tree: Name = Canadian Maple
Location (x,y) = (3.0,5.0), Tree: Name = Weeping Willow
Location (x,y) = (3.0,2.0), Tree: Name = Big Fur
Location (x,y) = (1.0,1.0)]
Part 04: HashMap of trees
=========================
{Weeping Willow=Tree: Name = Weeping Willow
Location (x,y) = (3.0,2.0), Canadian Maple=Tree: Name = Canadian Maple
Location (x,y) = (3.0,5.0), Big Fur=Tree: Name = Big Fur
Location (x,y) = (1.0,1.0)}
Part 05: TreeMap of trees
=========================
{Big Fur=Tree: Name = Big Fur
Location (x,y) = (1.0,1.0), Canadian Maple=Tree: Name = Canadian Maple
Location (x,y) = (3.0,5.0), Weeping Willow=Tree: Name = Weeping Willow
Location (x,y) = (3.0,2.0)}
BUILD SUCCESSFUL
Total time: 1 second
prompt >> exit
Please look at the source code carefully and answer the questions that follow:
[1a] (5 pts). Draw and label a diagram that shows the organizational arrangement of java packages
and user-defined java source code files.
8
[1b] (5 pts). Draw and label a diagram that shows the relationship among the Feature, AbstractFeature, Location, Tree and TestScene classes.
[1c] (5 pts). Draw and label a diagram showing the layout of memory generated by the statements
in Part 01 of TestScene.java.
9
[1d] (5 pts). Draw and label a diagram showing the layout of memory after the statements in
Part 02 have finished.
[1e] (5 pts). What is the purpose of the syntax <Feature> in the statement:
ArrayList<Feature> array = new ArrayList<Feature>();
10
[1f ] (5 pts). Explain why objects of type Tree can be added to an arraylist of features, i.e.,
ArrayList<Feature> array = new ArrayList<Feature>();
array.add( tree01 );
[1g] (5 pts). In what important way do ArrayLists differ from HashSets?
[1h] (5 pts). Explain the purpose of each part in the statement:
Map<String, Feature> map = new HashMap<String, Feature>();
11
[1i] (5 pts). Draw and label a diagram showing the layout of memory generated by the statements
in Part 04 of TestScene.java.
[1j] (5 pts). In Part 05 of TestScene.java, what is the primary purpose of the HashMap to TreeMap
conversion?
12
Question 2: 30 points
In this question we explore the modeling of many-to-many relationships between US states and
their membership in geographical regions.
Geographical Regions
US States
WA
NY
CO
West Coast
East Coast
MD
CA
TX
FL
Southern States
Figure 3: Schematic for US states and their membership in geographical regions.
As a starting point, the right-hand side of Figure 3 shows a sampling of US states – CA is an
abbreviation for California, MD stands for Maryland, FL stands for Florida, and so forth. The
left-hand side of Figure 3 shows how groups of states can be bundled into geographical regions.
For example, CA and WA belong to the West Coast region; Maryland and Florida are part of
the East Coast region; Texas and Florida belong to the Southern States region. A many-to-many
relationship exists between geographical regions and US states because regions contain multiple
states and, conversely, states such as Florida belong to multiple regions.
Now let’s assume that software for modeling the many-to-many relationship is contained into files,
State.java and GeographicalRegion.java. A third file, SimUSA.java, creates state and geographical
region objects, and then systematically assembles dependencies in the many-to-many relationship.
File: State.java
source code
/*
* =======================================================
* State.java; Create a "US state" object .....
* =======================================================
*/
package geography;
import java.util.Collection;
13
import java.util.ArrayList;
import java.util.Iterator;
public class State {
private String
sName;
private int iPopulation;
// Collection of regions to which the state belongs.
private Collection<GeographicalRegion> regions;
// Constuctor method ...
public State( String sName ) {
this.sName = sName;
regions
= new ArrayList<GeographicalRegion>();
}
// Set and get methods for the State name ...
public void setName( String sName ) {
this.sName = sName;
}
public String getName() {
return sName;
}
// Set and get methods for the State Population ...
public void setPopulation( int iPopulation ) {
this.iPopulation = iPopulation;
}
public int getPopulation() {
return iPopulation;
}
// Add a State to a Region ....
public void addGeographicalRegion( GeographicalRegion region ) {
// Add new region to states membership ....
if ( getGeographicalRegions().contains(region) == false ) {
getGeographicalRegions().add(region);
}
// Update list of states belonging to region ....
if ( region.getStates().contains(this) == false ) {
region.getStates().add(this);
}
}
14
// Return collection of regions ...
public Collection<GeographicalRegion> getGeographicalRegions() {
return regions;
}
// Create String representation of student object ...
public String toString() {
String s = "State: " + sName + "\n";
s = s + "Population: " + iPopulation + "\n";
s = s + "Regions: ";
if ( regions.size() == 0 )
s = s + " none \n";
else {
Iterator iterator1 = regions.iterator();
while ( iterator1.hasNext() != false ) {
GeographicalRegion r = (GeographicalRegion) iterator1.next();
s = s + r.getName() + " ";
}
s = s + "\n";
}
return s;
}
}
File: GeographicalRegion.java
source code
/*
* ======================================================================
* GeographicalRegion.java; Create simple model of a geographical region.
* ======================================================================
*/
package geography;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class GeographicalRegion {
private String sName;
// Setup collection of states ....
private Collection<State> states;
15
// Constructor method ....
public GeographicalRegion(){
states = new ArrayList<State>();
}
// Methods to deal with the region name ...
public String getName() {
return sName;
}
public void setName(String regionName ) {
this.sName = regionName;
}
// Add a state to the geographical region ...
public void addState( State st ) {
if ( getStates().contains( st ) == false ) {
getStates().add( st );
}
// Update regions on the state side ....
if ( st.getGeographicalRegions().contains(this) == false ) {
st.getGeographicalRegions().add(this);
}
}
public Collection<State> getStates() {
return states;
}
public void setState( Collection<State> states ) {
this.states = states;
}
// Compute total population of the geographical region ...
public int getPopulation() {
int iPopulation = 0;
... Details of source code removed ... see Question [2f]...
return iPopulation;
}
// Create a String representation for the geographical region ...
public String toString() {
String s = "Geographical Region: " + sName + "\n";
s = s + "States: ";
if ( states.size() == 0 )
16
s = s + " none \n";
else {
Iterator iterator1 = states.iterator();
while ( iterator1.hasNext() != false ) {
State st = (State) iterator1.next();
s = s + st.getName() + " ";
}
s = s + "\n";
}
s = s + "Population: " + getPopulation() + "\n";
return s;
}
}
File: SimUSA.java
source code
/*
* ========================================================================
* SimUSA.java: Simulate many-to-many relationships between US states
*
and the geographical regions to which they belong.
* ========================================================================
*/
package demo;
import geography.*;
public class SimUSA {
public static void main( String[] args ) {
// [a]: Create objects for US states....
State md = new State( "MD"
md.setPopulation( 5 );
State fl = new State( "FL"
fl.setPopulation( 10 );
State ca = new State( "CA"
ca.setPopulation( 40 );
State tx = new State( "TX"
tx.setPopulation( 30 );
);
);
);
);
// [b]: Add states to the East Coast Region ...
GeographicalRegion east = new GeographicalRegion();
east.setName("East Coast");
east.addState( md );
east.addState( fl );
17
// [c]: Add states to the Southern Region ...
GeographicalRegion south = new GeographicalRegion();
south.setName("Southern States");
south.addState( tx );
south.addState( fl );
// [d]: Print details of state-region assocations ...
System.out.println( "Part 1: Summary of State/Geographical Region Associations" );
System.out.println( "=========================================================" );
System.out.println( md );
System.out.println( fl );
// [e]: Print details of region-state assocations ...
System.out.println( "Part 2: Summary of Geographical Region/State Associations" );
System.out.println( "=========================================================" );
System.out.println( east );
System.out.println( south );
}
}
The script of program input and output is as follows:
prompt >> ant run02
Buildfile: /Users/austin/ence688r.d/exam-code.d/build.xml
compile:
[javac] /Users/austin/ence688r.d/exam-code.d/build.xml:9:
run02:
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
[java]
Part 1: Summary of State/Geographical Region Associations
=========================================================
State: MD
Population: 5
Regions: East Coast
State: FL
Population: 10
Regions: East Coast Southern States
Part 2: Summary of Geographical Region/State Associations
=========================================================
Geographical Region: East Coast
States: MD FL
Population: 15
18
[java] Geographical Region: Southern States
[java] States: TX FL
[java] Population: 40
[java]
BUILD SUCCESSFUL
Total time: 1 second
prompt >> exit
Please look at the source code carefully and answer the questions that follow:
[2a] (5 pts). Draw and label a diagram that shows the organizational arrangement of java packages
and user-defined java source code files.
19
[2b] (5 pts). Draw and label a diagram that shows the relationship among the State, GeographicalRegion and SimUSA classes.
[2c] (5 pts). Draw and label a diagram showing the layout of memory generated by sections [a]
through [c] of SimUSA.java.
20
[2d] (5 pts). What is the purpose of the syntax <GeographicalRegion> in the statement:
private Collection<GeographicalRegion> regions;
[2e] (5 pts). Briefly explain the way in which the classes State and GeographicalRegion would
change if the many-to-many relationships were implemented as HashSets instead of ArrayLists.
21
[2f ] (5 pts). Write the code missing from the method getPopulation() in GeographicalRegion.java, i.e,
public int getPopulation() {
int iPopulation = 0;
... fill in the missing details here ....
return iPopulation;
}
22
Download