The Cartesian product of two sets A and B (also called the product set, set dire
ID: 3757595 • Letter: T
Question
The Cartesian product of two sets A and B (also called the product set, set direct product, or cross product) is defined to be the set of all points where and . Implement a class that compute the Cartesian product of two sets A and B. Your class should define the following datafields and class properties:
• double [ ] setA
• double [ ] setB
• double [ ] [ ] ProductAB
• void CartesianProduct ( )
• void DisplayCartesianProduct ( )
a. Write a test program that read two sets A, B as inputs, compute the Cartesian product of A and B, and display the result.
b. Use the Cartesian product class to implement the cloneable interface. In your test program create a clone object using the clone method.
Explanation / Answer
1.
double[] cartesianProduct(double[] setA, double[] setB) {
double[] ProductAB = new double[setA.length * setB.length];
double index = 0;
for (double v1: one) {
for (double v2: two) {
ProductAB[index] = v1 * v2;
index++;
}
}
return ProductAB;
}
2)
public class ProductCloneApp implements Cloneable
{
private String code;
private String description;
private double price;
// the code for the constructor and methods
@Override
public Object clone() throws CloneNotSupportedException
{
return super.clone();
}
public static void main(String args[]) throws CloneNotSupportedException
{
// create a new product
Product p1 = new Product();
p1.setCode("Cartesian product");
p1.setDescription("Clone");
p1.setPrice(49.50);
// Cartesian product
Product p2 = (Product) p1.clone();
// change a value in the cloned product
p2.setPrice(44.50);
if (p1.getPrice() == p2.getPrice())
{
System.out.println("FAILURE: The clone method of the Product class is not cloning data.");
}
else if (p1.getPrice() != p2.getPrice())
{
System.out.println("SUCCESS: The clone method of the Product class is cloning data.");
}
System.out.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.