Home > other >  Constructor cannot be applied to given types, Required: String Double String; Found: No arguments
Constructor cannot be applied to given types, Required: String Double String; Found: No arguments

Time:11-29

I'm new to Java, and in class we're given a project in which we have to get user's input with JOptionPane and calculating shipping cost of a package and insurance cost of the package. What I'm having trouble with is making the program go to a certain class after saying "no" to insurance and going to a specific "insurance" class after saying "yes" to insurance.

What ends up happening in my code is that in the class usePackage(), when I input Package pack = new Package(); or InsuredPackage pack = new InsuredPackage();, I end up getting an error"Constructor cannot be applied to given types, Required: String,double,String; Found: No Argument.

Here is the code to the 3 files.

import javax.swing.JOptionPane;

public class Package
{
   String name, packWeight;
   static String shipMethod;
   static double weight;
   static double totalCost;
   
   public static void calculateCost() {
      if (shipMethod.equals("A") || shipMethod.equals("a")) {        
         if(weight>=1&&weight<=8) 
            totalCost = 2.00;    
         else if(weight>=9&&weight<=16)
            totalCost = 3.00; 
         else if(weight >= 17)   
            totalCost = 4.00;
      }
      else if (shipMethod.equals("T") || shipMethod.equals("t")) {
         if(weight>=1&&weight<=8)     
            totalCost= 1.50;
         else if(weight>=9 && weight<=16)      
            totalCost = 2.35;
         else if (weight >= 17)    
            totalCost = 3.25;
      
      }
      else if(shipMethod.equals("M") || shipMethod.equals("m")) {
         if(weight>=1 && weight<=8)     
            totalCost = 0.50;
         else if(weight>=9 && weight<=16)   
            totalCost = 1.50;
         else if (weight >= 17)  
            totalCost = 2.15;
      }
   }

   public Package(String name, double weight, String shipMethod) {
      name = JOptionPane.showInputDialog(null, "Enter customer name");
      packWeight = JOptionPane.showInputDialog(null, "Package weight(in lbs)");
      shipMethod = JOptionPane.showInputDialog(null, "Enter shipping mode(Air(A), Truck(T), or Mail(M)");
      // convert to double
      try {
         weight = Double.parseDouble(packWeight);
      } 
      catch (Exception e) {
         System.out.println("Enter only numeric values for weight.");
         return;
      }
      calculateCost();
      display();
   }

   public void display() {
      // display output
      System.out.println("Customer name: "   name);
      System.out.println("Package weight: "   weight   " lbs");
      
      if(shipMethod.equals("A") || shipMethod.equals("a")) {
         System.out.println("Shipping Method: Air");
      }
      else if(shipMethod.equals("T") || shipMethod.equals("t")) {
         System.out.println("Shipping Method: Truck");
      }
      else if(shipMethod.equals("M") || shipMethod.equals("m")) {
         System.out.println("Shipping Method: Mail");
      }      
      System.out.println("Total Cost of shipping: $"   totalCost);
   }
}

------------------------------------------------------------
public class InsuredPackage extends Package
{
   double insurance;
    
   public InsuredPackage(String name, double weight, String shipMethod)
   {
    
       super(name, weight, shipMethod);
       calculateInsuranceCost();
   }
   
   private void calculateInsuranceCost()
   {
      if (totalCost >= 0 && totalCost <= 1) {
         insurance = 2.45;
         totalCost  = 2.45;
      }
      else if (totalCost >= 1.01 && totalCost <= 3.00) {
         insurance = 3.95;
         totalCost  = 3.95;
      }
      else if (totalCost >= 3.01) {
         insurance = 5.55;
         totalCost  = 5.55;
      }
   }
   
   @Override
   public void display() {
      super.display();
      System.out.println("Insurance Cost: "   insurance);
   }

}
------------------------------------------------------------
import javax.swing.JOptionPane;

public class UsePackage
{
   public static void main(String[] args)
   {
      String ins;
      
      ins=JOptionPane.showInputDialog(null, "Do you want insurance(yes/ no)");
      
      if(ins.equals("no")){     
         Package pack = new Package();
      }
      
      // check for insurance
      if(ins.equals("yes")) {
         InsuredPackage pack=new InsuredPackage();
      }
   }

}

I do not know what to do to debug this error.

CodePudding user response:

The error message clearly tells you what's wrong:

Required: String,double,String; Found: No Argument.

You declared your constructor like this:

public InsuredPackage(String name, double weight, String shipMethod) // The constructor needs 3 arguments

But you're using it like this:

InsuredPackage pack=new InsuredPackage();  // Zero arguments.  Whoops!

You have two choices:

  1. Create "pack" with the required arguments

    ... OR ...

  2. Declare another zero-argument constructor for class "InsuredPackage".

And please - PLEASE - get in the habit of indenting your code. For example: https://google.github.io/styleguide/javaguide.html#s4.1-braces

  •  Tags:  
  • java
  • Related