Department of Civil and Environmental Engineering, Spring Semester, 2013 ENCE 688R: Midterm Exam: 1 1/2 Hours, Open Book and Open Notes Name : Question Points 1 30 2 30 3 40 Total 100 1 Score Question 1: 30 points This question is about the principles of object-oriented program development and the basics of Java. [1a] (5 pts). Draw and label a diagram that shows the three types of relationships that can exist between classes. [1b] (5 pts). How would you extend the class hierarchy Figure 1. : Inheritance in modeling of a structure to represent a large building that contains a foot-bridge? (Just draw your solution on Figure 1). 2 Now let’s consider the fragment of code: 1 2 3 4 5 6 6 7 8 9 10 11 12 13 14 public static final int NoElements = 10; int [] iData = new int[ NoElements ]; int ii = 0; while ( ii < NoElements ) { iData [ ii ] = 2*ii; System.out.printf( "ii = %2d\n", ii ); if ( ii <= 7 ) { ii = ii + 2; } else { ii = ii + 3; } } [1c] (3 pts). What does code on line 1 do? [1d] (3 pts). What does code on line 2 do? [1e] (5 pts). Construct a table showing the “Loop No,” value of ii, ii < NoElements, the program output, ii <= 7, and ii = ii + 2 and/or ii = ii + 3 in the looping construct: 3 [1f ] (2 pts). What is the value of ii when the program execution reaches line 14 of the script? [1g] (3 pts). Draw and label a diagram showing the layout of memory for iData and its element values when the program reaches line 14. [1h] (2 pts). Briefly explain why Java is both a compiled language and an interpreted language? [1i] (2 pts). What is the purpose of the method main() in a Java application program? 4 Question 2: 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() ); } } 5 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; 6 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. [2a] (2 pts). Draw and label a schematic showing the relationship between the classes Circle and ColoredCircle. 7 [2b] (3 pts). List the variables and methods contained within the class Circle. [2c] (2 pts). What is the purpose of the line import java.lang.Math.*; in Circle.java? And why is it needed by this program? [2d] (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? 8 [2e] (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). [2f ] (3 pts). List the variables and methods defined in the source code for ColoredCircle [2g] (3 pts). List the variables and methods available to ColoredCircle via inheritance mechanisms from Circle 9 [2h] (3 pts). What is the purpose of each term in: public class ColoredCircle extends Circle { [2i] (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). [2j] (3 pts). List the files that will exist in the directory before and after the compilation command: prompt >> javac Circle.java [2k] (3 pts). Now suppose that all of the bytecode files in Part 2j are deleted. List the files that will exist in the directory before and after the compilation command: prompt >> javac ColoredCircle.java 10 Question 3: 40 points This question covers Java Programming with abstract classes and array lists. Absract class Shape Rectangle Circle Location Triangle Figure 2. Relationship among the classes Shape, Location, Rectangle, Circle and Triangle. Figure 2 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: 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. public class Location { double dX, dY; } Rectangle.java. The details of Rectangle.java are as follows: public class Rectangle extends Shape { double dSide1, dSide2; public Rectangle ( double dSide1, double dSide2, double dX, double dY ) { this.dSide1 = dSide1; 11 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. import java.util.ArrayList; import java.util.Iterator; 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; 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 ); 12 System.out.println("---------------------------------"); } } Please look at the source code carefully and answer the questions that follow: [3a] (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. [3b] (3 pts). What command would you give to run the Rectangle program? (Please do not write: I’d press the run button). [3c] (3 pts). What will the program output look like? 13 [3d] (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. [3e] (4 pts). What is the purpose of the method toString() in the statement? System.out.println( rA.toString() ); 14 Now let’s move onto Shape.java. [3f ] (5 pts). List two ways in which an abstract class (e.g., Shape.java) differs from a regular class (e.g., Circle.java). [3g] (5 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. 15 [3h] (5 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. [3i] (5 pts). Briefly indicated how you would modify the body of main() to take advantage of Java Generics. 16