In: Computer Science
Programming. Write only code for the following. Do not write any comments. You can submit your answer as .java file(s).
#1. Design a Java JProduct class for a product which implements both cloneable and comparable interfaces
The class should have the following private member variables:
and the class should have the following public member functions:
Please write your complete JProduct class and its testing class below
1a (35 pts) Write your complete Java code for the above Java JProduct class in JProduct.java file.
1b (35 pts) In a new file named testProduct.java, write your test Java class and its main method which will do the following:
//Create new project in Netbeans or in eclipse
//Create a new class with name JProduct.java
//Create Test class testProduct.java and run this class
____________________________________________________________________________________________
JProduct.java
/**
*
* @author
*/
public class JProduct implements Comparable<JProduct>,
Cloneable {
private int m_id;//product ID
private String m_name;//product name
private double m_wholesaleprice;//wholesale price
private String[] m_retailers;//all retailers who sell the
product
/**
* Default constructor: initializes the product to some default
one
*/
public JProduct() {
this.m_id = 10;//initialize the product id
this.m_name = "Test 1";//product name
this.m_wholesaleprice = 10.5;//wholesale price
this.m_retailers = new String[]{"Retailer 1", "Retailer 2",
"Retailer 3"};//retailer array
}
/**
* constructor #2: accepts id, name, wholesaleprice and retailers
as
* arguments. The method should set these values to the appropriate
instance
* variables
*
* @param m_id
* @param m_name
* @param m_wholesaleprice
* @param m_retailers
*/
public JProduct(int m_id, String m_name, double m_wholesaleprice,
String[] m_retailers) {
this.m_id = m_id;
this.m_name = m_name;
this.m_wholesaleprice = m_wholesaleprice;
this.m_retailers = m_retailers;
}
/**
* accepts wholesaleprice as argument.
*
* @param m_wholesaleprice
*/
public void setPrice(double m_wholesaleprice) {
this.m_wholesaleprice = m_wholesaleprice;
}
/**
* accepts all retailers from an array as argument.
*
* @param m_retailers
*/
public void setRetailers(String[] m_retailers) {
this.m_retailers = m_retailers;
}
/**
* Four respective get functions to retrieve the four instance
variables
* values.
*/
public int getM_id() {
return m_id;
}
public String getM_name() {
return m_name;
}
public double getM_wholesaleprice() {
return m_wholesaleprice;
}
public String[] getM_retailers() {
return m_retailers;
}
/**
*
* @return @throws CloneNotSupportedException
*/
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone(); //clone with shallow copy
}
@Override
public int compareTo(JProduct o) {
if (this.m_wholesaleprice > o.m_wholesaleprice) {
return 1;
} else {
return -1;
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------
testProduct.java
import java.util.Arrays;
public class testProduct {
public static void main(String[] args) throws
CloneNotSupportedException {
JProduct productOne = new JProduct();//productOne with default
constructor
JProduct productTwo = new JProduct(11, "Test Product 2", 20.5, new
String[]{"XYZ Retailer", "ABC Retailer", "efg
Retailer"});//explicit constructor
JProduct productThree;//productThree which is a clone of the
cheaper one
if (productOne.compareTo(productTwo) == 1) {
// System.out.println("productTwo is cheap");
productThree = (JProduct) productTwo.clone();
} else {
// System.out.println("productOne is cheap");
productThree = (JProduct) productOne.clone();
}
productThree.setPrice(30.25);//reset its price to a new price
productThree.setRetailers(new String[]{"Vihang Retailers","Divya
Retailer","Geetha Retailers"});// reset its retailers to a new set
of retailers
//here we used Arrays.toString to print the array without
loop
System.out.println("Product ID: "+productThree.getM_id()+"\t
Product Name: "+productThree.getM_name()+"\t Wholesale Price:
"+productThree.getM_wholesaleprice()+"\t
Retailers:"+Arrays.toString(productThree.getM_retailers()));
}
}
_________________________________________OUTPUT_________________________________________