<h2>
Answer:</h2><h2>
</h2>
================================================================
//Create class definition for Product class
public class Product {
//Product class has a name and a price property
//Declare those properties as follows
String name;
double price;
//The Product class has a constructor to initialize the name and price
//properties
.
//Declare the constructor as follows
public Product(String name, double price){
//Initialize the variables
this.name = name;
this.price = price;
}
//Write a getName method to retrieve the name of a Product object
public String getName() {
//This method returns the name value of the Product object
return name;
}
//Write a getPrice method to retrieve the price of a Product object
public double getPrice() {
//This method returns the price value of the Product object
return price;
}
}
===============================================================
//Create the definition for the ProductDemo class
public class ProductDemo {
//Create a main method for the application
public static void main(String[] args) {
//Create an object of the Product class
Product product1 = new Product("Toaster", 29.95);
//Create another object of the Product class
Product product2 = new Product("Machine", 89.89);
//Print out the name of the first product
System.out.println("Name of product 1 " + product1.getName());
//Print out the price of the first product
System.out.println("Price of product 1 " + product1.getPrice());
//Print out the name of the second product
System.out.println("Name of product 2 " + product2.getName());
//Print out the price of the second product
System.out.println("Price of product 2 " + product2.getPrice());
} // End of main method
} // End of class definition
=================================================================
<h2 /><h2>Sample Output:</h2><h2 />
>> Name of product 1 : Toaster
>> Price of product 1 : 29.95
>> Name of product 2 : Machine
>> Price of product 2 : 89.89
=================================================================
<h2>
Explanation:</h2>
The above program has been written in Java and contains comments explaining every part of the program. Please go through the comments.
The program contains two classes and these classes have been written above. The first class is the <em>Product</em> class and the second is the <em>ProductDemo</em> class.
The actual lines of the code have been written in bold face to enhance readability and distinguish from comments.
Sample output of a run of the program has been provided also.