CodeGym /Java Course /Java Multithreading /Big task: Java refactoring

Big task: Java refactoring

Java Multithreading
Level 5 , Lesson 16
Available

"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."

Big task: Java refactoring - 1
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.
Comments (19)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
catalin1989 Level 37, Romania
28 January 2024
This task was much more fun and I think it was more useful than the final task from the previous level, the MVC task.
matemate123 Level 50, Kraków, Poland
10 May 2023
So pleasureable tasks!
Justin Smith Level 41, Greenfield, USA, United States
22 January 2022
This is probably one of the most important large tasks so I recommend not skipping it. Even with the best planning, when working on a large project with a longer time-frame, you're likely at some point to realize that the structure of the project would be better a different way, and you'll have a big task moving methods around and stuff. If you're not careful, a disorganized approach to it will lead to forgetting certain important methods or variables that needed to be moved but weren't, and then you'll have the fun task of tracking down where the problem is. I haven't done this in Java before (since I'm new to it) but I've been through this process a lot in other languages.
Jurij Thmsn Level 29, Flensburg, Germany
17 November 2021
Not very hard, but still a really nice task. I wish CG would provide a tabular overview for the different techniques, though.
Jurij Thmsn Level 29, Flensburg, Germany
17 November 2021
This one is really nice: Refactoring Techniques
Andrei Level 41
30 April 2021
Nice program, I really enjoyed it. I wish there were more explanations about what we are doing and what is the lesson behind every task, like they explained in the final task. It would have been nice to put the pieces of the puzzle together and not follow instructions without really knowing what technique you are using. ✌💪👍
Shilpa nori Level 34, Rochester, United States
20 March 2021
These tasks are not super tough compared to other tasks. But definitely a lot of work!
Denys Level 41, Ukraine
16 February 2021
Tip: use ctrl+Shift+F to search for methods you want in directories
MaGaby2280 Level 41, Guatemala City, Guatemala
10 November 2020
Hard tasks but good ones and great for thinking and putting everything we have learned to use!
Lawson Level 29, Lagos, Nigeria
6 October 2020
My ide hangs anytime i want to do a task... Couldnt finish it tho... would come back to it later
MaGaby2280 Level 41, Guatemala City, Guatemala
9 November 2020
You have to check you plugin, there have been several updates lately
BlueJavaBanana Level 37
18 September 2020
These were actually pretty decent for the most part.