首页 > > 详细

159.251辅导、辅导Java设计程序

1902/159.251
MTUI DISD
MASSEY UNIVERSITY
MANAWAT¯U AND DISTANCE CAMPUSES
EXAMINATION FOR
159.251 Software Engineering Design and Construction
SEMESTER TWO 2019
Time allowed is THREE (3) hours.
All students are required to answer ALL questions.
There are 9 questions altogether.
Total marks: 52.
Write your answers in the Blue Answer Book supplied.
All relevant working must be shown to gain full marks.
Students may NOT remove any part of this question paper from the exam room.
The exam paper will be made available on the University Library website.
Page 1 of 10 CoS
Short Answers
[23 marks]
1. Version control and versioning
(a) Explain how a normal git workflow works in practice. Use examples (git
command) in your answer. (2 marks)
(b) Git branches are regularly used in projects. Explain how they work into a
team’s git workflow and the best practice usage for them. (2 marks)
[4 marks]
2. Build tools and process automation
(a) Explain the role of build tools such as Maven in the software development
process. Use an example of how Maven helps with automating tasks. (2
marks)
(b) Diamond dependencies occur when two dependencies both depend on a third
project but at a different version. Explain why this is a problem in Java, using
the idea of classloaders. (1 marks)
(c) Explain what a Semantic Version is and how developers should adjust it when
making a new version release. (2 marks)
(d) Explain the pros and cons of choosing version ranges when declaring dependencies.
(1 mark)
[6 marks]
Page 2 of 10 CoS
3. Software Design and Architecture
Most application architectures employ a 3-tier model:
(a) Draw the components of this model and how they link to each other. (2 marks)
(b) Explain how Domain-Driven Design allows this model to function without
having large amounts of duplication across layers. (1 mark)
Coupling and Cohesion
(c) Define what the terms coupling and cohesion are. (2 marks)
(d) Explain their implications to project design and software quality. (1 mark)
(e) Explain how the Interface Segregation Principle (ISP) fits in with the idea of
coupling. (1 marks)
[7 marks]
4. Miscellaneous
(a) Assume you are working on a commercial project. You wish to choose a
dependency that will fulfil a specific need in your project. There are two
options, one dependency with a GPLv2 licence, and the other with a BSD
licence. Explain the legal implications of using each in your project. (3 marks)
(b) Explain the difference in the software workflow between teams using a traditional
waterfall software development methodology versus an agile methodology.
Choose one specific agile methodology and explain it in your example.
(3 marks)
[6 marks]
Page 3 of 10 CoS
Code Quality, Analysis and Refactoring
[29 marks]
5. Design Patterns
Consider the following code
1 public class myClass {
2 private static myClass instance ;
3
4 private myClass () {}
5
6 public static myClass getInstance () {
7 if( instance == null ) {
8 instance = new myClass () ;
9 }
10 return instance ;
11 }
12 }
(a) Identify the design pattern used here. List the necessary features within the
code to implementing this design pattern. (3 marks)
(b) Explain the known pros and cons of using this pattern. Provide an example
of when we would want to use this design pattern. (2 marks)
[5 marks]
Page 4 of 10 CoS
6. Logging and Orthogonality
Consider the following codea
1 Logger logger = Logger . getLogger ("new logger ") ;
2 Appender appender = new ConsoleAppender (new SimpleLayout () ,
ConsoleAppender . SYSTEM_OUT ) ;
3 logger . addAppender ( appender ) ;
4 logger . setLevel ( Level . DEBUG ) ;
5 logger . error (" error (1) ") ;
6 logger . info (" info (1) ") ;
7 logger . warn (" warn (1) ") ;
8 logger . debug (" debug (1) ") ;
9 logger . setLevel ( Level . ERROR ) ;
10 logger . error (" error (2) ") ;
11 logger . debug (" debug (2) ") ;
12 logger . setLevel ( Level . INFO ) ;
13 logger . warn (" warn (2) ") ;
14 logger . debug (" debug (2) ") ;
What will be printed on the console if the following script is executed? (2 marks)
[2 marks]
aLogger, Appender, ConsoleAppender and Level are all classes in org.apache.log4j, the import
statement is omitted.
7. Code smells and Anti-patterns
Consider the following code (two classes, mainClass and smellyClass), which
works fine (no compilation issues). However, the program contains a number of
code smells that need to be refactored.
1 public class mainClass {
2 public int add (int x , int y ) { return x + y ; }
3
4 public int sub (int x , int y ) { return x - y ; }
5
6 public int divide (int x , int y ) { return x / y ; }
7
8 // save the results in a text file
9 public void save (int values ) throws IOException {
10 PrintWriter out = new PrintWriter (" results . txt") ;
11 out . println ( values ) ;
12 out . close () ;
13 }
14 } // end of class
Question 7 Continued Over. . .
Page 5 of 10 CoS
. . . Question 7 Continued:
1 public class smellyClass {
2 public static void main ( String [] args ) throws Exception {
3 foo1 () ;
4 foo2 () ;
5 foo3 () ;
6 }
7
8 // access methods in class mainClass and perform some arithmetic
operations
9 public static void foo1 () throws IOException {
10 int results = 0;
11 int x = 20;
12 int y = 15;
13
14 mainClass mc = new mainClass () ;
15 int r1 = mc . add (x , y ) ;
16 int r2 = mc . sub (x , y ) ;
17 int r3 = mc . divide (x , y) ;
18 results = r1 + r2 + r3 ;
19 mc . save ( results ) ;
20 }
21
22 // same as foo1 , but with different numbers
23 public static void foo2 () throws IOException {
24 int results = 0;
25 int x = 40;
26 int y = 30;
27
28 mainClass mc = new mainClass () ;
29 int r1 = mc . add (x , y ) ;
30 int r2 = mc . sub (x , y ) ;
31 int r3 = mc . divide (x , y) ;
32 results = r1 + r2 + r3 ;
33 mc . save ( results ) ;
34 }
35
36 // take input from users , and save the summation in a text file
as foo1 , but with different numbers
37 public static void foo3 () throws IOException {
38 Scanner i = new Scanner ( System . in ) ;
39 int c = 10;
40 int d = 20;
41
42 if ( c > d ) {
43 System . out . print (" integer : ") ;
44 int a = i . nextInt () ;
45
46 System . out . print (" integer : ") ;
47 int b = i . nextInt () ;
48 mainClass mc = new mainClass () ;
49
50 mc . save ( mc . add (a , b ) ) ;
51 }
52 }
53 }
Page 6 of 10 CoS
Question 7 Continued Over. . .
. . . Question 7 Continued:
(a) Identify the potential code smells in SmellyClass. Explain why these are
code smells and what damage they would potentially do if we left them in the
code. (3 marks)
(b) What refactoring recommendation(s) would you suggest here to improve the
code? Consider providing a short example of a refactoring recommendation
to one or two of the three methods. Answer this together with (c). (3 marks)
(c) Refactor code to be self-documenting using best practice (e.g., variable names,
comments etc..). Use the same answer (one code snippet) for both b and c.
(3 marks)
[9 marks]
Page 7 of 10 CoS
8. Metrics and Testing
Consider the following main production class (Search) which implements a fibonacci
search algorithma
:
1 public class Search {
2 public static int fibonacciSearch (int [] integers , int
elementToSearch ) {
3
4 int fibonacciMinus2 = 0;
5 int fibonacciMinus1 = 1;
6 int fibonacciNumber = fibonacciMinus2 + fibonacciMinus1 ;
7 int arrayLength = integers . length ;
8
9 while ( fibonacciNumber < arrayLength ) {
10 fibonacciMinus2 = fibonacciMinus1 ;
11 fibonacciMinus1 = fibonacciNumber ;
12 fibonacciNumber = fibonacciMinus2 + fibonacciMinus1 ;
13 } // end of while loop
14
15 int offset = -1;
16
17 while ( fibonacciNumber > 1) {
18 int i = Math . min ( offset + fibonacciMinus2 , arrayLength - 1)
;
19 if ( integers [ i ] < elementToSearch ) {
20 fibonacciNumber = fibonacciMinus1 ;
21 fibonacciMinus1 = fibonacciMinus2 ;
22 fibonacciMinus2 = fibonacciNumber - fibonacciMinus1 ;
23 offset = i ;
24 } else if ( integers [ i ] > elementToSearch ) {
25 fibonacciNumber = fibonacciMinus2 ;
26 fibonacciMinus1 = fibonacciMinus1 - fibonacciMinus2 ;
27 fibonacciMinus2 = fibonacciNumber - fibonacciMinus1 ;
28 } else
29 return i ;
30 }// end of while loop
31
32 if ( fibonacciMinus1 == 1 && integers [ offset + 1] ==
elementToSearch ) {
33 return offset + 1; }
34 return -1;
35 }// end of method
36 }
aFibonacci search is a technique of searching a sorted array using a divide and conquer algorithm
that narrows down possible locations with the aid of Fibonacci numbers.
Question 8 Continued Over. . .
Page 8 of 10 CoS
. . . Question 8 Continued:
The following is a test class (SearchTest) for the above algorithm:
1 class SearchTest {
2 private final Search search = new Search () ;
3 int [] listOfNumbers = {1 ,3 ,10 ,21 ,77 ,400 ,401};
4
5 @Test
6 void testFirstElement () {
7 int result = search . fibonacciSearch ( listOfNumbers ,1) ;
8 assertEquals (0 , result ) ;}
9
10 @Test
11 void testLastElement () {
12 int result = search . fibonacciSearch ( listOfNumbers ,401) ;
13 assertEquals (6 , result ) ;}
14
15 @Test
16 void testOutOfBoundary () {
17 // create an out of boundary value and test the value
18 // insert your code here
19 }
20
21 @Test
22 void testUnsortedArray () {
23 // create an unsorted array (int []) and test if the results
would still be valid or invalid
24 // insert your code here
25 }
26 }
(a) Calculate the following from the Search class (all subquestions) and
SearchTest class (for parts iii and iv):
i) Lines of code (physical - no comments). (1 mark)
ii) Cyclomatic complexity. (2 marks)
iii) Statement coverage. (1 mark)
iv) Branch coverage. (1 marks)
(b) Explain any deficiencies in the branch coverage results. (1 mark)
(c) Update the tests to increase the branch coverage (write the currently unimplemented
tests testOutOfBoundary and testUnsortedArray). (4 marks)
[10 marks]
Page 9 of 10 CoS
9. Process Automation and Continuous Integration
There is an existing Maven project with a local and remote git repository set up.
It has the following files which do not yet exist on the repositories:
.travis.yml
.gitignore
foo.java
bar.java
file1.jar
file2.jar
(a) Fill in the currently empty .travis.yml so that your Maven project will be
tested by Travis CI. (1 marks)
(b) Fill in the currently empty .gitignore file so that no JAR files get committed.
(1 marks)
(c) Write a sequence of git commands to make sure that the above files are included
in both your local and remote repositories. (1 mark)
[3 marks]
+ + + + + + + +
Page 10 of 10 CoS

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!