Question

In: Computer Science

Programming. Write only code for the following. Do not write any comments. You can submit your...

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:

  • m_id: an integer that holds the product ID
  • m_name: a string that holds the product name
  • m_wholesaleprice: a double that holds the wholesale price
  • m_retailers: a String array that holds all retailers who sell the product

and the class should have the following public member functions:

  • default constructor: initializes the product to some default one
  • constructor #2: accepts id, name, wholesaleprice and retailers as arguments.   The method should set these values to the appropriate instance variables
  • setPrice: accepts wholesaleprice as argument.   The method should set the argument to the appropriate instance variable
  • setRetailers: accepts all retailers from an array as argument. The method should copy the retailers to the respective instance variable.
  • Four respective get functions to retrieve the four instance variables' values.
  • Overrided compareTo()method: use wholesale price for comparison
  • Overrided clone() method which must conduct deep copy.

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 two JProduct objects(productOne, productTwo), productOne with default constructor and productTwo with the explicit constructor;
  •   then compare the two products and output the cheaper one’s id, name, wholesale price (test of comparable interface) and all its retailers;
  •   next create a third JProduct object productThree which is a clone of the cheaper one between productOne and productTwo, and then reset its price to a new price and reset its retailers to a new set of retailers and output its ID, name wholesale price, and all of its retailers.

Solutions

Expert Solution

//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_________________________________________


Related Solutions

write the code in MATLAB with comments and show the inputs and results of the code...
write the code in MATLAB with comments and show the inputs and results of the code for the question below. Write an .m file in MATLAB, that records audio (you can record your own voice for 20 seconds that was recorded using your phone), then take Fourier transform of the signal (probably FFT).
You will submit a proposed company/business for your project for approval. This can be any organization...
You will submit a proposed company/business for your project for approval. This can be any organization that would benefit from a marketing plan. Your team will assume the role that you have been hired to create a marketing plan for this organization. This organization is now your client. The first group assignment will be a written document, not to exceed two pages (single-spaced), providing the following information:  Group Members  Business Name  Product/Service Description  Information Resources (i.e.,...
I have to complete all //to do comments for the following code: /** * A ShoppingBasket...
I have to complete all //to do comments for the following code: /** * A ShoppingBasket holds zero or more Products and can provide information * about the Products. One can add Products to a ShoppingBasket during its * lifetime, reset the ShoppingBasket, create a copy which contains Products of * at least a certain value, and make various queries to the ShoppingBasket. * (Thus, the number of Products that will be stored by a ShoppingBasket object * is not...
CS 400 Assignment 5 Recursive/Backtracking: Generating Permutations WRITE CODE IN C++ PROGRAMMING LANGUAGE WITH COMMENTS INCLUDED...
CS 400 Assignment 5 Recursive/Backtracking: Generating Permutations WRITE CODE IN C++ PROGRAMMING LANGUAGE WITH COMMENTS INCLUDED Description: Mimic the code for N-queen problem (https://introcs.cs.princeton.edu/java/23recursion/Queens.java.html), develop a program that generates all permutations for the set {1, 2, 3, 4, 5}. The output should contain all 5! = 120 permutations. Output Sample: P#1: 1 2 3 4 5 P#2: 1 2 3 5 4 P#3: 1 2 4 3 5 ... P#120: 5 4 3 2 1 Hint: - Thoroughly study the...
There are two programming questions for you to do. Please submit two.s or .asm files. You...
There are two programming questions for you to do. Please submit two.s or .asm files. You can use the usual settings as the previous homework. But if you want to use pseudo-instructions, you will need to uncheck "Bare Machine" and check "Accept Pseudo Instructions" (10 points) Declare an array of integers, something like: . data size : . word 8 array : . word 23 , -12 , 45 , -32 , 52 , -72 , 8 , 13 Write...
Explain your code with comments. Solve in C++. 1. Write a str2int() function that convers a...
Explain your code with comments. Solve in C++. 1. Write a str2int() function that convers a string to integer. The function will take the number as a string then will return it as integer. E.g. str2int(“123”) will return 123 str2int(“005”) will return 5 str2int(“102”) will return 102
write this program in java... don't forget to put comments. You are writing code for a...
write this program in java... don't forget to put comments. You are writing code for a calendar app, and need to determine the end time of a meeting. Write a method that takes a String for a time in H:MM or HH:MM format (the program must accept times that look like 9:21, 10:52, and 09:35) and prints the time 25 minutes later. For example, if the user enters 9:21 the method should output 9:46 and if the user enters 10:52...
Instructions: 1. Please use only C as the language of programming. 2. Please submit the following:...
Instructions: 1. Please use only C as the language of programming. 2. Please submit the following: (1) the client and the server source files each (2) a brief Readme le that shows the usage of the program. 3. Please appropriately comment your program and name all the identifiers suitable, to enable enhanced readability of the code. Problem: Write an ftp client and an ftp server such that the client sends a request to ftp server for downloading a file. The...
Write any java programming to meet the following requirements. Your project must meet the following requirements:...
Write any java programming to meet the following requirements. Your project must meet the following requirements: 1. Specify input specification      Design input 2. Specify Output Specification     Design Output 3. The class must include set methods and get methods ( with or without parameters both) 4. Must include Array structure 5. Must include Input or Output Files or both 6. Demonstrate your knowledge of an Applet
Name your functions countOnesLoop() and countOnesWhere() and submit your code in a file named countOnes.py. Write...
Name your functions countOnesLoop() and countOnesWhere() and submit your code in a file named countOnes.py. Write a function that consists of a set of loops that run through an array and count the number of ones in it. Do the same thing using the where() function (use info(where) to find out how to use it) (in python)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT