Cash Receipt
Back to the Case Studies homepage
ProblemToday your manager wants you to write a complete Java program that can be used by a cashier to generate customer receipts for her convenience store which sells only three items. Welcome to ArtSupply. Cashier: Sarah 2 brush sets at $ 8.0 3 paint sets at $ 40.0 4 primer at $ 18.75 Product Total: $211.0 Tax: $ 14.77 Total = $ 225.77 Money received: $ 250.0 Change due: $ 24.23 Change rounded up to nearest nickel: $ 24.25
|
SideSectionTitleSideSection goes here. | |
---|---|---|
SolutionThere are 12 steps to solving this problem:
Break the problem downWhile this problem is a bit more lengthy than others, the first rule of computer science is to break the program into pieces. Ultimately we want to get the whole program done, but we need to break it into parts we can work on and do it bit by bit. This means reading the problem and figuring out what you need to do logically step by step. Declare your variablesBased on the problem description, we know we will need to use constants and use JOptionPane. JOptionPane means storing text in strings. We're also dealing with integers and decimal value numbers (doubles). Keep your code organizedThe COMP 1010 Coding Standards give the foundation required to complete the code organization task for this case study. You need to make sure your code is properly indented, variable names are properly named and comments are included in your code. Get it doneSome programs will take longer than one sitting to get done. You must realize and accept that some programs can be done quickly, and some take a very long time, and that no matter what you code, you will always have to deal with bugs. Start on the programs early enough and don't procrastinate. Look it overAfter your program is working, check for efficiency. This will come into play later on in the degree, but you should practice doing it now. Below are some methods to help you become more efficient. Optimize the Code by Removing Unnecessary VariablesAt the top of the main method, there should now be a number of variables declared. Notice that there are nine different "digit" variables which are only used once to store the same calculation. int digit1; int digit2; digit1 = (isbn % 10); int total = digit1 * 9; isbn = isbn / 10; digit2 = (isbn % 10); total = total + digit2 * 8; isbn = isbn / 10; The code sample from above shows two of the nine digit variables. These two variables can be replaced with a single one as follows: int digit; digit = (isbn % 10); int total = digit * 9; isbn = isbn / 10; digit = (isbn % 10); total = total + digit * 8; isbn = isbn / 10; Fix the Errors in the CodeThe messy code contained a total of eight different coding errors. Each error presented in this section is ordered as it appears in the code from the case study|cash receipt. Error OneString temp = JOptionPane.showInputDialog("") Technically, the above code does not break the functionality of the application as the method call still makes the input dialog appear to take input from the user. Although, from a usability standpoint the code does cause an error. The user who is running the application needs to know what to enter as input into the input dialog. Without a proper message, the user cannot be expected to know what the application is expecting as input. An example of a proper message is as follows: temp = JOptionPane.showInputDialog("Enter the first 9 digits of a 10-digit ISBN number."); Error Twoint isbn = Temp; There are two problems with the above code sample. The first problem being that the variable "Temp" is of type string and not of the primitive type int. The second problem is the name of the variable "Temp". The variable was originally declared as "temp" and Java is a case sensitive language. Both programs are repaired by replacing the code with the line below. isbn = Integer.parseInt(temp); Error Threeisbn = isbn // 10; An extra front slash changes the division operation into a comment which also comments out the semi-colon required at the end of every statement. Remove the extra front slash to correct the error. isbn = isbn / 10; Error Fourdigit = (isbn % 10) The statement above is missing the semi-colon at the end of the line. Add the semi-colon to fix the error. digit = (isbn % 10); Error Fivetotal = total + digit + 6; This error is known as a run time error. A run time error does not cause a compilation error, but it causes the program to produce incorrect results. In the explaination of the ISBN check digit, the sixth ISBN number should be multiplied by six, not added. This error is fixed by changing the second addition operation to a multiplication operation. total = total + digit * 6; Error Sixdigit = (isbn / 10); Just like error five, the above statement causes a run time error. To isolate the last digit in the ISBN number, the ISBN must have the modulus operator applied, not the division operator. The above statement is fixed by replacing the division operator with the modulus operator. digit = (isbn % 10); Error Sevendigit = (isbn * 10); The above statement is almost identical to #Error Six. The multiplication operator should be replaced with the modulus operator. digit = (isbn % 10); Error Eighttotal = total + digit - 1; The error in the code above is error five almost identical to #Error Five. Replace the subtraction operator with the multiplication operator to remedy this error. total = total + digit * 1; Add Code to Output Progress Reports as the Program ExecutesTo add progress reports to the program, three things need to be addressed:
By adding code to report on the status of the application, the user and yourself know what is happening behind the scenes when the program is executing. For a small application like this case study it may seem trivial. It is a good practice to get into as in larger programs consisting of many files each with many methods, it can be difficult to locate a run-time error in your code if the program does not explicitly say what it is currently doing. LocationBy now the code file should be broken up into multiple code blocks, as done earlier in this solution. Each code block should represent a major code segment in the program. Each code block can be considered a potential point for a progress report. Read over the code and decide which are vital points in the execution of the program. Report ContentThe next step is to decide what should be outputted to describe the progress of the program. This output can be the current value of a variable, an output statement saying that the program has reached a certain point in the code, or potentially a combination of both. A progress report should output data on the program that is relevant to its execution. For this case study, each calculation performed on the ISBN is vital to determining the check digit, therefore a progress report should output the value of each calculation. Adding Output CodeThe best way to add output to the program is by adding System.out statements to the appropriate code blocks. When adding the code, make sure the message that will be outputted is unique in comparison to the other progress report statements. Each statement should be unique as they are meant to identify the section of code being executed. If the statements are not unique, then there will be no way of telling which progress report has been outputted. Here is an example of the code before adding a System.out statement. //isolate last digit and multiply by 9 digit = (isbn % 10); total = digit * 9; isbn = isbn / 10; After the output statement is added, the code will look like the following: //isolate last digit and multiply by 9 digit = (isbn % 10); total = digit * 9; isbn = isbn / 10; System.out.println (digit + " * " + 9 + " for a running total of " + total); When the program is executing, the following output will appear in the progress window. The ISBN number used as input for the example below is 1-2345-6789. 9 * 9 for a running total of 81 Now the you will know what part of the code is being executed along with the status of the current calculation. Remember that this is not the only "correct" progress report. There can be many different progress reports based on how the vital points of the program were interpreted. Although, in this case study the calculations are exceptionally important points to the correct execution of the program. | ||
Code |