Question

In: Computer Science

Write a Java program to read in the 10 numbers in the example file Book1.csv provided...

Write a Java program to read in the 10 numbers in the example file Book1.csv provided above. The program should sum all the numbers, find the lowest number, find the highest number, and computer the average. Upon completion of the processing, the program should write a new text file named stats.txt with the information found in the following format where xxx represents a number calculated above.

The sum of the numbers is: xxx
The lowest number is: xxx
The highest number is : xxx
The average of the numbers is : xxx

Using the class Poem below. Write a complete Java program that creates three different objects of type Poem. The program shall then open a text file named poems.txt for writing and write the information about each poem to the text file. The program shall NOT write the toString() version of the object to the file, but write first the poem name on a line and then the poet name on a second line for each poem.

/**
 * Poem.java
 *
 * A class representing information about a poem for use in Chapter 5 Exercise 2
 *
 */
public class Poem
{

        private String name;
        private String poet;

        /**
         * no-arg constructor
         */
        public Poem()
        {
                // initialize attributes
                name = "unknown";
                poet = "unknown";
        }

        /**
         * @return the name
         */
        public String getName()
        {
                return name;
        }

        /**
         * @param name the name to set
         */
        public void setName(String name)
        {
                this.name = name;
        }

        /**
         * @return the poet
         */
        public String getPoet()
        {
                return poet;
        }

        /**
         * @param poet the poet to set
         */
        public void setPoet(String poet)
        {
                this.poet = poet;
        }

        @Override
        public String toString()
        {
                return "Poem [name=" + name + ", poet=" + poet + "]";
        }

}

===Exercise 3 Using the Poem class given in exercise 2, write a Java program to read from a text file named poem2.txt provided before. The program shall read the name and poet of each poem, create an object of type Poem for each name/poet pair and print all the read poem infor to the console.

We Real Cool
Gwendolyn Brooks
I Know Why the Caged Bird Sings
Maya Angelou
Hope is the Thing with Feathers
Emily Dickinson
The Road Not Taken
Robert Frost

Solutions

Expert Solution

import java.io.*;
import java.util.*;
public class Program
{
public static void main(String[] args) throws Exception
{
   File file = new File("E:/Book1.csv"); //open the file
   Scanner sc = new Scanner(file); //read the file
   String s[]=new String[2]; //to store file text line by line
   int min=9999,max=0,sum=0;
while (sc.hasNextLine()) //if there is a line
   {
s=sc.nextLine().split(","); //get the line pf text and split it with the delimiter comma
       sum=sum+Integer.parseInt(s[1]); //calculating sum of all numbers
       if(Integer.parseInt(s[1])<min)
           min=Integer.parseInt(s[1]); //getting minimum value
       if(Integer.parseInt(s[1])>max)
           max=Integer.parseInt(s[1]); //getting maximum value
   }
   int avg=(sum+min+max)/3; //average of sum, min and max
   FileWriter fw=new FileWriter("E:/stats.txt"); //crreating stats.txt file to write the output to it
fw.write("The sum of the numbers is: "+sum+"\n"); //writing the sumt to stats.txt
   fw.write("The lowest number is: "+min+"\n"); //writing the minimum value to stats.txt
   fw.write("The highest number is: "+max+"\n"); //writing the maximum value to stats.txt
   fw.write("The average of the numbers is: "+avg+"\n"); //writing the average to stats.txt
   fw.close();//close the filewrite object
}
}

---------------------------------------------------------

Book1.csv

------------------------------------

maths,100
science,200
geography,90
robotics,340

OUTPUT:

stats.txt

The sum of the numbers is: 730
The lowest number is: 90
The highest number is: 340
The average of the numbers is: 386



Related Solutions

Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Java question- Write a java program to process the number.txt file. Then count the numbers and...
Java question- Write a java program to process the number.txt file. Then count the numbers and calculate the total average, even numbers average, odd number average, then print the corresponding information. The result should be displayed in the following format there are XX numebers in the file there are xx even numbers there are xx odd numbers the total number average is xx the odd number average is xx the even number average is xx I am having trouble using...
build a python program that will be performing: - Read a CSV file 'annual.csv' enterprise into...
build a python program that will be performing: - Read a CSV file 'annual.csv' enterprise into a data structure - Count the number of rows and columns - Determine if the data contains empty values - Replace the empty values by 'NA' for strings, '0' for decimals and '0.0' for floats - Transform all Upper case characters to Lower case characters - Transform all Lower case characters to Upper case characters - save back the 'repaired' array as csv -...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: 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...
Write a program that processes numbers, corresponding to student records read in from a file, and...
Write a program that processes numbers, corresponding to student records read in from a file, and writes the required results to an output file (see main ( )). Your program should define the following functions: double read_double (FILE *infile) — Reads one double precision number from the input file. Note: You may assume that the file only contains real numbers. int read_integer (FILE *infile) - Reads one integer number from the input file. double calculate_sum (double number1, double number2, double...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the user for two words b. Print out how many words in the file fall between those words c. If one of the two words is not contained in the file, print out which word is not found in the file d. If both words are not found in the file, print out a message e. Sample output: Please type in two words: hello computer...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional int array. Your program must (1) read the name of the file from the command-line arguments, (2) open the file, (3) check that each line has the same number of characters and (4) check that the only characters in a line are ones and zeros. If there is no such command-line argument or no such file, or if any of the checks fail, your...
Java Write a program that will only accept input from a file provided as the first...
Java Write a program that will only accept input from a file provided as the first command line argument. If no file is given or the file cannot be opened, simply print “Error opening file!” and stop executing. A valid input file should contain two lines. The first line is any valid string, and the second line should contain a single integer. The program should then print the single character from the string provided as the first line of input...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT