Computer Science 202
Introduction to Programming
Fall 2012, The College of Saint Rose
CircleAreas BlueJ Project
Click here to download a BlueJ project for CircleAreas.
CircleAreas Source Code
The Java source code for CircleAreas is below. Click on a file name to download it.
/*
* Example CircleAreas: a program that reads in a series of numbers,
* ending in a negative or 0 value, and prints the results to a given file.
*
* Jim Teresco, The College of Saint Rose, CSC 202, Fall 2012
*
* $Id: CircleAreas.java 1962 2012-10-29 22:11:21Z terescoj $
*/
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class CircleAreas {
public static void main(String[] args) throws IOException {
// first, a Scanner to be used to read in a file name
// and the series of numbers from the keyboard.
Scanner keyboard = new Scanner(System.in);
// next, we read the file name
System.out.print("Enter the file name to use to store results: ");
String fileName = keyboard.next();
// now we are ready to open our file for writing
PrintWriter outFile = new PrintWriter(new File(fileName));
// next, we will loop until we get a 0
double radius;
do {
System.out.print("Enter the radius of the next circle: ");
radius = keyboard.nextDouble();
if (radius > 0.0) {
outFile.println(radius + " " + (Math.PI * radius * radius));
}
} while (radius > 0.0);
outFile.close();
}
}