Yung Moglis
Yung Moglis

Reputation: 1

Constructor is not being recognized

When I run the program I get: The constructor Package() is undefined and The constructor InsuredPackage() is undefined The classes Package and InsuredPackage work fine and have no issues, its the main thats giving me trouble. Package a = new Package(); and InsuredPackage b = new InsuredPackage(); are underlined.

import java.io.*;
import java.util.*;

public class homework06
{
    public static void main(String[] args)
    {
        ArrayList<String> simple = new ArrayList<String>();
        ArrayList<String> insured = new ArrayList<String>();
        Scanner sc = new Scanner(System.in);
        boolean bool = true;
        while(bool == true)
        {
            System.out.println("Choose Option:");
            System.out.printf("1. Simple Package\n2. Insured Package\n3. Exit\n");
            int x = sc.nextInt();
            if(x == 1)
            {
                Package a = new Package();
                a.id = sc.next();
                a.weight = sc.nextDouble();
            }
            else if(x == 2)
            {
                InsuredPackage b = new InsuredPackage();
                b.id = sc.next();
                b.weight = sc.nextDouble();
            }
            else if(x == 3)
            {
                bool = false;
            }
        }
        sc.close();
    }    
}

public class Package
{
    String id;
    double weight;

    public Package(String id,double weight)
    {
        this.id = id;
        this.weight = weight;
    }

    public String getID()
    {  
        return id;

    }

    public double getW()
    {
        return weight;
    }

    public double computeCost()
    {
        double charge=0;
        if(weight>0 && weight<=3)
        {
            charge = 3;
        }
        else if(weight>3)
        {
            charge = 3 + (0.7*(weight-3));
        }
        return charge;
    }

    public String toString()
    {
        return id + "," + weight + "," + computeCost();
    }

}

public class InsuredPackage extends Package
{    
    public InsuredPackage(String id,double weight)
    {
        super(id,weight);
    }

    public double computeCostExtra()
    {
        double extra=0;
        double charge = computeCost();
        if(charge>0 && charge<=5)
        {
            extra = 2;
        }
        else if(charge>5 && charge<=10)
        {
            extra = 3;
        }
        else if(charge>10)
        {
            extra = 5;
        }
        return charge + extra;
    }
    
    public String toString()
    {
        return id + "," + weight + "," + computeCostExtra();
    }
}



Upvotes: 0

Views: 115

Answers (2)

ray
ray

Reputation: 1670

Java create default constructor if you haven't declared any within your class. Default constructor is no-argument constructor.

Inside both InsuredPackage and Package classes you have create constructor with arguments. Therefore java will not create default constructor and yet you have called no-argument constructor within your code.

InsuredPackage b = new InsuredPackage();

and

Package a = new Package();

You need to create no-argument constructor inside you classes.

Upvotes: 1

Sujay Mohan
Sujay Mohan

Reputation: 950

When you add Constructor with arguments, java will not consider default no arguments constructor unless explicitly specified. So it will throw the error. Either you need to add the constructor or you need to pass the parameters as mentioned in the answer by @cheenu.

Upvotes: 0

Related Questions