Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Stop at step 12 once done making a library Stop at step 12 once done making a library Exercise 10-1 Work with packages and

JAVA Stop at step 12 once done making a libraryimage text in transcribedimage text in transcribedimage text in transcribed image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Stop at step 12 once done making a library

Exercise 10-1 Work with packages and libraries This exercise guides you through the process of using packages to organize the classes of an application, and it gives you a chance to work with a library. Review a project that uses packages 1. Open the project named ch10_ex1_LineItem that's in the ex_starts directory, and notice that this project is organized into packages. 2. Review the code for each of the classes, and note that the package statement for each class corresponds with the package directories that are shown in the Projects window. 3. Review the subdirectories and files of this directory: ex_startsch10_ex1 LineItem\src Note that these subdirectories and files correspond with the packages and classes for this project. Work with packages 4. Add a new package named murach.test to the project. 5. Move the LineItemApp class from the murach.app package to the murach.test package. When the Move Class dialog box is displayed, click the Refactor button so that NetBeans automatically modifies the package statement for this class. 6. Delete the package named murach.app. 7. Rename the murach.database package to murach.db. Note that NetBeans automatically renames the directory that corresponds with this package and modifies the package statement for the class that's stored in this package. 8. Open the ProductDB class and comment out its import statement. If you're using NetBeans, this should cause syntax errors that indicate that the ProductDB class can't find the Product class. To fix this, uncomment the import statement. 9. Run the project to make sure it's working correctly. Create a library 10. Use the Build command to compile the project. Then, look in the file system and note that the ch 10_exl_LineItem\dist subdirectory contains a JAR file named ch10_ex1 Lineltem.jar. 11. Rename the JAR file to murach.jar. Use a library 12. Copy the murach.jar file into the ch10_ex1 Product\src directory. Then, open the project named ch 10_exl_Product and review the code in the ProductApp class. 13. Delete the murach business, murach.database, and murach.presentation this should cause syntax errors in the ProductApp class that indicate that the packages, but not the murach.product package. If you're using NetBeans, packages in the import statements and the Product ProductDB, and Console classes can't be found. 14. Add the library that's stored in the murach.jar file to the project's Libraries folder. Note that the ProductApp class still can't find the ProductDB class. 15. Modify the import statement for the ProductDB class so it works correctly. Hint: You modified the name of this package earlier in this exercise. 16. Run this project to make sure it works correctly. with documentation package murach.app; import murach.business.*; import murach.database. ProductDB; import murach.presentation.Console; public class LineItemApp { public static void main(String args[]) { // display a welcome message System.out.println("Welcome to the Line Item Calculator"); System.out.println(); // perform 1 or more calculations String choice = ""; while (choice.equals IgnoreCase ("y")) { // get the input from the user String product Code = Console.getString("Enter product code: "); int quantity = Console.getInt("Enter quantity: ", 0, 1000); // use the Product DB class to get the Product object Product product = ProductDB.getProduct (product Code); // create the LineItem object LineItem lineItem = new LineItem(); lineItem.set Product (product); lineItem.set Quantity (quantity); // display the output System.out.println(); System.out.println("LINE ITEM"); System.out.println("Code: " + product.getCode()); System.out.println("Description: " + product.getDescription()); System.out.println("Price: + product.getPriceFormatted()); System.out.println("Quantity: " + lineItem.getQuantity()); System.out.println("Total: + lineItem.getTotalFormatted() + " "); // see if the user wants to continue choice = Console.getString("Continue? (v): "); System.out.println(); } } } package murach.business; O import java.text. Number Format; * The LineItem class represents a line item and is used by the * Product class. public class LineItem { private Product product; private int quantity; private double total; public LineItem() { this.product = null; this.quantity = 0; this.total = 0; public LineItem (Product product, int quantity) { this.product = product; this.quantity = quantity; } public void setProduct (Product product) { this.product product; } public Product getProduct() { return product; } public int getQuantity () { return quantity; } public void setQuantity (int quantity) { this.quantity quantity: } public double getTotal() { total = quantity * product.getPrice (); return total; } public String gettotalFormatted() { Number Format currency = Number Format.getCurrencyInstance(); return currency.format(this.getTotal()); } } 1 package murach.business; 2 import java.text.Number Format; ** * The Product class represents a product and is used by the * LineItem class. * @author Joel Murach * @version 1.0.0 co wioco Jouw public class Product { 13 14 15 16 17 D private String code; private String description; private double price; * Creates a Product with default values. 19 20 21 public Product() { code = ""; description = price = 0; } 22 23 24 25 26 27 28 29 30 D 31 32 * Sets the product's code. * @param code a String for the product's code */ public void setCode (String code) { this.code = code; } 33 34 35 * Gets the product's code. * @return a String for the product's code 36 37 38 39 public String getCode() { return code; } 40 41 42 43 * Sets the product's description. * @param description a String for the product's description 45 46 47 48 public void setDescription(String description) { this.description = description; } 49 49 50 * Gets the product's description. * @return a String for the product's description 51 52 53 54 V 55 56 57 58 59 60 public String getDescription() { return description; } * Sets the product's price. * @param price a double value for the product's price 61 0 62 63 64 public void setPrice (double price) { this.price = price; } 65 66 67 68 * Gets a double value for the product's price. * @return a double value that represents the product's price TH public double getPrice () { return price; } 71 72 73 74 75 76 77 78 79 80 * Gets a String for the product's price with * standard currency formatting * @return a String for the product's price with standard currency formatting applied ($1,000.00). public String getPriceFormatted() { Number Format currency = Number Format.getCurrencyInstance(); return currency.format (price); } 82 83 } 1 package murach.database; 2 3 o import murach.business.*; 4 public class ProductDB { 5 6 7 0 0 public static Product getProduct(String product Code) { // In a more realistic application, this code would // get the data for the product from a file or database // For now, this code just uses if/else statements // to return the correct product 11 12 13 14 // create the Product object Product product = new Product(); 20 21 22 23 // fill the Product object with data product.setCode (product Code); if (productCode.equals IgnoreCase ("java")) { product.setDescription ("Murach's Java Programming"); product.setPrice (57.50); } else if (product Code.equals IgnoreCase ("jsp")) { product.setDescription ("Murach's Java Servlets and JSP"); product.setPrice (57.50); } else if (product Code.equals IgnoreCase ("mysql")) { product.setDescription ("Murach's MySQL"); product.setPrice (54.50); } else { product.setDescription ("Unknown"); } return product; 24 25 26 27 28 29 30 31 32 } 1 package murach.presentation; import java.util.Scanner; public class Console { private static Scanner sc = new Scanner (System.in); 0 WBFCO O O UWN public static String getString(String prompt) { boolean isValid = false; String s = ""; while (!isValid) { System.out.print (prompt); if (sc.hasNext()) { s = sc.nextLine(); // read entire line isValid = true; } else { System.out.println("Error! Invalid string value. Try again."); } } return s; } 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 0 42 43 44 45 46 47 public static double getDouble(String prompt) { boolean isValid = false; double d = 0; while (!isValid) { System.out.print (prompt); if (sc. hasNext Double()) { d = sc.nextDouble(); isValid = true; } else { sc.next(); // discard the incorrectly entered double System.out.println("Error! Invalid decimal value. Try again."); } sc.nextLine(); // discard any other data entered on the line } return d; } public static double getDouble(String prompt, double min, double max) { double d = 0; boolean isValid = false; while (!isValid) { d = Console.getDouble (prompt); if (d = max) { 48 49 49 50 51 52 53 + max + "."); } else if (d >= max) { System.out.println( "Error! Number must be less than " } else { isValid = true; } 54 55 56 } return d; 57 58 59 D 60 61 62 63 64 65 66 public static int getInt(String prompt) { int i = 0; boolean isValid = false; while (!isValid) { System.out.print (prompt); if (sc.hasNextInt()) { i = sc.nextInt (); isValid = true; } else { sc.next(); // discard invalid data System.out.println("Error! Invalid integer value. Try again."); } sc.nextLine(); // discard any other data entered on the line } return i; } 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 co co co co co co 83 public static int getInt(String prompt, int min, int max) { int i = 0; boolean isValid = false; while (!isValid) { i = Console.getInt (prompt); if (i = max) { System.out.println( "Error! Number must be less than " + max + " } else { isValid = true; } } 87 88 89 90 91 return i; 92 93 } Exercise 10-1 Work with packages and libraries This exercise guides you through the process of using packages to organize the classes of an application, and it gives you a chance to work with a library. Review a project that uses packages 1. Open the project named ch10_ex1_LineItem that's in the ex_starts directory, and notice that this project is organized into packages. 2. Review the code for each of the classes, and note that the package statement for each class corresponds with the package directories that are shown in the Projects window. 3. Review the subdirectories and files of this directory: ex_startsch10_ex1 LineItem\src Note that these subdirectories and files correspond with the packages and classes for this project. Work with packages 4. Add a new package named murach.test to the project. 5. Move the LineItemApp class from the murach.app package to the murach.test package. When the Move Class dialog box is displayed, click the Refactor button so that NetBeans automatically modifies the package statement for this class. 6. Delete the package named murach.app. 7. Rename the murach.database package to murach.db. Note that NetBeans automatically renames the directory that corresponds with this package and modifies the package statement for the class that's stored in this package. 8. Open the ProductDB class and comment out its import statement. If you're using NetBeans, this should cause syntax errors that indicate that the ProductDB class can't find the Product class. To fix this, uncomment the import statement. 9. Run the project to make sure it's working correctly. Create a library 10. Use the Build command to compile the project. Then, look in the file system and note that the ch 10_exl_LineItem\dist subdirectory contains a JAR file named ch10_ex1 Lineltem.jar. 11. Rename the JAR file to murach.jar. Use a library 12. Copy the murach.jar file into the ch10_ex1 Product\src directory. Then, open the project named ch 10_exl_Product and review the code in the ProductApp class. 13. Delete the murach business, murach.database, and murach.presentation this should cause syntax errors in the ProductApp class that indicate that the packages, but not the murach.product package. If you're using NetBeans, packages in the import statements and the Product ProductDB, and Console classes can't be found. 14. Add the library that's stored in the murach.jar file to the project's Libraries folder. Note that the ProductApp class still can't find the ProductDB class. 15. Modify the import statement for the ProductDB class so it works correctly. Hint: You modified the name of this package earlier in this exercise. 16. Run this project to make sure it works correctly. with documentation package murach.app; import murach.business.*; import murach.database. ProductDB; import murach.presentation.Console; public class LineItemApp { public static void main(String args[]) { // display a welcome message System.out.println("Welcome to the Line Item Calculator"); System.out.println(); // perform 1 or more calculations String choice = ""; while (choice.equals IgnoreCase ("y")) { // get the input from the user String product Code = Console.getString("Enter product code: "); int quantity = Console.getInt("Enter quantity: ", 0, 1000); // use the Product DB class to get the Product object Product product = ProductDB.getProduct (product Code); // create the LineItem object LineItem lineItem = new LineItem(); lineItem.set Product (product); lineItem.set Quantity (quantity); // display the output System.out.println(); System.out.println("LINE ITEM"); System.out.println("Code: " + product.getCode()); System.out.println("Description: " + product.getDescription()); System.out.println("Price: + product.getPriceFormatted()); System.out.println("Quantity: " + lineItem.getQuantity()); System.out.println("Total: + lineItem.getTotalFormatted() + " "); // see if the user wants to continue choice = Console.getString("Continue? (v): "); System.out.println(); } } } package murach.business; O import java.text. Number Format; * The LineItem class represents a line item and is used by the * Product class. public class LineItem { private Product product; private int quantity; private double total; public LineItem() { this.product = null; this.quantity = 0; this.total = 0; public LineItem (Product product, int quantity) { this.product = product; this.quantity = quantity; } public void setProduct (Product product) { this.product product; } public Product getProduct() { return product; } public int getQuantity () { return quantity; } public void setQuantity (int quantity) { this.quantity quantity: } public double getTotal() { total = quantity * product.getPrice (); return total; } public String gettotalFormatted() { Number Format currency = Number Format.getCurrencyInstance(); return currency.format(this.getTotal()); } } 1 package murach.business; 2 import java.text.Number Format; ** * The Product class represents a product and is used by the * LineItem class. * @author Joel Murach * @version 1.0.0 co wioco Jouw public class Product { 13 14 15 16 17 D private String code; private String description; private double price; * Creates a Product with default values. 19 20 21 public Product() { code = ""; description = price = 0; } 22 23 24 25 26 27 28 29 30 D 31 32 * Sets the product's code. * @param code a String for the product's code */ public void setCode (String code) { this.code = code; } 33 34 35 * Gets the product's code. * @return a String for the product's code 36 37 38 39 public String getCode() { return code; } 40 41 42 43 * Sets the product's description. * @param description a String for the product's description 45 46 47 48 public void setDescription(String description) { this.description = description; } 49 49 50 * Gets the product's description. * @return a String for the product's description 51 52 53 54 V 55 56 57 58 59 60 public String getDescription() { return description; } * Sets the product's price. * @param price a double value for the product's price 61 0 62 63 64 public void setPrice (double price) { this.price = price; } 65 66 67 68 * Gets a double value for the product's price. * @return a double value that represents the product's price TH public double getPrice () { return price; } 71 72 73 74 75 76 77 78 79 80 * Gets a String for the product's price with * standard currency formatting * @return a String for the product's price with standard currency formatting applied ($1,000.00). public String getPriceFormatted() { Number Format currency = Number Format.getCurrencyInstance(); return currency.format (price); } 82 83 } 1 package murach.database; 2 3 o import murach.business.*; 4 public class ProductDB { 5 6 7 0 0 public static Product getProduct(String product Code) { // In a more realistic application, this code would // get the data for the product from a file or database // For now, this code just uses if/else statements // to return the correct product 11 12 13 14 // create the Product object Product product = new Product(); 20 21 22 23 // fill the Product object with data product.setCode (product Code); if (productCode.equals IgnoreCase ("java")) { product.setDescription ("Murach's Java Programming"); product.setPrice (57.50); } else if (product Code.equals IgnoreCase ("jsp")) { product.setDescription ("Murach's Java Servlets and JSP"); product.setPrice (57.50); } else if (product Code.equals IgnoreCase ("mysql")) { product.setDescription ("Murach's MySQL"); product.setPrice (54.50); } else { product.setDescription ("Unknown"); } return product; 24 25 26 27 28 29 30 31 32 } 1 package murach.presentation; import java.util.Scanner; public class Console { private static Scanner sc = new Scanner (System.in); 0 WBFCO O O UWN public static String getString(String prompt) { boolean isValid = false; String s = ""; while (!isValid) { System.out.print (prompt); if (sc.hasNext()) { s = sc.nextLine(); // read entire line isValid = true; } else { System.out.println("Error! Invalid string value. Try again."); } } return s; } 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 0 42 43 44 45 46 47 public static double getDouble(String prompt) { boolean isValid = false; double d = 0; while (!isValid) { System.out.print (prompt); if (sc. hasNext Double()) { d = sc.nextDouble(); isValid = true; } else { sc.next(); // discard the incorrectly entered double System.out.println("Error! Invalid decimal value. Try again."); } sc.nextLine(); // discard any other data entered on the line } return d; } public static double getDouble(String prompt, double min, double max) { double d = 0; boolean isValid = false; while (!isValid) { d = Console.getDouble (prompt); if (d = max) { 48 49 49 50 51 52 53 + max + "."); } else if (d >= max) { System.out.println( "Error! Number must be less than " } else { isValid = true; } 54 55 56 } return d; 57 58 59 D 60 61 62 63 64 65 66 public static int getInt(String prompt) { int i = 0; boolean isValid = false; while (!isValid) { System.out.print (prompt); if (sc.hasNextInt()) { i = sc.nextInt (); isValid = true; } else { sc.next(); // discard invalid data System.out.println("Error! Invalid integer value. Try again."); } sc.nextLine(); // discard any other data entered on the line } return i; } 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 co co co co co co 83 public static int getInt(String prompt, int min, int max) { int i = 0; boolean isValid = false; while (!isValid) { i = Console.getInt (prompt); if (i = max) { System.out.println( "Error! Number must be less than " + max + " } else { isValid = true; } } 87 88 89 90 91 return i; 92 93 }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 3 Lnai 6323

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

3642159389, 978-3642159381

More Books

Students also viewed these Databases questions

Question

If ( A^2 - A + I = 0 ), then inverse of matrix ( A ) is?

Answered: 1 week ago

Question

What is computer neworking ?

Answered: 1 week ago