/* Craig Persiko
AvgWordLength.java
This program demonstrates how to use a type cast
to divide to integers and get a floating point result.
*/
import java.util.Scanner;
public class AvgWordLength
{
public static void main (String[] args)
{
Scanner scan = new Scanner (System.in);
int num_chars, num_words;
double avg_length;
System.out.print("How many characters are in the document? ");
num_chars = scan.nextInt();
System.out.print("and how many words are there? ");
num_words = scan.nextInt();
avg_length = (double)num_chars / num_words;
System.out.printf("The average word length is %.2f characters.\n", avg_length);
}
}
/* Sample Output:
How many characters are in the document? 100
and how many words are there? 28
The average word length is 3.57 characters.
// without the type cast, the average word length calculated would have been 3
*/
syntax highlighted by Code2HTML, v. 0.9