The aim of this lab is to review of the basics of arrays and loops in Java and to introduce some new collection classes: ArrayList and HashMap.
Background Required: know the Java syntax for loop statements and arrays, importing and using Java library classes, using JUnit4 and Checkstyle in Eclipse.
1. Create a new Java project (File > New > Project or use the J+folder icon). Choose Java Project from the Java folder, click Next and type the project name week4 into the Project name field. Click Finish to create the new project and Yes when offered the Java Perspective.
2. Download the classes and into your week4 project area. Refer to previous labs for instructions on doing this if you need to. ArrayUtilities outlines a set of methods for operations on arrays of doubles. ArrayUtilitiesTest is a set of JUnit4 test cases to test your code.
3. Your task is to add missing Java code for the method signatures in ArrayUtilities. Search for the six //TODO lines in the file, clicking on the blue marks in the Eclipse right hand margin. Note that a return statement is needed for each method to clear the compiler errors. As you add code to each method, Run the JUnit tests by right clicking on ArrayUtilitiesTest.java
and select RunAs > JUnit test, continuing until you have a JUnit green bar. You can re-run tests at any stage by clicking on the green and yellow arrow in the JUnit window.
4. Once your code passes all tests, use the Eclipse formatter to correct any layout errors, and check your code with Checkstyle, and then submit.
The Java collection class, ArrayList, provides a convenient interface for a growable list of objects.
1. Download the class outline and the JUnit test file to your week 4 area.
2. Follow the //TODO lines and complete the code for each method until all tests are passed, as in the previous example. Note that ArrayList provides most of the functionality you need, so that only one line of code is needed for most methods. Use the Eclipse help system to see the documentation for methods of ArrayList by focussing with F2 over an ArrayList variable.
3. Once your code passes all tests, use the Eclipse formatter to correct any layout errors, and check your code with Checkstyle, and then submit.
When managing a collection of objects, it is often useful to be able to access those objects by a meaningful key, rather than by their position in the array. The java.util.HashMap class provides this functionality for you. You can learn about HashMap by accessing the documentation using Eclipse focus, or see CITS1220 lecture notes for arrays-loops. In this exercise you will refactor your CustomersList class to use the HashMap class. Refactoring (revising your code to improve it) is an important part of modern software engineering methods. Time spent planning your programming approach, or revising code when a better solution presents itself is time well spent!
1.
Create a copy of your CustomersList class called CustomersAccountsList. Add the import statement import java.util.HashMap;
and r
eplace the original customerslist instance variable with with private HashMap<String,BankAccount> customersaccountlist;
2.
Copy your BankAccount class from your week3 project into week4. You can drag and drop in the Eclipse navigator window.
3.
Now update (refactor) the code for each method in
CustomersAccountsList for the new data type.
4.
You can also refactor the test cases of CustomersListTests. Add your own test cases to cover particular properties of hash maps.
5.
Once your code passes all tests, use the Eclipse formatter to correct any layout errors, and check your code with Checkstyle, and then submit the Java source files for both your CustomersAccountsList class and your JUnit tests.
| ||
|
|