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:25

Mostly when we want to retain value across method, either we go for

  • Static variable
  • or pass the variable as reference

  • or pass the variable and return the updated variable and use it again

    int createPerson(int counter);

查看更多
成全新的幸福
3楼-- · 2019-09-08 12:26

i have to disagree with @Jon Skeet, i dont think you have to use a static variable. Why not simply return the number of items created, and keep the total count in a local variable in your main method ? (assuming youre allowed to modifiy method signatures of course, otherwise youre stuck with the global variable solution) you seem to have an unused counter right there already, why not use it?

查看更多
我想做一个坏孩纸
4楼-- · 2019-09-08 12:26

You can use a system property to store the quantity of calls...this way you don't have to use the file system or AOP or change method signature.

查看更多
虎瘦雄心在
5楼-- · 2019-09-08 12:27

You can use:

synchronized(this){
   System.setProperty("methodCounter",number++);
}

instead of a file based counter. And in the main method:

System.getProperty("methodCounter"); 
查看更多
叛逆
6楼-- · 2019-09-08 12:32

If you want to calculate how many times a static method has been called, you have to use a static variable - it's genuinely global state. EDIT: Yes, you can use the file system as another repository for global state, but I'd personally say that's generally a bad idea - particularly if it's just to get round a requirement in an academic question.

I guess there are some exceptions to this - if the static method is provably only called from one class, and that class is a singleton, then there could be an instance method in that singleton... but that's pretty much a corner case. In your case, create3Persons and create2Persons are both accessible from anywhere in the package, and they aren't instance methods in a singleton, so therefore there's no single context in which to keep the call count except a static variable.

Perhaps if you could explain a bit more about what you're trying to achieve, we could help you more...

EDIT: Yes, if you can rely on cooperative callers - and you can change the method signatures - then you could keep (say) an AtomicInteger, and make sure you always pass a reference to the same object into the createStudent method. Again, we'd really need to know what the rules of the question are...

查看更多
疯言疯语
7楼-- · 2019-09-08 12:35

i got the Solution instead of file handling i am doing this

 static int createPerson(int c){
        return (++c);
    }

     static int create2Persons(int c) {

    return Student.createPerson(Student.createPerson(c));
    }

    static int create3Persons(int c) {

        return Student.createPerson(Student.createPerson(Student.createPerson(c)));


    }




        public static int create5Persons(int i) {
   return Student.createPerson(Student.createPerson(Student.createPerson(Student.createPerson(Student.createPerson(i)))));
        }

and in the main method i have initialised an integer and i do this

 c += Student.create2Persons(i)-1;
          c += Student.create3Persons(i)-1;
          c += Student.create5Persons(i)-1;
          System.out.println(c+"c is");

this saves use of files

how to tag the thread as answered?

查看更多
登录 后发表回答