Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
COMP3200 Anonymous Questions and Answers Page (2021, 183-202) XML 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. Question 202: Submission reference: IN1353 Can I use an Array list rather than a linked list for Assignment 2 Answer 202: Not for the part that says you must use a LinkedList. Keywords: assign2 , method-removeUnknownStatus Question 201: Submission reference: IN1352 Question 199 (2021), when I call the add method it doesn't open a terminal window and show the red error message below, assuming the infectionTest object passed on hasnt been created. like how your code in the infectionTest will open up the terminal window when the status is not known. or have I misunderstood the question? Answer 201: Unfortunately, you have misunderstood what should happen in this case. In the first assignment, there was a specific test class - it appeared green in the class diagram - but you don't have one yet for this assignment. So, you should not expect anything to happen after calling the addTest method. From looking at your code in a previous question, it will have worked. So, now go on to implement the getNumberOfTests methods and if you call that after addTest it should return 1 to confirm that there is now 1 test in the collection. You can then call addTest more times and each time call getNumberOfTests and confirm it has been added. Keywords: assign2 , method-addTest Question 200: Submission reference: IN1351 Hello Sir, I was doing the timeTick method as well, was wondering if this would work code deleted Answer 200: That is close but the value to check for is 0 rather than 59 because 0 indicates that the value has just rolled over. Question 199: Submission reference: IN1350 okay, this is my code now, but it doesn't seem to work code deleted Answer 199: In what sense doesn't it work? How is that manifested when you call the method? I recommend that you define the getNumberOfTests methods so that you can confirm that the test has actually been added to the collection. Keywords: assign2 , method-addTest Referrers: Question 201 (2021) Question 198: Submission reference: IN1349 I also forgot about the "if" part, it tells me that "bad operand Infection Test for unary operator !' would it make sense to say test == null? if(!test){ } Answer 198: The boolean operator '!' can only be applied to a boolean expression, which is why you are getting that error. If you want to test whether a variable (var, say) contains a reference to an object then you will need to use var == null Question 197: Submission reference: IN1348 Hi sir, this is my work for assign2 so far, but am uncertain about my addTest code cause of the keyword "this" code deleted Answer 197: If you don't wish to use the word this then I suggest that you call the field something other than test, which is not the best name for the collection anyway, so worth changing to avoid confusion. Keywords: assign2 , method-addTest Question 196: Submission reference: IN1347 Could you explain what the addtest is supposed to store becuase when I attempt to enter various types such as string or int , it doesnt allow me to add it to the list. What is a valid input for a test to be added ? Answer 196: The input/parameter is an InfectionTest object, so a String or int is not of the correct type. As the description of the method says in the assignment, you have to create one (on the object bench) before calling the method and pass it as the parameter (just click on it when the parameter dialog appears). Keywords: assign2 , method-addTest Referrers: Question 222 (2021) , Question 230 (2021) , Question 300 (2021) , Question 305 (2021) , Question 425 (2021) Question 195: Submission reference: IN1346 When tested, my add method wouldn't work: code deleted Could you guide me as to why that is? Answer 195: It looks correct to me - the commented out line can be deleted because it is incorrect. What makes you say it doesn't work? If you create an InfectionTest object on the object bench and pass it to the call to addTest then the item will be added to the ArrayList. Question 194: Submission reference: IN1345 Is there a testing class for assignment 2, the InfectionsResult class, if there is where can we find it? also, I had a question about the HashSet and linked list methods, do we have to make a new set inside the method to store the selected tests in it? cause that's what I've done Answer 194: There is not a test class yet. I will make it available from the Assignments section when there is one. Note that HashSet and LinkedList are classes rather than methods. You are advised to create those objects inside the methods rather than outside them. Keywords: assign2 Question 193: Submission reference: IN1344 Hello David, For assignment 2, do we have to import the ArrayList package? Thank you Answer 193: If you are using an ArrayList object to store the InfectionTest objects then you will need to import the ArrayList class from the java.util package, as in the music-organizer projects. Keywords: class-ArrayList , assign2 Question 192: Submission reference: IN1343 Submission reference Question 191 (2021). Hi, To clarify my question (why are there spaces in the set). When I inspect the returned object I am taken to a HashMap and when I inspect that I am taken to HashMap.node. When I inspect that the tests that were stored are located at index 3, 8 and 14 (out of 16 nodes), meaning 0,1,2,4,5 etc are empty. If (when I've done my tests) I println the size of the HashSet, it does tell me that there are 3 items in the set so I'm sure I've done it right but I'm also suspicious because of how "deep" the data is i.e. I have to inspect the HashSet and then HashSet.node to get to the list. Is this right or should the list (HashSet.node) be the thing that is returned? Answer 192: A HashSet is implemented differently, internally, from an ArrayList. The hashing technique involves generating the index of an object from the contents of the object. In order to make that access efficient, hash-based data structures are always larger than they need to be to hold their current contents, and the contents are not stored sequentially. That is why there are gaps when you look at the internal contents of the HashSet your program creates. Keywords: class-HashSet , hashing Question 191: Submission reference: IN1342 Hi, I have a question about HashSets from assignment 2. My question is: Why are the items that I add to the HashSet added in a random order and why are there spaces in the set? Answer 191: The nature of a set is that it is unordered - that is a fundamental part of its definition. I am not sure what you mean by there being spaces in the set. Could you clarify that part of your question? Keywords: class-HashSet , assign2 Referrers: Question 192 (2021) Question 190: Submission reference: IN1341 Hello David, can you tell me what a listed array is and how it differs from a normal array? Thanks. Answer 190: I assume that you mean an ArrayList. ArrayList is a class that offers a more flexible way to collection multiple objects together. When an array is created, it has a fixed size and offers no methods for storing or retrieving items to/from it. An ArrayList makes use of an array to provide a more usable interface for storing, retrieving and iterating over it. The main feature over an array is that it does not have a fixed size. It an expand indefinitely to hold as many objects as it needs to. If an array is filled up, you would have to create another one and copy the contents of the old one into it to achieve the same effect. Keywords: class-ArrayList , array Question 189: Submission reference: IN1340 It has to be this, from what I have understood this says when seconds are rolled over, minutes increment when the minute is rolled over, hour increments. although that makes me confused, does the seconds.increment(); have to be there in the first place... like how before you has this in the original code minutes.increment(); if(minutes.getValue() ==0)... public void timeTick() { if(seconds.getValue() == 0) { // it just rolled over! minutes.increment(); } if(minutes.getValue() == 0){ hours.increment(); } updateDisplay(); } Answer 189: Still not right, I am afraid. Remember that timeTick is called every second, so the test of minutes' value is made every second. If the current time is "00:00:01" (for instance) then the minutes value is zero. So the hours gets incremented and the time becomes "01:00:01". Notice that, in your code, the seconds wasn't incremented but the hour was, even though just one second has passed. When timeTick is called on the next second then the time moves on to "02:00:01" because the minutes is still zero. That will just go on and on and on every second, just incrementing the hours and not changing anything else. The code will always implement what is actually written rather than what you think is written. Keywords: ex-3-57 Question 188: Submission reference: IN1339 sorry I realized I made a mistake in the method call code, should have been ...> 2000000 but i put an assignment sign Answer 188: Indeed. Keywords: ex-3-55 Question 187: Submission reference: IN1338 code deleted Progress? Answer 187: A little. Now, every time minutes is zero the hours get incremented. So, for a full minute the hours are incremented every second. Keywords: ex-3-57 Question 186: Submission reference: IN1337 Ohh okay I thinkkkkk I see what you mean, sorry its all getting a little confusing so I could still be wrong, something like code deleted Answer 186: That's more like what is meant but it would be worth re-reading the exercise because: You haven't created a Screen object. You haven't called the clear method. You are not calling methods properly. You are calling one method on screen and another on picture. You have forgotten what = means. Take a bit of time over this because there are so many errors that it looks like you are rushing to get it finished. Keywords: ex-3-55 Question 185: Submission reference: IN1336 Would this work? public void timeTick() { seconds.increment(); if(seconds.getValue() == 0) { // it just rolled over! minutes.increment(); } minutes.increment(); if(minutes.getValue() == 0){ hours.increment(); } updateDisplay(); } Answer 185: No. If you look at the logic of what you have written the following would happen: Seconds would be incremented. If the seconds just rolled over then the minutes would be incremented - that is correct. The minutes are incremented. This is done every time the seconds are incremented - that is clearly not correct because the minutes and second are going at the same rate. In fact, the minutes are going slightly faster because they get an extra increment every 60 seconds. If the minutes just rolled over then the hours are incremented. Since the minutes are going at nearly the same rate as the seconds, the hours are going to be incremented once every 60 seconds, which is clearly not right. Keywords: ex-3-57 Question 184: Submission reference: IN1335 Something like this? (Question 180 (2021) and Question 182 (2021)) I think I may have complicated it (you forgot to delete the code of the previous one) code deleted Answer 184: I didn't forget to delete the code. I left it in place because it wasn't right and so this discussion might be helpful to others. No, the updated code you sent is still not right because you haven't taken on board that the code should not be inside the Screen class. The point of the exercise is to write the code in a different class - actually, all you need is something like this: public class SomeOtherClassThatIsntScreen { public void clearScreenPossibly() { // Write the code here. ... } } Question 183: Submission reference: IN1334 would this be correct for exer3.57, and would it be almost similar to what we have to do in exer3.58? public void timeTick() { seconds.increment(); if(seconds.getValue() == 0) { // it just rolled over! minutes.increment(); hours.increment(); } updateDisplay(); Answer 183: That would not work because the hours are incremented every time the minutes are incremented, which is not how a clock works. You are right that the solution to ex 3.57 leads naturally to a solution to ex 3.58. Keywords: ex-3-57 , ex-3-58 This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. Last modified Mon Jan 17 21:11:18 2022 This document is maintained by David Barnes, to whom any comments and corrections should be addressed.