In: Computer Science
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
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