This is for java: Here is the code: Thank you Exercise 10-3 Work with an array o
ID: 3705448 • Letter: T
Question
This is for java:
Here is the code:
Thank you
Exercise 10-3 Work with an array of product data In this exercise, you'll work with a two-dimensional array that stores the data for three products Review the application 1. Open the project named chl0 ex3 ProductArray in the extra ex starts directory . Open the classes and review the code. Note that the ProductDB class defines a two- dimensional array that stores the data for three products. Also, note that this class contains three methods that aren't implemented. Run the application. At this point, it should print some messages to the console, but these messages won't contain any product data. 2. Add the code that works with the array of product data 3. In the ProductDB class, add code that implements the getProductByIndex method. This 4. In the Main class, add code that uses the getProductByIndex method and displays the 5. Run the application and make sure it works correctly. At this point, it should print data 6. In the ProductDB class, add code that implements the getProductByCode method. This 7. In the Main class, add code that uses the getProductByCode method and displays the 8. Run the application and make sure it works correctly. At this point, it should print data 9. In the ProductDB class, add code that implements the getAllProducts method. This 10. In the Main class, add code that uses the getAllProducts method and displays the data should return a Product object for the product data at the specified index. data that's returned by it. for the PRODUCT BY INDEX heading. should return a Product object for the product data with the specified code data that's returned by it. for the PRODUCT BY CODE heading should return an array of Product objects for all of the product data. that's returned by it. To do that, you'll need to loop through the array of Product objects that's returned. 11. Run the application and make sure it works correctly. At this point, it should print data for the LIST OF ALL PRODUCTS headingExplanation / Answer
Below is your code: -
//ProductDB.java
package murach.db;
public class ProductDB {
/* Declare productsArray is the string type of array */
private static String[][] productsArray = { { "java", "Murach's Java Programming", "57.50" },
{ "jsp", "Murach's Java Servlets and JSP", "57.50" }, { "mysql", "Murach's MySQL", "54.50" } };
/* implementation of getProductByIndex method */
public static Product getProductByIndex(int i) {
if (i >= 0 && i < productsArray.length) {
return new Product(productsArray[i][0], productsArray[i][1], Double.valueOf(productsArray[i][2]));
}
return null;
}
/* implementation of getProductByCode method */
public static Product getProductByCode(String code) {
for (int i = 0; i < productsArray.length; ++i) {
if (productsArray[i][0].equalsIgnoreCase(code)) {
return getProductByIndex(i);
}
}
return null;
}
/* implementation of getAllProducts method */
public static Product[] getAllProducts() {
Product[] products = new Product[productsArray.length];
for (int i = 0; i < productsArray.length; ++i) {
products[i] = getProductByIndex(i);
}
return products;
}
}
//Product.java
package murach.business;
public class Product {
private String code;
private String description;
private double price;
/* Implementation of default constructor of Product class */
public Product() {
code = "";
description = "";
price = 0;
}
/* Implementation of parameterized constructor of Product class */
public Product(String code, String description, double price) {
this.code = code;
this.description = description;
this.price = price;
}
/* setter method for Code */
public void setCode(String code) {
this.code = code;
}
/* getter method for Code */
public String getCode() {
return code;
}
/* setter method for description */
public void setDescription(String description) {
this.description = description;
}
/* getter method for description */
public String getDescription() {
return description;
}
/* setter method for price */
public void setPrice(double price) {
this.price = price;
}
/* getter method for price */
public double getPrice() {
return price;
}
/* Implementation of getPriceFormatted method */
public String getPriceFormatted() {
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(price);
}
}
package murach.ui;
//Main.java
public class Main {
public static void main(String[] args) {
/* Create the object for Product class */
Product product = ProductDB.getProductByCode("jsp");
/* Display statement */
System.out.println("PRODUCT BY CODE");
System.out.println("Code: " + product.getCode());
System.out.println("Description: " + product.getDescription());
System.out.println("Price: " + product.getPriceFormatted());
System.out.println("----------------------------------------------");
product = ProductDB.getProductByIndex(0);
/* Display statement */
System.out.println("PRODUCT BY INDEX");
System.out.println("Code: " + product.getCode());
System.out.println("Description: " + product.getDescription());
System.out.println("Price: " + product.getPriceFormatted());
System.out.println("----------------------------------------------");
Product[] products = ProductDB.getAllProducts();
/* Display statement */
System.out.println("LIST OF ALL PRODUCTS");
for (int i = 0; i < 3; i++) {
System.out.println("Code: " + products[i].getCode());
System.out.println("Description: " + products[i].getDescription());
System.out.println("Price: " + products[i].getPriceFormatted());
System.out.println();
}
System.out.println("----------------------------------------------");
}
}
Output
PRODUCT BY CODE
Code: jsp
Description: Murach's Java Servlets and JSP
Price: $57.50
----------------------------------------------
PRODUCT BY INDEX
Code: java
Description: Murach's Java Programming
Price: $57.50
----------------------------------------------
LIST OF ALL PRODUCTS
Code: java
Description: Murach's Java Programming
Price: $57.50
Code: jsp
Description: Murach's Java Servlets and JSP
Price: $57.50
Code: mysql
Description: Murach's MySQL
Price: $54.50
----------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.