I\'m fairly confused with input and output in java, and I can\'t seem to figure
ID: 3760960 • Letter: I
Question
I'm fairly confused with input and output in java, and I can't seem to figure this program out, any help would be greatly appreciated!
Make a new project and class in Eclipse called R15, with a main method. Set up the Run Configuration so that R15 has the program arguments:
Phase 1
Create two methods with the following signatures:
public static void readFile( String inputFile ){ }
public static void writeFile( String outputFile ){ }
Within the main method, call the readFile method with the first command line argument (args[0]), then call the writeFile method with the second command line argument (args[1]).
Phase 2
Implement the writeFile method to open the output file, prompt the user with "Enter text:", then read input from the user via the keyboard, print the input to the file, and close the output file. When the user types stop (uppercase or lowercase) by itself on its own line, the method should terminate.
Given this input from the console:
Enter text:
Hello! Here are some words.
stop can be written here, but it still makes it in.
so will this stop.
stop!
stop
The output file should exactly match the following:
Hello! Here are some words.
stop can be written here, but it still makes it in.
so will this stop.
stop!
Phase 3
Create methods with the following signatures:
public static double computeAreaCircle( double radius ){ }
public static double computeAreaRectangle( double height, double width ){ }
public static double computeAreaTriangle( double height, double base ){ }
Implement all of the methods shown above. When in need of pi, it is acceptable to use Math.PI
Test each method using your own data values.
Phase 4
Now design a file format that specifies a keyword for a shape on each line, followed by the value(s) needed for that shape. The file format must allow a user to define any number of the following shapes in any order:
The name of the file is input.txt and it is placed inside your R15 project directory.
Circle
- User specifies radius
Rectangle
- User specifies height and width
Triangle
- User specifies height and base
Implement the readFile method such that it can read and parse the file, discarding the keywords and calling all appropriate methods for that shape. The keywords in the input file are chosen by you. Next print the results of each computation to the console, including the shape name, method name, input values, and calculated value. Make a data file that has several shapes, with the types intermixed, for testing.
Thank you again for any help, I just can't seem to figure it out and no one is around to help me out in person right now!
input.txt output.txt
This is the same as reading command line arguments from the terminal.
Here's some code that provides you some examples. It's not meant to be run as a program. You can add this code to your R15 somewhere and comment it out so you have it as a reference:
Explanation / Answer
Phase 4:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class R15
{
public static void main(String[] args)
{
readFile(args[0]);
writeFile(args[1]);
}
public static void readFile(String inputFile)
{
double area, radius, width, height, base;
try
{
Scanner reader = new Scanner(new File(inputFile));
while (reader.hasNext())
{
String shape = reader.next().toUpperCase();
switch (shape)
{
case "CIRCLE":
radius = reader.nextDouble();
area = computeAreaCircle(radius);
System.out.printf("Circle: radius = %.2f, area = %.2f ", radius, area);
break;
case "RECTANGLE":
width = reader.nextDouble();
height = reader.nextDouble();
area = computeAreaRectangle(width, height);
System.out.printf("Rectangle: width = %.2f, height = %.2fm area = %.2f ", width, height, area);
break;
case "TRIANGLE":
height = reader.nextDouble();
base = reader.nextDouble();
area = computeAreaTriangle(height, base);
System.out.printf("Triangle: base = %.2f, height = %.2f, area = %.2f ", base, height, area);
break;
}
}
reader.close();
}
catch (FileNotFoundException e)
{
System.out.println("Cannot read " + inputFile);
System.exit(0);
}
}
public static void writeFile(String outputFile)
{
try
{
PrintWriter writer = new PrintWriter(new File(outputFile));
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter text:");
while (true)
{
String line = keyboard.nextLine();
if (line.equalsIgnoreCase("stop"))
break;
writer.println(line);
}
writer.close();
}
catch (FileNotFoundException e)
{
System.out.println("ERROR!");
System.exit(0);
}
}
public static double computeAreaCircle(double radius)
{
return Math.PI * radius * radius;
}
public static double computeAreaRectangle(double height, double width)
{
return width * height;
}
public static double computeAreaTriangle(double height, double base)
{
return (height * base) / 2.0;
}
}
Phase 2:
static void writeFile(String outputFile) throws FileNotFoundException
{
Scanner keyboard = new Scanner(System.in);
PrintWriter pwOut = new PrintWriter(outputFile);
System.out.println("Enter Text:");
while (true)
{
String userinput = keyboard.nextLine();
if (!userinput.toLowerCase().equals("stop"))
{
pwOut.print(userinput);
}
else
{
break;
}
}
keyboard.close();
pwOut.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.