Department of Civil and Environmental Engineering, Spring Semester, 2012

advertisement
Department of Civil and Environmental Engineering,
Spring Semester, 2012
ENCE 688R: Final Exam: 2 Hours, Open Book and Open Notes
Name :
Question
Points
1
30
2
30
3
20
Total
80
1
Score
Question 1: 30 points
This question covers Java programming with abstract classes and array lists.
Absract class
Shape
Rectangle
Circle
Location
Triangle
Figure 1: Relationship among the classes Shape, Location, Rectangle, Circle and Triangle.
Figure 1 shows the relationship among the classes Shape, Location, Rectangle, Circle and Triangle. The following code is a snapshot of the actual implementation that only covers Shape.java,
Location.java, Rectangle.java and the test program defined in EngineeringProperties.java.
Shape.java. The details of Shape.java are as follows:
source code
public abstract class Shape {
Location c = new Location();
public
public
public
public
public
abstract
abstract
abstract
abstract
abstract
double
double
String
double
double
getX();
getY();
toString();
perimeter();
area();
}
Location.java. The (x,y) coordinates of a point are handled in Location.java.
source code
public class Location {
double dX, dY;
}
Rectangle.java. The details of Rectangle.java are as follows:
source code
2
public class Rectangle extends Shape {
double dSide1, dSide2;
public Rectangle ( double dSide1, double dSide2, double dX, double dY ) {
this.dSide1 = dSide1;
this.dSide2 = dSide2;
c.dX = dX;
c.dY = dY;
}
public String toString() {
return "Rectangle : Side1 = " + dSide1 + " Side2 = " + dSide2 ;
}
public double perimeter() {
return 2.0*(dSide1 + dSide2);
}
public double area() {
return dSide1*dSide2;
}
public double getX() { return c.dX; }
public double getY() { return c.dY; }
}
EngineeringProperties.java.
source code
import java.util.ArrayList;
import java.util.List;
public class EngineeringProperties {
public static void main ( String args[] ) {
// Create and initialize a grid of ten shapes
List shapes = new ArrayList();
Shape s0 = new Rectangle( 0.25, 0.25, 0.0, 2.0 );
Shape s1 = new Rectangle( 0.25, 0.25, 1.0, 1.0 );
// Add shapes to the array list ....
shapes.add( s0 );
shapes.add( s1 );
// Compute and print total shape area .....
double dArea = 0.0;
3
for (int ii = 1; ii <= shapes.size(); ii = ii + 1) {
Shape s = (Shape) shapes.get(ii-1);
dArea = dArea + s.area();
}
System.out.println("");
System.out.printf("Total Area
= %10.2f\n", dArea );
System.out.println("---------------------------------");
}
}
Please look at the source code carefully and answer the questions that follow:
[1a] (5 pts). Write a main() method for Rectangle.java. The method should allocate and initialize
an object of type Rectangle and then print its contents.
Now suppose that the new version of Rectangle.java (containing your main() method) is compiled
without error.
[1b] (2 pts). What command would you give to run the Rectangle program?
4
[1c] (3 pts). What will the program output look like?
[1d] (5 pts). Draw and label a diagram showing the layout of memory produced by the statement:
Rectangle rA = new Rectangle( 1.0, 1.0, 3.0, 4.0 );
Note: Make sure that you capture the relationship between Rectangle, Shape and Location.
[1e] (3 pts). What is the purpose of the method toString() in the statement?
System.out.println( rA.toString() );
5
Now let’s move onto Shape.java.
[1f ] (5 pts). List two ways in which an abstract class (e.g., Shape.java) differs from a regular class
(e.g., Circle.java).
[1g] (4 pts). Draw and label a diagram showing the layout of memory that occurs after the block
of statements:
List shapes = new ArrayList();
Shape s0 = new Rectangle( 0.25, 0.25, 0.0, 2.0 );
Shape s1 = new Rectangle( 0.25, 0.25, 1.0, 1.0 );
shapes.add( s0 );
shapes.add( s1 );
has finished.
6
[1h] (3 pts). The block of code:
double dArea = 0.0;
for (int ii = 1; ii <= shapes.size(); ii = ii + 1) {
Shape s = (Shape) shapes.get(ii-1);
dArea = dArea + s.area();
}
walks along the arraylist and computes total area of the shapes. Construct a simple table to
show the value of dArea as a function of variable ii.
7
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 2: Schematic for US states and their membership in geographical regions.
As a starting point, the right-hand side of Figure 2 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 2 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;
8
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);
}
}
9
// 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;
10
// 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 )
11
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 );
12
// [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
13
[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.
14
[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.
15
[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.
16
[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;
}
17
Question 3: 20 points
This question covers use of the Java collections framework and software design patterns for the
implementation of a two-dimensional plotting library.
To see the kinds of features that such a library might support, consider the two-dimensional plot
of voltage measurements (Volts) versus time (sec) shown in Figure 3.
Figure 3: A simple matlab-like plot.
The two dimensinal plot contains multiple curves, a title, and labels for the x- and y- axes, labels for
individual curves, and a dashed-line grid. Standard optional features would include the ability to
draw curves in specific colors (e.g., blue, green, red) and a legend of symbols displayed in the figure
(not shown in Figure 3). An advanced implementation would allow for multiple plots, automatically
relabel the plot when the figure is resized and/or provide users with the ability to zoom in on a
small portion of a figure.
18
[3a] (10 pts.) Draw and label a diagram (or series of diagrams) that shows how you would use the
composite design pattern to layout the contents of Figure 3. Clearly indicate the various
types of leaf node that will be supported in your plotting package.
19
[3b] (10 pts.) Develop a few ideas for how HashSets and HashMaps might be used in your plotting
application. All reasonable answers will be accepted.
20
Download