Yuri
Yuri

Reputation: 99

Constructor of a child class

folks.... i have a child class GateNot which extends Gate. I'm not sure how to fill out the constructor of GateNot since Im not given any instance variables inside a child class. What would be the approach?

public class GateNot extends Gate {

    public GateNot(Wire input, Wire output)
    {
        super()
    }

}


import java.util.*;


public abstract class Gate implements Logic {

    private List<Wire> inputs;
    private Wire output;
    private String name;

    public Gate(String name, List<Wire> ins, Wire out)
    {
        this.name = name;
        this.output = out;

        if(ins.size() == 0 || ins.isEmpty())
            throw new ExceptionLogicParameters(false, 1, 0);
        else 
            this.inputs = ins;
    }

    @Override
    public void feed(List<Signal> inSigs)
    {
        if(inSigs.size() != inputs.size())
            throw new ExceptionLogicParameters(false, inputs.size(), inSigs.size());
        else
        {
            for(int i = 0; i < inSigs.size(); i++)
            {
                inputs.get(i).setSignal(inSigs.get(i));
            }
        }

    }

    @Override
    public void feed(String name)
    {
        if(!(this.name.equals(name)))
                throw new ExceptionLogicMalformedSignal(name.charAt(0), "Invalid logic input");
        else
        {
            Signal signalValue = Signal.fromString(name.charAt(0));
        }

    }

    @Override
    public List<Signal> read()
    {
        List<Signal> signals = new ArrayList<>();
        signals.add(output.getSignal());

        return signals;
    }

    @Override
    public String toString()
    {
        return this.name+"( " + inputs.toString() + " | " + output.toString() + " )";
    }

    @Override
    public boolean equals(Object other)
    {
        if(other instanceof Gate)
        {
            Gate someGate = (Gate)other;
            return (this.inputs == someGate.inputs) && (this.output.equals(someGate.output)
                    && (this.name.equals(someGate.name)));
        }
        else 
            return false;
    }

    public List<Wire>getInputs()
    {
        return this.inputs;
    }

    public Wire getOutput()
    {
        return this.output;
    }

    public String getName()
    {
        return this.name;
    }

    public void setInputs(List<Wire> inputs)
    {
        this.inputs = inputs;
    }

    public void setOutput(Wire output)
    {
        this.output = output;
    }

    public void setName(String name)
    {
        this.name = name;
    }

}

Upvotes: 1

Views: 235

Answers (1)

Eran
Eran

Reputation: 393851

You have to call the super class's constructor.

Based on the argument types and names of the super class's constructor, I'd say this is what you need :

public GateNot(Wire input, Wire output)
{
    super("Not", Arrays.asList(new Wire[]{input}), output);
}
  • You can pass whatever String you wish as name
  • You should convert the input to a List of inputs
  • You can pass the output as is

Upvotes: 4

Related Questions