Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!

Jim Harvey

Greenhorn
+ Follow
since Jun 08, 2007
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jim Harvey

All one has to do is follow directions.....

* Description: This is a GUI program to convert all letters in a string to
*uppercase letters.
*
*/
public class
{
/**
* This will create the UppercaseConverter dialog
* @param args
*/
public static void main( String[] args )
{
new UppercaseConverter();
}

}
15 years ago
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( "" );

}

}
15 years ago
Here's what I came up with.
*/
public class
{

/**
* @param args
*/
public static void main( String[] args )
{
Scanner input = new Scanner( System.in ); // Used for getting user input

System.out.println( "Fractions should be entered in the following format: a/b " );
System.out.println( "Where 'a' is the numerator, and 'b' is the denominator" );
System.out.println( "" );

// This indicates whether the user should be prompted to enter another operation
boolean askAgain = true;

while( askAgain ) {

try {

// Prompt for the first fraction and create it
System.out.print( "Please enter the first fraction in the format 'a/b': " );
String strFraction1 = input.nextLine();
Fraction fraction1 = new Fraction( strFraction1 );

// Prompt for the second fraction and create it
System.out.print( "\nPlease enter the second fraction in the format 'a/b': " );
String strFraction2 = input.nextLine();
Fraction fraction2 = new Fraction( strFraction2 );

// Prompt for the operation
System.out.println( "Please select one of the following operations: " );
System.out.println( "a. Addition" );
System.out.println( "b. Subtraction" );
System.out.println( "c. Multiplication" );
System.out.println( "d. Division" );
String operation = input.nextLine().trim();

Fraction result = null; // The resulting fraction
String operator = null; // The operator symbol representing this operation
if( operation.equalsIgnoreCase( "a" ) || operation.equalsIgnoreCase( "Addition" )) {
result = fraction1.add( fraction2 );
operator = " + ";
} else if( operation.equalsIgnoreCase( "b" ) || operation.equalsIgnoreCase( "Subtraction" )) {
result = fraction1.subtract( fraction2 );
operator = " - ";
} else if( operation.equalsIgnoreCase( "c" ) || operation.equalsIgnoreCase( "Multiplication" )) {
result = fraction1.multiply( fraction2 );
operator = " * ";
} else if( operation.equalsIgnoreCase( "d" ) || operation.equalsIgnoreCase( "Division" )) {
result = fraction1.divide( fraction2 );
operator = " / ";
}

System.out.println( "\nThe following is the result: " );
System.out.println( fraction1 + operator + fraction2 + " = " + result );
System.out.print( "\nWould you like to perform another operation? (Y/N): " );
String strAskAgain = input.nextLine();
if( strAskAgain.equalsIgnoreCase( "y" )) {
askAgain = true;
} else {
askAgain = false;
}
} catch( Exception e ) {
System.out.println( "\n" + e.getMessage() );
System.out.println( "Please try again" );
askAgain = true;
}

}
}

}
15 years ago
Okay you experts, I need need help with an assignment:

Rational fractions are of the form a/b, where a and b are integers and b !=0. By "fraction" we mean rational fractions. Suppose a/b and c/d are fractions. Arithmetic operations on fractions are defined by the following rules:

a/b + c/d = (ad + bc)/bd
a/b - c/d = (ad - bc)/bd
a/b * c/d = ac/bd
(a/b) / (c/d) = ad /bc

I have to design the class fraction that can be used to manipulate fractions in a program. The class fraction must include methods to add, subtract, multiply, and divide fractions. When you add, subtract, multiply, or divide fractions, your answer need not be in the lowest terms.

Write a Java console program using the class fraction that performs operations on fractions. Override the method toString so that the fraction can be output using the output statement.

Can someone please help me with this assignment or at least get me started in the right direction. Thanks in advance/jrh
15 years ago
Can someone pls show me or get me started in the right direction for writing a while loop to perforem the following steps:

1. Prompt the user to input two integers: dirstNum and secondNum.
2. Output all results to a file, placing an appropriate lable between each section of output.
3. Output all odd numbers between firstNum and secondNum.
4. Output the sum of all even numbers between firstNum and secondNum.
5. Output the numbers and their square between firstNum and secondNum.
6. Output the sun of the squares of the odd numbers between firstNum and secondnum.

Thank you very much/jrh
15 years ago
Can someone pls show me or get me started in the right direction for writing a while loop to perforem the following steps:

1. Prompt the user to input two integers: dirstNum and secondNum.
2. Output all results to a file, placing an appropriate lable between each section of output.
3. Output all odd numbers between firstNum and secondNum.
4. Output the sum of all even numbers between firstNum and secondNum.
5. Output the numbers and their square between firstNum and secondNum.
6. Output the sun of the squares of the odd numbers between firstNum and secondnum.

Thank you very much/jrh
15 years ago
Hi, Can someone pls assist me in writing this GUI program. I'm attempting to write a GUI program to convert all letters in a string to uppercase letters. For example harvey808 would be converted to HARVEY808.

class Uppercase {
public static void main(String[] args)
{
String s = "harvey808";
String s2 = s.toUpperCase();
System.out.println(s2);
}
}

Thank you very much.

Best,
jrh
[ June 08, 2007: Message edited by: Jim Harvey ]
15 years ago