Fred,
This is for you... let's see what you can do!
*/
public class Assignment1
{
/**
* @param args
*/
public static void main( String[] args )
{
// Create the scanner object used for input
Scanner input = new Scanner(System.in);
// Get the first integer
System.out.print("Please enter an integer: ");
int firstNum = input.nextInt(); // The first int
// Get the second integer. Ask for it again if necessary (if it is not
// greater than the first integer).
int secondNum = firstNum; // The second int
while( secondNum <= firstNum ) {
System.out
.print("Please enter another integer that is greater than the first: ");
secondNum = input.nextInt();
if( secondNum <= firstNum ) {
System.out.println(secondNum + " is not greater than " + firstNum
+ ". Please try again." );
}
}
// Open up the file for writing
PrintWriter writer = null; // Writer used to write to a file
try {
writer = new PrintWriter( new BufferedWriter( new FileWriter( "assignment1.txt" )));
// Output odd numbers
outputOddNumbers( writer, firstNum, secondNum );
// Output the sum of all even numbers
outputSumOfEvenNumbers( writer, firstNum, secondNum );
// Output the squares of all numbers
outputSquares( writer, firstNum, secondNum );
// Output the sum of squares
outputSumOfSquaresOfOddNumbers( writer, firstNum, secondNum );
} catch( IOException e ) {
System.out.println( "There was a problem with the file: " + e.getMessage() );
} finally {
if( writer != null ) {
writer.close();
}
}
}
/**
* This will output all the odd numbers between firstNum (inclusive) and secondNum
* (inclusive) to a file.
* @param writer The writer object to which we print the results.
* @param firstNum The first integer.
* @param secondNum The second integer.
*/
private static void outputOddNumbers( PrintWriter writer, int firstNum, int secondNum )
{
writer.println( "The following is a list of odd integers between " + firstNum +
" and " + secondNum + "." );
writer.println( "-----------------------------------------------------------------" );
int startNum; // This will be the first odd number
if( firstNum % 2 == 1 ) {
// firstNum is odd
startNum = firstNum;
} else {
// firstNum is even
startNum = firstNum + 1;
}
// Start at the first odd number, and add 2 each time in a loop to get the next
// odd number. Stop after we pass secondNum.
for( int i = startNum; i <= secondNum; i += 2 ) {
writer.println( i );
}
// Add a blank line
writer.println( "" );
}
/**
* This will output the sum of all the even numbers between firstNum (inclusive) and
* secondNum (inclusive) to a file.
* @param writer The writer object to which we print the results.
* @param firstNum The first integer.
* @param secondNum The second integer.
*/
private static void outputSumOfEvenNumbers( PrintWriter writer, int firstNum, int secondNum )
{
writer.println( "The following is the sum of all even integers between " + firstNum +
" and " + secondNum + "." );
writer.println( "-----------------------------------------------------------------" );
int startNum; // This will be the first even number
if( firstNum % 2 == 0 ) {
// firstNum is even
startNum = firstNum;
} else {
// firstNum is odd
startNum = firstNum + 1;
}
// Start at the first even number, and add 2 each time in a loop to get the next
// even number. Stop after we pass secondNum.
int sum = 0; // The sum of all even numbers
for( int i = startNum; i <= secondNum; i += 2 ) {
sum += i;
}
// Write the sum out
writer.println( sum );
// Add a blank line
writer.println( "" );
}
/**
* This will output the all the numbers, and their squares, between firstNum (inclusive) and
* secondNum (inclusive) to a file.
* @param writer The writer object to which we print the results.
* @param firstNum The first integer.
* @param secondNum The second integer.
*/
private static void outputSquares( PrintWriter writer, int firstNum, int secondNum )
{
writer.println( "The following is the squares of all numbers between " + firstNum +
" and " + secondNum + "." );
writer.println( "-----------------------------------------------------------------" );
// Output every number and their square.
for( int i = firstNum; i <= secondNum; i++ ) {
writer.println( i + "^2=" + ( i*i ));
}
// Add a blank line
writer.println( "" );
}
/**
* This will output the sum of all the squares of the odd numbers between firstNum
* (inclusive) and secondNum (inclusive) to a file.
* @param writer The writer object to which we print the results.
* @param firstNum The first integer.
* @param secondNum The second integer.
*/
private static void outputSumOfSquaresOfOddNumbers( PrintWriter writer, int firstNum, int secondNum )
{
writer.println( "The following is the sum of all the squares of odd integers between " +
firstNum + " and " + secondNum + "." );
writer.println( "-----------------------------------------------------------------" );
int startNum; // This will be the first odd number
if( firstNum % 2 == 1 ) {
// firstNum is odd
startNum = firstNum;
} else {
// firstNum is even
startNum = firstNum + 1;
}
// Start at the first odd number, and add 2 each time in a loop to get the next
// odd number. Stop after we pass secondNum.
int sum = 0; // This is the sum of squares
for( int i = startNum; i <= secondNum; i += 2 ) {
sum += i * i;
}
// Output the sum
writer.println( sum );
// Add a blank line
writer.println( "" );
}
}