辅导COMP172101、Programming留学生讲解、辅导Java语言、辅导Java编程设计
- 首页 >> 其他 COMP172101
This question paper consists
of 5 printed pages, each
of which is identified by the
Code Number COMP172101.
This is an open book examination.
Any written or printed material is permitted.
c UNIVERSITY OF LEEDS
School of Computing
May/June 2017
COMP1721
Object-Oriented Programming
Answer all three questions
Time allowed: 2 hours
Page 1 of 5 TURN OVER FOR QUESTIONSCOMP172101
Question 1
A programmer creates the Java class shown in Figure 1 on page 3, to represent a bank
account in which balance must always be greater than zero.
The questions that follow concern this class. You may include small fragments of code
in your answers where appropriate, but keep in mind that full marks can be obtained with
purely descriptive answers, if they are sufficiently accurate and detailed. Note also that you
will 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. Explain
the 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 observation
that “the constructor, the deposit method and the withdraw method are OK, but they
should really be given exception specifications.”
What does the developer mean by this observation? Is this a sensible criticism to make
of 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 the
message “Sorry, that amount of cash is not valid” printed when the circumstances
in (i) apply. [3 marks]
(e) Explain why it might be useful to alter line 3 of the class definition so that balance is
declared as protected rather than private. [2 marks]
(f) A programmer wishes to produce a variant of BankAccount in which the minimum
value allowed for a deposit is 100. Explain in detail how she could achieve this in an
efficient manner. [4 marks]
[Question 1 total: 20 marks]
CONTINUED ON FACING PAGE Page 2 of 5COMP172101
1 class BankAccount {
2 private String holder;
3 private int balance;
4
5 public BankAccount(String holder, int balance) {
6 if (balance <= 0) {
7 throw new IllegalArgumentException("invalid balance");
8 }
9 this.holder = holder;
10 this.balance = balance;
11 }
12
13 public String getAccountHolder() {
14 return holder;
15 }
16
17 public int getBalance() {
18 return balance;
19 }
20
21 public void deposit(int amount) {
22 if (amount <= 0) {
23 throw new IllegalArgumentException("invalid amount");
24 }
25 balance += amount;
26 }
27
28 public void withdraw(int amount) {
29 if (amount <= 0) {
30 throw new IllegalArgumentException("invalid amount");
31 }
32 balance -= amount;
33 }
34 }
Figure 1: BankAccount class used in Question 1
Page 3 of 5 TURN OVERCOMP172101
Question 2
(a) A programmer decides to use Java instead of C in a project, partly because “errors are
handled 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 do
with “the cleaner structure and improved reusability of the code, and the way it better
models 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, rather
than an ArrayList. Why is this not a problem? What, in general, is the benefit
of doing this? [3 marks]
(iii) What are the circumstances under which ArrayList is an appropriate choice of
implementation for this particular list? [3 marks]
(d) A computer science student is given the task of writing Java software to tally votes in
a Student Union election. He decides to represent candidate names as a list of strings
and 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 the
task, explaining clearly why it is superior. Give an example of the code needed to
create an instance of this data structure. [4 marks]
[Question 2 total: 20 marks]
CONTINUED ON FACING PAGE Page 4 of 5COMP172101
Question 3
(a) The UML diagram below shows part of the object-oriented design for a mobile app that
manages data from the training activities of runners and cyclists.
Write down the Java code implied by this class diagram. Focus on classes and their
fields; do not speculate about methods or their implementations. Assume that the class
DateTime 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 of
relationship between these two classes and those shown in (a)? Draw a simple UML
class diagram to illustrate your answer. [4 marks]
(c) A software developer suggests altering the UML diagram in (a) so that the name of the
Activity 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 value
of 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 generate
more sensible and useful output? [3 marks]
(e) The same programmer now implements the Measurement class. She decides it would
be useful if Measurement objects could be sorted into chronological order. Describe
how this feature could be added to the class. [3 marks]
[Question 3 total: 20 marks]
[Grand total: 60 marks]
Page 5 of 5 END
This question paper consists
of 5 printed pages, each
of which is identified by the
Code Number COMP172101.
This is an open book examination.
Any written or printed material is permitted.
c UNIVERSITY OF LEEDS
School of Computing
May/June 2017
COMP1721
Object-Oriented Programming
Answer all three questions
Time allowed: 2 hours
Page 1 of 5 TURN OVER FOR QUESTIONSCOMP172101
Question 1
A programmer creates the Java class shown in Figure 1 on page 3, to represent a bank
account in which balance must always be greater than zero.
The questions that follow concern this class. You may include small fragments of code
in your answers where appropriate, but keep in mind that full marks can be obtained with
purely descriptive answers, if they are sufficiently accurate and detailed. Note also that you
will 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. Explain
the 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 observation
that “the constructor, the deposit method and the withdraw method are OK, but they
should really be given exception specifications.”
What does the developer mean by this observation? Is this a sensible criticism to make
of 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 the
message “Sorry, that amount of cash is not valid” printed when the circumstances
in (i) apply. [3 marks]
(e) Explain why it might be useful to alter line 3 of the class definition so that balance is
declared as protected rather than private. [2 marks]
(f) A programmer wishes to produce a variant of BankAccount in which the minimum
value allowed for a deposit is 100. Explain in detail how she could achieve this in an
efficient manner. [4 marks]
[Question 1 total: 20 marks]
CONTINUED ON FACING PAGE Page 2 of 5COMP172101
1 class BankAccount {
2 private String holder;
3 private int balance;
4
5 public BankAccount(String holder, int balance) {
6 if (balance <= 0) {
7 throw new IllegalArgumentException("invalid balance");
8 }
9 this.holder = holder;
10 this.balance = balance;
11 }
12
13 public String getAccountHolder() {
14 return holder;
15 }
16
17 public int getBalance() {
18 return balance;
19 }
20
21 public void deposit(int amount) {
22 if (amount <= 0) {
23 throw new IllegalArgumentException("invalid amount");
24 }
25 balance += amount;
26 }
27
28 public void withdraw(int amount) {
29 if (amount <= 0) {
30 throw new IllegalArgumentException("invalid amount");
31 }
32 balance -= amount;
33 }
34 }
Figure 1: BankAccount class used in Question 1
Page 3 of 5 TURN OVERCOMP172101
Question 2
(a) A programmer decides to use Java instead of C in a project, partly because “errors are
handled 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 do
with “the cleaner structure and improved reusability of the code, and the way it better
models 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
(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, rather
than an ArrayList. Why is this not a problem? What, in general, is the benefit
of doing this? [3 marks]
(iii) What are the circumstances under which ArrayList is an appropriate choice of
implementation for this particular list? [3 marks]
(d) A computer science student is given the task of writing Java software to tally votes in
a Student Union election. He decides to represent candidate names as a list of strings
and 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 the
task, explaining clearly why it is superior. Give an example of the code needed to
create an instance of this data structure. [4 marks]
[Question 2 total: 20 marks]
CONTINUED ON FACING PAGE Page 4 of 5COMP172101
Question 3
(a) The UML diagram below shows part of the object-oriented design for a mobile app that
manages data from the training activities of runners and cyclists.
Write down the Java code implied by this class diagram. Focus on classes and their
fields; do not speculate about methods or their implementations. Assume that the class
DateTime 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 of
relationship between these two classes and those shown in (a)? Draw a simple UML
class diagram to illustrate your answer. [4 marks]
(c) A software developer suggests altering the UML diagram in (a) so that the name of the
Activity 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 value
of 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 generate
more sensible and useful output? [3 marks]
(e) The same programmer now implements the Measurement class. She decides it would
be useful if Measurement objects could be sorted into chronological order. Describe
how this feature could be added to the class. [3 marks]
[Question 3 total: 20 marks]
[Grand total: 60 marks]
Page 5 of 5 END