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 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...
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?
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 | |...
Please write the program in JAVA and provide me with the output screenshots!! Assignment Objectives •...
Please write the program in JAVA and provide me with the output screenshots!! Assignment Objectives • Be able to write methods • Be able to call methods Introduction Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing the smaller tasks (calling the methods) in the correct...
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.
Please attach the output screenshots, narrative descriptions, or paste the Python codes. Please write code to...
Please attach the output screenshots, narrative descriptions, or paste the Python codes. Please write code to print the type of the following variables. Please write down the codes and the output as well. x = 3.5 y = '3.5' z = {1:'John', 2:'Wick', 3:'Barry', 4:'Allen'}
JAVA: How do I fix the last "if" statement in my code so that outputs the...
JAVA: How do I fix the last "if" statement in my code so that outputs the SECOND HIGHEST/MAXIMUM GPA out of the given classes? public class app { private static Object minStudent; private static Object maxStudent; public static void main(String args[ ]) { student st1 = new student("Rebecca", "Collins", 22, 3.3); student st2 = new student("Alex", "White", 19, 2.8); student st3 = new student("Jordan", "Anderson", 22, 3.1); student[] studentArray; studentArray = new student[3]; studentArray[0] = st1; studentArray[1] = st2; studentArray[2]...
1a. Write the three-dimensional state of uniform stress in a deformable medium 1b. . Show from...
1a. Write the three-dimensional state of uniform stress in a deformable medium 1b. . Show from table below that total true stain is equal to the sum of the incremental true strains. Consider a rod of 2 cm long that is elongated in three increments, each has a strain of 0.1. Increment length e 0 2 0.1 1 2.20 0.1 2 2.42 0.1 3 2.662 0.1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT