JAVA PROGRAMMING ASSIGNMENT
The context for this assignment (all parts) is the development of a small software desktop application in the context of keeping track of fuel consumption and money spent on petrol for drivers. The program will allow the user to check the average consumption for the last tank filling, average consumption for the last 5 tank fillings, average amount paid, as well as visualise the data from recent weeks.
This assignment will test your knowledge of and skills in writing application software for a particular task, understanding the business rules of a particular problem, coding these in a computer program, developing a graphical user interface, connecting to a file and connecting to a database.
For this assignment, you will use the Java programming language and development will be on the Eclipse IDE platform as practised in the computer lab classes.
Part A
You are to write a software desktop application using the Java programming language. The program enables a user, assuming that they are a motorist (car, motorbike, van, truck, etc.), to keep track of the fuel consumption and money spent on fuel. The user can enter the amount of fuel purchased at the petrol station, the kilometres driven, the total amount paid for the fuel, and the date of the purchase. To this end, you need to provide labels that guide the user as well as appropriate input GUI components (your choice of text fields, drop down menus, etc.).
You have a great degree of freedom in what GUI elements you choose and how you would like to design the layout of your GUI. The above example is really just that – an example the design of which you can copy if you are feeling uninspired to come up with your own design. What matters is the functionality of the design and that the user can input the required data in a sensible fashion.
Input values for the fuel purchased, distance driven and amount paid are floating point values. The date needs to be entered as day, month, year. The average fuel consumption for the last tank filling and the average fuel consumption for the last 5 tank fillings need to be displayed as a floating point number with 1 decimal place. The average amount paid for the last 5 tank fillings needs to be displayed as a floating point number with two decimal places and the “$” sign in front.
The drawing area needs to be at least 350 x 250 pixels in size (bigger is OK, but not smaller). The drawing area should have a clearly visible border. The drawing area should show labels for the current tank filling and the 4 most recent tank fillings. For simplicity, these can be labelled as “Wk1”, “Wk2”, …., “Wk5” as an in the example above. There should be a baseline, drawn in black colour. The average consumption for the current tank filling and each of the 4 most recent tank fillings is to be shown as a bar graph (see example), with the bars drawn in the colour chosen by the user.
To visualise the average fuel consumption for the current tank filling and across the current and the 4 most recent tank fillings, horizontal lines are to be drawn in the colours chosen by the user (see example).
Your task in Part A will be to develop a GUI that allows a user to input the data in a suitable GUI using Java Swing components and the Java WindowBuilder, and then to visualise the data. To this end, the user can
input the data using Java Swing GUI elements, e.g. text fields, radio buttons, drop down lists, etc.,
calculate various averages,
visualise the consumption as a bar graph,
select different colours for various elements, and
exit the program in a defined way via a Quit button.
The data should be entered through the GUI and
the averages calculated when the Calculate button is clicked, and
visualised when the Draw button is clicked.
Data beyond the 5 weeks do not need to be saved in Part A, neither in memory (in an array or vector, for example), nor in a text file.
Notes:
For this assignment, you do not have to do any checking for invalid user input (you may of course, if you want to), for example you do not have to check whether the user has typed in a negative number or a letter in the text boxes for fuel purchased, distance driven and amount paid. Checking for invalid user input will be discussed in lectures later in the semester.
Your user interface does not have to be identical to the one above. This is just an example of how the GUI could look.
Your GUI should update any labels in an appropriate manner when the user enters new data and clicks on the Calculate button.
What the tutors will be looking for
The tutor’s instructions include
Constants vs literals. Using constants is important for the ease of maintenance. Not using constants will result in lower marks. For example, consider using a constant for the width of each bar in the bar graph. (2 marks)
GUI Design. Is it simple to use and easy to understand? Are all required components there? (4 marks)
Program code layout. Separate blocks of code by a blank line. Use comments. (3 marks)
A comment is not an essay. Comments are important for the maintenance of your program and should contain enough details, but keep them concise. Don’t comment every single line. (2 marks)
The program must have a prologue. (2 marks)
Good names for your variables and constants. Check style against the Java style guide attached below. (4 marks)
Does the program work correctly? Are the averages calculated correctly? Are the bar graphs drawn the right way? (10 marks)
Total: 27 marks
Step by Step Guide for Part A
1. Draw a sketch of your graphical user interface on paper. What Java Swing components will you use? Where will you place them? The above GUI is just an example. You can copy the design of it or create your own.
2. Create a new Java Project the same way as before (see lecture notes Lecture 1 Week 4 and Tutorial notes Week 5).
3. Add an Application Window to it (right-click on src, then New → Other → WindowBuilder → Swing Designer → Application Window
Part B
In Part B of the assignment, the input will not come directly from the user any more, but rather from a text file.
The file fuel.txt contains the details of several tank fillings. Each line of this file contains the details of one tank filling. Each tank filling has a date, the amount of fuel purchased (floating point number), the distance driven (floating point number) and the amount paid (floating point number) as data on one line of the text file, separated by commas. The day, month and year in the date are integer numbers separated by “/”.
Here’s an example of one line from the text file:
12/3/2012,68.32,756.9,85.56
You will need to copy fuel.txt into the same directory of your Eclipse project that holds the .project file.
Your task will be to add code to your program that reads the tank filling data from the text file, puts these into appropriate storage in memory (I suggest you create a data class similar to the example in star4.java and then create an array or vector that will hold each instantiation of that data class), adds the dates to a JList object, calculates and displays the average over all tank fillings in the text file, and upon selection of a date by the user, displays the average consumption for that date in a text label. You do not need to visualise this information as bar graphs (as in Part A).
You can do all parts (A, B and C) in the same program, simply extend the GUI and add new methods / functions as required. Alternatively, you can create a separate GUI file for each part as long as they are all part of the same Java project.
For testing, the details of the file may change, but the structure will not.
Notes:
For this part of the Java assignment, add Java Swing GUI components that allow the user to select a date, via a JList, and that updates the calculations and text label displays with the information for the selected tank filling.
The calculations and updates should occur as soon as a date is selected in the JList.
Try to reuse code, such as the code for the calculation of the averages, from Part A. This will mean that you should have the GUI code separated from the calculation code (i.e. in a separate method or separate methods). This is good programming style!
Remember that it is good practice to have a Quit button in your GUI to exit the program
Part C
In Part C of the assignment, the input will now come from a MS Access database contained in the file fuel.mdb. This file is available on the IIT / IST G web site on Moodle (UC LearnOnline).
There is one table in the database, tblFuel.
The table tblFuel contains fields “date”, “fuelPurchased”, “distanceDriven”, and “amountPaid”. A typical record would be <12/3/2012,68.32,756.9,85.56>.
Write a Java program with a GUI (Note: extending your GUI program from Parts A and B is perfectly acceptable, but you can also write a separate Java file for Part C; either way is fine and will not influence your marks) that allows the user to do the following:
Let the user select a date from a list (e.g. JList) and display the information for the selected tank filling in appropriate text labels. Calculate and display the average fuel consumption for the selected tank filling. Calculate and display the average fuel consumption over all tank fillings in the database. (Let us assume these are all for the same verhicle.)
Create a database report consisting of all dates, including the fields “date”, “fuelPurchased”,
“distanceDriven”, and “amountPaid”. The report should be in descending date order, i.e. the most recent date first. It should be written to a text file “databaseFuel.txt”. Make sure that your columns are properly aligned. Text columns should be left aligned, number columns (including date columns) right aligned.
Notes:
You may extend your GUI from Part B to include Part C. If you do, label the parts clearly with “Part A”, “Part B”, and “Part C”, so that your tutors know which part of your program is in reply to what part of the assignment. Alternatively, you may write a separate file for Part C, but must ensure that it is part of the same Java project as Part A and B.
Use a Disconnected Database Access model, i.e. connect to the database, run the SQL command, get the resulting virtual table in the ResultSet object, then disconnect again. Do not connect to the database and download / store all database information in local storage.
You do not need to visualise this information as bar graphs (as in Part A).
Reports
In this part of the assignment, you will write a number of database reports. Each report should
include a header,
include a column header,
have columns lined up, with text columns left justified and numeric columns right justified.
make provision for multi-page reports. At least one of your reports should actually be more than one (fictional) page in length. Do this by defining the page length as 5.
All reports should be written to a text file rather than the printer. If you wish, you can then use Notepad to produce a hardcopy of the reports. However, you do not need to submit a hardcopy of the report.
What the tutors will be looking for
The tutor’s instructions include (apart from the usual such as good variable names, prologue / comments, code layout, …)
Make sure that the lines in the database report is split into pages and the columns are lined up and correctly formatted. (4 marks)
Correct connection to the database. (2 marks)
Correct identification of all information in a date’s tank filling data. (2 marks)
Correct display of the information in text labels. (2 marks)
Java Style Guide
General
Your programs should be
Simple
Easy to read and understand
Well structured
Easy to maintain
Simple programs are just that. Avoid convoluted logic, nested if-statements and loops, duplicating execution (such as reading files multiple times), repeated code, being “clever”.
Programs can be made easier to read by following the “Layout” and “Comments” guidelines below.
Well structured code uses methods and functions to help tame complexity.
If programs are simple, easy to understand and well structured they will be easy to maintain. Easily maintained programs will also use constants rather than literals, and use built-in functions and types.
Layout
The text editor in the Eclipse IDE does a good job of automatically laying out your program. You can control this operation to some extent (using the Tools/Options menu). However, you are unlikely to need to do so.
To get java progeamming assignment help, please contact to our live chat adviser