Due: 9am, November 2, 2007.
This assignment will give you practice at writing Java programs where:
Click here to download the pdf file of questions.
A few hints. Most of what you need can be found on the "java examples" web page and in the green class reader:
/* * ================================================================== * FourNumbers.java: Prompt user for four floating point numbers, * print each number, and then the smallest and largest values. * ================================================================== */ import java.lang.Math; import java.io.*; public class FourNumbers { public static void main( String args[] ) { double dA, dB, dC, dD; String sLine; // Prompt user for numbers dA, dB, and dC. .... details removed .... System.out.println("Please enter dA, dB, dC and dD"); .... details removed .... // Print each of the numbers .... .... details removed .... // Ccmpute and print largest/smallest values.... .... details removed .... } /* * ============================================================ * Method getTextFromConsole(): Read line of text from keyboard * * Input : None. * Output : String sLine -- character string of keyboard input * ============================================================ */ public static String getTextFromConsole() { String inLine = ""; ... get details from java examples web page .... return inLine; } }
The program source code will be contained in a file called FourNumbers.java. Source code for the FourNumbers class will contained two user-defined methods: main() and getTextFromConsole().
Option 1. You can test for an error before it happens (i.e., x = 0 and x = 2). Unfortunately, the increment of 0.2 cannot be stored exactly inside a computer, so the looping construct will not walk along the sequence
-4.0 -3.8 -3.6 .... 9.8 10.0
exactly. So instead of writing something like:
if ( dX == 0 ) { ... print appropriate error message ... } if ( dX == 2 ) { ... print appropriate error message ... }
you should construct your test around a tolerance, e.g.,
if ( Math.abs(dX-2.0) < 0.000005 ) { ... print appropriate error message ... }
Option 2. Ignore the fact that errors in y(x) will occur, and simply adjust your output according to the following outcomes: (1) y(x) results in a number (no problem!), (2) y(x) causes an overflow (Infinity), (3) y(x) causes an underflow (-Infinity), and (4) y(x) evaluates to something that isn't a number (NaN). For a hint on a suitable coding strategy, see the Numerical Error Conditions code on the java examples page.
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 October 2007 by Mark Austin
Copyright © 2007,
Department of Civil and Environmental Engineering, University of Maryland