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

Goal: to write a Python program that will read a playlist from a CSV file and...
Goal: to write a Python program that will read a playlist from a CSV file and display it in the console in the form of a table. https://s3.eu-west-2.amazonaws.com/bb-python-modules/Coursework/CW3/playlist_text_question.html The following is a link to the question. It includes all instruction and a template file (with additional instructions) where the answer must be completed.
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 -...
Write a Java program to read a set of integers from a file, dataX, and a...
Write a Java program to read a set of integers from a file, dataX, and a set of ranges from a second file, rangeX, and, for each range [a, b] in rangeX, report the SUM of all the integers in dataX which are in the range [a, b]. As the integers are read from file dataX, insert them in a binary search tree. After all the integers have been inserted into the binary search tree, read the ranges from file...
Write a java program that will read a file called stateinfo.txt and will store the information...
Write a java program that will read a file called stateinfo.txt and will store the information of the file into a map. The stateinfo.txt file contains the names of some states and their capitals. The format of stateinfo.txt file is as follows. State                Capital ---------                         ----------- NSW               Sydney VIC                 Melbourne WA                 Perth TAS                 Tasmania QLD                Brisbane SA                   Adelaide The program then prompts the user to enter the name of a state. Upon receiving the user input, the program should...
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...
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 to read in a list of domain names from the provided file (domains.dat),...
Write a program to read in a list of domain names from the provided file (domains.dat), and print the reverse domain names in sorted order. For example, the reverse domain of cs.princeton.edu is edu.princeton.cs. This computation is useful for web log analysis. To do so, create a data type Domain, using reverse domain name order. The H file contains the required functions. The compareTo function is provided for you. This will allow you to compare 2 Domain objects. Also use...
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java 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 output file should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT