Ask Question
25 December, 05:25

One of the constructors for PrintStream class has a single OutputStream argument. Assume that dos is a variable that references an OutputStream object. Create a PrintStream object using dos and assign the resulting reference to ps, a PrintStream variable that has already been declared.

+3
Answers (1)
  1. 25 December, 07:14
    0
    If this were a question on a test, it sounds like the answer they are going for is simply ps = new PrintStream (dos); Normally you would declare it at the same time as you construct it, which would be PrintStream ps = new PrintStream (dos);

    The first part of this, "PrintStream ps", declares a variable of type PrintStream called ps. The second part, assigning to it "new PrintStream (dos) " calls one of the OutputStream class's constructors, which actually allocates the storage and assigns it to ps.

    You can't really write a program to exemplify this question as stated, because OutputStream is an abstract class so it cannot be directly instantiated. In other words, you can't actually have a variable that is a straight-up "OutputStream" object (you can have only a variable of one of its subclasses).

    So in order to actually have an OutputStream object, you would have to use one of its subclasses, for example FileOutputStream or ObjectOutputStream. With the majority of Objects in java, you can also call what's called a "default constructor" to create them, if that is what suits your purposes.

    A default constructor has no parameters, so its call would just look like new PrintStream () (except that in this case there is not a default constructor for PrintStream because it needs an indication of on what to open a PrintStream. If you are just learning constructors, I think the example was not the best chosen it that could have been because there are added complications) The general way of declaring a non-primitive type variable (e. g. not char/int/double/float etc.) in java is to both declare it and create storage for it with the constructor at the same time: Object o = new Object ();

    This format can be used with many variables. You would substitute in whatever type it really was instead of Object. Note that classes start in a capital letter like this, so if you see a variable type of Java's beginning with a capital letter, it will be an Object of some kind (or should be, although not all people follow the rule when they write their own classes) There are also various other constructors (each class has its own set).

    The system determines which constructor to use by what (if any) parameter types are passed in, in the order they are passed (So you could not have two different constructors that would have the same parameters, e. g. there could not be two Object (int) constructors, because it wouldn't know how to tell them apart) So, for example, the FileOutputStream contains (among other things) a constructor to take a filename as a parameter:

    FileOutputStream (File file) Creates a file output stream to write to the file represented by the specified File object. An example of calling it would be: FileOutputStream dos = new FileOutputStream ("myfilename"); When you are writing constructors for the classes you create, you want to make sure that you at the minimum always include at least a default constructor (one with no arguments).

    This is good practice. If you do not write any constructors, java will behind the scenes write a default constructor for you that you can just call. But then if you write a constructor with arguments, java will no longer have that default constructor behind the scenes so it's good practice to write one.

    This all can sound complicated when you are learning it, but it is actually very useful to be able to just construct an instance of your class with all its values set via a single call, without all the duplication of setting variables (because that will all be centralised within the relevant constructor). I wrote a sample program for your question, but it has to have some extra things in order to work, and for brevity it includes some extremely poor practice regarding exceptions. public class UsePrintStream { public static void main (String args[]) throws java. io. FileNotFoundException, java. io. IOException { FileOutputStream dos = new FileOutputStream ("myfilename"); PrintStream ps = new PrintStream (dos); ps. close (); dos. close (); } }

    Source (s) : Java API (this shows the Java hierarchy of classes, with their constructors, fields, and methods. It is frame-based; to use it, navigate to the page, use your browser's search function to locate the class you are interested in on the left column, click on it, and then look up or read what you need to in the main page)
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “One of the constructors for PrintStream class has a single OutputStream argument. Assume that dos is a variable that references an ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers