1. create a class containing two data fields: stockName (a string), and stockPrice (a double). and implementing the Comparable interface (check Java API).  Write the appropriate accessor and mutator methods, as well as the toString, equals, and compareTo methods. The equals methods returns true if two Stock objects have the same name and price, otherwise false. The compareTo method takes a Stock object as argument and returns an positive integer if the calling object’s price is greater than the argument object’s stock price, 0 if the two stocks have the same price, otherwise a negative integer. 2. Demonstrate the class in a program to: 3. Analyze the worst-case time complexity for sorting an array of Stock objects and searching a stock in the array. Suppose the problem size (the array size is n). Requirements: 1. program should be well-documented 2. time efficiency analysis should be typed in a .txt or .docx file. Upload the file 3. Submit the repl.it link of the program Purchase the answer to view it

To create a class that implements the Comparable interface and contains the data fields stockName (a string) and stockPrice (a double), as well as the required methods, we can start by defining the class as follows:

“`java
public class Stock implements Comparable {
private String stockName;
private double stockPrice;

// Constructor
public Stock(String name, double price) {
this.stockName = name;
this.stockPrice = price;
}

// Accessor methods
public String getStockName() {
return this.stockName;
}

public double getStockPrice() {
return this.stockPrice;
}

// Mutator methods
public void setStockName(String name) {
this.stockName = name;
}

public void setStockPrice(double price) {
this.stockPrice = price;
}

// toString method
@Override
public String toString() {
return “Stock: ” + this.stockName + “, Price: $” + this.stockPrice;
}

// equals method
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Stock otherStock = (Stock) obj;
return this.stockName.equals(otherStock.stockName) && this.stockPrice == otherStock.stockPrice;
}

// compareTo method
@Override
public int compareTo(Stock otherStock) {
return Double.compare(this.stockPrice, otherStock.stockPrice);
}
}
“`

In the class above, we have implemented the Comparable interface, which requires us to define the `compareTo` method. Additionally, we have implemented the `equals` method for comparing equality based on the stock name and price.

To demonstrate this class in a program, we can create instances of `Stock` objects and use the methods defined in the class. Here is an example program:

“`java
public class StockDemo {
public static void main(String[] args) {
Stock stock1 = new Stock(“Apple”, 200.0);
Stock stock2 = new Stock(“Microsoft”, 150.0);

System.out.println(stock1.toString());
System.out.println(stock2.toString());

System.out.println(“Are stock1 and stock2 equal? ” + stock1.equals(stock2));

int comparisonResult = stock1.compareTo(stock2);
if (comparisonResult > 0) {
System.out.println(stock1.getStockName() + ” has a higher price than ” + stock2.getStockName());
} else if (comparisonResult == 0) {
System.out.println(stock1.getStockName() + ” and ” + stock2.getStockName() + ” have the same price”);
} else {
System.out.println(stock2.getStockName() + ” has a higher price than ” + stock1.getStockName());
}
}
}
“`
This program creates two `Stock` objects, `stock1` with a stock name “Apple” and a price of 200.0, and `stock2` with a stock name “Microsoft” and a price of 150.0. The program then prints the details of each stock, checks their equality, and compares their prices using the `compareTo` method.

Now, let’s move on to analyzing the worst-case time complexity for sorting an array of `Stock` objects and searching for a stock in the array.

Need your ASSIGNMENT done? Use our paper writing service to score better and meet your deadline.


Click Here to Make an Order Click Here to Hire a Writer