Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java error: constructor in class cannot be applied to given types

Tags:

java

I just added the constructor Building and I thought everything would work fine, but I'm getting an error on line 43. When I create the object, Building b = new Building();, it says I need to have a double and int in the argument, so I did as it said, but I just keep getting more errors. What am I doing wrong?

// This program lets the user design the area and stories of a building multiple times
// Author: Noah Davidson
// Date: February 20, 2014

import java.util.*;

public class Building // Class begins
{
    static Scanner console = new Scanner(System.in);

    double area; // Attributes of a building
    int floors;

    public Building(double squarefootage, int stories)
    {
        area = squarefootage;
        floors = stories;
    }

    void get_squarefootage() // User enters the area of floor
    {
        System.out.println("Please enter the square footage of the floor.");
        area = console.nextDouble();
    }

    void get_stories() // The user enters the amount of floors in the building
    {
        System.out.println("Please enter the number of floors in the building.");
        floors = console.nextInt();
    }

    void get_info() // This function prints outs the variables of the building
    {
        System.out.println("The area is: " + area + " feet squared");
        System.out.println("The number of stories in the building: " + floors + " levels");
    }

    public static void main(String[] args) // Main starts
    {
        char ans; // Allows for char

        do{ // 'do/while' loop starts so user can reiterate
            // the program as many times as they desire

            Building b = new Building(); // Creates the object b
            b.get_squarefootage(); // Calls the user to enter the area
            b.get_stories(); // Calls the user to enter the floors
            System.out.println("---------------");
            b.get_info(); // Displays the variables
            System.out.println("Would you like to repeat this program? (Y/N)");
            ans = console.next().charAt(0); // The user enters either Y or y until
                                            // they wish to exit the program

        } while(ans == 'Y' || ans == 'y'); // Test of do/while loop
    }
}
like image 400
NJD Avatar asked Mar 11 '14 23:03

NJD


People also ask

Which type of constructor is not possible in Java?

Java constructor can not be static A constructor is called when an object of a class is created, so no use of the static constructor.

Which keywords are not allowed with constructor in Java?

But, a constructor in Java cannot be overridden therefore, there is no need of using the final keyword with the constructor. Since you cannot override a constructor you cannot provide body to it if it is made abstract. Therefore, you cannot use abstract keyword with the constructor.

Which constructor Cannot accept parameters?

1. No-argument constructor: A constructor that has no parameter is known as the default constructor. If we don't define a constructor in a class, then the compiler creates a default constructor(with no arguments) for the class.

Which constructor Cannot be used for process Synchronisation?

No, constructor cannot be synchronized. Because constructor is used for instantiating object, when we are in constructor object is under creation. So, until object is not instantiated it does not need any synchronization. Any attempt to do so is against java coding syntax.


2 Answers

Your problem is this line here: Building b = new Building(); // Creates the object b

Your constructor is set up to take two arguments, a double and an int, but you pass neither.

Try something like this to remove the error:

double area = 0.0;
int floors = 0;
Building b = new Building(area, floors);

Perhaps a better idea would be to just have a constructor that took no parameters:

public Building() {
    this.area = 0.0;
    this.floors = 0;
}

After I apply these changes, the code compiles and runs... (see the picture below)

Confirmation that the program compiles and runs

like image 50
Josh Engelsma Avatar answered Nov 14 '22 23:11

Josh Engelsma


I have fixed and tested your code. It now runs. You need to add two arguments to the constructor (double and int).

import java.util.*;

public class Building // The class begins
{
    static Scanner console = new Scanner(System.in);

    double area; // Attributes of a building
    int floors;

    public Building (double squarefootage, int stories)
    {
        area = squarefootage;
        floors = stories;
    }

    void get_squarefootage() // The user enters the area of floor
    {
        System.out.println ("Please enter the square footage of the floor.");
        area = console.nextDouble();
    }

    void get_stories() // The user enters the amount of floors in the building
    {
        System.out.println ("Please enter the number of floors in the building.");
        floors = console.nextInt();
    }

    void get_info() // This function prints outs the vaibles of the building
    {
        System.out.println ("The area is: " + area + " feet squared");
        System.out.println ("The number of stroies in the building: " + floors + " levels");
    }

    public static void main(String[] args) // Main starts
    {
        char ans; // Allows for char

        do{ // 'do/while' loop starts so user can reiterate
            // the program as many times as they desire

            double a = 1;
            int c = 2;
            Building b = new Building(a, c); // Creates the object b
            b.get_squarefootage(); // Calls the user to enter the area
            b.get_stories(); // Calls the user to enter the floors
            System.out.println("---------------");
            b.get_info(); // Displays the variables
            System.out.println("Would you like to repeat this program? (Y/N)");
            ans = console.next().charAt(0); // The user enters either Y or y until
                                            // they wish to exit the program

        } while(ans == 'Y' || ans == 'y'); // Test of do/while loop
    }
}
like image 40
Neptune Avatar answered Nov 14 '22 21:11

Neptune