Your task is to read a file containing arithmetic instructions such as Each inst
ID: 3683970 • Letter: Y
Question
Your task is to read a file containing arithmetic instructions such as
Each instruction contains an integer, an operator (+, -, or *), and another integer.
Return an array list of the results. If there is any error, throw an IOException.
Complete the following file:
Arithmetic.java
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Arithmetic
{
/**
This class reads a file containing arithmetic expressions and returns an
array list of the results.
@param filename the file name
@return a list of results
*/
public static ArrayList<Integer> read(String filename) throws IOException
{
. . .
}
// This method checks your work. Do not touch it.
public static String check(String filename)
{
try
{
return read(filename).toString();
}
catch (IOException ex)
{
return "I/O exception thrown";
}
catch (Exception ex)
{
return ex.getClass().getName();
}
}
}
(P.S. where the "..." is where I have to add new code can't change anything else"
Explanation / Answer
Hi ,
Kindly find below program as well text file.
arithmetic_instructions.txt
=================
3 + 4
4 - 10
7 * 11
55 - 10
=====================
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class Arithmetic {
/**
* This class reads a file containing arithmetic expressions and returns an
* array list of the results.
*
* @param filename
* the file name
* @return a list of results
*/
public static ArrayList<Double> read(String filename) throws IOException {
ArrayList<Double> result = new ArrayList<Double>();
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(filename));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
result.add((Double) evaluate(sCurrentLine));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
public static Double evaluate(String expression){
String[] parts =expression.split("(?=[\*|\+|-])|(?<=[\*|\+|-])"); //Regex to find operator
Double result = Double.parseDouble(parts[0]);
for (int i = 1; i < parts.length; i += 2) {
String op = parts[i];
double val = Double.parseDouble(parts[i+1]);
switch (op) {
case "+" :
result += val;
break;
case "-" :
result -= val;
break;
case "*" :
result *= val;
break;
}
}
return result;
}
// This method checks your work. Do not touch it.
public static String check(String filename) {
try {
return read(filename).toString();
} catch (IOException ex) {
return "I/O exception thrown";
} catch (Exception ex) {
return ex.getClass().getName();
}
}
public static void main(String args[]) {
System.out.println(check("arithmetic_instructions.txt"));
}
}
Let me know if you still have any questions.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.