Reputation: 107
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
}
}
Upvotes: 8
Views: 94127
Reputation: 2646
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)
Upvotes: 9
Reputation: 627
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
}
}
Upvotes: 2