美文网首页
讲解:COMP172101、Programming、Java、J

讲解:COMP172101、Programming、Java、J

作者: bingernai | 来源:发表于2020-01-10 12:51 被阅读0次

    COMP172101This question paper consistsof 5 printed pages, eachof which is identified by theCode Number COMP172101.This is an open book examination.Any written or printed material is permitted.c UNIVERSITY OF LEEDSSchool of ComputingMay/June 2017COMP1721Object-Oriented ProgrammingAnswer all three questionsTime allowed: 2 hoursPage 1 of 5 TURN OVER FOR QUESTIONSCOMP172101Question 1A programmer creates the Java class shown in Figure 1 on page 3, to represent a bankaccount in which balance must always be greater than zero.The questions that follow concern this class. You may include small fragments of codein your answers where appropriate, but keep in mind that full marks can be obtained withpurely descriptive answers, if they are sufficiently accurate and detailed. Note also that youwill not be penalised for minor errors of syntax in code fragments.(a) This class will not behave as required, due to a bug in one of the methods. Explainthe nature of this bug. Explain carefully how the code should be changed to fix it.[3 marks](b) A programmer tries to create a BankAccount object in his program like this:BankAccount acc = new BankAccount(John Smith);What would he see when he tries to compile this code? Why would he see this?[2 marks](c) A software developer reviewing the implementation of this class makes the observationthat “the constructor, the deposit method and the withdraw method are OK, but theyshould really be given exception specifications.”What does the developer mean by this observation? Is this a sensible criticism to makeof the code? Explain your reasoning. [4 marks](d) A program using the class contains the following piece of code:myAccount.deposit(cash);System.out.println(Cash deposited);(i) Under what circumstances would the program user not see the “Cash deposited”message? Explain your reasoning. [2 marks](ii) Describe what would need to be added to this code in order for the user to see themessage “Sorry, that amount of cash is not valid” printed when the circumstancesin (i) apply. [3 marks](e) Explain why it might be useful to alter line 3 of the class definition so that balance isdeclared as protected rather than private. [2 marks](f) A programmer wishes to produce a variant of BankAccount in which the minimumvalue allowed for a deposit is 100. Explain in detail how she could achieve this in anefficient manner. [4 marks][Question 1 total: 20 marks]CONTINUED ON FACING PAGE Page 2 of 5COMP1721011 class BankAccount {2 private String holder;3 private int balance;45 public BankAccount(String holder, int balance) {6 if (balance 7 throw new IllegalArgumentException(invalid balance);8 }9 this.holder = holder;10 this.balance = balance;11 }1213 public String getAccountHolder() {14 return holder;15 }1617 public int getBalance() {18 return balance;19 }2021 public void deposit(int amount) {22 if (amount 23 throw new IllegalArgumentException(invalid amount);24 }25 balance += amount;26 }2728 public void withdraw(int amount) {29 if (amount 30 throw new IllegalArgumentExc代写COMP172101作业、Programming留学生作业代做、代写Java语言作业、代写Java编程设计作业 代写eption(invalid amount);31 }32 balance -= amount;33 }34 }Figure 1: BankAccount class used in Question 1Page 3 of 5 TURN OVERCOMP172101Question 2(a) A programmer decides to use Java instead of C in a project, partly because “errors arehandled better and debugging is easier”.With reference to the features and the programming environments of both languages,explain why the programmer might have arrived at this conclusion. [4 marks](b) The programmer in (a) notes that her main reason for choosing Java over C has to dowith “the cleaner structure and improved reusability of the code, and the way it bettermodels the problem domain”.Explain carefully what she means by this statement. [4 marks](c) The questions that follow concern this statement from a Java program:List data = new ArrayList();(i) The author of this code has made a typing error that will prevent it from compiling.Why does the problem occur? What change is needed to fix it? [2 marks](ii) The statement refers to a List on the left hand side of the assignment, ratherthan an ArrayList. Why is this not a problem? What, in general, is the benefitof doing this? [3 marks](iii) What are the circumstances under which ArrayList is an appropriate choice ofimplementation for this particular list? [3 marks](d) A computer science student is given the task of writing Java software to tally votes ina Student Union election. He decides to represent candidate names as a list of stringsand the corresponding total votes for each candidate as a list of integers.Explain the drawbacks of this approach. Propose a better Java data structure for thetask, explaining clearly why it is superior. Give an example of the code needed tocreate an instance of this data structure. [4 marks][Question 2 total: 20 marks]CONTINUED ON FACING PAGE Page 4 of 5COMP172101Question 3(a) The UML diagram below shows part of the object-oriented design for a mobile app thatmanages data from the training activities of runners and cyclists.Write down the Java code implied by this class diagram. Focus on classes and theirfields; do not speculate about methods or their implementations. Assume that the classDateTime is available in a package called java.time. [7 marks](b) The app in (a) also has classes called Run and BikeRide. What is the ideal form ofrelationship between these two classes and those shown in (a)? Draw a simple UMLclass diagram to illustrate your answer. [4 marks](c) A software developer suggests altering the UML diagram in (a) so that the name of theActivity class is in italics. Why has he made this suggestion? [3 marks](d) A programmer implementing the Location class from (a) tries printing out the valueof a Location object. She sees output like the following:Why does she see this? What would she have to do in order for printing to generatemore sensible and useful output? [3 marks](e) The same programmer now implements the Measurement class. She decides it wouldbe useful if Measurement objects could be sorted into chronological order. Describehow this feature could be added to the class. [3 marks][Question 3 total: 20 marks][Grand total: 60 marks]Page 5 of 5 END转自:http://www.7daixie.com/2019052140782336.html

    相关文章

      网友评论

          本文标题:讲解:COMP172101、Programming、Java、J

          本文链接:https://www.haomeiwen.com/subject/cuvpactx.html