Assignment 02
Information
The marks allocated to each question are given in brackets in the right margin.
Instructions for Assignment
The answers to this assignment need to be prepared by a word processor. You can use MS Word or Open Office. Please show all your work for all questions. This will give your tutor an opportunity to award you some marks for partially correct answers.
Important
1. ALL assignments will be used for assessment purposes and any missing ones will gain zero mark. For example, if you get 70, 60, 0 (missing), and 50 in Assignments 01, 02, 03 and 04 respectively, the average is (70+60+0+50)/4 = 45 and it is a pass for assignments. Normally failing to get an average of 40 marks in assignments means you will fail this course and you need to pay again to retake it.
2. Please backup your answer file against system failure.
3. In providing your answers, you should state clearly which question/part you are answering (in your answer file). Marks will be deducted if you do not do so.
Question 1 [20 marks]
You should be able to answer this question after you have completed Units 1 to 3. This is a question to consolidate important concepts learnt in these units and Assignment 01.
(a) Learning debugging is important if you like to be a programmer. To verify a program is doing what it should, a programmer should know the expected (correct) values of certain variables at specific places of the program. Therefore make sure you know how to perform the instructions by hand to obtain these values. Remember, you should master the technique(s) of debugging.
Create a new project Assignment02 in NetBeans and copy the following program into a new Java class. The author of the program intends to find the sum of the numbers 2, 7 and 12.
public class Assignment02Q1a { public static void main(String[] args) { int from = 2; int to = 12; int sum = 0; for (int i=from; i<to; i++) { // (1) sum += i; // (2) } System.out.println("The sum is "+sum); } } |
(i)
Run the program. What is the output?
(ii)
What is the expected value of sum just before the for loop is executed?
(iii)
Write down the three expected intermediate sums after the integers 2, 7 and 12 are added one by one (in the given order) to an initial value of one.
(iv)
Since we have only a few executable statements here, the debugging is not difficult. Insert a System.out.println() statement just after the statement indicated by the comment "// (2)" to print out sum. What are the values of sum printed (press Ctrl-C to stop the program if necessary)?
(v)
What modification(s) is/are needed to make the program correct?
NetBeans allows you to view the values of variables at specific points (called breakpoints). This saves you the effort of inserting/removing println() statements. Again, you must know the expected (correct) values of those variables at the breakpoints. If you like, you can try to explore the use breakpoints yourself.
[8
(b) The following is about creating a class Backpack and testing it. In every part, correct any syntax errors indicated by NetBeans until no such error messages.
(i)
Create a class Backpack with the attributes brand and volume. The attributes are used to store the brand and the volume of the backpack respectively. Choose suitable types for them. Copy the content of the file as the answers to this part. No screen dump is recommended to minimize the file size.
(ii)
Add a method setVolume() to the Backpack class with appropriate parameter(s) and return type to set the volume of the backpack. Copy the content of the method as the answers to this part.
(iii)
Add another method getBrand() to the Backpack class with appropriate parameter(s) and return type to get the brand of the backpack. Copy the content of the method as the answers to this part. You should create other setter/getter methods in your class file but no marks are allocated for them since they are similar to the ones here and in part (ii).
(iv)
Write a method capacity() which returns the category of the backpack as a string using an ifthen-else or switch-case statement. The category of the backpack is determined by the following table.
Capacity | Volume |
"Large" | Not less than 50 |
"Medium" | 20 to less than 50 |
"Light" | Less than 20 |
Copy the content of the method as the answers to this part.
(v)
Create another class TestBackpack with a method main() to test the class Backpack. In main(), create a backpack object backpackA and print the message "An object backpackA of class Backpack has been created". Run the program. Copy the content of the file and the output showing the message as the answers to this part.
(vi)
In the class TestBackpack, add the following before the end of main():
1. Display a dialog box (see the bottom of p.35 of Unit 3 for an example of such a dialog box) which contains the message "Input a value for volume (>0)".
2. Assume the input is a number, check if it is less than or equal to zero. If so, display the message "Volume must be > 0" in another dialog box (see the second dialog box on p.36 for an example) and go to 1.
3. Set the volume of backpackA to the input volume.
4. Make use of the method category(), print the category of backpackA.
Run the program and input 42 in step 2. Copy the content of the class and the output as the answers to this part.
Remember to add a suitable import statement since dialog boxes are used.
[12]
Question 2 [30 marks]
You should be able to answer this question after you have completed Unit 4.
(a) Write a class DialogBox containing the following methods:
(i)
inputJPYAmount() : which displays a dialog box asking user to "Input the JPY Amount: " and finally returns the input as a real number.
Before the returning the input, ask the user to confirm using a dialog box, which contains "The input is: <user input>, is it correct?", where "<user input>" is the input of the user. If the user does not confirm "Yes", return zero. Copy the class, including import statement(s), as the answers to this part.
(ii)
checkJPYAmount() : which calls inputJPYAmount() to get the JPY amount to check and nothing is returned. The JPY amount should be greater than zero. If not so, display the error message "The JPY amount should be greater than zero" and then call inputJPYAmount() to get the JPY amount again until it is correct. You need to use a loop to achieve this. Copy the method as the answer to this part.
(iii)
main() : which creates a DialogBox object and calls the method checkJPYAmount() for testing. Copy the method as the answer to this part.
[10]
(b) Write a class Conversion containing the following methods:
(i)
Constructor : which builds the frame shown on the right side. The frame consists of a text field for inputting a JPY amount, a label with 10 spaces for an equivalent JPY amount in USD, and a button to start the calculation. Declare any necessary attributes in the class and add appropriate action listeners for future use. Copy the class, including import statement(s), as the answers to this part.
(ii)
actionPerformed() : which performs the calculation and puts the result on the label when the button is pressed. You can assume one JPY is equivalent to 0.0063 USD. You can assume a valid real number is entered in the textfield. Copy the method as the answer to this part.
(iii)
main() : which creates a Conversion object and sets it visible for testing. Copy the method as the answer to this part.
[20]
Question 3 [20 marks]
You should be able to answer this question after you have completed Unit 4.
Write a class Launcher containing the following methods:
(a) Constructor : which builds the frame shown below. The frame consists of a menu bar, two menus (Launch and Exit), some menu items, and a text area. The menu items of the Launch menu are shown and there is a single menu item "Exit" on the Exit menu. Declare any necessary attributes in the class and add appropriate action listeners for future use. Copy the class, including import statement(s), as the answers to this part.
(b) actionPerformed() : which performs necessary actions when each menu item is selected. Run the classes TestBackpack, DialogBox and Conversion when the menu items "Launch Backpack", "Launch DialogBox" and "Launch Conversion" is selected respectively. To launch TestBackpack, you may use the following statement:
TestBackpack.main(null);
Quit the program when the menu item "Exit" is selected. Copy the method as the answer to this part.
[9]
(c) main() : which creates a Launcher object and sets it visible for testing. Copy the method as the answer to this part.
[1]
Question 4 [30 marks]
You should be able to answer this question after you have completed Units 5.
(a) You can look for leisure facilities and programme of LCSD through its home page:
https://www.lcsd.gov.hk/en/index.html
Suppose you like to see the squash training course. You should at least try to do the following: (1) and client the search button, there are probably different courses at different sports centres. Then find and select one course.
(2) On the home page, click and find one. Using a table similar to Self Test 5.3, evaluate and give comments on the above Web interface based on the ten "characteristics of a good user interface" covered in the course. Remember to give your reason for each of the points.[20
(b) Using two or more of the six "key principles of user interface design", discuss how to improve the user interface of the Conversion class in part (b) of Question 2, which is shown again for your convenient reference. You need to state each of the principles involved and then give your suggestion(s)[10]
**** End ****