1. Start the NetBeans IDE and open the project named ch02_ex_1_Invoice. This pro
ID: 3548896 • Letter: 1
Question
1. Start the NetBeans IDE and open the project named ch02_ex_1_Invoice. This project should be in the directory: c:murachjava etbeansex_starts
2. Open the file named InvoiceApp.java. Review the code for this file and note that NetBeans doesn't display any errors.
3. Test this application with valid subtotal entries like 50, 150, 250, and 1000 so it's easy to see whether or not the calculations are correct.
4. test the application with a subtotal value like 233.33. This will show that the application doesn't round the results to two decimal places.
5. Test the application with an invalid subtotal value like $1000. This time, the application should crash. Study the error message that's displayed and determine which line of cource code the InvoiceApp class was running when the error occured. Then, jump to this line by clicking on the link to it.
6. restart the application, enter a valid subtotal, and enter 20 when the program asks you whether you want to continue. Whap happens and why?
7. Restart the application and enter two values separated by whitespace before pressing the Enter key. Whap happens and why?
You need the source code to the Murach's Java Programming book found here:
http://www.murach.com/downloads/javp.htm
Explanation / Answer
1. Start the NetBeans IDE and open the project named ch02_ex_1_Invoice. This project should be in the directory: c:murachjava etbeansex_starts
2. Open the file named InvoiceApp.java. Review the code for this file and note that NetBeans doesn't display any errors.
3. Test this application with valid subtotal entries like 50, 150, 250, and 1000 so it's easy to see whether or not the calculations are correct.
// OUTPUT.
Welcome to the Invoice Total Calculator
Enter subtotal: 50
Discount percent: 0.0
Discount amount: 0.0
Invoice total: 50.0
Continue? (y/n): y
Enter subtotal: 150
Discount percent: 0.1
Discount amount: 15.0
Invoice total: 135.0
Continue? (y/n): y
Enter subtotal: 250
Discount percent: 0.2
Discount amount: 50.0
Invoice total: 200.0
Continue? (y/n): y
Enter subtotal: 1000
Discount percent: 0.2
Discount amount: 200.0
Invoice total: 800.0
Continue? (y/n): n
// YES CALCULATIONS ARE CORRECT. BASED ON CODE.
if (subtotal >= 200)
discountPercent = .2;
else if (subtotal >= 100)
discountPercent = .1;
else
discountPercent = 0.0;
//
4. test the application with a subtotal value like 233.33. This will show that the application doesn't round the results to two decimal places.
// OUTPUT
Welcome to the Invoice Total Calculator
Enter subtotal: 233.33
Discount percent: 0.2
Discount amount: 46.666000000000004
Invoice total: 186.66400000000002
Continue? (y/n): n
// YES Application doesn't round the results to two decimal places
5. Test the application with an invalid subtotal value like $1000. This time, the application should crash. Study the error message that's displayed and
determine which line of cource code the InvoiceApp class was running when the error occured. Then, jump to this line by clicking on the link to it.
// APPLICATION CRASHED AT LINE 20
Welcome to the Invoice Total Calculator
Enter subtotal: $1000
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at InvoiceApp.main(InvoiceApp.java:20)
Java Result: 1
// BELOW LINE CAUSED THAT ERROR.
double subtotal = sc.nextDouble();
6. restart the application, enter a valid subtotal, and enter 20 when the program asks you whether you want to continue. Whap happens and why?
// OUTPUT
Welcome to the Invoice Total Calculator
Enter subtotal: 250
Discount percent: 0.2
Discount amount: 50.0
Invoice total: 200.0
Continue? (y/n): 20
AFTER ENTERING 20 when the program asks you whether you want to continue PROGRAM TERMINATED.
because choice equal to 20 since choice.equalsIgnoreCase("y") is not true.
while conditions fails and program is terminated.
7. Restart the application and enter two values separated by whitespace before pressing the Enter key. Whap happens and why?
// OUTPUT
Welcome to the Invoice Total Calculator
Enter subtotal: 100 4
Discount percent: 0.1
Discount amount: 10.0
Invoice total: 90.0
Continue? (y/n):
//
PROGRAM TERMINATED HERE BECAUSE choice read that 4 into string thus choice have value of string 4.
because choice equal to 4 since choice.equalsIgnoreCase("y") is not true.
while conditions fails and program is terminated.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.