Starting from:

$30

Assignment 8: Refactoring

Assignment 8: Refactoring
ECE 325 OBJECT-ORIENTED SOFWARE
DES (LEC A1 Fa18)
Assignment 8: Refactoring
. A working copy of your solution must be
submitted to eClass before this date.
For the assignment, you will need to find out information about refactoring patterns, which does not
appear in the lecture slides. The following location is an excellent starting
point:http://www.refactoring.com/catalog/index.html. 1. Consider the following two code fragments.
Which fragment is more understandable? And why? (Hint: Think code smells)
A.
doublepotentialEnergy(double mass, double height) {
return mass * height * 9.81;
}
B.
static final double g = 9.81;
doublepotentialEnergy(double mass, double height) {
return mass * g * height;
Assignment https://eclass.srv.ualberta.ca/mod/assign/view.php?id=3237750
1 of 7 2018-12-22, 9:21 p.m.
}
2. Consider the following code fragment.
publicintfunny(int a, int b) {
int temp = a * b;
if (temp 100) {
return temp * 0.95;
} else {
return temp * 0.25;
}
}
Apply the following refactoring patterns to this code fragment:
A. Inline Temp
B. Extract Method Followed by Replace Temp with Query
C. Discuss the two new code fragments, which refactoring pattern is more appropriate in this
situation? And why?
3. Consider the following two code fragments.
A.
privateint currentBalance;
intwithdrawFromBankAccount(int amountToBeWithdrawn) {
if (amountToBeWithdrawn currentBalance)
return -1;
else {
currentBalance -= amountToBeWithdrawn;
return 0;
}
}
Assignment https://eclass.srv.ualberta.ca/mod/assign/view.php?id=3237750
2 of 7 2018-12-22, 9:21 p.m.
B.
privateint currentBalance;
voidwithdrawFromBankAccount(int amountToBeWithdrawn) throws BalanceException {
if (amountToBeWithdrawn currentBalance)
throw new BalanceException();// You can assume that BalanceException is defined
currentBalance -= amountToBeWithdrawn;
}
Which refactoring pattern has been applied to the first fragment to transform it into the second code
fragment? Explain why the second code fragment is superior to the first fragment.
4. Consider the following two code fragments (adapted from java.lang.Long ).
A.
public static Long valueOf(long l) {
final int offset = 128;
if (l = - 128 && l <= 127) {// will cache
return LongCache.cache[(int) l + offset];
}
return new Long(l);
}
B.
Assignment https://eclass.srv.ualberta.ca/mod/assign/view.php?id=3237750
3 of 7 2018-12-22, 9:21 p.m.
public static Long valueOf(long l) {
if (l = - 128 && l <= 127) {// will cache
return LongCache.cache[(int) l + 128];
}
return new Long(l);
}
Fragment A is translated into Fragment B by applying the Inline Temp refactoring; and Fragment B is
translated into Fragment A by applying the Introduce Explaining Variable refactoring. Provide
possible situations (or contexts) where it would be advantageous to apply either refactoring to the
appropriate code fragment.
5. Consider the following two code fragments.
A.
public class A {
public intk(long i) {
return 10;
}
}
public class B extends A {
public intk(int i) {
return 20;
}
public static voidmain(String[] args) {
System.out.println(new A().k(2));
}
}
B.
Assignment https://eclass.srv.ualberta.ca/mod/assign/view.php?id=3237750
4 of 7 2018-12-22, 9:21 p.m.
public class A {
public intk(long i) {
return 10;
}
public intk(int i) {
return 20;
}
}
public class B extends A {
public static voidmain(String[] args) {
System.out.println(new A().k(2));
}
}
Which refactoring pattern has been applied to the first fragment to transform it into the second code
fragment? Explain the impact (upon the results from function test) of the transformation.
Submission status
Attempt number This is attempt 1 ( 1 attempts allowed ).
Submission status Submitted for grading
Grading status Graded
Due date Thursday, 29 November 2018, 5:00 PM
Time remaining Assignment was submitted 1 day 6 hours early
Last modified Wednesday, 28 November 2018, 10:25 AM
File submissions
ArunWoosaree.java
Export to portfolio
Assignment https://eclass.srv.ualberta.ca/mod/assign/view.php?id=3237750
5 of 7 2018-12-22, 9:21 p.m.
Submission comments
Comments (0)
Edit submission
Make changes to your submission
Feedback
Grade 20.00 / 20.00
Graded on Thursday, 6 December 2018, 1:06 AM
Graded by Mona Nashaat Ali Elmowafy
Assignment https://eclass.srv.ualberta.ca/mod/assign/view.php?id=3237750
6 of 7 2018-12-22, 9:21 p.m.
You are logged in as Arun Woosaree (Log out)
ECE 325 (LEC A1 Fa18)
Help
Email
Assignment https://eclass.srv.ualberta.ca/mod/assign/view.php?id=3237750
7 of 7 2018-12-22, 9:21 p.m.

More products