阅读以下说明和Java代码。
[说明]
已知类Stock和类JavaMain都定义在JavaMain.java文件中,类Stock的定义中有四处错误,分别在代码的第01、02、06、07行。请修改错误并给出修改后该行的完整代码,并写出改正错误后程序运行的输出结果。
[Java代码]
01 public class Stock{
02 static {
03 shares=0;
04 share_val=0.0;
05 }
06 private Stock(){getData();}
07 private Stock(int n, double pr=0){
08 shares=n;
09 share_val=pr;
10 getData();
11 }
12 public void getData(){
13 System.out.print(shares+":"+share_val+"");
14 }
15 private int shares; //非静态变量
16 private double share_val; //非静态变量
17 };
18
19 public class JavaMain{
20 public static void main(String args[]){
21 Stock a=new Stock();
22 Stock b=new Stock(1,67.5);
23 //其它代码省略,且代码无输出
24 }
25 }
第1题:
阅读以下说明和 C++代码,填补代码中的空缺,将解答填入答题纸的对应栏内。 【说明】 在股票交易中,股票代理根据客户发出的股票操作指示进行股票的买卖操作。其类图如图5-1所示,相应的c++代码附后。图5-1 类图
【C++代码】 include <iostream> include <string> include <vector> using namespace std; class Stock { private: string name; int quantity; public: Stock(string name ,int quantity) { this->name= name;this->quantity = quantity; } void buy() { cout<<" [买进]股票名称: "<< name << ",数量: "<< quantity << endl;} void sell() { cout<<" [卖出]股票名称: " << name << ",数量:"<< quantity <<endl; } }; class Order { public: virtual void execute() = 0; }; classBuyStock: (1) { private: Stock* stock; public: BuyStock(Stock* stock) { (2) = stock; } void execute() { stock->buy () ; } }; //类SellStock的实现与BuyStock类似,此处略 c1ass Broker { private: vector < Order*> orderList; pub1ic: void takeOrder( (3) order) { orderList.push_back(order);} void p1aceOrders() { for (inti=O; i<orderList.size(); i++) { (4) -> execute () ; } orderList.c1ear(); } }; c1ass StockCommand { pub1ic: void main () { Stock* aStock = new Stock("股票 A" ,10); Stock* bStock = new Stock("股票 B" ,20); Order* buyStockOrder = new BuyStock(aStock); Order* se11StockOrder = new Se11Stock(bStock); Broker* broker = new Broker(); broker->takeOrder(buyStockOrder); broker->takeOrder(se11StockOrder); broker-> (5) () ; } }; int main() { StockCommand* stockCommand = new StockCommand(); stockCommand->main(); de1ete stockCommand; }
第2题:
第3题:
第4题:
阅读以下说明和 Java 代码,填补代码中的空缺,将解答填入答题纸的对应栏内。 【说明】 在股票交易中,股票代理根据客户发出的股票操作指示进行股票的买卖操作。其类图如图 6-1 所示。相应的Java 代码附后。图6-1 类图
【 Java 代码】 import java.util.ArrayList; import java.util.List; class Stock { private String name; private int quantity; public Stock(String name ,int quantity) { this.name = name; this.quantity = quantity; } public void buy() { System.out.println("[ 买进]: " + name + ",数量. " + quantity);} public void sell() { System.out.println("[ 卖出]: " + name + ",数量. " + quantity);} } interface Order { void execute(); } class BuyStock (1) Order { private Stock stock; public BuyStock(Stock stock) { (2) = stock; } public void execute() { stock.buy();} } //类SellStock实现和BuyStock 类似,略 class Broker { private List<Order> orderList = new ArrayList<Order>(); public void takeOrder( (3) order) { orderList.add(order); } public void placeOrders() { for ( (4) order : orderList) { order.execute(); } orderList.clear(); } } public class StockCommand { public static void main(String[] args) { Stock aStock = new Stock("股票 A" ,10); Stock bStock = new Stock("股票 B" ,20); Order buyStockOrder = new BuyStock(aStock); Order sellStockOrder = new SellStock(bStock ); Broker broker = new Broker(); broker.takeOrder(buyStockOrder); broker.takeOrder(sellStockOrder); broker. (5) ; } }
第5题: