Prashin Jeevaganth
Prashin Jeevaganth

Reputation: 1363

Inheritance with Java constructor that uses Scanner

I'm an undergraduate that just started learning Java. This problem arises from an assignment which would be to create a Event Simulator, but I will be showing only the parts of the code I have an issue with, in particular the constructor.

I intend to do create a Queue object and a PriorityQueue object that inherits from the same Queue object, as I want to use the functions of the Queue Object. I intend to create them in a class called QueueManager, but the problem I have now is I want to use a Scanner in the constructor of the Queue(parent class), but I just want to create an empty PriorityQueue (child class) object without scanning.

The error message from the compiler

error: constructor Queue in class Queue cannot be applied to given types;
    public PriorityQueue(){
                          ^
  required: String[]
  found: no arguments
  reason: actual and formal argument lists differ in length

What would be the way to do this, or is it even a good program design?

Queue Manager

class QueueManager{
    private PriorityQueue p;
    private Queue q;

    public QueueManager(String [] args){
        this.q=new Queue(args);
        this.p=new PriorityQueue();
    }

Queue

import java.util.Scanner;

class Queue{
    protected Event [] events =new Event [100];
    private int start=0;
    private int end=0;
    protected int size=0;
    private double time=0;


    public Queue(String [] args){
        Scanner s= new Scanner(System.in);
        int pos=0;
        while (s.hasNextDouble()){       
            double j=s.nextDouble();
            Event arrives=new Event("arrives",new Customer(j));
            this.events[pos]=arrives;
            pos++;
        }
        this.end=pos-1;
        this.size=pos;
    }

PriorityQueue

class PriorityQueue extends Queue{
    private double totaltime=0;
    private int totalsize=0;

    public PriorityQueue(){
    }; 

PS: I came from a Python background and learnt some OOP over there(which seems abit rigged), without any emphasis on programming design practices. If anyone has some hard and fast rules for Java from Python and OOP programming design tips, feel free to share it. Thanks.

Upvotes: 0

Views: 933

Answers (1)

Nathan Hughes
Nathan Hughes

Reputation: 96424

If you define a constructor that takes arguments, you don't get a no-ergument constructor for free anymore. So you're calling a constructor that isn't there, which is what the compiler is griping about.

Here you don't use the args passed into the Queue constructor, just delete the arguments from the constructor definition. If you need both, then you have to define separate constructors, one that takes no arguments, and one that takes the String[] argument.

For separation of concerns: the stuff you have in your Queue constructor really belongs in a separate test harness script, it isn't specific to the Queue itself.

Upvotes: 1

Related Questions