COMP3200 Anonymous Questions and Answers |
This page lists the various questions and answers. To submit a question, use the anonymous questions page. You may find the keyword index and/or top-level index useful for locating past questions and answers.
We have taken the liberty of making some minor typographical corrections to some of the questions as originally put. Although most of the questions here will have been submitted anonymously, this page also serves to answer some questions of general interest to those on the course.
Submission reference: IN2654
When finding and removing from a collection using while loop. please could you explain further what the line which follows the while condition means and why its needed and what the 2nd word is? i understand the first word is a data type because its starts with a capital letter. e.g from the lecture slides when removing:
Track t = it.next();when searching:
Track track = tracks.get(index);
In both cases, a variable of type Track is being declared and initialised in a single statement. In the first example, the variable is called t
and in the second it is called track
.
In the first example, the variable is being initialised by calling the next()
method on the Iterator object called it
. In the second example, the variable is initialised with the Track object returned by a call to the get()
method on a tracks
ArrayList.
Submission reference: IN2653
For some reason my test class is not testing, so is this method right. Im not sure whether im searching for the right thing in the while loop
code deleted
The test class should always successfully compile against your code unless you have changed any of the pre-defined method headers so it would be worth trying to fix that.
No, that is quite a long way from being correct. For instance, you aren't using the hasNext method of the Iterator to control the loop or check that there is another Assistant object to consider before calling the next method. Also, your logic is incorrect as it will never set the location or availability of the target assistant.
Keywords: assign2 , method-setAvailability
Submission reference: IN2652
for Question 97 (2022), would this be better as it stops the while loop searching for any more identities?
code deleted
Unfortunately, what you have implemented doesn't work because it exits the loop if the first assistant in the collection doesn't have a matching identity.
However, that solution is also problematic because it hides the fact that the loop is not actually going to iterate over the collection, as appears to be the case from the loop's condition. In effect, a break statement is equivalent to a goto statement and it has long been considered a bad idea - Edsger Dijkstra wrote an influential rationale for this back in 1968. For a more recent explanation see Every wondered why goto is considered harmful.
It would be much clearer to express the loop's condition as a search as described in the Monday lecture in week 16. That way, it will be clear from the loop's condition that the iteration either stops when the matching assistant has been found or there are no more to check.
Keywords: assign2 , method-setAvailability , break-statement , goto-statement
Submission reference: IN2651
In assignment 2 its not been specified that we need to include error prevention f.e. if inputting an identity that's not in the ArrayList for the setAvailability method. currently my program just does nothing, do we need to be coding checks and error messages for invalid inputs like this?
Good question. If the spec. doesn't explicitly say that you should print an error message then don't do so. If a matching Assistant cannot be found for a given identity, just do nothing.
Keywords: assign2 , error-messages , method-setAvailability
Submission reference: IN2650
Hello, i believe I've finished this method the correct way, would this achieve full marks?
code deleted
The logic looks correct. However, there are a couple of improvements you could make in terms of quality:
Keywords: assign2 , method-setAvailability
Referrers: Question 99 (2022)
Submission reference: IN2649
code deletedthis is my code for the findNearestAvailable method. Whenever I call the method, i get a logic error in which null is outputted, which I am confused about as the assistant object would overight it when the nearest is found. What do I need to do to circumvent this error?
Unfortunately, your overall approach really needs a rethink as it will rarely find a matching assistant. The approach you appear to be trying is to start with a distance of 0 from the target location and then gradually increasing that distance until an available assistant is found. The problem is that that approach would only work if you iterate over the collection multiple times: firstly test everyone with a distance of 0, then of 1/-1, then of 2/-2, etc. However, as you only have one loop, the effect is that you try the different distances with different assistance on the only iteration.
While your approach would work, if you had enough multiple iterations, it's not really a viable way to tackle this method and there are much more efficient approaches that genuinely only require a single iteration.
One way to approach it is to choose the first available assistant but replace them if you find a closer one, and repeat that process until you have checked them all once.
Keywords: assign2 , method-findNearestAvailable
Submission reference: IN2648
I am attempting to finish the challenge question but I could not figure out the last two failures given to me by the test class. The two failures are AssistantCentreTest.findAssistantNeighbours() and AssistantCentreTest.findAssistantMultipleNeighbours()
code deleted
The source of the problem is what you are doing in the final else-part inside the loop.
Submission reference: IN2647
code deletedthis is my code for the findNearestAvailable method. I have a slight issue in which null is returned despite the return statement in one of the if statements. I also get a "missing return statement" error when i remove return null. How can i alter this code in order to accept the first return statement?
This is a method in which you will definitely need to consider every Assistant object in the collection. So, a return statement inside the loop will be inappropriate. Therefore, the final statement of the method will be one that returns the Assistant object that was found during the iteration.
At the moment, the logic in the body of your method needs a complete rethink because you should not be altering the value of the location parameter.
Keywords: assign2 , method-findNearestAvailable
Submission reference: IN2646
Good morning, im trying to work through the availability part of this method and im not sure what method to call in the Assistant class as none that involve availability use parameters. This is what ive tried but it gives me an error that this method cannot be applied to given types.
assistant.isAvailable(available);
In the Assistant class there are three methods related to availability: isAvailable, setAvailable and setOccupied. None take a parameter. That's because the Assistant object has all the information it needs internally to be able to carry out the associated action:
So, to complete the setAvailability method, you need to work out which of those need to be called depending on the value of the available
parameter to the setAvailability method.
Keywords: assign2 , method-setAvailability
Referrers: Question 142 (2022)
Submission reference: IN2645
Hello, i am starting the setAvailablity method and wondering if it needs to be a for each or while loop?
A good way to answer that for yourself is to see if you can work out whether you will always need to iterate over the whole collection (for-each loop) or might be able to stop before reaching the end (while-loop). For instance, if you might find the Assistant with matching identity in the first location of the collection then you could stop after the first one and not look at the rest. That would mean that a while-loop rather than a for-each loop would be appropriate.
Keywords: assign2 , method-setAvailablity
Submission reference: IN2644
taking in your feedback from Question 90 (2022), ive got this code, but it gives me the error: method getLocation in class Assistant cannot be applied to given types; required: no arguments found: int reason: actual and formal argument lists differ in length. im not sure what this means
code deleted
The getLocation method in the Assistant class doesn't take a parameter - note in the source of that class that its parentheses are empty. However, note that if you want to change the location of an Assistant, getLocation is not the method to call. There is a different one - not called setLocation - that you will find if you look in the source of the Assistant class. That method does take a parameter.
Keywords: assign2 , method-setAvailability
Submission reference: IN2643
this is the if statement i have created for the set availability method for just the location bit, not the availability, and im not sure how to set the location to the value entered in the parameter. The setLocation(getLocation()); line gives me an error that getLocation is not a declared method.
code deleted
Because you need to change the location of the matching individual Assistant object, you need to call the methods on that Assistant object. For instance, along these lines:
assistant.methodName(any-parameters-required);
Keywords: assign2 , method-setAvailability
Referrers: Question 91 (2022)
Submission reference: IN2642
I've written comments prepending some private methods I created for Assignment 2's AssistantCentre class. By default Javadoc doesn't create documentation for comments prepended to a private method. Are my comments necessary? If so, is it necessary to configure Javadoc to also create documentation files for my private methods?
It is best to include them anyway. Javadoc can be configured to show the comments for private methods but it doesn't by default.
Submission reference: IN2641
i have started my setAvailability method and not sure if ive started it correct. This is what ive gone with and at the moment have one error on the assistant.getLocation = location; line which says getLocation is undeclared.
code deleted
Once you have found the matching Assistant object, you need to set its location and availability via method calls rather than assignments. If you look at the source code of the Assistant class you should be able to identify the methods your code needs to call.
Also, have a think about whether you need to continue the iteration once the Assistant has been identified.
Keywords: assign2 , method-setAvailability
Submission reference: IN2640
Would this void list method do what its meant to do?
code deleted
That looks fine.
Keywords: assign2 , method-list
Submission reference: IN2638
For Question 85 (2022), thank you for your feedback. Would this method now be correct?
code deleted
Yes, that is much better. Just remember that variable names should always start with a lower-case letter. Only class names should start with an upper-case letter.
Keywords: assign2 , method-getNumberOfAssistants
Submission reference: IN2637
Good afternoon, would it be possible to tell me what i am doing wrong here. I get loads of errors for this. I understand i need to be returning the count that i have attempted to create in this method but im just unsure on how to execute it.
code deleted
You have a local variable assistants
which you are trying to use as an integer variable, but you have given it the type Assistant. So you need to change the type. Also, I would advise against giving the local variable the same name as the collection field ecause that will be confusing and prevent you from accessing the field.
Another issue is the value you are returning at the end of the method. That should be the count of how many assistants were found at the given location but you are returning something related to the size of the collection, which is unrelated to the number found.
Keywords: assign2 , method-getNumberOfAssistants
Referrers: Question 86 (2022)
Submission reference: IN2636
In the brief for assignment 2 a style guide is referenced. The style guide advises to put curly brackets on the end of the line for anything other than methods or classes. e.g.
while(condition) { statements }instead of
while(condition) { statements }My question is this: is adhering to this a necessity for full marks? I'd rather always give curly brackets their own line.
You are welcome to use your own style. The only requirement is that you are consistent in how you lay out your code. You won't lose marks if you use a different style.
Submission reference: IN2635
In regard to the last lesson, as you said we would need iterators and while loops for our assignment 2, in my case I have "finished" the assignment (no erros/failures from test class) however I used no iterators/while loops as I just used a combination of everything learned before project week (plus a bit extra learning for things like challenge question). Would this make me lose marks because of this or it is ok to leave my code unchanged?
As long as your code is functionally correct, then you won't lose marks if you haven't explicitly used while-loops and Iterators. You should thoroughly test your code with data that goes beyond that in the test class to really gain confidence.
While it is possible to complete the assignment accurately without while and Iterator, you have to be really careful and the quality of your code might suffer as a result.
Submission reference: IN2634
public void listAllFiles(){ for(String files:files){ System.out.println(files); } } public void listAllFiles(){ for(String shaek:files){ System.out.println(files); } }these 2 method are identical but i changed the object name in the for each loop in the second. when running the programs the first one prints file5 file4 file3 and the second one [file5, file4, file3] [file5, file4, file3] [file5, file4, file3] i would like to know why the second one list the whole collection and why it print in an array.
The difference is that you are using the name files
for two different things in the first example. In the first example, the local variable, which is the for-loop variable, is what gets used in the body of the loop. So, as it gets set, one after the other, to the filenames in the collection, each different name gets printed out.
In the second version, you are not using the loop variable in the body of the loop, so each time around the loop, your code is printing out the full list.
A better approach is to use different names for the loop variable and the collection and use the local variable name in the body of the loop. For instance
public void listAllFiles() { for(String filename : files){ System.out.println(filename); } }
Keywords: method-listAllFiles
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. |