Question

In: Computer Science

public class Question4 { /* For this exercise you will be doing some basic operations to...

public class Question4
{
/* For this exercise you will be doing some basic operations to write to a file.
You will be creating your very own Secrets of the Universe (TM)
  
You can use the PrintWriter class to write to files (Chapter 11.4)
  
Your code should perform the following actions:
  
1) Open/Create a file for writing at the following location: "files/question4/MySecretsOfTheUniverse.txt"
* If the file already exists you will want to overwrite its contents.
2) Add at least 3 of your own secrets of the universe to the file.
* Each secret should be on its own line.
3) Make sure to close your file so that the results are saved.

*Note: Your code should not throw any checked exceptions.
*Note: You will need to import some classes from the java.io package.
*/
public static void Question4()
{
  
}
  
public static void main(String[] args)
{
Question4();
}
}

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: the output file will be created if not exist or overwritten if already exists, but you have to ensure that the directory files/question4/ exist before running this program. If not, create the directory in root directory of your project, and then run the program.

// Question4.java

import java.io.File;

import java.io.PrintWriter;

public class Question4 {

      /*

      * For this exercise you will be doing some basic operations to write to a

      * file. You will be creating your very own Secrets of the Universe (TM)

      *

      * You can use the PrintWriter class to write to files (Chapter 11.4)

      *

      * Your code should perform the following actions:

      *

      * 1) Open/Create a file for writing at the following location:

      * "files/question4/MySecretsOfTheUniverse.txt" If the file already exists

      * you will want to overwrite its contents. 2) Add at least 3 of your own

      * secrets of the universe to the file. Each secret should be on its own

      * line. 3) Make sure to close your file so that the results are saved.

      *

      * Note: Your code should not throw any checked exceptions.Note: You will

      * need to import some classes from the java.io package.

      */

      public static void Question4() {

            // creating a File object

            File file = new File("files/question4/MySecretsOfTheUniverse.txt");

            try {

                  // opening print writer to write to the file. the file will be

                  // created if not exist, but you have to ensure that the directory

                  // files/question4/ exists.

                  PrintWriter writer = new PrintWriter(file);

                  // writing three random statements to the file. I dont know anything

                  // about the secrets of universe you mentioned, so I'm just writing

                  // some random stuff that came into my mind. You can replace below

                  // lines with whatever you want to write.

                  writer.println("Humans are aliens from Mars, sent here to colonize Earth after they screwed up mars.");

                  writer.println("This universe and everything in it are probably brain cells of some huge cosmic organism.");

                  writer.println("There may be other forms of life co-exist here on our Earth that we can't see,"

                              + " because they vibrate at a different frequency outside of all our measurements");

                  //closing file, saving changes

                  writer.close();

            } catch (Exception e) {

                  //printing error if exception is occurred.

                  System.err.println(e);

            }

      }

      public static void main(String[] args) {

            Question4();

      }

}


Related Solutions

Programming Exercise Implement the following class design: class Tune { private:    string title; public:   ...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:    Tune();    Tune( const string &n );      const string & get_title() const; }; class Music_collection { private: int number; // the number of tunes actually in the collection int max; // the number of tunes the collection will ever be able to hold Tune *collection; // a dynamic array of Tunes: "Music_collection has-many Tunes" public: // default value of max is a conservative...
/** * This class implements a basic Binary Search Tree that supports insert and get operations....
/** * This class implements a basic Binary Search Tree that supports insert and get operations. In * addition, the implementation supports traversing the tree in pre-, post-, and in-order. Each node * of the tree stores a key, value pair. * * @param <K> The key type for this tree. Instances of this type are used to look up key, value * pairs in the tree. * @param <V> The value type for this tree. An instance of this...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) {...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,1,2}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: " + WordsCharsLetters[1]); System.out.println("Letters: " + WordsCharsLetters[2]); } static int[] calculateAndPrintChars(String sentence) { int[] WCL = new int[3]; String[] sentenceArray=sentence.split(" "); WCL[0] = sentenceArray.length; int letterCount=0; for(int i=0;i<sentence.length();i++) { if(Character.isLetter(sentence.charAt(i))) letterCount++; } WCL[1]...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
Using the class Date that you defined in Exercise O3, write the definition of the class...
Using the class Date that you defined in Exercise O3, write the definition of the class named Employee with the following private instance variables: first name               -           class string last name                -           class string ID number             -           integer (the default ID number is 999999) birth day                -           class Date date hired               -           class Date base pay                 -           double precision (the default base pay is $0.00) The default constructor initializes the first name to “john”, the last name to “Doe”, and...
These are some of the preliminary tests that you will be doing on days 1-2. For...
These are some of the preliminary tests that you will be doing on days 1-2. For each test, write down what type of molecule would give a positive test and what a positive result would look like. DNP: Solubility: Beilstein: Ignition:
I need a basic and simple Java code for this exercise: (Check password) Some websites impose...
I need a basic and simple Java code for this exercise: (Check password) Some websites impose certain rules for passwords. Write a method that checks whether a string is a valid password. Suppose the password rules are as follows: ■ A password must have at least eight characters. A character is alpha or digit. ■ A password consists of only letters and digits. No other symbols. ■ A password must contain at least two digits. Write a program that prompts...
Do you believe that we as a society are doing enough to inform the public about...
Do you believe that we as a society are doing enough to inform the public about date rape and date rape drugs? If not, what do you think should happen to make society more aware? What things can women or men do to safeguard themselves against being a victim of date rape or dating violence?
You have some friends who are doing a group project with you. The goal is to...
You have some friends who are doing a group project with you. The goal is to test H0 : u = 5 vs H1 : u > 5. They have to go, but before they leave they tell you what they have found. They have a sample mean = 4.8, a critical number x bar ( this would be x with a bar over it ) critical = 4.9, ( xc = 4.9 , where the underline should be on...
How are you doing in this class? Please elaborate on whether or not you've been finding...
How are you doing in this class? Please elaborate on whether or not you've been finding the lecture materials and teaching methods helpful. Also, do you think you can apply what you've been learning in this class in your current or future workplace?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT