Java: An Introduction to Problem Solving and Programming (7th Edition)
Java: An Introduction to Problem Solving and Programming (7th Edition)
7th Edition
ISBN: 9780133766264
Author: Walter Savitch
Publisher: PEARSON
bartleby

Videos

Question
Book Icon
Chapter 10, Problem 1E
Expert Solution & Answer
Check Mark
Program Plan Intro

Program plan:

  • Include the required header files.
  • Create a class “Gettysburg”.
    • Define main function.
      • Declare a variable “filename” with the value as file name.
      • In “try” block,
        • Create an object for “PrintWriter” class and pass the parameter as “filename”.
        • Write the contents into the file.
        • Close the file.
      • In “catch” block,
        • If the file not found in the folder, throw a “FileNotFoundException”.
      • Display the statement.

Explanation of Solution

 Program:

//import the header file

import java.io.*;

//definition of "Gettysburg" class

public class Gettysburg

{

    //definition of main method

    public static void main(String[] args)

    {

        //declare the variable and assign the filename

        String filename = "gettysburg.txt";

        //try block

        try

        {

/*create object for "PrintWriter" class and pass the file name*/

PrintWriter outputStream = new PrintWriter(filename);

            //write the contents to the file

outputStream.println("Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.");

outputStream.println("Now we are engaged in a great civil war, testing whether that nation, or any nation, so conceived and so dedicated, can long endure.");

outputStream.println("We are met on a great battle-field of that war.");

outputStream.println("We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live.");

outputStream.println("It is altogether fitting and proper that we should do this.");

outputStream.println("But, in a larger sense, we can not dedicate-we can not consecrate-we can not hallow-this ground.");

outputStream.println("The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract.");

outputStream.println("The world will little note, nor long remember what we say here, but it can never forget what they did here.");

outputStream.println("It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. ");

outputStream.println("It is rather for us to be here dedicated to the great task remaining before us - that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion - that we here highly resolve that these dead shall not have died in vain - that this nation, under God, shall have a new birth of freedom - and that government of the people, by the people, for the people, shall not perish from the earth.");

            //close the file

            outputStream.close();

        }

        //catch block

        catch(FileNotFoundException e)

        {

            //display the error message

System.out.println("Error opening the file " + filename);

            //exit the program

            System.exit(0);

        }

        //display the message

System.out.println("The file gettysburg.txt has been written.");

    }

}

Sample Output

Output:

The file gettysburg.txt has been written.

Output file:

Screenshot of “gettysburg.txt” text file

Java: An Introduction to Problem Solving and Programming (7th Edition), Chapter 10, Problem 1E

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Q4. Consider the following two design alternatives. Walmart Walmart Store locartion 1 * capacity - Associate Name 1 id position Design Alternative "A" Store locartion 1 capacity Associate Name 1 * id position Design Alternative "B" a) Explain the semantic differences between the two designs, if any. b) Explain the differences in how each design alternative may be implemented in Java. c) which design alternative may require more storage requirement in your opinion, and why?
Design a schematic for a compartmental model that includes compartments, flows, and parameters with their respective units, using Figure 1 as a guide. For each flow, determine whether it is best represented by a first-order transfer, a Michaelis-Menten saturable process, or a different method.
9. Consider the diagram on the right. Using this diagram and the four following terms: (a) lonization Energy, (b) Electron Affinity, (c) Mulliken Electronegativity, and (d) Polarizability, label each arrow with the correct term (you can label the arrows with the corresponding letter for space purposes). Please provide labels for both species X and Y. lonization Limit b) Indicate why. Energy- Species X Species Y Which species (X or Y) has the highest electronegativity? Which has the largest polarizability? c)( 2) Consider BH3 (boron trihydride) and TIH3 (thallium trihydride). Which one is more polarizable and why? Which one would have stronger intermolecular forces and why?

Chapter 10 Solutions

Java: An Introduction to Problem Solving and Programming (7th Edition)

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:9780357392676
Author:FREUND, Steven
Publisher:CENGAGE L
Files & File Systems: Crash Course Computer Science #20; Author: CrashCourse;https://www.youtube.com/watch?v=KN8YgJnShPM;License: Standard YouTube License, CC-BY
UNIX Programming (Part - 10) The File System (Directories and Files Names); Author: ITUTEES;https://www.youtube.com/watch?v=K35faWBhzrw;License: Standard Youtube License