Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone please show me how to get this code working correctly. It doesn\'t h

ID: 3543815 • Letter: C

Question

Can someone please show me how to get this code working correctly. It doesn't have to be exact and I know you don't know the exact methodNames or variables, just show how to do the steps listed in this code with accurate java coding.



    public String add(String category, Question q) {
        // Get the vector of questions from a category file
        // Add the question object to the vector
        // Save the vector back to the category file
        // Return the result of the save operation
    }
    

Explanation / Answer

public String add(String category, Question q) {

// Get the vector of questions from a category file

File f = new File(category);

f.openNewFile();

Vextor<Question> Q = new Vector<Question>();

BufferedReader reader = new BufferedReader(new FileReader(f));

String line = null;

while ((line = reader.readLine()) != null)

{

Q.add((Question)line);

}


// Add the question object to the vector

Q.add(q);

// Save the vector back to the category file

Iterator i = new iterator<Question>(Q);

FileWriter w = new FileWriter(f);

while(i.hasNext())

{

w.write(i.next());

}

w.flush();

w.close();

// Return the result of the save operation

return Q;

}