Calculate total calls made to a method without usi

2019-09-08 12:03发布

UPDATE:: OK i am putting the original problem statement here

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.

once i come to know how many objects i have to create its quite trivial to program the later part

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.

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

this example forbids the use of static variable, it is a sort of brain teaser so do Not use static

this is my Main.java file

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();
    }

}

and this is my Student.java file

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();
    }
}

How should i modify this program so that i calculate how many times has been the function createPerson being called??

7条回答
对你真心纯属浪费
2楼-- · 2019-09-08 12:41

Use AOP (Aspect-Oriented Programming).

查看更多
登录 后发表回答