Frisbee68
Frisbee68

Reputation: 67

Working with constructors and extremely confused

This is my first time trying to do anything with "constructors" and after an hour or so of looking around for help on this topic, I still feel like I have no idea what I am doing.

So here is a class file that I created for a different program, and its supposed to have 2 constructors. I tried my best, but compiler kept telling me I need identifiers.

How do I identify the constructors?

   public class property
{
int storey = 0;
int width = 0;
int length = 0;

    property(int storey, int width, int length)
    {
        {
        this.storey = storey;
        this.width = width;
        this.length = length;
        }

    }

    property(int width, int length)
    {
        this(1, width, length);

    }


    public int calculateArea(int area)
    {

        return (storey * width * length);

    }

    public double calculatePrice(double price)
    {

       return (((storey * width * length) * 2.24) *1.15);

    }

}

Upvotes: 0

Views: 171

Answers (5)

SimonC
SimonC

Reputation: 6718

The compiler is telling you that you need to specify what type the p1 and p2 variables should be. For example:

property(int p1)
{
  // constructor
}

Some other advice:

  • Class names should be upper camel case, i.e. Property.
  • Both constructors are assigning fields to themselves, you should specify storey, width, and length as constructor arguments, then use the this keyword to assign them to fields:

Property(int storey, int width, int length)
{
  this.storey = storey;
  this.width = width;
  this.length = length;
}
  • When you want to default values in a constructor then you can call other constructors:

Property(int width, int length)
{
  this(1, width, length);
}
  • calculateArea and calculatePrice should return the calculated values. Assigning to a parameter will have no effect:

public int calculateArea()
{
    return (storey * width * length);
}
  • Add accessors for the property's fields:

public int getStorey()
{
    return storey;
}

You can then use your property like:

BufferedReader br = new BufferedReader(System.in); 
System.out.println("storey: ");     
int storey = Integer.parseInt(br.readLine()); 

System.out.println("width: ");     
int width = Integer.parseInt(br.readLine()); 

System.out.println("length: "); 
int length = Integer.parseInt(br.readLine()); 

Property p = new Property(storey, width, length); 
System.out.println("property dimensions:width " + p.calculateArea()); 
System.out.println("width: " + p.getWidth()); 
System.out.println("length: " + p.getLength()); 
System.out.println("storeys: " + p.getStoreys()); 
System.out.println("area: " + p.calculateArea());
System.out.println("price: " + p.calculatePrice());    

Upvotes: 3

Nobilis
Nobilis

Reputation: 7448

The constructor needs to know what type p1 and p2 are. And you would probably want to do something with those values, e.g. you could assign the values of p1 or p2 to either width or length.

You've written if(storey >> 1) - don't you mean if (storey > 1)?

Also I would provide some default values for the constructor in case storey isn't 1. Like:

property(int s, int w, int l)
{
    if (l > 1)
    {
        storey = s;
        width = w;
        length = l;
    }
    else
    {
        storey = 0;
        width = 0;
        length = 0;
    }
}

Upvotes: 1

David DeMar
David DeMar

Reputation: 2650

The p1 and p2 you pass into your constructors need to have a type associated with them. So you would this:

public property(int p1)
{
    //do something
}

Upvotes: 0

PSR
PSR

Reputation: 40318

Here you need to specify datatypes.How the compiler know which type they are

 property(p1) 

Example: property(int p1)

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240860

You need to specify type of your argument of constructors, here p1 and p2

for example:

property(int p2)

Upvotes: 0

Related Questions