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

This for a Python Class Suppose that a scientist is doing some important research work that...
This for a Python Class Suppose that a scientist is doing some important research work that requires her to use rabbits in her experiments. She starts out with one adult male rabbit and one adult female rabbit. At the end of each month, a pair of adult rabbits produce one pair of offspring, a male and a female. These new offspring will take one month to mature and become adults. To illustrate this, consider the first two months. At the...
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]...
please answer as text You are given a Node class and a List class: public class...
please answer as text You are given a Node class and a List class: public class Node {        int           data;        Node       next;        Node(int d, Node n){             data = d;             next = n;       } } public class List{        Node header; } Write a java method insertNodeBefore( ) which takes two integers: int ref which is the data of the node that we need to insert a new node before, and int d which is...
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT