0

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);

    }

}
Frisbee68
  • 67
  • 1
  • 8
  • 1
    p1 and p2 need to have data types: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html – JP_ May 28 '13 at 02:47
  • 1
    As the answers show, you need data types for your contructor parameters, but I suspect you also want multiple parameters too. It looks like you are assigning the member valiables storey, width and length their default values. That doesn't seem right. I suspect you meant to pass those values into the constructor. – Mr Moose May 28 '13 at 02:53
  • What are p1 and p2. If they are variable then declare those first. – JDGuide May 28 '13 at 03:21
  • declare the datatypes of p1 and p2, and also override the multiple constructors with different type and no. of arguments. – Asad May 28 '13 at 04:28

5 Answers5

3

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());    
SimonC
  • 6,590
  • 1
  • 23
  • 40
  • But I can't give values to the storey width and height...like they are being entered into another program that uses the property class – Frisbee68 May 28 '13 at 03:19
  • @user1965245, I'm not sure I understand your problem. Perhaps you could expand on how you want to use your property class? – SimonC May 28 '13 at 03:25
  • I'm using the property class to assign variables to the storey width and length, as well as to calculate area and price. In my actual program, the user enters 3 values, one for storey width and height, and then the program calculates the area and price. – Frisbee68 May 28 '13 at 03:37
  • So you would have something 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("area: " + p.calculateArea());` – SimonC May 28 '13 at 03:54
  • I tried this, the compiler kept saying it could not find the symbol for calculateArea and calculatePrice – Frisbee68 May 28 '13 at 04:05
  • Probably because you declared the two methods with parameters. Have you tried making the changes that I've suggested? If so, could you update the question with your current code? – SimonC May 28 '13 at 04:48
  • As I said, you need to remove the arguments to `calculateArea` and `calculatePrice`. – SimonC May 28 '13 at 05:29
  • My assignment says i need to have a behavior/method called calculateArea and another called calculatePrice – Frisbee68 May 28 '13 at 05:32
  • Sure, but neither should take an argument. Please see how I've defined `calculateArea()`, not `calculateArea(int area)`. – SimonC May 28 '13 at 05:39
  • now the section in my other code for the program where i use Property p = new Property(storey, width, length); is giving me "cannot find symbol" error, because its looking for storey width and length in the program's class instead of property class :/ – Frisbee68 May 28 '13 at 06:18
  • I've updated my answer to include an example of how you access the property's values. – SimonC May 28 '13 at 07:14
  • I'm still getting cannot find symbol errors for p.getWidth and storey and length. Just those 3 errors. – Frisbee68 May 28 '13 at 14:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30764/discussion-between-simonc-and-user1965245) – SimonC May 28 '13 at 15:05
1

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;
    }
}
Nobilis
  • 7,310
  • 1
  • 33
  • 67
  • all i need it to do is figure out if storey is 1 or if storey is bigger than 1. – Frisbee68 May 28 '13 at 02:50
  • But if storey is initially 0, how can it ever be 1 upon initialisation? Take a look at the code I've added with my edit. And for testing if something is bigger than another value you would need the `>` sign, `>>` is right bit shifting and does something completely different. – Nobilis May 28 '13 at 02:53
  • Here's a good SO explanation of what a good constructor should look like: http://stackoverflow.com/questions/579445/java-constructors It is standard practice to declare the member variables (storey, width, length) private and have getters and setters to modify them (but only once the object has been created), an SO explanation: http://stackoverflow.com/questions/2036970/tutorial-on-getters-and-setters The member variables would be assigned values by the constructor upon initialising the object, it's good to have several different constructors depending on how many arguments you are passing. – Nobilis May 28 '13 at 03:25
0

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

for example:

property(int p2)
jmj
  • 237,923
  • 42
  • 401
  • 438
  • now it is saying property(int) is already defined in class property for property(int p2) – Frisbee68 May 28 '13 at 02:53
  • 1
    You need to overload constructor, how would java know which one to execute if you have same signature for both of them ? – jmj May 28 '13 at 02:59
0

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

 property(p1) 

Example: property(int p1)

PSR
  • 39,804
  • 41
  • 111
  • 151
0

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
}
David DeMar
  • 2,390
  • 2
  • 32
  • 45