In: Computer Science
Please show fully functioning Java code and screenshots of outputs. Please separate by 1a and 1b.
#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:
JProduct.java Code:-
//JProduct.java
public class JProduct implements
Cloneable,Comparable<JProduct>{
private int m_id;
private String productName;
private double price;
public JProduct()
{
m_id=000;
productName="Defalut";
price=0.0;
}
public JProduct(int m_id, String productName, double
wholesalePrice) {
this.m_id = m_id;
this.productName = productName;
this.price = wholesalePrice;
}
public void setPrice(double price) {
this.price = price;
}
public int getM_id() {
return m_id;
}
public String getProductName() {
return productName;
}
public double getPrice() {
return price;
}
@Override
public int compareTo(JProduct o)
{
if(this.getPrice()>o.getPrice()) return 1;
if(this.getPrice()<o.getPrice()) return -1;
else return 0;
}
@Override
protected JProduct clone() throws CloneNotSupportedException
{
return (JProduct) super.clone(); //To change body of generated
methods, choose Tools | Templates.
}
}
JProduct.java Code Screenshot:-
testProduct.java Code:-
//testProduct.java
public class testProduct {
public static void main(String[] args) throws
CloneNotSupportedException {
JProduct productOne=new JProduct();
JProduct productTwo=new JProduct(101, "Mobile", 125.5);
JProduct productThree;
if(productOne.compareTo(productTwo)<0)
{
System.out.println("Cheaper product is :");
System.out.println("Id : "+productOne.getM_id());
System.out.println("Name: "+productOne.getProductName());
System.out.println("Wholesale price:
"+productOne.getPrice());
productThree= productOne.clone();
}
else
{
System.out.println("Cheaper product is :");
System.out.println("Id : "+productTwo.getM_id());
System.out.println("Name: "+productTwo.getProductName());
System.out.println("Wholesale price:
"+productTwo.getPrice());
productThree=productOne.clone();
}
productThree.setPrice(200);
System.out.println("\n\nId : "+productThree.getM_id());
System.out.println("Name: "+productThree.getProductName());
System.out.println("Wholesale price:
"+productThree.getPrice());
}
}
testProduct.java Code Screenshot:-
Output Screenshot:-