Write a
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.
- Define main function.
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.");
}
}
Output:
The file gettysburg.txt has been written.
Output file:
Screenshot of “gettysburg.txt” text file
Want to see more full solutions like this?
Chapter 10 Solutions
Java: An Introduction to Problem Solving and Programming (7th Edition)
Additional Engineering Textbook Solutions
Starting Out with C++ from Control Structures to Objects (9th Edition)
Starting Out With Visual Basic (8th Edition)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
SURVEY OF OPERATING SYSTEMS
Modern Database Management
Degarmo's Materials And Processes In Manufacturing
- 4. def modify_data(x, my_list): X = X + 1 my_list.append(x) print(f"Inside the function: x = {x}, my_list = {my_list}") num = 5 numbers = [1, 2, 3] modify_data(num, numbers) print(f"Outside the function: num = {num}, my_list = {numbers}") Classe Classe that lin Thus, A pro is ref inter Ever dict The The output: Inside the function:? Outside the function:?arrow_forwardpython Tasks 5 • Task 1: Building a Library Management system. Write a Book class and a function to filter books by publication year. • Task 2: Create a Person class with name and age attributes, and calculate the average age of a list of people Task 3: Building a Movie Collection system. Each movie has a title, a genre, and a rating. Write a function to filter movies based on a minimum rating. ⚫ Task 4: Find Young Animals. Create an Animal class with name, species, and age attributes, and track the animals' ages to know which ones are still young. • Task 5(homework): In a store's inventory system, you want to apply discounts to products and filter those with prices above a specified amount. 27/04/1446arrow_forwardOf the five primary components of an information system (hardware, software, data, people, process), which do you think is the most important to the success of a business organization? Part A - Define each primary component of the information system. Part B - Include your perspective on why your selection is most important. Part C - Provide an example from your personal experience to support your answer.arrow_forward
- Management Information Systemsarrow_forwardQ2/find the transfer function C/R for the system shown in the figure Re དarrow_forwardPlease original work select a topic related to architectures or infrastructures (Data Lakehouse Architecture). Discussing how you would implement your chosen topic in a data warehouse project Please cite in text references and add weblinksarrow_forward
- Please original work What topic would be related to architectures or infrastructures. How you would implement your chosen topic in a data warehouse project. Please cite in text references and add weblinksarrow_forwardWhat is cloud computing and why do we use it? Give one of your friends with your answer.arrow_forwardWhat are triggers and how do you invoke them on demand? Give one reference with your answer.arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning