Question

● Write code to read the content of the text file input.txt using JAVA. For each...

● Write code to read the content of the text file input.txt using JAVA. For each line in input.txt, write a new line in the new text file output.txt that computes the answer to some operation on a list of numbers. ● If the input.txt has the following: Min: 1,2,3,5,6 Max: 1,2,3,5,6 Avg: 1,2,3,5,6 Your program should generate output.txt as follows: The min of [1, 2, 3, 5, 6] is 1. The max of [1, 2, 3, 5, 6] is 6. The avg of [1, 2, 3, 5, 6] is 3.4. ● Assume that the only operations given in the input file are min, max and avg, and that the operation is always followed by a list of comma separated integers. You should define the functions min, max and avg that take in a list of integers and return the min, max or avg of the list. ● Your program should handle any combination of operations and any length of input numbers. You can assume that the list of input numbers are always valid integers and that the list is never empty. ● Compile, save and run your file

Homework Answers

Answer #1

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

public class Test {

public static void main(String[] args) throws IOException {

FileInputStream fi = new FileInputStream("input.txt");
FileOutputStream fo = new FileOutputStream("output.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fi));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fo));

String strLine;
while ((strLine = br.readLine()) != null) {

String[] arr = strLine.split(" ");
String[] nos = arr[1].split(",");

Set<Integer> set = new HashSet<Integer>();
for(int i = 0; i<nos.length; i++){
int no = Integer.parseInt(nos[i]);
set.add(no);
}
TreeSet<Integer> sortedSet = new TreeSet<Integer>(set);

switch(arr[0]) {

case "Min:":
String msg1="The Min of [" +arr[1]+ "] is " +(Integer)sortedSet.first();
bw.write(msg1);
bw.newLine();

break;

case "Max:":
String msg2="The Max of [" +arr[1]+ "] is " +(Integer)sortedSet.last();
bw.write(msg2);
bw.newLine();
break;

case "Avg:":
Object[] noarray = sortedSet.toArray();
int noarraysize = noarray.length-1;
int sum=0;
for(int i=0;i<=noarraysize;i++) {

int no=Integer.valueOf(noarray[i].toString());
sum = sum + no;
if(i==noarraysize) {
String msg3="The Avg of [" +arr[1]+ "] is " +(double)sum/noarray.length;
bw.write(msg3);
bw.newLine();
}
}
break;

case "Sum:":
Object[] noarray1 = sortedSet.toArray();
int noarraysize1 = noarray1.length-1;
int sum1=0;
for(int i=0;i<=noarraysize1;i++) {
int no=Integer.valueOf(noarray1[i].toString());
sum1 = sum1 + no;
if(i==noarraysize1) {
String msg4="The Sum of [" +arr[1]+ "] is " +sum1;
bw.write(msg4);
bw.newLine();
}
}
break;

}

}
br.close();
bw.close();

}
}

/* input.txt */

/*output.txt*/

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program for: In a text file INPUT.TXT integers separated by a space, perhaps in...
Write a program for: In a text file INPUT.TXT integers separated by a space, perhaps in a few lines. In a single file view to create a list of these numbers and find the arithmetic mean of the list elements. The resulting value is recorded in a text file OUTPUT.TXT.
Write a program that reads a file named input.txt and writes a file that contains the...
Write a program that reads a file named input.txt and writes a file that contains the same contents, but is named output.txt. The input file will contain more than one line when I test this. Do not use a path name when opening these files. This means the files should be located in the top level folder of the project. Do not use a copy method that is supplied by Java. Your program must read the file line by line...
Write a Java program that reads words from a text file and displays all the words...
Write a Java program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. 1. You must use one of following Concrete class to store data from the input.txt file. Vector, Stack, ArrayList, LinkedList 2. To sort those words, you should use one of existing interface methods available in Collection or List class.
I need python code for this. Write a program that inputs a text file. The program...
I need python code for this. Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. Uppercase words should take precedence over lowercase words. For example, 'Z' comes before 'a'. The input file can contain one or more sentences, or be a multiline list of words. An example input file is shown below: example.txt the quick brown fox jumps over the lazy dog An example of the program's output...
1) Write a program that stores the first 50 integers to a text file named numbers.txt....
1) Write a program that stores the first 50 integers to a text file named numbers.txt. Each number should appear on a line all by itself.
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
Project File Processing. Write a program that will read in from input file one line at...
Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespaces,...
In Python: Problem 4. Write a program [call it minmax.py] that accepts three integers in the...
In Python: Problem 4. Write a program [call it minmax.py] that accepts three integers in the range [0-100] as input from the user. Your program should then determine and print the smallest and largest integers in the values entered by the user using comparison operators *not* using predefined min or max Python functions. For instance: If the user input is: 36 5 99 your program should give as output: min = 5 max = 99 If the user input is:...
JAVA Write a program that will search a text file of strings representing numbers of type...
JAVA Write a program that will search a text file of strings representing numbers of type int and will write the largest and the smallest numbers to the screen. Include appropriate error handling in case a line contains more than one int or it contains a String. Wrap all the work you are doing with the file in a try/catch/finally block with appropriate "catch" sections; the finally block will be where you close your file object (if desired, look this...
You are required to write a program in JAVA based on the problem description given. Read...
You are required to write a program in JAVA based on the problem description given. Read the problem description and write a complete program with necessary useful comment for good documentation. Compile and execute the program. ASSIGNMENT OBJECTIVES: • To introduce queue data structure. DESCRIPTIONS OF PROBLEM: Exercise : Write a program to reverse element of a stack. For any given word (from input), insert every character (from the word) into a stack. The output from the stack should be...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT