For this assignment you are required to develop a windowed


Assignment: JAVA Program using array of objects

Objectives

This assessment item relates to the unit 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 unit 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, number plates and hours 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 CarParkGUI.java is supplied (via the Moodle web site) which supplies the basic functionality of the interface. We are also going to store the information in an array of CarPark objects.

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 four JLabels for the heading and the prompts.

There are three JTextFields in which the customer name, number plate and hour are entered. There is also a JTextArea for displaying the output.

Four JButtons are at the bottom which link to blank methods for implementing the functionality of the application.

CarPark class

First step is to create a class called CarPark (CarPark.java) which will not contain a main.

The CarPark class will be very simple which will contain three private instance variables:

o customerName as a String
o numberPlate as a String
o hours as an integer

The following public methods will have to be implemented:

o A default constructor
o A parameterised constructor
o Three set methods (mutators)
o Three get methods (accessors)
o calculateParkingFee value returning method*

*You will create a public value returning method calculateParkingFee(), the parking fee can be derived from the instance variable hours. Typically we do not store calculated values in a database so the fee will be derived via your calculateParkingFee () method. Do not store the fee as an instance variable.

CarParkGUI class

Once the CarPark class is implemented and fully tested we can now start to implement the functionality of the GUI interface.

Data structures

For this assignment we are going to store the customer names, number plates and hours in an array of CarPark objects. Do not use the ArrayList data structure.

Declare an array of CarPark objects as an instance variable of CarParkGUI class, the array should hold ten* entries. Use a constant for the maximum entries allowed.

*Ten cars are unrealistic for a carpark but we will use this value during development of this program.

private CarPark [] carParkArray = new CarPark[MAX_CARS];

We need another instance variable (integer) to keep track of the number of customers being entered and use this for the index into the array of CarPark objects.

private int currentCar = 0;

CarPark class

First step is to create a class called CarPark (CarPark.java) which will not contain a main.

The CarPark class will be very simple which will contain three private instance variables:

o customerName as a String
o numberPlate as a String
o hours as an integer

The following public methods will have to be implemented:

o A default constructor
o A parameterised constructor
o Three set methods (mutators)
o Three get methods (accessors)
o calculateParkingFee value returning method*

*You will create a public value returning method calculateParkingFee(), the parking fee can be derived from the instance variable hours. Typically we do not store calculated values in a database so the fee will be derived via your calculateParkingFee () method. Do not store the fee as an instance variable.

CarParkGUI class

Once the CarPark class is implemented and fully tested we can now start to implement the functionality of the GUI interface.

Data structures

For this assignment we are going to store the customer names, number plates and hours in an array of CarPark objects. Do not use the ArrayList data structure.

Declare an array of CarPark objects as an instance variable of CarParkGUI class, the array should hold ten* entries. Use a constant for the maximum entries allowed.

*Ten cars are unrealistic for a carpark but we will use this value during development of this program.

private CarPark [] carParkArray = new CarPark[MAX_CARS];

We need another instance variable (integer) to keep track of the number of customers being entered and use this for the index into the array of CarPark objects.

private int currentCar = 0;

Button options

1. Enter button: enter()

For assignment two we are going to use the JTextFields for our input.

When the enter button is pushed the program will transfer to the method: enter() this is where we read the customer name, number plate and hours and add them to the CarPark array.

The text in the JTextFields is retrieved using the getText() method: String customerName = nameField.getText(); Repeat this to read in the customer's number plate.

When we read the hours input 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.

int hours = Integer.parseInt(hoursField.getText());

We need to add these values customer name, number plate and hours to the array of CarPark objects. When we declared our array using the new keyword, only an array of references were created and we need to create an instance of each of the Carpark objects. When we create the CarPark object we can pass the customer name, number plate and hours to the object via the parameterised constructor.

carParkArray[currentCar] =
new CarPark(customerName, numberPlate, hours);

Remember to increment currentCar at the end of the enter method.

Next we will output the entry including the customer's parking fee in the JTextArea.

need to create an instance of each of the Carpark objects. When we create the CarPark object we can pass the customer name, number plate and hours to the object via the parameterised constructor.

carParkArray[currentCar] =
new CarPark(customerName, numberPlate, hours);

Remember to increment currentCar at the end of the enter method.

Next we will output the entry including the customer's parking fee in the JTextArea.

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 and if there is no text then display the following error dialog and use the return statement to exit the method, the focus should return to the name field.

The number plate and hours fields should also be checked for text and appropriate error dialogs displayed.

We will not worry about checking data types or numeric ranges in this assignment.

2. Display all customers' names, number plates, hours and fees : displayAll()

When this option is selected, display all of the customer names, number plates, hours and fees which have been entered so far. At the end of the list display the average hours of stay and the total of the fees collected.

Use a loop structure to iterate through the array, you should be sharing the printing functionality with the enter button, hint: use the method to display a line of data from the enter method.

Only print the entries which have been entered so far and not the whole array, use your instance variable currentCar as the termination value in your loop.

Sum the hours in the loop for the average calculation and sum the fees for displaying at the end.

If no entries have been added then clear the text area and display the following error dialog, repeat this for your search.

3. Search for a customer name: search()

You can just use a simple linear search which will be case insensitive. Use the JOptionPane.showInputDialog() method to input the customer's name.

If the search is successful display the details about the customer.

If the search is unsuccessful display an appropriate message and clear the text area.

4. 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 (within the supplied code).

During a typical exiting process we may want to ask the user if they want to save any files they have opened 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), you should try and write a method which accepts an error message as a parameter and displays the error message in a message dialog within the method; use JOptionPane.ERROR_MESSAGE as the last parameter in the message dialog.

Constants must be used for all literal numbers in your code (calculateParkingFees method and maximum entries).

Look at the marking criteria to ensure you have completed all of the necessary items.

Refer to a Java reference textbook and the unit and lecture material (available on the unit 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, complete report and the UML class diagram.

Supplied Code

Download, compile and run the supplied code available from the unit 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.

Attachment:- Java-Assignment.rar

Solution Preview :

Prepared by a verified Expert
JAVA Programming: For this assignment you are required to develop a windowed
Reference No:- TGS02756433

Now Priced at $35 (50% Discount)

Recommended (91%)

Rated (4.3/5)