Question

In: Computer Science

Please show fully functioning Java code and screenshots of outputs. Please separate by 1a and 1b....

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:

  • 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

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:-


Related Solutions

Please show screenshot outputs and fully functional code for the Java program. Write the following methods...
Please show screenshot outputs and fully functional code for the Java program. Write the following methods to   1) read the content of an array of 5 doubles public static double[] readingArray() 2) find and print:the smallest element in an array of 5 double public static void smallest(double [] array) 3) find and print:the largest element in an array of 5 doubles pubic static void largest (double [] array) In the main method - invoke readingArray and enter the following numbers...
OPERATING SYSTEMS HOMEWORK: PLEASE CODE IN JAVA with comments & POST SCREENSHOTS OF OUTPUTS SCAN This...
OPERATING SYSTEMS HOMEWORK: PLEASE CODE IN JAVA with comments & POST SCREENSHOTS OF OUTPUTS SCAN This algorithm is performed by moving the R/W head back-and-forth to the innermost and outermost track. As it scans the tracks from end to end, it process all the requests found in the direction it is headed. This will ensure that all track requests, whether in the outermost, middle or innermost location, will be traversed by the access arm thereby finding all the requests. This...
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h"...
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h" #include"BST.cpp" using namespace std; std::vector<std::string> tokenize(char line[]) {    std::vector<std::string> tok;        std::string word = "";        for (int i = 0; i < strlen(line); i++)        {            if (i == strlen(line) - 1)            {                word += line[i];                tok.push_back(word);                return tok;       ...
Java and please have screenshots with source code and output \item[(1)] A palindrome is a string...
Java and please have screenshots with source code and output \item[(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a method to test if a LinkedList<Long> has Long values that form a Fibonacci sequence from the beginning to the end and return true if it is and false otherwise. A sequence of values is Fibonnaci if every third value is equal to sum of the previous two. Eg., 3,4,7,11,18,29 is a Fibonacci sequence whereas 1,2,3,4 is not, because 2+3 is not equal to 4....
Please answer 1a and 1b. 1a. What is dark matter? How was its existence discovered, and...
Please answer 1a and 1b. 1a. What is dark matter? How was its existence discovered, and why do astronomers think it must be present? 1b. What are quasars and how are they used to study the evolution of the Universe?
Please post screenshots of outputs. Also make sure to use stacks and queues. You will design...
Please post screenshots of outputs. Also make sure to use stacks and queues. You will design a program to keep track of a restaurants waitlist using a queue implemented with a linked list. Make sure to read pages 1215-1217 and 1227-1251 1. Create a class named waitList that can store a name and number of guests. Use constructors to automatically initialize the member variables. 2. Add the following operations to your program: a. Return the first person in the queue...
1. Please use Python 3 programing. 2. Please share your code. 3. Please show all outputs....
1. Please use Python 3 programing. 2. Please share your code. 3. Please show all outputs. Create a GUI Calculator with the following: Title : Calculator Label and Entry box for 1st Number Label and Entry box for 2nd Number Buttons for *, /, +, - Label for result and Displaying the result
This question is about java program. Please show the output and the detail code and comment...
This question is about java program. Please show the output and the detail code and comment of the each question and each Class and interface. And the output (the test mthod )must be all the true! Thank you! Question1 Create a class Animal with the following UML specification: +-----------------------+ | Animal | +-----------------------+ | - name: String | +-----------------------+ | + Animal(String name) | | + getName(): String | | + getLegs(): int | | + canFly(): boolean | |...
Write a R-script to (and show the outputs of your code) (a) Create a sequence of...
Write a R-script to (and show the outputs of your code) (a) Create a sequence of numbers starting at 3.5 and ending at 10.7 with increments of 0.79. Find the variance and mean of those numbers. And finally sort the vector in a decreasing manner (b) Create a 3 different 3 by 3 matrices such that each of the numbers 1,2,...,9 appear exactly once (Sudoku style) in each of the matrices.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT