Department of Civil and Environmental Engineering, Spring Semester, 2012

advertisement
Department of Civil and Environmental Engineering,
Spring Semester, 2012
ENCE 688R: Midterm Exam: 1 1/2 Hours, Open Book and Open Notes
Name :
Question
Points
1
30
2
50
Total
80
1
Score
Question 1: 30 points
This question is about Java’s mechanisms for “programming by extension.” We begin with the
class Circle, which defines circles by their radius and (x,y) co-ordinates of the center, and then
develop a second class, ColoredCircle, for Colored cirles. The details of class Circle are as follows:
source code
/*
* =================================================================
* Circle(): This class defines circles.
* =================================================================
*/
import java.lang.Math.*;
public class Circle {
double dX, dY, dRadius;
// Constructor
public Circle() {}
public Circle( double dX, double dY, double dRadius ) {
this.dX = dX;
this.dY = dY;
this.dRadius = dRadius;
}
// Compute the circle area ....
public double Area() {
return Math.PI*dRadius*dRadius;
}
// Copy circle parameters to a string format ...
public String toString() {
return "(x,y) = (" + dX + "," + dY + "): Radius = " + dRadius;
}
// Exercise methods in class Circle ...
public static void main( String [] args ) {
System.out.println("Exercise methods in class Circle");
System.out.println("================================");
Circle cA = new Circle( 1.0, 2.0, 3.0 );
System.out.println("Circle cA : " + cA.toString() );
System.out.println("Circle cA : Area = " + cA.Area() );
}
}
2
And the details of class ColoredCircle are as follows:
source code
/*
* =================================================================
* coloredCircle(): This class defines colored circles.
* =================================================================
*/
import java.awt.Color;
public class ColoredCircle extends Circle {
private Color color;
// Constructor methods
public ColoredCircle() {
super();
this.color = Color.red;
}
public ColoredCircle( double dX, double dY,
double dRadius, Color color ) {
super();
this.dX = dX;
this.dY = dY;
this.dRadius = dRadius;
this.color
= color;
}
// Retrieve colors ....
public String getColors() {
return "Color (r,g,b) = (" + color.getRed() +
"," + color.getGreen() +
"," + color.getBlue() + ")";
}
// ===============================================
// Exercise methods in class ColoredCircle()......
// ===============================================
public static void main( String [] args ) {
System.out.println("Exercise methods in class ColoredCircle");
System.out.println("=======================================");
// Create, initialize, and print circle "cA" ...
ColoredCircle cA = new ColoredCircle( 1.0, 2.0, 3.0, Color.blue );
cA.dX = 1.0;
3
cA.dY = 2.0;
cA.dRadius = 3.0;
cA.color
= Color.blue;
System.out.println( "Circle cA:" + cA.toString() );
System.out.println( cA.getColors() );
// Create, initialize, and print circle "cB" ...
ColoredCircle cB = new ColoredCircle( 1.0, 2.0, 3.0, Color.orange );
System.out.println( "Circle cB:" + cB.toString() );
System.out.println( cB.getColors() );
}
}
The program input/output generated by main() in Circle is:
prompt >> java Circle
Exercise methods in class Circle
================================
Circle cA : (x,y) = (1.0,2.0): Radius = 3.0
Circle cA : Area = 28.274333882308138
prompt >>
And the program output generated by main() in ColoredCircle is:
prompt >> java ColoredCircle
Exercise methods in class ColoredCircle
=======================================
(x,y) = (1.0,2.0): Radius = 3.0
Color (r,g,b) = (0,0,255)
(x,y) = (1.0,2.0): Radius = 3.0
Color (r,g,b) = (255,200,0)
prompt >>
Please examine the source code carefully and answer the following questions.
[1a] (2 pts). Draw and label a schematic showing the relationship between the classes Circle and
ColoredCircle.
4
[1b] (3 pts). List the variables and methods contained within the class Circle.
[1c] (2 pts). What is the purpose of the line
import java.lang.Math.*;
in Circle.java? And why is it needed by this program?
[1d] (3 pts). Suppose that the line:
Circle cA = new Circle( 1.0, 2.0, 3.0 );
in method main() of Circle is replaced by:
Circle cA = new Circle( );
How would you modify the source code so that the overall functionality of main() remains
unchanged?
5
[1e] (3 pts). How would you modify Circle.java so that the circle area is printed to three
decimal places of accuracy (i.e., Circle cA: Area = 28.274).
[1f ] (3 pts). List the variables and methods defined in the source code for ColoredCircle
[1g] (3 pts). List the variables and methods available to ColoredCircle via inheritance mechanisms from Circle
6
[1h] (3 pts). What is the purpose of each term in:
public class ColoredCircle extends Circle {
[1i] (2 pts). What is the purpose of the method call super() in
public ColoredCircle() {
super();
this.color = Color.red;
}
Suppose that Circle.java and ColoredCircle.java are located in the same directory (with no
other files present).
[1j] (3 pts). List the files that will exist in the directory before and after the compilation command:
prompt >> javac Circle.java
[1k] (3 pts). Now suppose that all of the bytecode files in part 1j are deleted. List the files that
will exist in the directory before and after the compilation command:
prompt >> javac ColoredCircle.java
7
Question 2: 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
/*
8
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.
9
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.
============================================================
10
*/
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.
=======================================================================
11
*/
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);
12
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]
[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
===========================
13
[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:
[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 Feature, AbstractFeature, Location, Tree and TestScene classes.
[2c] (5 pts). Draw and label a diagram showing the layout of memory generated by the statements
in Part 01 of TestScene.java.
15
[2d] (5 pts). Draw and label a diagram showing the layout of memory after the statements in
Part 02 have finished.
[2e] (5 pts). What is the purpose of the syntax <Feature> in the statement:
ArrayList<Feature> array = new ArrayList<Feature>();
16
[2f ] (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 );
[2g] (5 pts). In what important way do ArrayLists differ from HashSets?
[2h] (5 pts). Explain the purpose of each part in the statement:
Map<String, Feature> map = new HashMap<String, Feature>();
17
[2i] (5 pts). Draw and label a diagram showing the layout of memory generated by the statements
in Part 04 of TestScene.java.
[2j] (5 pts). In Part 05 of TestScene.java, what is the primary purpose of the HashMap to TreeMap
conversion?
18
Download