Homework Assignment 3
Due: 9am, April 1, 2009.
This assignment will give you practice at writing Java programs where:
Solve problems 2, 3, and 5 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:
/*
* ==================================================================
* ThreeNumbers.java: Prompt user for three floating point numbers,
* print each number, and then the smallest and largest values.
* ==================================================================
*/
import java.lang.Math;
import java.io.*;
public class ThreeNumbers {
public static void main( String args[] ) {
double dA, dB, dC;
String sLine;
// Prompt user for numbers dA, dB, and dC.
.... details removed ....
System.out.println("Please enter dA, dB, and dC");
.... 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;
}
}
Notice that the source code for the method getTextFromConsole() needs to be "inside" the class ThreeNumbers.
import java.lang.Math;
public class FourFours {
// =============================================
// Compute and return iNo !
// =============================================
public static int factorial( int iNo ) {
// Make sure that the input argument is positive
if (iNo < 0) throw
new IllegalArgumentException("iNo must be >= 0");
// Use simple look to compute factorial....
int factorial = 1;
for(int i = 2; i <= iNo; i++)
factorial *= i;
return factorial;
}
// ==================================================
// Compute and print expressions involving four fours
// ==================================================
public static void main ( String args[] ) {
System.out.printf("\n");
System.out.printf("Expressions involving four fours \n");
System.out.printf("================================================\n");
System.out.printf("Four fours: 0 = %10d\n", 4/4 - 4/4 );
System.out.printf("Four fours: 1 = %10d\n", 44/44 );
// .... etc ..... etc ......
System.out.printf("================================================\n");
}
}
The output to my solution looks like:
Expressions involving four fours ================================================ Four fours: 0 = 0 Four fours: 1 = 1 Four fours: 2 = 2 Four fours: 3 = 3 Four fours: 4 = 4 Four fours: 5 = 5 Four fours: 6 = 6 Four fours: 7 = 7 Four fours: 8 = 8 Four fours: 9 = 9 Four fours: 10 = 10 Four fours: 11 = 11 Four fours: 12 = 12 Four fours: 13 = 13 Four fours: 14 = 14 Four fours: 15 = 15 Four fours: 16 = 16 Four fours: 17 = 17 Four fours: 18 = 18 Four fours: 19 = 19 ... and ... Four fours: 73 = 73 ================================================
Note. For each problem, hand in a copy of your program source code and a script of I/O for typical program usage.
If you are working on UNIX/Mac OS X then the procedure for creating a script is easy -- just 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.
If you are working on Windows, just cut-and-paste the output into a Word document.
Developed in March 2009 by Mark Austin
Copyright © 2009,
Department of Civil and Environmental Engineering, University of Maryland