OOP Lab03 BasicOOTechniques
OOP Lab03 BasicOOTechniques
0. Assignment Submission
For this lab class, you will have to turn in your work twice, specifically:
Right after the class: for this deadline, you should include any work you have done within the lab
class.
10PM three days after the class: for this deadline, you should include the source code of all
sections of this lab, into a branch namely “release/lab03” of the valid repository.
After completing all the exercises in the lab, you have to update the use case diagram and the class
diagram of AIMS project.
Each student is expected to turn in his or her own work and not give or receive unpermitted aid.
Otherwise, we would apply extreme methods for measurement to prevent cheating. Please write
down answers for all questions into a text file named “answers.txt” and submit it within your
repository.
1
https://docs.microsoft.com/en-us/azure/devops/repos/git/git-branching-guidance?view=azure-devops
1
● We name branches with meaningful names. See Table 1-Branching policy.
● We had better keep branches as close to master as possible; otherwise, we could face
merge hell.
● Generally, when we merge a branch with its origin, that branch has been history. We usually
do not touch it a second time.
● We must strictly follow the policy for release branch. Others are flexible.
[1] If we want to update your solutions within a week after the deadline, we could make a new hotfix
branch (e.g., hotfix/stop-the-world). Then we merge the hotfix branch with master and
with release branch for last submitted assignment (e.g., release/lab05). In case we already create
a release branch for the current week assignment (e.g., release/lab06), we could merge the
hotfix branch with the current release branch if need be, or we can delete and then recreate current
release branch.
[2] Latest versions of projects in release branch serve as the submitted assignment
Let’s use Release Flow as our Git workflow and apply it to refactor our repositories.
Step 1: Create new branch in our local repository. We create a new branch
refactor/apply-release-flow from our master branch.
Step 2: Make our changes, test them, and push them. We move the latest versions of all our
latest file from previous labs such that they are under the master branch directly.
See https://www.atlassian.com/git/tutorials/undoing-changes to undo changes in case of problems.
To improve commit message, see https://thoughtbot.com/blog/5-useful-tips-for-a-better-commit-message.
Step 3: Make a pull request for reviews from our teammates 2. We skip this step since we are
solo in this repository. We, however, had better never omit this step when we work as a team.
2
https://www.atlassian.com/git/tutorials/making-a-pull-request
2
Step 4: Merge branches. Merge the new branch refactor/apply-release-flow into
master branch.
The result is shown in the following figure.
Hints:
Typical steps for a new branch:
Create and switch to a new branch (e.g. abc) in the local repo: git checkout -b abc
Make modification in the local repo
Commit the change in the local repo: git commit -m “What you had change”
Create a new branch (e.g. abc) in the remote repo (GitHub through GUI)
Push the local branch to the remote branch: git push origin abc
Merge the remote branch (e.g. abc) to the master branch (GitHub through GUI)
After completing all the tasks of that week, and merge all branches into master branch, you should create a release/labxx
branch from the master in the remote repo (GitHub).
For example, in the lab03, there may be 9 main tasks. So, one possible way to apply release flow is to create 9
branches:
- Create a branch refactor/apply-release-flow for refactoring the repository following the Release Flow
- Create a branch topic/method-overloading for the exercise on method overloading
- Create a branch topic/passing-parameter for the exercise where you investigate on Java’s parameter
passing
- Create a branch topic/class-members for the exercise where you practice with classifier member and
instance member
- Create a branch feature/print-cart for the implementation of the print items in cart feature
- Create a branch feature/search-cart for the implementation of the search items in cart feature
- Create a branch topic/store for the implementation of the class Store
- Create a branch refactor/packages for refactoring the projects in your repository using packages
- Create a branch topic/memory-management-string for the String, StringBufer &
StringBuilder exercise
Refer to the demonstration of Release Flow in the last section of this lab for more detailed guide.
3. Passing parameter
- Question: Is JAVA a Pass by Value or a Pass by Reference programming language?
First of all, we recall what is meant by pass by value or pass by reference.
● Pass by value: The method parameter values are copied to another variable and then the copied
object is passed to the method. That's why it's called pass by value
● Pass by reference: An alias or reference to the actual parameter is passed to the method. That's why
it's called pass by reference.
Now, you will practice with the DigitalVideoDisc class to test how JAVA passes parameters. For
this exercise, you will need to temporarily add a setter for the attribute title of the DigitalVideoDisc class.
Create a new class named TestPassingParameter in the current project
● Check the option for generating the main method in this class like in Figure 2
4
Figure 2. Create TestPassingParameter by Eclipse
In the main() method of the class, typing the code below in Figure 3:
DVD
jungleDVD (“Jungle”)
cinderellaDVD
DVD
(“Cinderella”) 5
Figure 3. Source code of TestPassingParameter.
Figure 4. Results(1)
To test whether a programming language is passing by value or passing by reference, we usually use the
swap method. This method aims to swap an object to another object.
● After the call of swap(jungleDVD, cinderellaDVD) why does the title of these two
objects still remain?
● After the call of changeTitle(jungleDVD, cinderellaDVD.getTitle()) why is
the title of the JungleDVD changed?
After finding the answers to these above questions, you will understand that JAVA is always a pass
by value programming language.
Please write a swap() method that can correctly swap the two objects.
6
click in the left margin of the line in the Java editor and select Toggle Breakpoint. This is equivalent to
double-clicking in the left margin of the line.
To delete a breakpoint, toggle the breakpoint one more time. The blue dot in front of the line will
disappear (Figure 6).
7
Figure 6. The breakpoint is deleted
To deactivate the breakpoint, navigate to the Breakpoints View and uncheck the tick mark next to the
breakpoint you want to deactivate (Figure 7). The program will only stop at activated breakpoints.
For this example, we will need to keep this breakpoint, so make sure to set the breakpoint again after
practicing with deleting/deactivating it before moving to the next section.
8
Figure 8. Run Debug from a class
Alternatively, you can select the project root node in the Project Explorer and click the debug icon in the
Eclipse toolbar (Figure 9)
The application will now be started with Eclipse attached as debugger. Confirm to open the Debug
Perspective.
4.2.3. Step Into, Step Over, Step Return, Resume:
- In the Debug Perspective, you can observe the Step Into/Over/Return & Resume/Terminate
buttons on the toolbar as in Figure 10.
9
Figure 10. Stepping Commands on the Toolbar in Debug Perspective
- With debugger options, the difference between "Step into" and "Step over" is only noticeable if
you run into a function call:
o "Step into" (F5) means that the debugger steps into the function
o "Step over" (F6) just moves the debugger to the next line in the same Java action
- With "Step Return" (pressing F7), you can instruct the debugger to leave the function; this is
basically the opposite of "Step into."
- Clicking "Resume" (F8) instructs the debugger to continue until it reaches another breakpoint.
For this example, we need to see the execution of the swap function, so we choose Step Into. The
debugger will step into the implementation of the swap function in line 18 (Figure 11).
10
Figure 12. Variables shown in Variable View
Click Step Over and watch the change in the value of variables o1, o2 & tmp.
Repeat this until the end of the swap function (Figure 13, Figure 14, Figure 15).
11
Figure 14. Step over line 19 of swap function
12
Figure 16. Step return to main function
The variable jungleDVD still has a title attribute with value “Jungle”. You can change this value by
clicking on it and change it to “abc”, for example (Figure 17).
Click Step Over and see the result in the output in the Console (Figure 18)
14
- In the CartTest class, write codes to test all methods you have written in this exercise. You should
create sample DVDs and carts, like in this code snippet:
+ For creating a package, right click to the project (or go to menu File) and choose New -> Package. Type
the full path of package including parent packages, separated by a dot.
15
- Keep the text file for answering questions in the lab, the “Requirement” & “Design” folders should be
moved inside the root folder of AimsProject, next to its src/ and bin/ folder.
- Your structure of your labs should be at least as below. You can create sub-packages for more
efficently organizing your classes in both projects and all listing packages. All the excercises of lab01
should be put in the corresponding package of one project - the OtherProjects project.
+ AimsProject
hust.soict.dsai.aims.disc.DigitalVideoDisc
hust.soict.dsai.aims.cart.Cart
hust.soict.dsai.aims.store.Store
hust.soict.dsai.aims.Aims
hust.soict.dsai.test.cart.CartTest
hust.soict.dsai.test.store.StoreTest
hust.soict.dsai.test.disc.TestPassingParameter
+ OtherProjects
hust.soict.dsai.lab01
16
9. String, StringBuilder and StringBuffer
- In the OtherProjects project, create a new package hust.soict.globalict.garbage for
ICT or hust.soict.dsai.garbage for DS-AI. We work with this package in this exercise.
- Create a new class ConcatenationInLoops to test the processing time to construct String using
+ operator, StringBuffer and StringBuilder.
Page 1 of 22
Figure 23. Sample code for GarbageCreator
Change the code in line 14-17 above to use StringBuffer instead of “+” operator to build string and
observe result
Now we add a new topic or a new feature to our application. The next section shows us how to apply
Release Flow in this hypothesis.
10.2. Demonstration
Step 1. Update local repository.
Issue the following command and resolve conflicts if any.
(master) $ git pull
Page 2 of 22
Step 5. Create a new branch in the remote repository (GitHub through
GUI).
- Firstly, under the “Code” tab of the top navigation bar, choose the drop-down button with the
branch name (in this case “master”) on the top left.
- Secondly, enter the new branch name “feature/demonstrate-release-flow” into the text field and
click “Create branch: feature/demonstrate-release-flow from ‘master’”.
- The following figure shows the result of our efforts in this step.
Page 3 of 22
Figure 28. Branch Creation in GitHub GUI (3/3)
Step 7. Create a pull request in GitHub GUI (for working in a team only)
- Firstly, choose “Pull requests” tab from the top navigation bar.
- Secondly, click the button “New pull request” in the top right corner of the interface.
- Then, pick the target branch and current branch. Besides, at the bottom of the interface, we can see
the changes between current branch and the target branch. Choose “Create pull request” to the top
right.
Note: the target branch will affect the destination branch which we want our branch merge to in
the next step.
Page 4 of 22
- Figure 31. Creation of a Pull Request in GitHub GUI (3/4)
- Lastly, choose reviewers for the pull request. We can also change the commit message, and add
comment as we desire. Choose “Create pull request”
- The following figure shows the result of our efforts in the dashboard of GitHub. The added
reviewers also can see the pull requests in their dashboard. When the changes are viewed, we can
merge the branches.
Page 5 of 22
Step 8. Merge the new remote branch to the master branch.
- Open the pull request.
- Choose “Merge pull request”. You can choose one of several merge options from the drop-down
menu
- Lastly, change the commit message if need be. We cannot change the destination branch. Choose
“Confirm merge” (as shown in Figure 33)
- The following figure shows the result of our efforts. The changes from the target branch have been
merged to the target branch “master”.
Page 6 of 22