Reference no: EM132414360
COIT11222 - Programming Fundamentals - Central Queensland University
Assessment - JAVA Program using array of objects
Objectives
This assessment item relates to the course learning outcomes as stated in the Unit Profile.
Details
For this assignment, you are required to develop a Windowed GUI Java Program to demonstrate you can use Java constructs including input/output via a GUI interface, Java primitive and built-in types, Java defined objects, arrays, selection and looping statements and various other Java commands. Your program must produce the correct results.
The code for the GUI interface is supplied and is available on the course website, you must write the underlying code to implement the program. The command buttons are linked to appropriate methods in the given code. Please spend a bit of time looking at the given code to familiarise yourself with it and where you have to complete the code. You will need to write comments in the supplied code as well as your own additions.
Assignment Specification
In assignment one we read in multiple customer names, contact phones and parcel weights using both Scanner and GUI dialogs. In this assignment we are going to read in the data and output information via a GUI interface, the code for the GUI interface CourierServiceApp.java is supplied (via the Moodle web site) which supplies the basic functionality of the interface. We are going to store the information in four parallel arrays. The following image is the GUI interface when the CourierServiceApp program runs.
Look at the code supplied and trace the execution and you will see the buttons are linked to blank methods (stubs) which you will implement the various choices via the buttons.
The GUI interface contains three JLabels for the prompts.
There are three JTextFields in which the customer name, contact phone and parcel weight are read. Five JButtons are after the parcel weight JTextField which link to blank methods for implementing the functionality of the application.
There is also a JTextArea for displaying the output.
Data structures
For this assignment we are going to store the data of customer names, contact phone, parcel weight and the delivery fee in four parallel arrays. Among four parallel arrays, the data in first three arrays are entered through the corresponding text field while the data in the array of delivery fee is obtained via a method call. The implementation details of the method for calculating the delivery fee - calculateDeliveryFee(int weight) is the same as the method definition in Week5.java of Assignment 1(Just without using the keyword ‘static' in the method header). Do not use the ArrayList data structure.
Declare an array of customer name of String type, the array should hold twenty entries. You can use a constant for the maximum entries allowed.
private String [] customerName = new String[MAX_NUM];
Similarly, you can declare other arrays to represent the contact phone, parcel weight and delivery fee. We need another instance variable (integer) to keep track of the number of the customer being entered and use this for the index into the parallel arrays.
private int currentCustomer = 0;
Button options
1. Enter button: enterData()
For assignment two we are going to use the JTextFields for our input. An entry with sample data in three textFields is shown below.
When the Enter button is pushed the program will transfer to the method: enterData(), this is where we read the customer name, contact phone and parcel weight and assign them to the elements of arrays that represent these variables. Then call the method of calculateDelievryFee(int w) to get the value of delivery fee.
The text in the JTextFields is retrieved using the getText() method: customerName[currentCustomer] = nameField.getText(); contactPhone[currentCustomer] = contactPhoneField.getText();
(Note: here we assume customerName[], contactPhone[] and weight[] have been declared as arrays with right type)
When we read the input of parcel weight we are reading a string from the GUI, we will need to convert this to an integer using the Integer wrapper class as per assignment one.
weight[currentCustomer] = Integer.parseInt(weightField.getText());
We need to call the method calculateDelievryFee(weight[currentCustomer]) to get the delivery fee, where the array element weight[i] is as the parameter passing to the method. This statement can be written as follows:
fee[currentCustomer]= calculateDeliveryFee(weight[currentCustomer]);
Remember to increment currentCustomer at the end of the enterData () method.
With the sample data entered in last page, the execution result of clicking ‘Enter' button is as follows.
The supplied code may contain a method for printing the heading and the line underneath. Just like the JTextField the JTextArea has a method setText() to set the text in the text area, note this overwrites the contents of the text area. In addition, the JTextArea also has a method named append() that allows the new generated text being added to the existing text.
When the data has been entered and displayed, the customer name, contact phone and parcel weigh JTextFields should be cleared. We can clear the contents by using: textField.setText("");
The focus should then return to the customer name JTextField by using:
nameField.requestFocus();
Data validation (you can implement this after you have got the basic functionality implemented)
If one or three textFields has not been entered data and the ‘Enter' button is clicked, your program should pop up a message box to remind user the required input.
Use an if statement at the beginning of the method and after displaying the error dialog use the
return statement to exit the method.
if (nameField.getText().compareTo("") == 0) // true when blank
Use the above code to check the name field for text if there is no text display the following error dialog and use the return statement to exit the method, the focus should return to the name field.
The contact phone and parcel weight fields should also be checked for text and the focus should be returned to the contact phone field and the parcel weight field respectively.
We will not worry about checking data types or numeric ranges in this assignment.
2. Display all customer data including delivery fee: displayAll()
When this option is selected, display all of the customer names, contact phones and parcel weights plus the delivery fees which have been calculated so far. At the end of the list display the number of entries, the average parcel weight and the total delivery fee value. Here it is an example with 3 entries data.
Use a loop structure to iterate through the array, you should be sharing the printing functionality with the Enter button.
Only print the entries which have been entered so far and not the whole array, use your instance variable currentCustomer as the termination value in your loop. You can sum the parcel weight in the loop for the average calculation and sum the delivery fee value and finally use the append() method of the JTextArea to display them on the text area.
If no entries have been added clear the text area and display the following error dialog, repeat this for your search.
3. Search for a customer's result: search()
You can just use a simple linear search which will be case insensitive. Use the
JOptionPane.showInputDialog() method to input the customer name.
If the search is successful display the details about this customer's delivery request. See the following sample result.
If the search is unsuccessful display an appropriate message and clear the text area.
4. Sort all entries by parcel weight
When the button ‘Sort' is pressed, the data of parcel weight are sorted in ascending order and accordingly the customer names, contact phones and deliver fees are re-listed to reflect this order change, as shown as below. We recommend using the simple soring algorithm-Bubble sort in Chapter 8 of textbook to implement sorting function.
5. Exit the application: exit()
The exit method is called when the user pushes the exit button or when they select the system close (X at top right hand corner), try and find the code which does this.
During a typical exiting process we may want to ask the user if they want to save any files they have open or if they really want to exit, in this assignment we will just print an exit message.
Extra Hints
Your program should be well laid out, commented and uses appropriate and consistent names (camel notation) for all variables, methods and objects.
Ensure you have header comments in both source files, include name, ID, filename, date and purpose of the class.
Make sure you have no repeated code (even writing headings and lines in the output)
Constants must be used for all literal numbers in your code (calculateDeliveryFee(int w) method).
Look at the marking criteria to ensure you have completed all of the necessary items
Refer to a Java reference textbook and the course and lecture material (available on the course web site) for further information about the Java programming topics required to complete this assignment. Check output, check code and add all of your comments and complete report.
Supplied Code
Download, compile and run the supplied code available from the course web site.
You will see the GUI interface has been implemented and you have to implement the underlying code, use the supplied method stubs and add you own methods. Look for // ToDo comments in the code which contain hints.
Again no code should be repeated in your program.
Attachment:- JAVA Program using array of objects.rar