Thursday 30 April 2020, 14:30 BST
(24 hour open online assessment – Indicative duration 2 hours)
DEGREES of MSc in Information Technology, MSc in Software Development and MSc in IT Cyber Security
PROGRAMMING
Copyright By PowCoder代写 加微信 powcoder
COMPSCI 4039 (Answer all 4 questions.)
This examination paper is worth a total of 60 marks
For each of the following code snippets, write the output that the code would produce and describe why the output is as it is, paying particular attention to the looping criteria: how i is initialized, how and when it is changed, and how the loop terminates.
for(int i=0;i<5;i++) {
System.out.println(i);
for(int i=1;i<7;i*=2) {
System.out.println(i);
int i = 0;
while(i<3) {
System.out.println(i++);
int i = 3;
while(i<5) {
System.out.println(++i);
Summer Diet
-1- Continued Overleaf/
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24.
int cp = le+(ri-le)/2; int m1 = go(a,le,cp); int m2 = go(a,cp,ri); if(m1 > m2){
return m1; }else {
return m2; }
int i = 5; do {
System.out.println(i);
}while(i<4);
Examine the following class and answer the questions below:
public class Mystery { public static int c = 0;
public static void main(String[] args) { int[] a = {8,1,-3,12,4};
System.out.println(go(a,0,a.length)); }
public static int go(int[] a,int le,int ri) { c += 1;
if(ri - le <= 1) { return a[le];
Summer Diet
-2- Continued Overleaf/
The main method passes an array of type int to the method go. From examining the code, deduce what useful property of the array the method go returns.
This program makes use of a particular programming style described in the course (see e.g. line 15). What is it?
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29.
Study the following class and then answer the questions below.
Describe how the program achieves its goal. In particular, step through the different calls to the go method, in the order in which they happen, and what the method returns. You do not need to go through all the calls (there will be 9 in this example), just enough to be able to describe how the program works.
If the input array was changed to {1,4,10,3,10} which position in the array would the value returned by the first call to go (line 5) correspond to? Justify your answer. The first position is position zero.
The program currently only works with int arrays. Describe any modifications you would make to the original code that would allow it to work with any type for which is possible to compare two elements.
Summer Diet
-3- Continued Overleaf/
public class ExamQ3 { private int a;
public ExamQ2(int b) { a = b;
private int getA() {
return a; }
private void setA(int b) { a = b;
public String toString() {
return "" + a; }
public static void add3(int a) { a += 3;
public static void add3Obj(ExamQ2 e) {
e.setA(3 + e.getA()); }
public static void main(String[] args) { inta=5;
ExamQ2 ee = new ExamQ2(a); add3(a); System.out.println(a); add3Obj(ee); System.out.println(ee.getA());
The four lines starting from line 24 contain two calls to methods that add 3 to an integer. State the outputs at lines 25 and 27. Describe in detail why you believe the outputs are as you have stated.
A local school awards pupils with points for performing good deeds in the class (tidying up, helping others, etc). At the end of each week, the pupil with the highest number of points wins a prize. You are part of a team that has been asked by the school to help them write a program to keep track of the points each week and why they were awarded. The program stores the current points in a text file, in which each line corresponds to one instance of a pupil having been awarded points (i.e. each pupil may appear in multiple lines):
What is the meaning of private on line 2?
Line 9 contains a method signature. Copy the signature, and label each of its
components, describing their meaning.
Overriding, overloading, interfaces, superclasses and subclasses are all concepts and phenomena that we use when building object oriented programs. One is used in this example. State which one, and on which line.
Describe all of the things being created on line 23.
(and the fields are separated by spaces). When the program is run, it first loads this file, storing the data, and then waits for the user to type an input. There are three types of input:
The user can add points to a student by typing:
ADD
The user can reset all pupils to having no points with:
Or list all pupils in descending order of total points with:
Summer Diet -4- Continued Overleaf/
(a) The team decides to solve this problem with a solution that includes a class Pupil, objects of which will represent each pupil and a class AwardedPoints that will represent a particular instance of points being awarded to a student. The Pupil class should store references to all instances of points awarded to that pupil. Provide code for a suitable AwardedPoints class. Justify all attributes and methods you include based on the problem description above. It should be possible to make an object with or without a String argument representing
(b) Provide code for a suitable Pupil class. Justify any methods you include based on the problem description above. You can assume that any particular Pupil will never be awarded points more than 100 times.
(c) Two Pupil objects will be defined as equal if they share the same first and second names. Write a .equals(Object o) method for your class that meets this specification. You can assume throughout that two different pupils never share both a first and second name.
(d) It is decided to implement the ADD command by creating a new temporary Pupil object with the data entered by the user in the ADD command. This temporary Pupil object will then be compared against all pupils in the array to find a match using the .equals method (from part (d)). Write a method that takes as its arguments the Pupil array, a reference to an individual Pupil object and to an AwardedPoints object. The method should find the entry in the array that matches the individual Pupil object and add the AwardedPoints object to the pupil. If the pupil does not exist in the array, it should be added to the array.
(e) Write code to read the data file and store the results into an array of Pupil objects. You do not need to include exception handling and you can assume that the data file is always correctly formatted. The first line of the file corresponds to the first pupil (i.e. there are no headers in the file). You can assume there will never be more than 100 pupils.
(f) AssumethatanotherteammemberhasprovidedacompareTomethodallowingyouto use Arrays.sort to sort your Pupil array so that the highest scoring pupil is in the first position. Write code that will prompt the user for input, parse the input, decide upon the relevant action and then perform that action. You can assume that the user always enters the data correctly.
Summer Diet -5- Continued Overleaf/
(g) Other team members are programmers but are not experienced in object oriented (OO) programming. Describe to them four benefits of an OO approach for this application. Be as specific as possible, ensuring that you make reference to features of this particular program. As they are programmers, you can assume that they are familiar with concepts like variables and methods. Each benefit should require no more than one or two sentences.
(h) Within the team, before development you discussed two possible solutions. One that consisted of just a Pupil class and the one that was ultimately chosen consisting of separate classes to represent Pupils and AwardedPoints. Describe why the second option is better. Ensure your answer is specific to this application, and not an argument for object orientation in general.
Summer Diet -6- /END
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com