Due: 9am, May 12, 2008.
No Extensions .. I will hand out solutions on the last day of class.
This assignment will give you practice at writing Java programs where:
Solve problems 30, 33, 36 and 43 in the Java Programming Exercises.
A few hints. Most of what you need can be found on the "java examples" web page and in the blue class reader:
/* * ========================================================================== * Rectangle.java : A library of methods for creating and managing rectangles * * Definition of Rectangles * ------------------------ * Rectangles are defined by the (x,y) coordinates of corner points that * are diagonally opposite. * * Methods * ------------------------ * * double area() -- returns the area of a rectangle * double perimeter() -- returns the perimeter of a rectangle * * Written By : Mark Austin November 2005 * ========================================================================== */ import java.lang.Math; public class Rectangle { protected double dX1, dY1; // Coordinate (x,y) for corner 1.... protected double dX2, dY2; // Coordinate (x,y) for corner 2.... // Constructor methods .... public Rectangle() {} public Rectangle( double dX1, double dY1, double dX2, double dY2 ) { this.dX1 = dX1; this.dY1 = dY1; this.dX2 = dX2; this.dY2 = dY2; } // Convert rectangle details to a string ... public String toString() { return "Rectangle: Corner 1: (x,y) = " + "(" + dX1 + "," + dY1 + ")\n" + " Corner 2: (x,y) = " + "(" + dX2 + "," + dY2 + ")\n"; } // ==================================== // Compute rectangle area and perimeter // ==================================== public double area() { return Math.abs( (dX2-dX1)*(dY2-dY1) ); } public double perimeter() { return 2.0*Math.abs(dX2-dX1) + 2.0*Math.abs( dY2-dY1 ); } // Exercise methods in the Rectangle class .... public static void main ( String args[] ) { System.out.println("Rectangle test program "); System.out.println("==========================="); // Setup and print details of a small rectangle.... Rectangle rA = new Rectangle( 1.0, 1.0, 3.0, 4.0 ); System.out.println( rA.toString() ); // Print perimeter and area of the small rectangle.... System.out.println( "Perimeter = " + rA.perimeter() ); System.out.println( "Area = " + rA.area() ); } }
You can down load the source code for Vertex.java from the "java examples" web page. Then reorganize the rectangle code to take advantage of Vertex.java.
/* * ============================================================= * RainfallAnalysis.java: The java program conducts a simple * statistical analysis on monthly rainfall measurements. * * Written By: Mark Austin November 2005 * ============================================================= */ import java.lang.Math; public class RainfallAnalysis { // No of columns in print array task... public final static int NoColumns = 6; // ========================================================= // main method : this is where the program execution begins. // ========================================================= public static void main ( String [] args ) { // [a] Populate rainfall data array (measured over 30 days) float [] fRainfall = { 1.1F, 0.8F, 1.1F, 1.2F, 0.5F, 0.2F, 0.05F, 0.0F, 0.0F, 0.0F, 0.4F, 0.5F, 0.6F, 0.8F, 1.0F, 1.2F, 3.0F, 2.4F, 1.5F, 1.0F, 1.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 2.0F, 2.0F, 2.4F }; // [b] Print contents of fRainfall in six columns ... System.out.println("Array: fRainfall"); System.out.println("----------------"); for (int i = 1; i <= fRainfall.length; i = i + 1) { System.out.printf( " %4.2f", fRainfall [i-1] ); if (i % NoColumns == 0 || i == fRainfall.length ) System.out.println(""); } System.out.println("----------------"); // [c] Compute and print rainfall measurement statistics.. System.out.println(""); System.out.println("Statistics of Daily Rainfall"); System.out.println("============================"); System.out.printf("Max value = %5.2f\n", maxValue( fRainfall )); System.out.printf("Min value = %5.2f\n", minValue( fRainfall )); System.out.printf("Range of values = %5.2f\n", range( fRainfall )); System.out.printf("Mean value = %5.2f\n", mean( fRainfall )); System.out.printf("Standard deviation = %5.2f\n", std( fRainfall )); System.out.println("============================"); } /* ================================================== */ /* Methods to compute maximum/minimum daily rainfalls */ /* ================================================== */ public static float maxValue ( float fA [] ) { float fMax = fA[0]; for (int i = 0; i < fA.length; i = i + 1) fMax = Math.max( fMax, fA [i] ); return fMax; } public static float minValue ( float fA [] ) { float fMin = fA[0]; for (int i = 0; i < fA.length; i = i + 1) fMin = Math.min( fMin, fA [i] ); return fMin; } /* ========================================== */ /* Method to compute range in daily rainfalls */ /* ========================================== */ public static float range ( float fA [] ) { return ( maxValue (fA) - minValue(fA) ); } /* ====================================================== */ /* Compute mean and standard deviation of daily rainfalls */ /* ====================================================== */ public static float mean ( float fA [] ) { float fSum = 0.0F; for (int i = 0; i < fA.length; i = i + 1) fSum = fSum + fA[i]; return fSum/fA.length; } public static float std ( float fA [] ) { float fMean = mean ( fA ); float fSum = 0.0F; for (int i = 0; i < fA.length; i = i + 1) fSum = fSum + fA[i]*fA[i]; return (float) Math.sqrt(fSum/fA.length - fMean*fMean); } }
I suggest that you download and compile this source code. Then download the source code to DataArray.java, which can be found on the "java examples" web page.
Your task is to re-write RainfallAnalysis.java so that it takes advantage of objects/methods in DataArray.java. A suitable statement for creating the data array is:
DataArray rainfall = new DataArray("March Rainfall Measurements", 30 );
You should find that the new version of RainfallAnalysis.java is quite short. Moreover, the files before and after compilation should be:
Before After ================================================================= DataArray.java DataArray.java RainfallAnalysis.java RainfallAnalysis.java DataArray.class RainfallAnalysis.class =================================================================
Notice how code the main() method systematically exercises the operations for complex number arithmetic. Then, write a file GeometricSeries.java that prompts a user for the appropriate input and then computes the series summations by calling methods in Complex.java.
To help you get started, here is a skeleton of code for GeometricSeries.java . A sample script of program i/o is:
Script started on Mon May 5 08:36:43 2008 prompt >> prompt >> java GeometricSeries Welcome to the Geometriic Series Computer ----------------------------------------- Please enter complex number "a" Coefficent a : Real : 1.0 Coefficent a : Imaginary : 2.0 Please enter number of terms in series "a" No of terms in series : 3 The complex no you have entered is :s = 1.0+2.0i No of terms in series is = 3 Summation with basic for-loop ======================================= Summation 1 = -13.0+4.0i Summation using Horner's formula ======================================= Summation 2 = -13.0+4.0i Summation with geometric-series formula ======================================= Summation 3 = -13.0+4.0i prompt >> prompt >> java GeometricSeries Welcome to the Geometriic Series Computer ----------------------------------------- Please enter complex number "a" Coefficent a : Real : -1.0 Coefficent a : Imaginary : 3.0 Please enter number of terms in series "a" No of terms in series : 12 The complex no you have entered is :s = -1.0+3.0i No of terms in series is = 12 Summation with basic for-loop ======================================= Summation 1 = -788535.0-383985.0i Summation using Horner's formula ======================================= Summation 2 = -788535.0-383985.0i Summation with geometric-series formula ======================================= Summation 3 = -788535.0-383985.0i prompt >> prompt >> exit script done on Mon May 5 08:37:20 2008
Notice that because we are working with complex number arithmetic here, "1" needs to represeted as "1 + 0i"....
import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Footprint { String sName; List triangle = new ArrayList(); // ============================================== // Set name of the footprint ... // ============================================== public void setName( String sName ) { ... fill in details here ... } // ============================================== // Compute total area of footprint .... // ============================================== public double area() { double buildingArea = 0.0; // Walk along triangles and sum area .... .... fill in details here .... return buildingArea; } // ============================================== // Compute x coordinate of centroid .... // ============================================== public double getCentroidX() { double buildingArea = area(); double firstMomentX = 0.0; // Compute first moment of area about origin ... .... fill in details here .... return firstMomentX/buildingArea; } // ============================================== // Compute y coordinate of centroid .... // ============================================== public double getCentroidY() { .... fill in details here .... } // ============================================== // Create string description of the footprint ... // ============================================== public String toString() { String s = "Footprint: " + sName + "\n"; ... fill in details here .... return s; } // ======================================================= // Exercise methods in Footprint class ..... // ======================================================= public static void main( String args[] ) { // Create building footprint object .... Footprint avw = new Footprint(); avw.setName("A.V. Williams Bldg"); // Create triangles to cover the footprint ... Triangle a = new Triangle("a", 1.0, 1.0, 1.0, 6.0, 3.0, 1.0 ); .... fill in details here ... // Add triangles to footprint ... avw.triangle.add( a ); .... fill in details here ... // Print details of footprint, its area and centroid .... System.out.println ( avw.toString() ); System.out.printf ( "*** Footprint Area = %8.3f\n", avw.area() ); System.out.printf ( "*** Centroid: X = %8.3f\n", avw.getCentroidX() ); System.out.printf ( " : Y = %8.3f\n", avw.getCentroidY() ); } }
The pair of statements:
Footprint avw = new Footprint(); avw.setName("A.V. Williams Bldg");
and then sets the name to "AV Williams Bldg." You will need to fill in the details of setName() -- it's only one line of code.
Each footprint object contains an arraylist of Triangle objects. To save space, I use the fancy constructor in Triangle.java, namely:
Triangle a = new Triangle("a", 1.0, 1.0, 1.0, 6.0, 3.0, 1.0 );
Then to add the triangle to the array list, simply type:
avw.triangle.add( a );
This statement basically says "go to the footprint object referenced by avw, look inside and find the variable called triangle. In this case, triangle is a List. Then add the reference to triangle "a" to the arraylist.
A summary of my output is as follows:
Script started on Tue May 6 09:36:54 2008 prompt >> prompt >> java Footprint Footprint: A.V. Williams Bldg ================================= Triangle: "a" ================================= Edge("e1") connects nodes (n3, n1) Edge("e2") connects nodes (n1, n2) Edge("e3") connects nodes (n2, n3) Node("n3") is at (1.0,1.0) Node("n1") is at (1.0,6.0) Node("n2") is at (3.0,1.0) .... details of triangles b-f removed .... *** Footprint Area = 28.000 *** Centroid: X = 5.000 : Y = 3.929 prompt >> prompt >> exit Script done on Tue May 6 09:37:01 2008
Note. For each problem, hand in a copy of your program source code and a script of I/O for typical program usage. To create a script, type something like:
prompt >> script output-file
Now all input/output on the screen will be echoed to the file "output-file". To terminate the script, type:
prompt >> exit
Now print "output-file" and hand it in.
Developed in April/May 2008 by Mark Austin
Copyright © 2008,
Department of Civil and Environmental Engineering, University of Maryland