Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Create a ProductException class whose constructor receives a String that cons

ID: 3557317 • Letter: 2

Question

2. Create a ProductException class whose constructor receives a String that consists of a product number and price. Save the file as ProductException.java. Create a Product class with two fields, productNum and price. The Product constructor requires values for both fields. Upon construction, throw a ProductException if the product number does not consist of three digits, if the price is less than $0.01, or if the price is over $1,000. Save the class as Product.java. Write an application that establishes at least four Product objects with valid and invalid values. Display an appropriate message when a Product object is created successfully and when one is not. Save the file as ThrowProductException.java. thanks. Points here only, need ASAP!!!

Explanation / Answer

// Create a ProductException class whose constructor receives a String that consists of a product number and price.
// Save the file as ProductException.java.

class ProductException extends Exception
{
    public ProductException(String str)
    {
       System.out.println("Invalid Product Object");
   }
}

//Create a Product class with two fields, productNum and price.
//The Product constructor requires values for both fields.
//Upon construction, throw a ProductException if the product number does not consist of three digits,
//if the price is less than $0.01, or if the price is over $1,000.
//Save the class as Product.java.

class Product
{
   private int productNum;
   private double price;
   public Product(int num, double p) throws ProductException
   {
   String str = Integer.toString(num);
   if(str.length()!=3) throw new ProductException(str+Double.toString(p));
   productNum = num;
   if(p<0.01 || p > 1000)
   throw new ProductException(str+Double.toString(p));
   price = p;
   System.out.println("Product Object Created Successfully");
   }
}
// Write an application that establishes at least four Product objects with valid and invalid values.
// Display an appropriate message when a Product object is created successfully and when one is not.
// Save the file as ThrowProductException.java.

public class ThrowProductException
{
   public static void main(String[] args) throws ProductException
   {
    try
   {
   Product P1 = new Product(123,10);
   }
   catch(ProductException PE)
   {
  
   }
   try { Product P2 = new Product(23,10);}
   catch(ProductException PE) { }
   try { Product P3 = new Product(123,10000);}
   catch(ProductException PE) { }
   try {    Product P4 = new Product(456,10);}
   catch(ProductException PE) { }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote