Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Construtor Drink in class Drink cannot be applied to given type

I am facing an error. The code contains two classes, one is Store and the other is Drink. The purpose is to calculate the total cost. I have written the code according to the UML class diagram which is given. I declared classes and variable according to it.

The error is:

constructor Drink in class Drink cannot be applied to given types;

Drink firstOrder=new Drink();
                         ^
  required: String,double
  found: no arguments
  reason: actual and formal argument lists differ in length

Here is my code. This is my assignment work, can anyone help me out on this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public class Store
{

    private Drink orderOne;
    private Drink orderTwo;
    public Store(String name,double cost,String nameone,double costone)
    {
        Drink firstOrder=new Drink();
        firstOrder.orderOne=name;
        firstOrder.orderOne=cost;
        Drink secondOrder=new Drink();
        secondOrder.orderTwo=nameone;
        secondOrder.orderTwo=costone; 
    }
    
    public static void main(String args[]) {
        Store s1=new Store("Lemon Tea",5.25,Coffe,3.99);
        Drink d2=new Drink("Lemon Tea",5.25);
        Drink d2=new Drink("Coffe",3.99);
        s1.printReport();
    }

    void printReport()
    {
        Drink dr=new Drink();
        dr.printDrink();
        dr.getCost();
    }
}
class Drink{
    private String name;
    private double cost;

    public Drink(String name2,double cost2)
    {
        name=name2;
        cost=cost2;
    }

    void printDrink()
    {
        store obj;
        System.out.println("Drink order one is ");
        System.out.print(obj.name);
        System.out.print("( " );
        System.out.print(cost);
        System.out.print(")");
        System.out.println("Drink order two is ");
        System.out.print(obj.nameOne);
        System.out.print("( " );
        System.out.print(costOne);
        System.out.print(")");
    }

    double getCost()
    {
        store obj1;
        double total;
        total=obj1.cost+obj1.cost1;
        System.out.println("Total Cost : $ "+ total);
    }
}

>Solution :

Add a public Drink() {} constructor to your Drink class, that needs a second constructor, without argument.

class Drink{
    private String name;
    private double cost;

   public Drink() {
   }

   public Drink(String name2,double cost2) {
       name=name2;
       cost=cost2;
    }

    void printDrink() {
        store obj;
        System.out.println("Drink order one is ");
        System.out.print(obj.name);
        System.out.print("( " );
         System.out.print(cost);
         System.out.print(")");
       System.out.println("Drink order two is ");
        System.out.print(obj.nameOne);
        System.out.print("( " );
         System.out.print(costOne);
         System.out.print(")");
    }

    double getCost() {
        store obj1;
        double total;
        total=obj1.cost+obj1.cost1;
        System.out.println("Total Cost : $ "+ total);
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading