This document contains an examination paper for an Advanced Programming course taken in May 2013. It provides instructions for students taking the exam, including answering two of three questions, with each question worth 50 marks. It then lists the three exam questions. Question 1 involves explaining the purpose and features of a version control system to a small software company not currently using one. It also involves analyzing code for unit testing a rectangle area calculation method. Question 2 covers the advantages of generic classes in Java and modifying code to make a class generic. Question 3 is about test-driven development and converting test code to JUnit tests.
This document contains an examination paper for an Advanced Programming course taken in May 2013. It provides instructions for students taking the exam, including answering two of three questions, with each question worth 50 marks. It then lists the three exam questions. Question 1 involves explaining the purpose and features of a version control system to a small software company not currently using one. It also involves analyzing code for unit testing a rectangle area calculation method. Question 2 covers the advantages of generic classes in Java and modifying code to make a class generic. Question 3 is about test-driven development and converting test code to JUnit tests.
This document contains an examination paper for an Advanced Programming course taken in May 2013. It provides instructions for students taking the exam, including answering two of three questions, with each question worth 50 marks. It then lists the three exam questions. Question 1 involves explaining the purpose and features of a version control system to a small software company not currently using one. It also involves analyzing code for unit testing a rectangle area calculation method. Question 2 covers the advantages of generic classes in Java and modifying code to make a class generic. Question 3 is about test-driven development and converting test code to JUnit tests.
This document contains an examination paper for an Advanced Programming course taken in May 2013. It provides instructions for students taking the exam, including answering two of three questions, with each question worth 50 marks. It then lists the three exam questions. Question 1 involves explaining the purpose and features of a version control system to a small software company not currently using one. It also involves analyzing code for unit testing a rectangle area calculation method. Question 2 covers the advantages of generic classes in Java and modifying code to make a class generic. Question 3 is about test-driven development and converting test code to JUnit tests.
1. (a) Imagine that you have just started working for a small software development company. You ask your manager what version control system (VCS) they use. She replies that they don't use any because they don't see the point of version control as they are a small company and do backups every night. How would you explain why they should consider using a VCS? Your answer should include the overall purpose of a VCS and the main features that you would expect of a VCS. Also include some examples of the types of problems that might occur if they continue without using a VCS.
[20 marks]
[5 marks] The overall purpose of a VCS is to manage, track the doc changes to the SW and potentially are the project artifacts. So that, for example, changes can be undone if necessary any conflicted updates by different developers can be resolved and any different branches of sw can be maintained.
Main features [12 marks] i) Ability to revert to any past version of the sw ii) Ability to record reason for changes so you can find out particular changes were made and why iii) Ability to have different lines of development which can be remerged if necessary iv) Allow multiple ppl to work on the same sw. In particular making ppl aware of who is working on which piece of sw and helping to integrate changes
Examples: [3 marks] a) 2 ppl might take a copy of a program to work on it. One may fix a bug and another add a new feature. The changes made by one of the ppl will be lost when the sw is copied back to the central store. b) A bug maybe introduce by a change made to the sw. it may be difficult to revert to the latest working version of the sw before the change was made.
(b) Study the two Java classes given below. package junitq;
public class Utils {
private static boolean isValidRectangle(int a, int b) { return ((a > 0) && (b > 0));
} /**
* Calculate the area of a rectangle
* @param a length of one side of the rectangle
* @param b length of the other side of a rectangle
* @return if a and b define a valid rectangle return the * area of the rectangle i.e. a x b otherwise throw
* an IllegalArgumentException */
public static int calcRectangleArea(int a, int b) { if (!isValidRectangle(a, b)) { throw new IllegalArgumentException("invalid rectangle");
} return (a * b);
} }
package junitq;
import static junitq.Utils.*;
public class Tester {
public static void main(String[] args) { System.out.println("calcRectangleArea(1, 1000) = "
(ii) Class Tester partially automates unit testing of the method calcRectangleArea() in class Utils. However, it suffers from the serious disadvantage that the person running the test has to check the output and work out if each test passed or failed.
Explain how JUnit helps to solve this problem. To help illustrate your answer convert the three tests in class Tester into three JUnit 4 tests. Explain the use of the @Test annotation and the assertEquals() methods in your answer.
[12 marks]
it provides support for creating and running automated test that explicit tell you if test pass or fail
@Test //identify a test method to be run by JUnit Public void test1(){ assertEquals(1000, calcRectangleArea(1,1000)); } //assertEquals() a method that will cause a test to fail unless it two parameter are equal @Test Public void test2(){ assertEquals(1000, calcRectangleArea(1000,1)); } @Test Public void test3(){ assertEquals(10000, calcRectangleArea(100,100)); } (iii) There is one important possible outcome of calling calcRectangleArea() that the code is class Tester does not test. Identify this result and write a JUnit test to check for it.
[5 marks] The outcome as being the throwing of an illegal argument exception if the two parameters dont define a valid rectangle.
@Test(expected = IllegalArgumentException.class) Public void test4(){ Int result = calcRectangleArea(0,100); }
OR
@Test Public void test5(){ Try{ Int result = calcRectangleArea(0,100); Fail (Should have thrown IllegalArgumentException); }catch (IllegalArgumentException iae){ } }
(c) JUnit is often used as part of Test-driven Development. Explain what is meant by the term Test-driven Development and outline the Test- driven development cycle.
[10 marks] it is a technic where by programming proceeds in very small increment with an automated test for each addition to the program being return before the code to satisfy the test.
The cycle: 1) to create the test for the new feature or improvement to be added to the program 2) run all the tests created so far. The new test should fail. 3) Write code that should make the test pass. Only write sufficient code for the test to pass 4) Run all test created so far. Now they should all pass. If not go back to step 3 and fix the code. 5) Refactor the code. Make sure all the test still pass Start cycle again
2. (a) Explain the advantages of generic classes in Java. [6 marks]
Generic classes enable the compiler to check that the class is being used with the correct types. This is a major advantage for classes such as containers where w/o generic they have to work with class object to be flexible. This means that type conversion errors could only be detected at run time.
Generic allow simplification of coding because casting object back to their original type when accessing them in a generic class is not needed.
(b) Study the two Java classes given below. Assume that any necessary package statements and imports have been included. Line numbers have been included in case you wish to refer to them in your answer.
3. Point p = new Point(10, 45); 4. System.out.println(p); 5. }
6. }
(i) What will be output when the main() method in class GenQ is run?
[2 marks]
10, 45 (ii) Give the code changes necessary to convert Point into a generic class so that the variables x and y are both of the generic type T. Add lines the main() method of GenQ to show that class Point will work with various types including Strings and Integers.
[7 marks]
public class point <T> { private T x; private T y; public point (T x, T y){ this.x = x; this.y = y; } @override Public string toString() { Return (+x+,+y+); } Public T getX(){ Return x; } Public void setX (T x){ This.x = x; } Public T getY(){ Return y; } Public void setY(T y){ This.y = y; } }
(iii) Explain what is meant by a bounded type and the purpose of declaring a class where the type parameter is a bounded type.
Illustrate your answer by giving the code changes necessary to class Point so that type T has an upper bound of Number. What effect will this have on the lines you added to GenQ for part (ii) above?
[9 marks]
A bounded type is a parameterized type that is limited. For example, a class for manipulating number might want the type it deal with restricted to subclass of type number. That way code within the class can make use of the method of class number rather than be restricted to those of class object.
Public class Point <T extends number> The attempt to use string as the parameterize type will now fail.
(c) Study the Java program given below. Line numbers have been included so that you can refer to them in your answer.
1. class Toothbrush {
2. public void brushTeeth() {
3. for (int i = 0; i < 100; i++) { 4. if (i % 2 == 0) { 5. System.out.println("up"); 6. } else { 7. System.out.println("down"); 8. } 9. try { 10. Thread.sleep(5); 11. } catch (InterruptedException ex) { } 12. }
30. System.out.println("*** first line ***"); 31. Toothbrush t = new Toothbrush();
32. Person alfie = new Person("alfie", t); 33. Person kat = new Person("kat", t); 34. alfie.start();
35. kat.start(); 36. System.out.println("*** last line ***");
37. } 38. }
(i) Give sample output that the program could produce when run. Comment on the output in detail and identify ways in which the output may differ from that which you give.
[16 marks]
***first line*** ***last line*** alfie starts brushing up kat starts brushing up down down up up down down up up down down alfie finishes brushing kat finishes brushing
(ii) Explain the purpose of the keyword synchronized in Java. Illustrate your answer by discussing what would happen if it were added to the declaration of the method brushTeeth() in the code above.
[10 marks]
Synchronized is used to lock access to block of code so that only one threat may execute it at a time. Thus, thread sharing an object can be prevented from interfering with each other while executing sensitive code.
If synchronized was added to the method brushTeeth(): Alfie or Kat could execute brushTeeth() at a time while the other waited to get the lock. This will ensure the up and down outputs alternate Identifying some non-obvious eg that last line will still come out before the brushing completes or that the 2 nd persons start brushing statement will appear while the other person is brushing
3. (a) Study the Java code given below. It consists of one interface and four classes. It is prototype code written by a programmer who is developing a library of code to allow access to location information. Line numbers have been included in case you wish to refer to them in your answer.
1. interface LocationSource {
2. public double getLatitude();
3. }
4. class GPS implements LocationSource {
5. public double getLatitude() {
6. System.out.println("getLatitude() in GPS"); 7. return 51.48;
8. }
9. } 10. class Network implements LocationSource {
11. public double getLatitude() {
12. System.out.println("getLatitude() in Network");
13. return 48.85;
14. }
15. } 16. class LocationGetter {
17. public static LocationSource getLocationSource( 18. boolean indoors, boolean lowBattery) { 19. if (indoors || lowBattery) {
20. return new Network(); 21. } else {
22. return new GPS(); 23. } 24. }
25. } 26. public class PatQ {
27. public static void main(String[] args) { 28. LocationSource a = LocationGetter.getLocationSource(true, true); 29. double lata = a.getLatitude();
(i) What will be output when the main() method in class PatQ is run? Explain your answer.
[9 marks]
getLatitude() in Network 48.85 getLatitude() in Network 48.85 getLatitude() in GPS 51.48
(ii) The above code is a very simple example of a well-known Design Pattern. Give the name of the pattern. Give the standard UML class diagram for the pattern. Explain the main benefit of the pattern with reference to the example above.
[14 marks]
Design pattern = factory
factory class diagram Factory - solution UML.pptx
Benefit of factory pattern a) A new type of product can be added without most of the system needing to be changed b) A new class implementing LocationSource could be added of the existing implementation swapper
(iii) Having developed the prototype the programmer decides that there should be at most one instance of the classes GPS and Network at any one time. Which additional pattern could be implemented to ensure this? Give the code changes required classes GPS, Network and LocationGetter to implement this pattern.
[14 marks]
Class GPS implements LocationSource{ Private static GPS obj = null; Private GPS(){ Obj = new GPS(); } Public static synchronized GPS getInstance(){ If(obj == null){ Obj=new GPS(); } return obj; } @override Public double getLatitude(){ System.out.println(getLatitude() in GPS); Return 51.48; } }
Class Network implements LocationSource{ Private static Network obj = null; Private Network() { Obj = new Network(); } Public static synchronized Network getInstance(){ If(obj == null) { Obj = new Network(); } Return obj; } @override Public double getLatitude() { System.out.println (getLatitude() in Network); Rertun 48.85; } }
Class LocationGetter { Public Static LocationSource getLocationSource (Boolean indoors, Boolean lowBattery) { If (indoors || lowBattery) { Return Network.getInstance(); } else { Return GPS.getInsance(); } } }
(b) Design Patterns are one form of reuse. Identify and describe one other form of reuse which we have studied on the course and compare and contrast it with the use of design patterns.
[13 marks]
javabean Inheritance
The comparison point 1) Compile code vs design knowledge 2) Level of abstraction involved 3) The life cycle that it applied in. Design pattern it may be identified in earliest stages.