public class Stock { private string symbol; privateString name; private double previousClosingPrice; private double currentPrice; public stock(){ public Stock(string newSymbol,string newName) {symbol = newSymbol;name = newName; public double getChangePercent() {return (currentPrice - previousClosingPrice)/previousClosingPrice; public double getPreviousClosingPrice(){return previousClosingPrice; public double getCurrentPrice() ( return currentPrice; public void setCurrentPrice(double newCurrentPrice) currentPrice = newCurrentPrice; public void setPreviousClosingPrice(double newPreviousClosingPrice){previousClosingPrice = newPreviousClosingPrice; Which of the followings has an error if it is written in a separate class called TestStock,java?A)Stock stock = new Stock("WKUCS","Wenzhou-Kean Univ.Computer Science"); stock.setCurrentPrice(90); System.out.printIn("percentage change is:" + stock.getChangePercent()); Stock stock = new Stock("WKUCS","Wenzhou-Kean Univ, Computer Science");B)stock.setPreviousClosingPrice(1231); stock.setCurrentPrice(2231); System.out.printIn("Previous Price:"+ stockgetPreviousClosingPrice());System.out.printIn("Current Price:+ stock.getCurrentPrice());(C) Stock stock = new Stock("WKUCS","Wenzhou-Kean Univ.Computer Science");stock.setCurrentPrice(100):System.out.println("Stock Name is: "+ stock.name); DStock stock=new Stock();System.out.printIn("percentage change is:"+ stock.getCurrentPrice());
时间: 2024-02-11 14:07:52 浏览: 68
The correct code for a separate class called TestStock that does not have any errors is:
```
public class TestStock {
public static void main(String[] args) {
Stock stock = new Stock("WKUCS", "Wenzhou-Kean Univ.Computer Science");
stock.setCurrentPrice(90);
System.out.println("Percentage change is: " + stock.getChangePercent());
Stock stock2 = new Stock("WKUCS", "Wenzhou-Kean Univ.Computer Science");
stock2.setPreviousClosingPrice(1231);
stock2.setCurrentPrice(2231);
System.out.println("Previous Price: " + stock2.getPreviousClosingPrice());
System.out.println("Current Price: " + stock2.getCurrentPrice());
}
}
```
Option C has an error because the `name` field of the `Stock` class is declared as private, so it cannot be accessed directly from another class like this:
```
Stock stock = new Stock("WKUCS", "Wenzhou-Kean Univ.Computer Science");
stock.setCurrentPrice(100);
System.out.println("Stock Name is: " + stock.name);
```
Option D has an error because the `Stock` class does not have a default constructor, so you cannot create an object of this class without passing any arguments:
```
Stock stock = new Stock();
System.out.println("Percentage change is: " + stock.getCurrentPrice());
```
阅读全文