"Hi, Amigo!"
"Hello, Captain Squirrels, sir!"
"Do you smell that, soldier?"
"No, I don't."
"The smell is most likely coming from your terminal. Let's see what you're doing there."
"Huh? How can code stink?"
"True, it definitely can't stink... but it often has an odor."
"Captain, I'm developing a new personnel management system for the ship. Look, the project is almost ready."
"Well, it's time for you to achieve Zen in refactoring. Only then will you be able to cleanse your code. Contact Agent IntelliJ IDEA for your assignment. He'll give you all the instructions."
"Yes, sir! Carrying out your order, sir."
14
Task
Java Multithreading,
level 5,
lesson 16
Locked
Refactoring (part 1)
It's time for a little refactoring. Wikipedia says, "Refactoring
or reorganizing code is the process of changing the internal structure of a program, without
affecting its external behavior and with an aim to making it easier to understand.
14
Task
Java Multithreading,
level 5,
lesson 16
Locked
Refactoring (part 2)
2.1. Extract a subclass.
2.1.1. Add the Soldier class to the human package.
2.1.2. Get rid of the isSoldier field.
2.1.3. Move the necessary methods from Human to Soldier.
2.1.4. Update the Human constructor's signature.
2.2. Pull up the body of the constructor.
7
Task
Java Multithreading,
level 5,
lesson 16
Locked
Refactoring (part 3)
3.1. Push down a field. Push down the course field to the appropriate class. Make it private.
3.2. Push down a method. Push down the getter for the course field to the appropriate class.
3.3. Extract an interface.
3.3.1. Create an Alive interface in the human package.
14
Task
Java Multithreading,
level 5,
lesson 16
Locked
Refactoring (part 4)
4.1. Replace inheritance with delegation.
4.1.1. The University class must not inherit Student.
4.1.2. The University class must have a students list. Don't forget to initialize it.
4.1.3. Add a setter and getter for students.
14
Task
Java Multithreading,
level 5,
lesson 16
Locked
Refactoring (part 5)
5.1. Create a template method.
5.1.1. Add a String getPosition() method to the Human class. It should return the string "Person".
5.1.2. Override this method in the Student and Teacher classes. The method should return "Student" and "Teacher", respectively.
28
Task
Java Multithreading,
level 5,
lesson 16
Locked
Refactoring (part 6)
6.1. Replace a parameter with a set of specialized methods. Replace the Student class's setValue() method
with specialized setCourse() and setAverageGrade() methods.
6.2. Add a parameter. Add a double parameter to the getStudentWithAverageGrade() method so the average grade of the required student.
14
Task
Java Multithreading,
level 5,
lesson 16
Locked
Refactoring (part 7)
7.1. Parameterize a method. Replace the incAverageGradeBy01() and incAverageGradeBy02() methods
with an incAverageGrade(double delta) method.
7.2. Pass an entire object. Rewrite the addStudentInfo() method so that it
takes a Student object as an argument.
7
Task
Java Multithreading,
level 5,
lesson 16
Locked
Refactoring (part 8)
8.1. Remove a setter. Remove the setId() method. The id field should be set only when
the object is created.
8.2. Hide a method (field). Change the visibility of the nextId field to match
the area where it is used.
28
Task
Java Multithreading,
level 5,
lesson 16
Locked
Refactoring (part 9)
9.1. Self-encapsulate a field. Rewrite the incAverageGrade() method, using a setter and getter to access averageGrade.
9.2. Replace an array field with an object. Replace the int[] size array with an instance of a new Size type,
containing the following public fields: int height and int weight.
14
Task
Java Multithreading,
level 5,
lesson 16
Locked
Refactoring (part 10)
Understand the code in the car package.
10.1. Replace a constructor with a factory method.
10.1.1. Declare Truck, Sedan, and Cabriolet classes that inherit Car.
10.1.2. Add constructors that have an int numberOfPassengers constructors.
14
Task
Java Multithreading,
level 5,
lesson 16
Locked
Refactoring (part 11)
11.1. Replace an error code with an exception. Rewrite the fill(double numberOfGallons)
method to throw an Exception if there is an error.
11.2. Decompose a conditional expression.
11.2.1. Add and implement a method in the Car class that determines whether the passed date belongs to
summer.
14
Task
Java Multithreading,
level 5,
lesson 16
Locked
Refactoring (part 12)
12.1. Combine conditional expressions.
12.1.1. In the Car class, add a private method that indicates whether passengers can be carried:
boolean canPassengersBeCarried(). The method should return true if
the driver is available (isDriverAvailable) and there is fuel.
28
Task
Java Multithreading,
level 5,
lesson 16
Locked
Refactoring (part 13)
Understand the code in the user package.
13.1. Extract a method. Add a printInfo() method that displays the first and last name
on the console, formatted as follows -
First name: Jason;
Last name: Slater.
Replace the duplicate code in printUsers() with a method call.
14
Task
Java Multithreading,
level 5,
lesson 16
Locked
Refactoring (part 14)
14.1. Move a field. Replace the isAnnaMale and isRomanMale fields with a male field in the appropriate class.
Add a setter and getter for the new field (when choosing method names, consider the field type).
14.2. Extract a class.
14.2.1. Add an Address class to the user package.
7
Task
Java Multithreading,
level 5,
lesson 16
Locked
Refactoring (part 15)
Great. You've mastered basic refactoring techniques: Pull up a field, pull up a method, Encapsulate a collection, extract a subclass, pull up the body of a constructor, push down a method, push down a field, extract an interface, collapse a hierarchy, replace inheritance with delegation.
GO TO FULL VERSION