Homework Assignment 4
Due: November 17, 2006.
No extensions. I will hand out solutions next Friday and you should
study them for midterm2.
This assignment will give you practice at:
Write Java programs to solve Problems 16, 19 and 20 in the Java Exercises Handout (see the yellow book).
Hints
Source Code Schematic for Problem 16
/*
* =====================================================================
* RainfallAnalysis.java: The java program conducts a simple statistical
* analysis on monthly rainfall measurements.
*
* Written By: Mark Austin
* Modified By: ........... November 2006
* =====================================================================
*/
import java.text.*;
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)
double [] fRainfall = { 1.1 ... fill in missing details ..... };
// [b] Print contents of fRainfall in six columns ...
System.out.println("Array: fRainfall");
System.out.println("----------------");
... fill in missing details ....
System.out.println("----------------");
// [b] 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 double maxValue ( double dA [] ) {
... fill in missing details ....
}
public static double minValue ( double dA [] ) {
... fill in missing details ....
}
/* ========================================== */
/* Method to compute range in daily rainfalls */
/* ========================================== */
public static double range ( double dA [] ) {
... fill in missing details ....
}
/* ====================================================== */
/* Compute mean and standard deviation of daily rainfalls */
/* ====================================================== */
public static double mean ( double dA [] ) {
... fill in missing details ....
}
public static double std ( double dA [] ) {
... fill in missing details ....
}
}
The output generated by my version of the program is:
prompt >> java RainfallAnalysis Array: fRainfall ---------------- 1.10 0.80 1.10 1.20 0.50 0.20 0.05 0.00 0.00 0.00 0.40 0.50 0.60 0.80 1.00 1.20 3.00 2.40 1.50 1.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 2.00 2.00 2.40 ---------------- Statistics of Daily Rainfall ============================ Max value = 3.00 Min value = 0.00 Range of values = 3.00 Mean value = 0.83 Standard deviation = 0.84 ============================ prompt >>
Source Code Schematic for Problem 19
/*
* ===========================================================================
* CubeRoot.java: This program prompts the user for a number and then computes
* and prints its cube root.
*
* Draft: Mark Austin
* Finished By: ........ November, 2006
* ===========================================================================
*/
import java.lang.Math;
import java.util.*;
import java.io.*;
import java.text.*;
public class CubeRoot {
public static void main( String args[] ) {
double dNumber;
String sLine;
// Print welcome message.
.... fill in details .....
// Prompt user for number...
.... fill in details .....
// Compute and print cube root...
System.out.printf("CubeRoot( %8.2f ) = %8.2f\n",
dNumber, cubeRoot (dNumber));
}
/*
* ==================================================================
* cubeRoot(): Compute cube root of a floating-point no.
*
* Input : double dNumber -- Number that cube root is to be computed.
* Output : double dX -- Cube root of dNumber.
* ==================================================================
*/
public static final double EPSILON = 0.00001;
public static double cubeRoot ( double dNumber ) {
boolean bConvergenceAchieved = false;
double dX, dXnext;
/* [a] : No iteration is required for trivial cases */
.... fill in details .....
/* [b] : Compute cube root */
dX = dNumber/2.0; // initial guess for cube root of dNumber ...
while( bConvergenceAchieved == false ) {
// Compute next estimate of cube root ...
.... fill in details .....
// Check for convergence of answer ...
.... fill in details .....
// Update estimate of cube root...
.... fill in details .....
}
return dX;
}
/*
* =============================================================
* Method getTextFromConsole() : Get line of input from keyboard
*
* Input : None.
* Output : String sLine -- character string of keyboard input
* =============================================================
*/
public static String getTextFromConsole() {
String inLine = "";
.... fill in details ....
}
Here is a sample output from my program:
Script started on Thu Nov 09 14:42:33 2006 prompt >> prompt >> java CubeRoo Compute Cube Root of Number --------------------------------------- Input Number: -81.00 *** Cube root estimate = -27.01646091 *** Cube root estimate = -18.04796585 *** Cube root estimate = -12.11486821 *** Cube root estimate = -8.26054007 *** Cube root estimate = -5.90270925 *** Cube root estimate = -4.71006683 *** Cube root estimate = -4.35709793 *** Cube root estimate = -4.32695962 *** Cube root estimate = -4.32674872 *** Cube root estimate = -4.32674871 CubeRoot( -81.00 ) = -4.33 prompt >> exit script done on Thu Nov 09 14:42:48 2006
I have put a print statement inside the loop so that you can see how the algorithm converges.
Source Code Schematic for Problem 20
/*
* ===========================================================================
* DartExperiment.java: This program simulates and experiment where darts are
* thrown at a board. The proportion of darts falling
* inside a circle relative to the total number of darts
* thrown provides an approximate estimate of pi.
*
* Draft: Mark Austin
* Finished By: ........ November, 2006
* ===========================================================================
*/
import java.lang.Math;
import java.util.*;
import java.io.*;
import java.text.*;
public class DartExperiment {
public static void main( String args[] ) {
double dX, dY;
int iNoPoints = 100000;
int iNoInside = 0;
// Print welcome message.
System.out.println("Estimating pi with Dart Experiment ");
System.out.println("---------------------------------------");
// Conduct numerical experiment ...
for ( int i = 1; i <= iNoPoints; i = i + 1 ) {
// Generate scaled coordinate point (dX, dY) ...
.... details of code removed ....
// Is point inside the circle? ....
.... details of code removed ....
}
// Compute and print estimate of pi ...
.... details of code removed ....
}
}
Here is the output from my program when the experiment is conducted with 100,000 data points.
Estimating pi with Dart Experiment --------------------------------------- (X,Y) = (-0.010970, -0.430134) (X,Y) = (0.817009, -0.484418) (X,Y) = (0.542297, -0.721900) ... 99,995 lines of output removed .... (X,Y) = (-0.030996, -0.524783) (X,Y) = (0.279210, -0.171811) Estimate of pi = 3.145920
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 November 2006 by Mark Austin
Copyright © 2006, Department of Civil and Environmental Engineering, University of Maryland