Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings


Homework answers / question archive / Modify theFileSum

Modify theFileSum

Computer Science

Modify theFileSum.java (code listing 5-21) and FileSumDemo.java (code listing 5-22) as follows

1. FileSum class: Add a private integer field called "count" to the FileSum class.

2. FileSum class: Inside the constructor initialize the "count" to zero. Best place would be after line 28.

3. FileSum class: Also inside the constructor, after accumulating the "sum" (line 38) you should increment the "count" by one.

4. FileSum class: Add a new method called getAverage() which should return a double value. This method should simply return the "sum" divided by the "count".

5. FileSumDemo class: Add a display statement for the average at the end of main() method of the FileSumDemo class. This should be very similar to display of the sum.

You can use the following starting files:

 

import java.util.Scanner;  // Needed for Scanner

import java.io.*;          // Needed for File and IOException

 

/**

 * This class reads a series of numbers from a file and

 * accumulates their sum. 

 */

 

public class FileSum

{

   private double sum;   // Accumulator

 

   /**

    * The constructor accepts a file name as its argument.

    * The file is opened, the numbers read from it, and   

    * their sum is stored in the sum field.

    */

 

   public FileSum(String filename) throws IOException

   {

      String str;  // To hold a line read from the file

 

      // Create the necessary objects for file input.

      File file = new File(filename);

      Scanner inputFile = new Scanner(file);

 

      // Initialize the accumulator.

      sum = 0.0;

 

      // Read all of the values from the file and

      // calculate their total.

      while (inputFile.hasNext())

      {

         // Read a value from the file.

         double number = inputFile.nextDouble();

        

         // Add the number to sum.

         sum = sum + number;

      }

 

      // Close the file.

      inputFile.close();

   }

 

   /**

    * The getSum method returns the value in the sum field.

    */

 

   public double getSum()

   {

       return sum;

   }

}

 

 

import java.io.*;  // Required for IOException

 

/**

 * This program demonstrates the FileSum class.

 */

 

public class FileSumDemo

{

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

   {

      // Create an instance of the FileSum class.

      FileSum fs = new FileSum("Numbers.txt");

     

      // Display the sum of the values in Numbers.txt.

      System.out.println("The sum of the numbers in " +

                         "Numbers.txt is " +

                         fs.getSum());

   }

}

 

 

8.7

7.9

3.0

9.2

12.6
 

QUESTION 2

Modify the class NameTester (code listing 6-28) such that it will NOT crash (throw an run-time exception).

Add the following code in NameTester.java:

1. In main(), add three string variables to hold first name, middle name, and last name.

2. Starting with line 13, either initialize or get user input for the three string variables from above.

3. After initializing the three strings, call the appropriate set..() methods of the FullName object "name" declared on line 12.

4. Finally, run the NameTester to ensure no run-time errors (exceptions).

After successful testing,  I will upload the modified NameTester.java and the unmodified FullName.java.

 

You can use the following stating files:

 

/**

 * This class stores a person's first, last, and middle names.  

 * The class is dangerous because it does not prevent operations

 * on null reference fields.

 */

 

public class FullName

{

   private String lastName,   // To hold a last name

                  firstName,  // To hold a first name

                  middleName; // To hold a middle name

 

   /**

    * The following method sets the lastName field.

    */

 

   public void setLastName(String str)

   {

      lastName = str;

   }

 

   /**

    * The following method sets the firstName field.

    */

 

   public void setFirstName(String str)

   {

      firstName = str;

   }

 

   /**

    * The following method sets the middleName field.

    */

 

   public void setMiddleName(String str)

   {

      middleName = str;

   }

 

   /**

    * The following method returns the length of the 

    * full name.

    */

 

   public int getLength()

   {

      return lastName.length() + firstName.length() +

             middleName.length();

   }

 

   /**

    * The following method returns the full name.

    */

 

   public String toString()

   {

      return firstName + " " + middleName + " " +

             lastName;

   }

}

 

 

 

/**

 * This program creates a FullName object, and then calls the 

 * object's getLength method before values are established for

 * its reference fields. As a result, this program will crash.

 */

 

public class NameTester

{

   public static void main(String[] args)

   {

      // Create a FullName object.

      FullName name = new FullName();

     

      // Display the length of the name.

      System.out.println(name.getLength());

Option 1

Low Cost Option
Download this past answer in few clicks

18.99 USD

PURCHASE SOLUTION

Already member?


Option 2

Custom new solution created by our subject matter experts

GET A QUOTE