-2

Here is a basic class I have set up to calculate earnings with stocks. Focus on the "Stock" constructor here:

public class Stock{
    private String symbol;
    private int totalShares;
    private double totalCost;

    public void Stock(String symbol){
        this.symbol = symbol;
        totalShares = 0;
        totalCost = 0.0;
    }

    public double getProfit(double currentPrice){
        double marketValue = totalShares * currentPrice;
        return marketValue - totalCost;
    }

    public void purchase(int shares, double pricePerShare){
        totalShares += shares;
        totalCost += shares*pricePerShare;
    }

    public int getTotalShares(){
        return totalShares;
    }
}

I have created a subclass called DividendStock, which is supposed to calculate dividend earnings.

public class DividendStock extends Stock{
    private double dividends;

    public DividendStock(String symbol){
        super(symbol);
        dividends = 0.0;
    }

    public void payDividend(double amountPerShare){
        dividends += amountPerShare*getTotalShares();
    }
}

The constructor of this class doesnt allow me to call the superclass's constuctor: super(symbol); The error message is as goes: "constructor Stock in class Stock cannot be applied to given types;"

I have searched for a solution, but everything seems to be in place. Any ideas of why it doesnt allow me to call this constructor?

kknydnai
  • 9
  • 1

2 Answers2

5

A constructor does not have any return type. When you put the return type it will be a normal method.

public void Stock(String symbol) {
    this.symbol = symbol;
    totalShares = 0;
    totalCost = 0.0;
}

Should be

public Stock(String symbol) {
    this.symbol = symbol;
    totalShares = 0;
    totalCost = 0.0;
}
Amit Bera
  • 7,075
  • 1
  • 19
  • 42
1

Replace it public void Stock(String symbol){ this.symbol = symbol; totalShares = 0; totalCost = 0.0; }

To public Stock(String symbol){ this.symbol = symbol; totalShares = 0; totalCost = 0.0; }

KMI
  • 258
  • 1
  • 10