Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculate total calls made to a method without using static
    primarykey
    data
    text
    <p>UPDATE:: OK i am putting the original problem statement here</p> <p>Given the Main class create a method createPerson and call it in any other method more than once, then on the basis of the number of times the createPerson has been executed you have to initialize the objects and input names of students and output the names.</p> <p>once i come to know how many objects i have to create its quite trivial to program the later part</p> <p>for the prior problem of finding the number of objects to be created i have chosen the way of file handling as i come from a C, C++ background where file handling is comparatively simple.</p> <p>now how should i modify the program such that i write an integer in the file, and later when i will read the file i will get the number of objects</p> <p>this example forbids the use of static variable, it is a sort of brain teaser so do <strong>Not</strong> use static</p> <p>this is my Main.java file</p> <pre><code>import java.io.IOException; public class Main { public static void main(String[] args) throws IOException{ int i; Student[] totalStudents = new Student[10]; Student.create3Persons(); Student.create2Persons(); } } </code></pre> <p>and this is my Student.java file</p> <pre><code>import java.io.*; public class Student { private static void createPerson() throws IOException{ int number=0; File file = new File("arg.txt", null); FileOutputStream fos = new FileOutputStream(file); DataOutputStream dos = new DataOutputStream(fos); FileInputStream fis = new FileInputStream(file); DataInputStream dis = new DataInputStream(fis); while(dis.readInt()!= -1) { number++; dos.writeInt(1); } } static void create2Persons() throws IOException{ Student.createPerson(); Student.createPerson(); } static void create3Persons() throws IOException{ Student.createPerson(); Student.createPerson(); Student.createPerson(); } } </code></pre> <p>How should i modify this program so that i calculate how many times has been the function createPerson being called??</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload