Creating Methods for Input/Output Files
Basically, I had to find various properties of a cylinder and make separate methods to do each of these. This is no problem and that runs fine. But every time I try to make a method to do input and output I get several errors and it will not run.
So right now I just have the input and output files being read inside the main method , but i need separate methods for the input and output files.
I need help creating these methods for input/output.
Thank You
Here's the code I have:
Code Java:
import java.io.*;
import java.util.*;
import javax.swing.*;
public class Shapes {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) throws FileNotFoundException {
Scanner inFile = new Scanner (new FileReader("dimensions.txt"));
PrintWriter output = new PrintWriter("C:\\Users\\user\\Desktop\\shapes.txt");
double x = inFile.nextDouble();
double y = inFile.nextDouble();
double height, radius, base, lateral, surface, volume, outputs;
radius = x;
height = y;
base = baseArea(radius);
lateral = lateralArea(radius, height);
surface = surfaceArea(lateral, base);
volume = cylinderVolume(base, height);
// this is just to check the calculations without having to open the output evertime
System.out.println(radius);
System.out.println(height);
System.out.println(base);
System.out.println(lateral);
System.out.println(surface);
System.out.println(volume);
output.println(base + " " + lateral + " " + surface + " " + volume);
input.close();
output.close();
}
// calculates the base area. "a" is the radius and "b" is the value for base area.
public static double baseArea(double a){
double b = Math.PI * a * a;
return b;
}
public static double lateralArea(double r, double h){
double cir = Math.PI * 2 * r;
double lat = cir * h;
return lat;
}
public static double surfaceArea (double la, double ba){
double sur = la + ba + ba;
return sur;
}
public static double cylinderVolume (double base, double h){
double vol = base * h;
return vol;
}
}
Re: Creating Methods for Input/Output Files
What will be the content of dimensions.txt?
Re: Creating Methods for Input/Output Files
Oh I'm sorry. I just chose two values to test it:
4.5
2.5