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 plus MyProgrammingLab with Pearson eText -- Access Card Package (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
Need help writing code to answer this question in Python! (image attached)
Need help with python code! How do I simplify my code for a beginner to understand, simple fixed format and centering? Such as:  print(f"As an int variable: {age_int:^7}") print(f"In numeric binary: {age_int:^7b}") My Code:name = input("Enter your name: ")print(f"In text name is: {' '.join(name)}")decimal_values = []binary_values = []for letter in name:   ascii_val = ord(letter)   binary_val = format(ascii_val, '08b')   decimal_values.append(str(ascii_val))   binary_values.append(binary_val)# Loop through each letter:print(f"In ASCII decimal: {' '.join(decimal_values)}")print(f"In ASCII binary: {' '.join(binary_values)}")# Ageage_str = input("Enter your age: ")age_int = int(age_str)print(f"As a string \"{age_str}\": {' '.join(age_str)}")age_decimal_values = []age_binary_values = []for digit in age_str:   ascii_val = ord(digit)   binary_val = format(ascii_val, '07b')   age_decimal_values.append(str(ascii_val))   age_binary_values.append(binary_val)print(f"In ASCII decimal: {'…
Don't use chatgpt or any other AI

Chapter 10 Solutions

Java: An Introduction to Problem Solving and Programming plus MyProgrammingLab with Pearson eText -- Access Card Package (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