没有构造函数,如何才能使这些代码发挥相同的功能?

时间:2019-04-13 21:39:38

标签: java algorithm constructor printwriter

我对编程非常陌生,我正在尝试修改在线找到的堆算法。从上一个问题开始,我能够使代码与PrintWriter一起使用,但是当尝试将此函数用作另一个类中的方法时,由于构造函数而出现错误。无需构造函数,如何将这些代码修改为相同的功能?

我对编程不是很熟悉,因此我尝试查看以前的问题。不知何故,我想到了使用嵌套类(不确定它们如何工作),但无济于事。该方法在自己的类中有效。

// Should be within a class

private PrintWriter _pw;

// This is the part that needs to go.
public HeapAlgo(PrintWriter pw) {
   this._pw = pw;
}

public void heapPermutation(String a[], int size, int n) throws IOException { 
// if size becomes 1 then prints the obtained 
// permutation 
    if (size == 1) 
        for (int i=0; i<n; i++) { 
            System.out.println(a[i] + "");
            this._pw.println(a[i] + ""); 
        }

    for (int i=0; i<size; i++) { 
        heapPermutation(a, size-1, n); 

        // if size is odd, swap first and last 
        // element 
        if (size % 2 == 1) { 
            String temp = a[0]; 
            a[0] = a[size-1]; 
            a[size-1] = temp; 
        }

        // If size is even, swap ith and last 
        // element 
        else { 
           String temp = a[i]; 
           a[i] = a[size-1]; 
           a[size-1] = temp; 
        } 
    }

}

public void heap() throws IOException 
{
FileWriter fw = new FileWriter("note.txt");
PrintWriter pw = new PrintWriter(fw);
File temp = new File("code.txt");
Scanner file = new Scanner(temp);

String substring = "";

    String a[] = new String[4];
  a[0] = "" + file.nextLine(); 
  a[1] = "" + file.nextLine();
  a[2] = "" + file.nextLine();
  a[3] = "" + file.nextLine();



HeapAlgo obj = new HeapAlgo(pw); // Pass in a writer
obj.heapPermutation(a, a.length, a.length);
pw.close();
} 

当我在大型类中运行方法时,出现错误提示\ “错误:方法声明无效;需要返回类型”。

任何帮助将不胜感激。谢谢。

编辑:我正在尝试对该构造函数进行编码:

    public CodeRunner() 
    { 
    random(); 
    HeapAlgo.heap(//not sure if anything should go here); 
    algorithm(); 
    }

其中random()创建随机字符串,并且algorithm函数对随机字符串的所有可能迭代执行算法。我正在尝试为每组随机字符串制作对象。

1 个答案:

答案 0 :(得分:0)

似乎以下元素应该在名为HeapAlgo的类中:

  1. 私有变量声明
private PrintWriter _pw;
  1. 构造函数本身
public HeapAlgo(PrintWriter pw)
  1. heapPermutation函数
public void heapPermutation(String a[], int size, int n) throws IOException

剩下的最后一个方法heap()应该放在其他一些类中(可能是您的main()函数所在的位置)并从那里调用。

或者,您确实可以使用内部类。将您提供的所有代码包装在一个类(可能称为Heap)中,然后将上述三个元素包装在一个内部类HeapAlgo中。这样的事情(我很快就输入了,所以可能需要纠正错误):

public class HeapUtil {
    public class HeapAlgo {
        private PrintWriter _pw;

        // This is the part that needs to go.
        public HeapAlgo(PrintWriter pw) {
           this._pw = pw;
        }

        public PrintWriter getPrintWriter(){
            return _pw;
        }

        public void heapPermutation(String a[], int size, int n) throws IOException { 
        // if size becomes 1 then prints the obtained 
        // permutation 
            if (size == 1) 
                for (int i=0; i<n; i++) { 
                    System.out.println(a[i] + "");
                    this._pw.println(a[i] + ""); 
                }

            for (int i=0; i<size; i++) { 
                heapPermutation(a, size-1, n); 

                // if size is odd, swap first and last 
                // element 
                if (size % 2 == 1) { 
                    String temp = a[0]; 
                    a[0] = a[size-1]; 
                    a[size-1] = temp; 
                }

                // If size is even, swap ith and last 
                // element 
                else { 
                   String temp = a[i]; 
                   a[i] = a[size-1]; 
                   a[size-1] = temp; 
                } 
            }
        }
    }

    public static HeapAlgo heap() throws IOException 
    {
        FileWriter fw = new FileWriter("note.txt");
        PrintWriter pw = new PrintWriter(fw);
        File temp = new File("code.txt");
        Scanner file = new Scanner(temp);

        String substring = "";

        String a[] = new String[4];
        a[0] = "" + file.nextLine(); 
        a[1] = "" + file.nextLine();
        a[2] = "" + file.nextLine();
        a[3] = "" + file.nextLine();

        HeapAlgo obj = new HeapAlgo(pw); // Pass in a writer
        obj.heapPermutation(a, a.length, a.length);
        return obj;
    }
}

请注意,在这种情况下,如果您想在此类文件之外使用HeapAlgo,则需要使用Heap.HeapAlgo


编辑:尝试上面的代码(我已对其进行编辑)。可能有一些错误,因为我实际上没有运行它。

用法如下:

public CodeRunner(){ 
    random(); 
    // heapAlgo is the heap object
    HeapAlgo heapAlgo = HeapUtil.heap(); 
    // this gives you access to the PrintWriter inside the HeapAlgo
    PrintWriter printWriter = heapAlgo.getPrintWriter();
    // do your other stuff
    algorithm(); 
}